/[pcre]/code/tags/pcre-8.37/pcre_jit_compile.c
ViewVC logotype

Diff of /code/tags/pcre-8.37/pcre_jit_compile.c

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

revision 678 by ph10, Sun Aug 28 15:23:03 2011 UTC revision 1276 by zherczeg, Sun Mar 10 17:35:23 2013 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-2008 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-2011                        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)
56    #define SLJIT_FREE(ptr) (PUBL(free))(ptr)
57  #define SLJIT_CONFIG_AUTO 1  #define SLJIT_CONFIG_AUTO 1
58    #define SLJIT_CONFIG_STATIC 1
59  #define SLJIT_VERBOSE 0  #define SLJIT_VERBOSE 0
60  #define SLJIT_DEBUG 0  #define SLJIT_DEBUG 0
61    
62  #include "sljit/sljitLir.c"  #include "sljit/sljitLir.c"
63    
64  #if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED  #if defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED
65  #error "Unsupported architecture"  #error Unsupported architecture
66  #endif  #endif
67    
68  /* Allocate memory on the stack. Fast, but limited size. */  /* Defines for debugging purposes. */
 #define LOCAL_SPACE_SIZE 32768  
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.
78    Fast, but limited size. */
79    #define MACHINE_STACK_SIZE 32768
80    
81    /* Growth rate for stack allocated by the OS. Should be the multiply
82    of page size. */
83  #define STACK_GROWTH_RATE 8192  #define STACK_GROWTH_RATE 8192
84    
85  /* Enable to check that the allocation could destroy temporaries. */  /* Enable to check that the allocation could destroy temporaries. */
# Line 79  The code generator follows the recursive Line 94  The code generator follows the recursive
94  expressions. The basic blocks of regular expressions are condition checkers  expressions. The basic blocks of regular expressions are condition checkers
95  whose execute different commands depending on the result of the condition check.  whose execute different commands depending on the result of the condition check.
96  The relationship between the operators can be horizontal (concatenation) and  The relationship between the operators can be horizontal (concatenation) and
97  vertical (sub-expression) (See struct fallback_common for more details).  vertical (sub-expression) (See struct backtrack_common for more details).
98    
99    'ab' - 'a' and 'b' regexps are concatenated    'ab' - 'a' and 'b' regexps are concatenated
100    'a+' - 'a' is the sub-expression of the '+' operator    'a+' - 'a' is the sub-expression of the '+' operator
101    
102  The condition checkers are boolean (true/false) checkers. Machine code is generated  The condition checkers are boolean (true/false) checkers. Machine code is generated
103  for the checker itself and for the actions depending on the result of the checker.  for the checker itself and for the actions depending on the result of the checker.
104  The 'true' case is called as the hot path (expected path), and the other is called as  The 'true' case is called as the matching path (expected path), and the other is called as
105  the 'fallback' path. Branch instructions are expesive for all CPUs, so we avoid taken  the 'backtrack' path. Branch instructions are expesive for all CPUs, so we avoid taken
106  branches on the hot path.  branches on the matching path.
107    
108   Greedy star operator (*) :   Greedy star operator (*) :
109     Hot path: match happens.     Matching path: match happens.
110     Fallback path: match failed.     Backtrack path: match failed.
111   Non-greedy star operator (*?) :   Non-greedy star operator (*?) :
112     Hot path: no need to perform a match.     Matching path: no need to perform a match.
113     Fallback path: match is required.     Backtrack path: match is required.
114    
115  The following example shows how the code generated for a capturing bracket  The following example shows how the code generated for a capturing bracket
116  with two alternatives. Let A, B, C, D are arbirary regular expressions, and  with two alternatives. Let A, B, C, D are arbirary regular expressions, and
# Line 105  we have the following regular expression Line 120  we have the following regular expression
120    
121  The generated code will be the following:  The generated code will be the following:
122    
123   A hot path   A matching path
124   '(' hot path (pushing arguments to the stack)   '(' matching path (pushing arguments to the stack)
125   B hot path   B matching path
126   ')' hot path (pushing arguments to the stack)   ')' matching path (pushing arguments to the stack)
127   D hot path   D matching path
128   return with successful match   return with successful match
129    
130   D fallback path   D backtrack path
131   ')' fallback path (If we arrived from "C" jump to the fallback of "C")   ')' backtrack path (If we arrived from "C" jump to the backtrack of "C")
132   B fallback path   B backtrack path
133   C expected path   C expected path
134   jump to D hot path   jump to D matching path
135   C fallback path   C backtrack path
136   A fallback path   A backtrack path
137    
138   Notice, that the order of fallback code paths are the opposite of the fast   Notice, that the order of backtrack code paths are the opposite of the fast
139   code paths. In this way the topmost value on the stack is always belong   code paths. In this way the topmost value on the stack is always belong
140   to the current fallback code path. The fallback code path must check   to the current backtrack code path. The backtrack path must check
141   whether there is a next alternative. If so, it needs to jump back to   whether there is a next alternative. If so, it needs to jump back to
142   the hot path eventually. Otherwise it needs to clear out its own stack   the matching path eventually. Otherwise it needs to clear out its own stack
143   frame and continue the execution on the fallback code paths.   frame and continue the execution on the backtrack code paths.
144  */  */
145    
146  /*  /*
147  Saved stack frames:  Saved stack frames:
148    
149  Atomic blocks and asserts require reloading the values of local variables  Atomic blocks and asserts require reloading the values of private data
150  when the fallback mechanism performed. Because of OP_RECURSE, the locals  when the backtrack mechanism performed. Because of OP_RECURSE, the data
151  are not necessarly known in compile time, thus we need a dynamic restore  are not necessarly known in compile time, thus we need a dynamic restore
152  mechanism.  mechanism.
153    
154  The stack frames are stored in a chain list, and have the following format:  The stack frames are stored in a chain list, and have the following format:
155  ([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]  ([ capturing bracket offset ][ start value ][ end value ])+ ... [ 0 ] [ previous head ]
156    
157  Thus we can restore the locals to a particular point in the stack.  Thus we can restore the private data to a particular point in the stack.
158  */  */
159    
160  typedef struct jit_arguments {  typedef struct jit_arguments {
161    /* Pointers first. */    /* Pointers first. */
162    struct sljit_stack *stack;    struct sljit_stack *stack;
163    PCRE_SPTR str;    const pcre_uchar *str;
164    PCRE_SPTR begin;    const pcre_uchar *begin;
165    PCRE_SPTR end;    const pcre_uchar *end;
166    int *offsets;    int *offsets;
167    uschar *ptr;    pcre_uchar *uchar_ptr;
168      pcre_uchar *mark_ptr;
169      void *callout_data;
170    /* Everything else after. */    /* Everything else after. */
171    int offsetcount;    int real_offset_count;
172    int calllimit;    int offset_count;
173    uschar notbol;    int call_limit;
174    uschar noteol;    pcre_uint8 notbol;
175    uschar notempty;    pcre_uint8 noteol;
176    uschar notempty_atstart;    pcre_uint8 notempty;
177      pcre_uint8 notempty_atstart;
178  } jit_arguments;  } jit_arguments;
179    
180  typedef struct executable_function {  typedef struct executable_functions {
181    void *executable_func;    void *executable_funcs[JIT_NUMBER_OF_COMPILE_MODES];
182    pcre_jit_callback callback;    PUBL(jit_callback) callback;
183    void *userdata;    void *userdata;
184  } executable_function;    pcre_uint32 top_bracket;
185      sljit_uw executable_sizes[JIT_NUMBER_OF_COMPILE_MODES];
186    } executable_functions;
187    
188  typedef struct jump_list {  typedef struct jump_list {
189    struct sljit_jump *jump;    struct sljit_jump *jump;
190    struct jump_list *next;    struct jump_list *next;
191  } jump_list;  } jump_list;
192    
 enum stub_types { stack_alloc, max_index };  
   
193  typedef struct stub_list {  typedef struct stub_list {
   enum stub_types type;  
   int data;  
194    struct sljit_jump *start;    struct sljit_jump *start;
195    struct sljit_label *leave;    struct sljit_label *quit;
196    struct stub_list *next;    struct stub_list *next;
197  } stub_list;  } stub_list;
198    
199    enum frame_types {
200      no_frame = -1,
201      no_stack = -2
202    };
203    
204    enum control_types {
205      type_commit = 0,
206      type_prune = 1,
207      type_skip = 2
208    };
209    
210  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);  typedef int (SLJIT_CALL *jit_function)(jit_arguments *args);
211    
212  /* The following structure is the key data type for the recursive  /* The following structure is the key data type for the recursive
213  code generator. It is allocated by compile_hotpath, and contains  code generator. It is allocated by compile_matchingpath, and contains
214  the aguments for compile_fallbackpath. Must be the first member  the aguments for compile_backtrackingpath. Must be the first member
215  of its descendants. */  of its descendants. */
216  typedef struct fallback_common {  typedef struct backtrack_common {
217    /* Concatenation stack. */    /* Concatenation stack. */
218    struct fallback_common *prev;    struct backtrack_common *prev;
219    jump_list *nextfallbacks;    jump_list *nextbacktracks;
220    /* Internal stack (for component operators). */    /* Internal stack (for component operators). */
221    struct fallback_common *top;    struct backtrack_common *top;
222    jump_list *topfallbacks;    jump_list *topbacktracks;
223    /* Opcode pointer. */    /* Opcode pointer. */
224    uschar *cc;    pcre_uchar *cc;
225  } fallback_common;  } backtrack_common;
226    
227  typedef struct assert_fallback {  typedef struct assert_backtrack {
228    fallback_common common;    backtrack_common common;
229    jump_list *condfailed;    jump_list *condfailed;
230    /* Less than 0 (-1) if a frame is not needed. */    /* Less than 0 if a frame is not needed. */
231    int framesize;    int framesize;
232    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
233    int localptr;    int private_data_ptr;
234    /* For iterators. */    /* For iterators. */
235    struct sljit_label *hotpath;    struct sljit_label *matchingpath;
236  } assert_fallback;  } assert_backtrack;
237    
238  typedef struct bracket_fallback {  typedef struct bracket_backtrack {
239    fallback_common common;    backtrack_common common;
240    /* Where to coninue if an alternative is successfully matched. */    /* Where to coninue if an alternative is successfully matched. */
241    struct sljit_label *althotpath;    struct sljit_label *alternative_matchingpath;
242    /* For rmin and rmax iterators. */    /* For rmin and rmax iterators. */
243    struct sljit_label *recursivehotpath;    struct sljit_label *recursive_matchingpath;
244    /* For greedy ? operator. */    /* For greedy ? operator. */
245    struct sljit_label *zerohotpath;    struct sljit_label *zero_matchingpath;
246    /* Contains the branches of a failed condition. */    /* Contains the branches of a failed condition. */
247    union {    union {
248      /* Both for OP_COND, OP_SCOND. */      /* Both for OP_COND, OP_SCOND. */
249      jump_list *condfailed;      jump_list *condfailed;
250      assert_fallback *assert;      assert_backtrack *assert;
251      /* For OP_ONCE. -1 if not needed. */      /* For OP_ONCE. Less than 0 if not needed. */
252      int framesize;      int framesize;
253    } u;    } u;
254    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
255    int localptr;    int private_data_ptr;
256  } bracket_fallback;  } bracket_backtrack;
257    
258  typedef struct bracketpos_fallback {  typedef struct bracketpos_backtrack {
259    fallback_common common;    backtrack_common common;
260    /* Points to our private memory word on the stack. */    /* Points to our private memory word on the stack. */
261    int localptr;    int private_data_ptr;
262    /* Reverting stack is needed. */    /* Reverting stack is needed. */
263    int framesize;    int framesize;
264    /* Allocated stack size. */    /* Allocated stack size. */
265    int stacksize;    int stacksize;
266  } bracketpos_fallback;  } bracketpos_backtrack;
267    
268  typedef struct braminzero_fallback {  typedef struct braminzero_backtrack {
269    fallback_common common;    backtrack_common common;
270    struct sljit_label *hotpath;    struct sljit_label *matchingpath;
271  } braminzero_fallback;  } braminzero_backtrack;
272    
273  typedef struct iterator_fallback {  typedef struct iterator_backtrack {
274    fallback_common common;    backtrack_common common;
275    /* Next iteration. */    /* Next iteration. */
276    struct sljit_label *hotpath;    struct sljit_label *matchingpath;
277  } iterator_fallback;  } iterator_backtrack;
278    
279  typedef struct recurse_entry {  typedef struct recurse_entry {
280    struct recurse_entry *next;    struct recurse_entry *next;
# Line 259  typedef struct recurse_entry { Line 286  typedef struct recurse_entry {
286    int start;    int start;
287  } recurse_entry;  } recurse_entry;
288    
289  typedef struct recurse_fallback {  typedef struct recurse_backtrack {
290    fallback_common common;    backtrack_common common;
291  } recurse_fallback;    BOOL inlined_pattern;
292    } recurse_backtrack;
293    
294    #define MAX_RANGE_SIZE 6
295    
296  typedef struct compiler_common {  typedef struct compiler_common {
297      /* The sljit ceneric compiler. */
298    struct sljit_compiler *compiler;    struct sljit_compiler *compiler;
299    uschar *start;    /* First byte code. */
300    int localsize;    pcre_uchar *start;
301    int *localptrs;    /* Maps private data offset to each opcode. */
302    const uschar *fcc;    int *private_data_ptrs;
303    sljit_w lcc;    /* Tells whether the capturing bracket is optimized. */
304    int cbraptr;    pcre_uint8 *optimized_cbracket;
305      /* Starting offset of private data for capturing brackets. */
306      int cbra_ptr;
307      /* Output vector starting point. Must be divisible by 2. */
308      int ovector_start;
309      /* Last known position of the requested byte. */
310      int req_char_ptr;
311      /* Head of the last recursion. */
312      int recursive_head_ptr;
313      /* First inspected character for partial matching. */
314      int start_used_ptr;
315      /* Starting pointer for partial soft matches. */
316      int hit_start;
317      /* End pointer of the first line. */
318      int first_line_end;
319      /* Points to the marked string. */
320      int mark_ptr;
321      /* Recursive control verb management chain. */
322      int control_head_ptr;
323      /* Points to the last matched capture block index. */
324      int capture_last_ptr;
325      /* Points to the starting position of the current match. */
326      int start_ptr;
327    
328      /* Flipped and lower case tables. */
329      const pcre_uint8 *fcc;
330      sljit_sw lcc;
331      /* Mode can be PCRE_STUDY_JIT_COMPILE and others. */
332      int mode;
333      /* \K is in the pattern. */
334      BOOL has_set_som;
335      /* Needs to know the start position anytime. */
336      BOOL needs_start_ptr;
337      /* Currently in recurse or assert. */
338      BOOL local_exit;
339      /* Newline control. */
340    int nltype;    int nltype;
341    int newline;    int newline;
342    int bsr_nltype;    int bsr_nltype;
343      /* Dollar endonly. */
344    int endonly;    int endonly;
345    sljit_w ctypes;    /* Tables. */
346    struct sljit_label *acceptlabel;    sljit_sw ctypes;
347      int digits[2 + MAX_RANGE_SIZE];
348      /* Named capturing brackets. */
349      sljit_uw name_table;
350      sljit_sw name_count;
351      sljit_sw name_entry_size;
352    
353      /* Labels and jump lists. */
354      struct sljit_label *partialmatchlabel;
355      struct sljit_label *quit_label;
356      struct sljit_label *forced_quit_label;
357      struct sljit_label *accept_label;
358    stub_list *stubs;    stub_list *stubs;
359    recurse_entry *entries;    recurse_entry *entries;
360    recurse_entry *currententry;    recurse_entry *currententry;
361      jump_list *partialmatch;
362      jump_list *quit;
363      jump_list *forced_quit;
364    jump_list *accept;    jump_list *accept;
365    jump_list *calllimit;    jump_list *calllimit;
366    jump_list *stackalloc;    jump_list *stackalloc;
# Line 290  typedef struct compiler_common { Line 371  typedef struct compiler_common {
371    jump_list *vspace;    jump_list *vspace;
372    jump_list *casefulcmp;    jump_list *casefulcmp;
373    jump_list *caselesscmp;    jump_list *caselesscmp;
374      jump_list *reset_match;
375    BOOL jscript_compat;    BOOL jscript_compat;
376  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
377    BOOL utf8;    BOOL utf;
378  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
379    BOOL useucp;    BOOL use_ucp;
380  #endif  #endif
381    jump_list *utf8readchar;  #ifndef COMPILE_PCRE32
382    jump_list *utf8readtype8;    jump_list *utfreadchar;
383  #endif  #endif
384    #ifdef COMPILE_PCRE8
385      jump_list *utfreadtype8;
386    #endif
387    #endif /* SUPPORT_UTF */
388  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
389    jump_list *getucd;    jump_list *getucd;
390  #endif  #endif
# Line 310  typedef struct compare_context { Line 396  typedef struct compare_context {
396    int length;    int length;
397    int sourcereg;    int sourcereg;
398  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED  #if defined SLJIT_UNALIGNED && SLJIT_UNALIGNED
399    int byteptr;    int ucharptr;
400    union {    union {
401      int asint;      sljit_si asint;
402      short asshort;      sljit_uh asushort;
403    #if defined COMPILE_PCRE8
404      sljit_ub asbyte;      sljit_ub asbyte;
405      sljit_ub asbytes[4];      sljit_ub asuchars[4];
406    #elif defined COMPILE_PCRE16
407        sljit_uh asuchars[2];
408    #elif defined COMPILE_PCRE32
409        sljit_ui asuchars[1];
410    #endif
411    } c;    } c;
412    union {    union {
413      int asint;      sljit_si asint;
414      short asshort;      sljit_uh asushort;
415    #if defined COMPILE_PCRE8
416      sljit_ub asbyte;      sljit_ub asbyte;
417      sljit_ub asbytes[4];      sljit_ub asuchars[4];
418    #elif defined COMPILE_PCRE16
419        sljit_uh asuchars[2];
420    #elif defined COMPILE_PCRE32
421        sljit_ui asuchars[1];
422    #endif
423    } oc;    } oc;
424  #endif  #endif
425  } compare_context;  } compare_context;
426    
427  enum {  /* Undefine sljit macros. */
428    frame_end = 0,  #undef CMP
   frame_setmaxindex = -1,  
   frame_setstrbegin = -2  
 };  
429    
430  /* Used for accessing the elements of the stack. */  /* Used for accessing the elements of the stack. */
431  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_w))  #define STACK(i)      ((-(i) - 1) * (int)sizeof(sljit_sw))
432    
433  #define TMP1          SLJIT_TEMPORARY_REG1  #define TMP1          SLJIT_SCRATCH_REG1
434  #define TMP2          SLJIT_TEMPORARY_REG3  #define TMP2          SLJIT_SCRATCH_REG3
435  #define TMP3          SLJIT_TEMPORARY_EREG2  #define TMP3          SLJIT_TEMPORARY_EREG2
436  #define STR_PTR       SLJIT_GENERAL_REG1  #define STR_PTR       SLJIT_SAVED_REG1
437  #define STR_END       SLJIT_GENERAL_REG2  #define STR_END       SLJIT_SAVED_REG2
438  #define STACK_TOP     SLJIT_TEMPORARY_REG2  #define STACK_TOP     SLJIT_SCRATCH_REG2
439  #define STACK_LIMIT   SLJIT_GENERAL_REG3  #define STACK_LIMIT   SLJIT_SAVED_REG3
440  #define ARGUMENTS     SLJIT_GENERAL_EREG1  #define ARGUMENTS     SLJIT_SAVED_EREG1
441  #define MAX_INDEX     SLJIT_GENERAL_EREG2  #define CALL_COUNT    SLJIT_SAVED_EREG2
442  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1  #define RETURN_ADDR   SLJIT_TEMPORARY_EREG1
443    
444  /* Locals layout. */  /* Local space layout. */
445  /* These two locals can be used by the current opcode. */  /* These two locals can be used by the current opcode. */
446  #define LOCALS0          (0 * sizeof(sljit_w))  #define LOCALS0          (0 * sizeof(sljit_sw))
447  #define LOCALS1          (1 * sizeof(sljit_w))  #define LOCALS1          (1 * sizeof(sljit_sw))
448  /* Two local variables for possessive quantifiers (char1 cannot use them). */  /* Two local variables for possessive quantifiers (char1 cannot use them). */
449  #define POSSESSIVE0      (2 * sizeof(sljit_w))  #define POSSESSIVE0      (2 * sizeof(sljit_sw))
450  #define POSSESSIVE1      (3 * sizeof(sljit_w))  #define POSSESSIVE1      (3 * sizeof(sljit_sw))
 /* Head of the saved local variables */  
 #define LOCALS_HEAD      (4 * sizeof(sljit_w))  
 /* Head of the last recursion. */  
 #define RECURSIVE_HEAD   (5 * sizeof(sljit_w))  
 /* Number of recursions. */  
 #define CALL_COUNT       (6 * sizeof(sljit_w))  
451  /* Max limit of recursions. */  /* Max limit of recursions. */
452  #define CALL_LIMIT       (7 * sizeof(sljit_w))  #define CALL_LIMIT       (4 * sizeof(sljit_sw))
 /* Last known position of the requested byte. */  
 #define REQ_BYTE_PTR     (8 * sizeof(sljit_w))  
 /* End pointer of the first line. */  
 #define FIRSTLINE_END    (9 * sizeof(sljit_w))  
453  /* The output vector is stored on the stack, and contains pointers  /* The output vector is stored on the stack, and contains pointers
454  to characters. The vector data is divided into two groups: the first  to characters. The vector data is divided into two groups: the first
455  group contains the start / end character pointers, and the second is  group contains the start / end character pointers, and the second is
456  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. */
457  #define OVECTOR_START    (10 * sizeof(sljit_w))  #define OVECTOR_START    (common->ovector_start)
458  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_w))  #define OVECTOR(i)       (OVECTOR_START + (i) * sizeof(sljit_sw))
459  #define OVECTOR_PRIV(i)  (common->cbraptr + (i) * sizeof(sljit_w))  #define OVECTOR_PRIV(i)  (common->cbra_ptr + (i) * sizeof(sljit_sw))
460  #define PRIV(cc)         (common->localptrs[(cc) - common->start])  #define PRIVATE_DATA(cc) (common->private_data_ptrs[(cc) - common->start])
461    
462    #if defined COMPILE_PCRE8
463    #define MOV_UCHAR  SLJIT_MOV_UB
464    #define MOVU_UCHAR SLJIT_MOVU_UB
465    #elif defined COMPILE_PCRE16
466    #define MOV_UCHAR  SLJIT_MOV_UH
467    #define MOVU_UCHAR SLJIT_MOVU_UH
468    #elif defined COMPILE_PCRE32
469    #define MOV_UCHAR  SLJIT_MOV_UI
470    #define MOVU_UCHAR SLJIT_MOVU_UI
471    #else
472    #error Unsupported compiling mode
473    #endif
474    
475  /* Shortcuts. */  /* Shortcuts. */
476  #define DEFINE_COMPILER \  #define DEFINE_COMPILER \
# Line 389  the start pointers when the end of the c Line 487  the start pointers when the end of the c
487    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))    sljit_set_label(sljit_emit_jump(compiler, (type)), (label))
488  #define JUMPHERE(jump) \  #define JUMPHERE(jump) \
489    sljit_set_label((jump), sljit_emit_label(compiler))    sljit_set_label((jump), sljit_emit_label(compiler))
490    #define SET_LABEL(jump, label) \
491      sljit_set_label((jump), (label))
492  #define CMP(type, src1, src1w, src2, src2w) \  #define CMP(type, src1, src1w, src2, src2w) \
493    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))    sljit_emit_cmp(compiler, (type), (src1), (src1w), (src2), (src2w))
494  #define CMPTO(type, src1, src1w, src2, src2w, label) \  #define CMPTO(type, src1, src1w, src2, src2w, label) \
495    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))
496  #define COND_VALUE(op, dst, dstw, type) \  #define OP_FLAGS(op, dst, dstw, src, srcw, type) \
497    sljit_emit_cond_value(compiler, (op), (dst), (dstw), (type))    sljit_emit_op_flags(compiler, (op), (dst), (dstw), (src), (srcw), (type))
498    #define GET_LOCAL_BASE(dst, dstw, offset) \
499      sljit_get_local_base(compiler, (dst), (dstw), (offset))
500    
501  static uschar* bracketend(uschar* cc)  static pcre_uchar* bracketend(pcre_uchar* cc)
502  {  {
503  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));
504  do cc += GET(cc, 1); while (*cc == OP_ALT);  do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 405  cc += 1 + LINK_SIZE; Line 507  cc += 1 + LINK_SIZE;
507  return cc;  return cc;
508  }  }
509    
510  /* Functions whose might need modification for all new supported opcodes:  /* Functions whose might need modification for all new supported opcodes:
511   next_opcode   next_opcode
512   get_localspace   get_private_data_length
513   set_localptrs   set_private_data_ptrs
514   get_framesize   get_framesize
515   init_frame   init_frame
516   get_localsize   get_private_data_copy_length
517   copy_locals   copy_private_data
518   compile_hotpath   compile_matchingpath
519   compile_fallbackpath   compile_backtrackingpath
520  */  */
521    
522  static uschar *next_opcode(compiler_common *common, uschar *cc)  static pcre_uchar *next_opcode(compiler_common *common, pcre_uchar *cc)
523  {  {
524  SLJIT_UNUSED_ARG(common);  SLJIT_UNUSED_ARG(common);
525  switch(*cc)  switch(*cc)
# Line 435  switch(*cc) Line 537  switch(*cc)
537    case OP_WORDCHAR:    case OP_WORDCHAR:
538    case OP_ANY:    case OP_ANY:
539    case OP_ALLANY:    case OP_ALLANY:
540      case OP_NOTPROP:
541      case OP_PROP:
542    case OP_ANYNL:    case OP_ANYNL:
543    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
544    case OP_HSPACE:    case OP_HSPACE:
# Line 447  switch(*cc) Line 551  switch(*cc)
551    case OP_CIRCM:    case OP_CIRCM:
552    case OP_DOLL:    case OP_DOLL:
553    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:  
554    case OP_CRSTAR:    case OP_CRSTAR:
555    case OP_CRMINSTAR:    case OP_CRMINSTAR:
556    case OP_CRPLUS:    case OP_CRPLUS:
557    case OP_CRMINPLUS:    case OP_CRMINPLUS:
558    case OP_CRQUERY:    case OP_CRQUERY:
559    case OP_CRMINQUERY:    case OP_CRMINQUERY:
560      case OP_CRRANGE:
561      case OP_CRMINRANGE:
562      case OP_CLASS:
563      case OP_NCLASS:
564      case OP_REF:
565      case OP_REFI:
566      case OP_RECURSE:
567      case OP_CALLOUT:
568      case OP_ALT:
569      case OP_KET:
570      case OP_KETRMAX:
571      case OP_KETRMIN:
572      case OP_KETRPOS:
573      case OP_REVERSE:
574      case OP_ASSERT:
575      case OP_ASSERT_NOT:
576      case OP_ASSERTBACK:
577      case OP_ASSERTBACK_NOT:
578      case OP_ONCE:
579      case OP_ONCE_NC:
580      case OP_BRA:
581      case OP_BRAPOS:
582      case OP_CBRA:
583      case OP_CBRAPOS:
584      case OP_COND:
585      case OP_SBRA:
586      case OP_SBRAPOS:
587      case OP_SCBRA:
588      case OP_SCBRAPOS:
589      case OP_SCOND:
590      case OP_CREF:
591      case OP_NCREF:
592      case OP_RREF:
593      case OP_NRREF:
594    case OP_DEF:    case OP_DEF:
595    case OP_BRAZERO:    case OP_BRAZERO:
596    case OP_BRAMINZERO:    case OP_BRAMINZERO:
597    case OP_BRAPOSZERO:    case OP_BRAPOSZERO:
598      case OP_PRUNE:
599      case OP_SKIP:
600      case OP_COMMIT:
601    case OP_FAIL:    case OP_FAIL:
602    case OP_ACCEPT:    case OP_ACCEPT:
603    case OP_ASSERT_ACCEPT:    case OP_ASSERT_ACCEPT:
604      case OP_CLOSE:
605    case OP_SKIPZERO:    case OP_SKIPZERO:
606    return cc + 1;    return cc + PRIV(OP_lengths)[*cc];
607    
608    case OP_CHAR:    case OP_CHAR:
609    case OP_CHARI:    case OP_CHARI:
610    case OP_NOT:    case OP_NOT:
611    case OP_NOTI:    case OP_NOTI:
   
612    case OP_STAR:    case OP_STAR:
613    case OP_MINSTAR:    case OP_MINSTAR:
614    case OP_PLUS:    case OP_PLUS:
615    case OP_MINPLUS:    case OP_MINPLUS:
616    case OP_QUERY:    case OP_QUERY:
617    case OP_MINQUERY:    case OP_MINQUERY:
618      case OP_UPTO:
619      case OP_MINUPTO:
620      case OP_EXACT:
621    case OP_POSSTAR:    case OP_POSSTAR:
622    case OP_POSPLUS:    case OP_POSPLUS:
623    case OP_POSQUERY:    case OP_POSQUERY:
624      case OP_POSUPTO:
625    case OP_STARI:    case OP_STARI:
626    case OP_MINSTARI:    case OP_MINSTARI:
627    case OP_PLUSI:    case OP_PLUSI:
628    case OP_MINPLUSI:    case OP_MINPLUSI:
629    case OP_QUERYI:    case OP_QUERYI:
630    case OP_MINQUERYI:    case OP_MINQUERYI:
631      case OP_UPTOI:
632      case OP_MINUPTOI:
633      case OP_EXACTI:
634    case OP_POSSTARI:    case OP_POSSTARI:
635    case OP_POSPLUSI:    case OP_POSPLUSI:
636    case OP_POSQUERYI:    case OP_POSQUERYI:
637      case OP_POSUPTOI:
638    case OP_NOTSTAR:    case OP_NOTSTAR:
639    case OP_NOTMINSTAR:    case OP_NOTMINSTAR:
640    case OP_NOTPLUS:    case OP_NOTPLUS:
641    case OP_NOTMINPLUS:    case OP_NOTMINPLUS:
642    case OP_NOTQUERY:    case OP_NOTQUERY:
643    case OP_NOTMINQUERY:    case OP_NOTMINQUERY:
644      case OP_NOTUPTO:
645      case OP_NOTMINUPTO:
646      case OP_NOTEXACT:
647    case OP_NOTPOSSTAR:    case OP_NOTPOSSTAR:
648    case OP_NOTPOSPLUS:    case OP_NOTPOSPLUS:
649    case OP_NOTPOSQUERY:    case OP_NOTPOSQUERY:
650      case OP_NOTPOSUPTO:
651    case OP_NOTSTARI:    case OP_NOTSTARI:
652    case OP_NOTMINSTARI:    case OP_NOTMINSTARI:
653    case OP_NOTPLUSI:    case OP_NOTPLUSI:
654    case OP_NOTMINPLUSI:    case OP_NOTMINPLUSI:
655    case OP_NOTQUERYI:    case OP_NOTQUERYI:
656    case OP_NOTMINQUERYI:    case OP_NOTMINQUERYI:
   case OP_NOTPOSSTARI:  
   case OP_NOTPOSPLUSI:  
   case OP_NOTPOSQUERYI:  
   cc += 2;  
 #ifdef SUPPORT_UTF8  
   if (common->utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
 #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:  
657    case OP_NOTUPTOI:    case OP_NOTUPTOI:
658    case OP_NOTMINUPTOI:    case OP_NOTMINUPTOI:
659    case OP_NOTEXACTI:    case OP_NOTEXACTI:
660      case OP_NOTPOSSTARI:
661      case OP_NOTPOSPLUSI:
662      case OP_NOTPOSQUERYI:
663    case OP_NOTPOSUPTOI:    case OP_NOTPOSUPTOI:
664    cc += 4;    cc += PRIV(OP_lengths)[*cc];
665  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
666    if (common->utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];    if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
667  #endif  #endif
668    return cc;    return cc;
669    
670    case OP_NOTPROP:    /* Special cases. */
671    case OP_PROP:    case OP_TYPESTAR:
672      case OP_TYPEMINSTAR:
673      case OP_TYPEPLUS:
674      case OP_TYPEMINPLUS:
675      case OP_TYPEQUERY:
676      case OP_TYPEMINQUERY:
677    case OP_TYPEUPTO:    case OP_TYPEUPTO:
678    case OP_TYPEMINUPTO:    case OP_TYPEMINUPTO:
679    case OP_TYPEEXACT:    case OP_TYPEEXACT:
680      case OP_TYPEPOSSTAR:
681      case OP_TYPEPOSPLUS:
682      case OP_TYPEPOSQUERY:
683    case OP_TYPEPOSUPTO:    case OP_TYPEPOSUPTO:
684    case OP_REF:    return cc + PRIV(OP_lengths)[*cc] - 1;
   case OP_REFI:  
   case OP_CREF:  
   case OP_CLOSE:  
   cc += 3;  
   return cc;  
   
   case OP_CRRANGE:  
   case OP_CRMINRANGE:  
   return cc + 5;  
685    
686    case OP_CLASS:    case OP_ANYBYTE:
687    case OP_NCLASS:  #ifdef SUPPORT_UTF
688    return cc + 33;    if (common->utf) return NULL;
689    #endif
690      return cc + 1;
691    
692  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
693    case OP_XCLASS:    case OP_XCLASS:
694    return cc + GET(cc, 1);    return cc + GET(cc, 1);
695  #endif  #endif
696    
697    case OP_RECURSE:    case OP_MARK:
698    case OP_ASSERT:    case OP_PRUNE_ARG:
699    case OP_ASSERT_NOT:    return cc + 1 + 2 + cc[1];
   case OP_ASSERTBACK:  
   case OP_ASSERTBACK_NOT:  
   case OP_REVERSE:  
   case OP_ONCE:  
   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 + 2;  
700    
701    default:    default:
702    return NULL;    return NULL;
703    }    }
704  }  }
705    
706  static int get_localspace(compiler_common *common, uschar *cc, uschar *ccend)  #define CASE_ITERATOR_PRIVATE_DATA_1 \
707        case OP_MINSTAR: \
708        case OP_MINPLUS: \
709        case OP_QUERY: \
710        case OP_MINQUERY: \
711        case OP_MINSTARI: \
712        case OP_MINPLUSI: \
713        case OP_QUERYI: \
714        case OP_MINQUERYI: \
715        case OP_NOTMINSTAR: \
716        case OP_NOTMINPLUS: \
717        case OP_NOTQUERY: \
718        case OP_NOTMINQUERY: \
719        case OP_NOTMINSTARI: \
720        case OP_NOTMINPLUSI: \
721        case OP_NOTQUERYI: \
722        case OP_NOTMINQUERYI:
723    
724    #define CASE_ITERATOR_PRIVATE_DATA_2A \
725        case OP_STAR: \
726        case OP_PLUS: \
727        case OP_STARI: \
728        case OP_PLUSI: \
729        case OP_NOTSTAR: \
730        case OP_NOTPLUS: \
731        case OP_NOTSTARI: \
732        case OP_NOTPLUSI:
733    
734    #define CASE_ITERATOR_PRIVATE_DATA_2B \
735        case OP_UPTO: \
736        case OP_MINUPTO: \
737        case OP_UPTOI: \
738        case OP_MINUPTOI: \
739        case OP_NOTUPTO: \
740        case OP_NOTMINUPTO: \
741        case OP_NOTUPTOI: \
742        case OP_NOTMINUPTOI:
743    
744    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_1 \
745        case OP_TYPEMINSTAR: \
746        case OP_TYPEMINPLUS: \
747        case OP_TYPEQUERY: \
748        case OP_TYPEMINQUERY:
749    
750    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2A \
751        case OP_TYPESTAR: \
752        case OP_TYPEPLUS:
753    
754    #define CASE_ITERATOR_TYPE_PRIVATE_DATA_2B \
755        case OP_TYPEUPTO: \
756        case OP_TYPEMINUPTO:
757    
758    static int get_class_iterator_size(pcre_uchar *cc)
759    {
760    switch(*cc)
761      {
762      case OP_CRSTAR:
763      case OP_CRPLUS:
764      return 2;
765    
766      case OP_CRMINSTAR:
767      case OP_CRMINPLUS:
768      case OP_CRQUERY:
769      case OP_CRMINQUERY:
770      return 1;
771    
772      case OP_CRRANGE:
773      case OP_CRMINRANGE:
774      if (GET2(cc, 1) == GET2(cc, 1 + IMM2_SIZE))
775        return 0;
776      return 2;
777    
778      default:
779      return 0;
780      }
781    }
782    
783    static int get_private_data_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend)
784  {  {
785  int localspace = 0;  int private_data_length = 0;
786  uschar *alternative;  pcre_uchar *alternative;
787    pcre_uchar *name;
788    pcre_uchar *end = NULL;
789    int space, size, i;
790    pcre_uint32 bracketlen;
791    
792  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */  /* Calculate important variables (like stack size) and checks whether all opcodes are supported. */
793  while (cc < ccend)  while (cc < ccend)
794    {    {
795      space = 0;
796      size = 0;
797      bracketlen = 0;
798    switch(*cc)    switch(*cc)
799      {      {
800        case OP_SET_SOM:
801        common->has_set_som = TRUE;
802        cc += 1;
803        break;
804    
805        case OP_REF:
806        case OP_REFI:
807        common->optimized_cbracket[GET2(cc, 1)] = 0;
808        cc += 1 + IMM2_SIZE;
809        break;
810    
811      case OP_ASSERT:      case OP_ASSERT:
812      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
813      case OP_ASSERTBACK:      case OP_ASSERTBACK:
814      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
815      case OP_ONCE:      case OP_ONCE:
816        case OP_ONCE_NC:
817      case OP_BRAPOS:      case OP_BRAPOS:
818      case OP_SBRA:      case OP_SBRA:
819      case OP_SBRAPOS:      case OP_SBRAPOS:
820      case OP_SCOND:      private_data_length += sizeof(sljit_sw);
821      localspace += sizeof(sljit_w);      bracketlen = 1 + LINK_SIZE;
     cc += 1 + LINK_SIZE;  
822      break;      break;
823    
824      case OP_CBRAPOS:      case OP_CBRAPOS:
825      case OP_SCBRAPOS:      case OP_SCBRAPOS:
826      localspace += sizeof(sljit_w);      private_data_length += sizeof(sljit_sw);
827      cc += 1 + LINK_SIZE + 2;      common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] = 0;
828        bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
829      break;      break;
830    
831      case OP_COND:      case OP_COND:
832      /* Might be a hidden SCOND. */      case OP_SCOND:
833      alternative = cc + GET(cc, 1);      /* Only AUTO_CALLOUT can insert this opcode. We do
834      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)         not intend to support this case. */
835        localspace += sizeof(sljit_w);      if (cc[1 + LINK_SIZE] == OP_CALLOUT)
836          return -1;
837    
838        if (*cc == OP_COND)
839          {
840          /* Might be a hidden SCOND. */
841          alternative = cc + GET(cc, 1);
842          if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
843            private_data_length += sizeof(sljit_sw);
844          }
845        else
846          private_data_length += sizeof(sljit_sw);
847        bracketlen = 1 + LINK_SIZE;
848        break;
849    
850        case OP_CREF:
851        i = GET2(cc, 1);
852        common->optimized_cbracket[i] = 0;
853        cc += 1 + IMM2_SIZE;
854        break;
855    
856        case OP_NCREF:
857        bracketlen = GET2(cc, 1);
858        name = (pcre_uchar *)common->name_table;
859        alternative = name;
860        for (i = 0; i < common->name_count; i++)
861          {
862          if (GET2(name, 0) == bracketlen) break;
863          name += common->name_entry_size;
864          }
865        SLJIT_ASSERT(i != common->name_count);
866    
867        for (i = 0; i < common->name_count; i++)
868          {
869          if (STRCMP_UC_UC(alternative + IMM2_SIZE, name + IMM2_SIZE) == 0)
870            common->optimized_cbracket[GET2(alternative, 0)] = 0;
871          alternative += common->name_entry_size;
872          }
873        bracketlen = 0;
874        cc += 1 + IMM2_SIZE;
875        break;
876    
877        case OP_BRA:
878        bracketlen = 1 + LINK_SIZE;
879        break;
880    
881        case OP_CBRA:
882        case OP_SCBRA:
883        bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
884        break;
885    
886        CASE_ITERATOR_PRIVATE_DATA_1
887        space = 1;
888        size = -2;
889        break;
890    
891        CASE_ITERATOR_PRIVATE_DATA_2A
892        space = 2;
893        size = -2;
894        break;
895    
896        CASE_ITERATOR_PRIVATE_DATA_2B
897        space = 2;
898        size = -(2 + IMM2_SIZE);
899        break;
900    
901        CASE_ITERATOR_TYPE_PRIVATE_DATA_1
902        space = 1;
903        size = 1;
904        break;
905    
906        CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
907        if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
908          space = 2;
909        size = 1;
910        break;
911    
912        CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
913        if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
914          space = 2;
915        size = 1 + IMM2_SIZE;
916        break;
917    
918        case OP_CLASS:
919        case OP_NCLASS:
920        size += 1 + 32 / sizeof(pcre_uchar);
921        space = get_class_iterator_size(cc + size);
922        break;
923    
924    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
925        case OP_XCLASS:
926        size = GET(cc, 1);
927        space = get_class_iterator_size(cc + size);
928        break;
929    #endif
930    
931        case OP_RECURSE:
932        /* Set its value only once. */
933        if (common->recursive_head_ptr == 0)
934          {
935          common->recursive_head_ptr = common->ovector_start;
936          common->ovector_start += sizeof(sljit_sw);
937          }
938      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
939      break;      break;
940    
941        case OP_CALLOUT:
942        if (common->capture_last_ptr == 0)
943          {
944          common->capture_last_ptr = common->ovector_start;
945          common->ovector_start += sizeof(sljit_sw);
946          }
947        cc += 2 + 2 * LINK_SIZE;
948        break;
949    
950        case OP_PRUNE_ARG:
951        common->needs_start_ptr = TRUE;
952        common->control_head_ptr = 1;
953        /* Fall through. */
954    
955        case OP_MARK:
956        if (common->mark_ptr == 0)
957          {
958          common->mark_ptr = common->ovector_start;
959          common->ovector_start += sizeof(sljit_sw);
960          }
961        cc += 1 + 2 + cc[1];
962        break;
963    
964        case OP_PRUNE:
965        case OP_SKIP:
966        common->needs_start_ptr = TRUE;
967        /* Fall through. */
968    
969        case OP_COMMIT:
970        common->control_head_ptr = 1;
971        cc += 1;
972        break;
973    
974      default:      default:
975      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
976      if (cc == NULL)      if (cc == NULL)
977        return -1;        return -1;
978      break;      break;
979      }      }
980    
981      if (space > 0 && cc >= end)
982        private_data_length += sizeof(sljit_sw) * space;
983    
984      if (size != 0)
985        {
986        if (size < 0)
987          {
988          cc += -size;
989    #ifdef SUPPORT_UTF
990          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
991    #endif
992          }
993        else
994          cc += size;
995        }
996    
997      if (bracketlen != 0)
998        {
999        if (cc >= end)
1000          {
1001          end = bracketend(cc);
1002          if (end[-1 - LINK_SIZE] == OP_KET)
1003            end = NULL;
1004          }
1005        cc += bracketlen;
1006        }
1007    }    }
1008  return localspace;  return private_data_length;
1009  }  }
1010    
1011  static void set_localptrs(compiler_common *common, int localptr, uschar *ccend)  static void set_private_data_ptrs(compiler_common *common, int private_data_ptr, pcre_uchar *ccend)
1012  {  {
1013  uschar *cc = common->start;  pcre_uchar *cc = common->start;
1014  uschar *alternative;  pcre_uchar *alternative;
1015    pcre_uchar *end = NULL;
1016    int space, size, bracketlen;
1017    
1018  while (cc < ccend)  while (cc < ccend)
1019    {    {
1020      space = 0;
1021      size = 0;
1022      bracketlen = 0;
1023    switch(*cc)    switch(*cc)
1024      {      {
1025      case OP_ASSERT:      case OP_ASSERT:
# Line 657  while (cc < ccend) Line 1027  while (cc < ccend)
1027      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1028      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1029      case OP_ONCE:      case OP_ONCE:
1030        case OP_ONCE_NC:
1031      case OP_BRAPOS:      case OP_BRAPOS:
1032      case OP_SBRA:      case OP_SBRA:
1033      case OP_SBRAPOS:      case OP_SBRAPOS:
1034      case OP_SCOND:      case OP_SCOND:
1035      common->localptrs[cc - common->start] = localptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1036      localptr += sizeof(sljit_w);      private_data_ptr += sizeof(sljit_sw);
1037      cc += 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1038      break;      break;
1039    
1040      case OP_CBRAPOS:      case OP_CBRAPOS:
1041      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1042      common->localptrs[cc - common->start] = localptr;      common->private_data_ptrs[cc - common->start] = private_data_ptr;
1043      localptr += sizeof(sljit_w);      private_data_ptr += sizeof(sljit_sw);
1044      cc += 1 + LINK_SIZE + 2;      bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1045      break;      break;
1046    
1047      case OP_COND:      case OP_COND:
# Line 678  while (cc < ccend) Line 1049  while (cc < ccend)
1049      alternative = cc + GET(cc, 1);      alternative = cc + GET(cc, 1);
1050      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1051        {        {
1052        common->localptrs[cc - common->start] = localptr;        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1053        localptr += sizeof(sljit_w);        private_data_ptr += sizeof(sljit_sw);
1054        }        }
1055      cc += 1 + LINK_SIZE;      bracketlen = 1 + LINK_SIZE;
1056        break;
1057    
1058        case OP_BRA:
1059        bracketlen = 1 + LINK_SIZE;
1060        break;
1061    
1062        case OP_CBRA:
1063        case OP_SCBRA:
1064        bracketlen = 1 + LINK_SIZE + IMM2_SIZE;
1065        break;
1066    
1067        CASE_ITERATOR_PRIVATE_DATA_1
1068        space = 1;
1069        size = -2;
1070        break;
1071    
1072        CASE_ITERATOR_PRIVATE_DATA_2A
1073        space = 2;
1074        size = -2;
1075        break;
1076    
1077        CASE_ITERATOR_PRIVATE_DATA_2B
1078        space = 2;
1079        size = -(2 + IMM2_SIZE);
1080      break;      break;
1081    
1082        CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1083        space = 1;
1084        size = 1;
1085        break;
1086    
1087        CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1088        if (cc[1] != OP_ANYNL && cc[1] != OP_EXTUNI)
1089          space = 2;
1090        size = 1;
1091        break;
1092    
1093        CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1094        if (cc[1 + IMM2_SIZE] != OP_ANYNL && cc[1 + IMM2_SIZE] != OP_EXTUNI)
1095          space = 2;
1096        size = 1 + IMM2_SIZE;
1097        break;
1098    
1099        case OP_CLASS:
1100        case OP_NCLASS:
1101        size += 1 + 32 / sizeof(pcre_uchar);
1102        space = get_class_iterator_size(cc + size);
1103        break;
1104    
1105    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1106        case OP_XCLASS:
1107        size = GET(cc, 1);
1108        space = get_class_iterator_size(cc + size);
1109        break;
1110    #endif
1111    
1112      default:      default:
1113      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1114      SLJIT_ASSERT(cc != NULL);      SLJIT_ASSERT(cc != NULL);
1115      break;      break;
1116      }      }
1117    
1118      if (space > 0 && cc >= end)
1119        {
1120        common->private_data_ptrs[cc - common->start] = private_data_ptr;
1121        private_data_ptr += sizeof(sljit_sw) * space;
1122        }
1123    
1124      if (size != 0)
1125        {
1126        if (size < 0)
1127          {
1128          cc += -size;
1129    #ifdef SUPPORT_UTF
1130          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1131    #endif
1132          }
1133        else
1134          cc += size;
1135        }
1136    
1137      if (bracketlen > 0)
1138        {
1139        if (cc >= end)
1140          {
1141          end = bracketend(cc);
1142          if (end[-1 - LINK_SIZE] == OP_KET)
1143            end = NULL;
1144          }
1145        cc += bracketlen;
1146        }
1147    }    }
1148  }  }
1149    
1150  /* Returns with -1 if no need for frame. */  /* Returns with a frame_types (always < 0) if no need for frame. */
1151  static int get_framesize(compiler_common *common, uschar *cc, BOOL recursive)  static int get_framesize(compiler_common *common, pcre_uchar *cc, BOOL recursive)
1152  {  {
1153  uschar *ccend = bracketend(cc);  pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);
 uschar *end;  
1154  int length = 0;  int length = 0;
1155  BOOL possessive = FALSE;  int possessive = 0;
1156  BOOL needs_frame = FALSE;  BOOL stack_restore = FALSE;
1157  BOOL needs_maxindex = FALSE;  BOOL setsom_found = recursive;
1158  BOOL setsom_found = FALSE;  BOOL setmark_found = recursive;
1159    /* The last capture is a local variable even for recursions. */
1160    BOOL capture_last_found = FALSE;
1161    
1162  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))  if (!recursive && (*cc == OP_CBRAPOS || *cc == OP_SCBRAPOS))
1163    {    {
1164    length = 3 + 2;    possessive = length = (common->capture_last_ptr != 0) ? 5 : 3;
1165    needs_maxindex = TRUE;    /* This is correct regardless of common->capture_last_ptr. */
1166    possessive = TRUE;    capture_last_found = TRUE;
1167    }    }
1168    
1169  cc = next_opcode(common, cc);  cc = next_opcode(common, cc);
# Line 716  while (cc < ccend) Line 1172  while (cc < ccend)
1172    switch(*cc)    switch(*cc)
1173      {      {
1174      case OP_SET_SOM:      case OP_SET_SOM:
1175      case OP_RECURSE:      SLJIT_ASSERT(common->has_set_som);
1176        stack_restore = TRUE;
1177      if (!setsom_found)      if (!setsom_found)
1178        {        {
1179        length += 2;        length += 2;
1180        setsom_found = TRUE;        setsom_found = TRUE;
1181        }        }
1182      cc += (*cc == OP_SET_SOM) ? 1 : 1 + LINK_SIZE;      cc += 1;
1183      break;      break;
1184    
1185      case OP_ASSERT:      case OP_MARK:
1186      case OP_ASSERT_NOT:      case OP_PRUNE_ARG:
1187      case OP_ASSERTBACK:      SLJIT_ASSERT(common->mark_ptr != 0);
1188      case OP_ASSERTBACK_NOT:      stack_restore = TRUE;
1189      case OP_ONCE:      if (!setmark_found)
     if (needs_frame || length > 0)  
1190        {        {
1191        cc = bracketend(cc);        length += 2;
1192        break;        setmark_found = TRUE;
1193        }        }
1194      /* Check whether a frame must be created. */      cc += 1 + 2 + cc[1];
1195      end = bracketend(cc);      break;
1196      while (cc < end)  
1197        {      case OP_RECURSE:
1198        if (*cc == OP_SET_SOM || *cc == OP_CBRA || *cc == OP_CBRAPOS      stack_restore = TRUE;
1199            || *cc == OP_SCBRA || *cc == OP_SCBRAPOS || *cc == OP_RECURSE)      if (common->has_set_som && !setsom_found)
1200          needs_frame = TRUE;        {
1201        cc = next_opcode(common, cc);        length += 2;
1202        SLJIT_ASSERT(cc != NULL);        setsom_found = TRUE;
1203        }        }
1204        if (common->mark_ptr != 0 && !setmark_found)
1205          {
1206          length += 2;
1207          setmark_found = TRUE;
1208          }
1209        if (common->capture_last_ptr != 0 && !capture_last_found)
1210          {
1211          length += 2;
1212          capture_last_found = TRUE;
1213          }
1214        cc += 1 + LINK_SIZE;
1215      break;      break;
1216    
1217      case OP_CBRA:      case OP_CBRA:
1218      case OP_CBRAPOS:      case OP_CBRAPOS:
1219      case OP_SCBRA:      case OP_SCBRA:
1220      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1221      if (!needs_maxindex)      stack_restore = TRUE;
1222        if (common->capture_last_ptr != 0 && !capture_last_found)
1223        {        {
       needs_maxindex = TRUE;  
1224        length += 2;        length += 2;
1225          capture_last_found = TRUE;
1226        }        }
1227      length += 3;      length += 3;
1228      cc += 1 + LINK_SIZE + 2;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1229      break;      break;
1230    
1231      default:      default:
1232        stack_restore = TRUE;
1233        /* Fall through. */
1234    
1235        case OP_NOT_WORD_BOUNDARY:
1236        case OP_WORD_BOUNDARY:
1237        case OP_NOT_DIGIT:
1238        case OP_DIGIT:
1239        case OP_NOT_WHITESPACE:
1240        case OP_WHITESPACE:
1241        case OP_NOT_WORDCHAR:
1242        case OP_WORDCHAR:
1243        case OP_ANY:
1244        case OP_ALLANY:
1245        case OP_ANYBYTE:
1246        case OP_NOTPROP:
1247        case OP_PROP:
1248        case OP_ANYNL:
1249        case OP_NOT_HSPACE:
1250        case OP_HSPACE:
1251        case OP_NOT_VSPACE:
1252        case OP_VSPACE:
1253        case OP_EXTUNI:
1254        case OP_EODN:
1255        case OP_EOD:
1256        case OP_CIRC:
1257        case OP_CIRCM:
1258        case OP_DOLL:
1259        case OP_DOLLM:
1260        case OP_CHAR:
1261        case OP_CHARI:
1262        case OP_NOT:
1263        case OP_NOTI:
1264    
1265        case OP_EXACT:
1266        case OP_POSSTAR:
1267        case OP_POSPLUS:
1268        case OP_POSQUERY:
1269        case OP_POSUPTO:
1270    
1271        case OP_EXACTI:
1272        case OP_POSSTARI:
1273        case OP_POSPLUSI:
1274        case OP_POSQUERYI:
1275        case OP_POSUPTOI:
1276    
1277        case OP_NOTEXACT:
1278        case OP_NOTPOSSTAR:
1279        case OP_NOTPOSPLUS:
1280        case OP_NOTPOSQUERY:
1281        case OP_NOTPOSUPTO:
1282    
1283        case OP_NOTEXACTI:
1284        case OP_NOTPOSSTARI:
1285        case OP_NOTPOSPLUSI:
1286        case OP_NOTPOSQUERYI:
1287        case OP_NOTPOSUPTOI:
1288    
1289        case OP_TYPEEXACT:
1290        case OP_TYPEPOSSTAR:
1291        case OP_TYPEPOSPLUS:
1292        case OP_TYPEPOSQUERY:
1293        case OP_TYPEPOSUPTO:
1294    
1295        case OP_CLASS:
1296        case OP_NCLASS:
1297        case OP_XCLASS:
1298    
1299      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1300      SLJIT_ASSERT(cc != NULL);      SLJIT_ASSERT(cc != NULL);
1301      break;      break;
1302      }      }
1303    
1304  /* Possessive quantifiers can use a special case. */  /* Possessive quantifiers can use a special case. */
1305  if (SLJIT_UNLIKELY(possessive) && !needs_frame && length == 3 + 2)  if (SLJIT_UNLIKELY(possessive == length))
1306    return -1;    return stack_restore ? no_frame : no_stack;
1307    
1308  if (length > 0)  if (length > 0)
1309    return length + 2;    return length + 1;
1310  return needs_frame ? 0 : -1;  return stack_restore ? no_frame : no_stack;
1311  }  }
1312    
1313  static void init_frame(compiler_common *common, uschar *cc, int stackpos, int stacktop, BOOL recursive)  static void init_frame(compiler_common *common, pcre_uchar *cc, int stackpos, int stacktop, BOOL recursive)
1314  {  {
 /* TMP2 must contain STACK_TOP - (-STACK(stackpos)) */  
1315  DEFINE_COMPILER;  DEFINE_COMPILER;
1316  uschar *ccend = bracketend(cc);  pcre_uchar *ccend = bracketend(cc) - (1 + LINK_SIZE);
1317  BOOL needs_maxindex = FALSE;  BOOL setsom_found = recursive;
1318  BOOL setsom_found = FALSE;  BOOL setmark_found = recursive;
1319    /* The last capture is a local variable even for recursions. */
1320    BOOL capture_last_found = FALSE;
1321  int offset;  int offset;
1322    
1323  if (stackpos < stacktop)  /* >= 1 + shortest item size (2) */
1324    {  SLJIT_UNUSED_ARG(stacktop);
1325    SLJIT_ASSERT(stackpos + 1 == stacktop);  SLJIT_ASSERT(stackpos >= stacktop + 2);
   return;  
   }  
1326    
1327  stackpos = STACK(stackpos);  stackpos = STACK(stackpos);
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS_HEAD);  
 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS_HEAD, TMP2, 0);  
 OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), STACK(stacktop), TMP1, 0);  
   
1328  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))  if (recursive || (*cc != OP_CBRAPOS && *cc != OP_SCBRAPOS))
1329    cc = next_opcode(common, cc);    cc = next_opcode(common, cc);
1330  SLJIT_ASSERT(cc != NULL);  SLJIT_ASSERT(cc != NULL);
# Line 802  while (cc < ccend) Line 1332  while (cc < ccend)
1332    switch(*cc)    switch(*cc)
1333      {      {
1334      case OP_SET_SOM:      case OP_SET_SOM:
1335      case OP_RECURSE:      SLJIT_ASSERT(common->has_set_som);
1336      if (!setsom_found)      if (!setsom_found)
1337        {        {
1338        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
1339        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setstrbegin);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1340        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1341        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1342        stackpos += (int)sizeof(sljit_w);        stackpos += (int)sizeof(sljit_sw);
1343        setsom_found = TRUE;        setsom_found = TRUE;
1344        }        }
1345      cc += (*cc == OP_SET_SOM) ? 1 : 1 + LINK_SIZE;      cc += 1;
1346      break;      break;
1347    
1348      case OP_ASSERT:      case OP_MARK:
1349      case OP_ASSERT_NOT:      case OP_PRUNE_ARG:
1350      case OP_ASSERTBACK:      SLJIT_ASSERT(common->mark_ptr != 0);
1351      case OP_ASSERTBACK_NOT:      if (!setmark_found)
1352      case OP_ONCE:        {
1353      cc = bracketend(cc);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
1354          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1355          stackpos += (int)sizeof(sljit_sw);
1356          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1357          stackpos += (int)sizeof(sljit_sw);
1358          setmark_found = TRUE;
1359          }
1360        cc += 1 + 2 + cc[1];
1361        break;
1362    
1363        case OP_RECURSE:
1364        if (common->has_set_som && !setsom_found)
1365          {
1366          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0));
1367          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -OVECTOR(0));
1368          stackpos += (int)sizeof(sljit_sw);
1369          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1370          stackpos += (int)sizeof(sljit_sw);
1371          setsom_found = TRUE;
1372          }
1373        if (common->mark_ptr != 0 && !setmark_found)
1374          {
1375          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
1376          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->mark_ptr);
1377          stackpos += (int)sizeof(sljit_sw);
1378          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1379          stackpos += (int)sizeof(sljit_sw);
1380          setmark_found = TRUE;
1381          }
1382        if (common->capture_last_ptr != 0 && !capture_last_found)
1383          {
1384          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
1385          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1386          stackpos += (int)sizeof(sljit_sw);
1387          OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1388          stackpos += (int)sizeof(sljit_sw);
1389          capture_last_found = TRUE;
1390          }
1391        cc += 1 + LINK_SIZE;
1392      break;      break;
1393    
1394      case OP_CBRA:      case OP_CBRA:
1395      case OP_CBRAPOS:      case OP_CBRAPOS:
1396      case OP_SCBRA:      case OP_SCBRA:
1397      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1398      if (!needs_maxindex)      if (common->capture_last_ptr != 0 && !capture_last_found)
1399        {        {
1400        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_setmaxindex);        OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->capture_last_ptr);
1401        stackpos += (int)sizeof(sljit_w);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, -common->capture_last_ptr);
1402        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, MAX_INDEX, 0);        stackpos += (int)sizeof(sljit_sw);
1403        stackpos += (int)sizeof(sljit_w);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1404        needs_maxindex = TRUE;        stackpos += (int)sizeof(sljit_sw);
1405          capture_last_found = TRUE;
1406        }        }
1407      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;      offset = (GET2(cc, 1 + LINK_SIZE)) << 1;
1408      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, OVECTOR(offset));
1409      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1410      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset));
1411      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(offset + 1));
1412      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP1, 0);
1413      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1414      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, TMP2, 0);
1415      stackpos += (int)sizeof(sljit_w);      stackpos += (int)sizeof(sljit_sw);
1416    
1417      cc += 1 + LINK_SIZE + 2;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1418      break;      break;
1419    
1420      default:      default:
# Line 854  while (cc < ccend) Line 1423  while (cc < ccend)
1423      break;      break;
1424      }      }
1425    
1426  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, frame_end);  OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackpos, SLJIT_IMM, 0);
1427  SLJIT_ASSERT(stackpos == STACK(stacktop + 1));  SLJIT_ASSERT(stackpos == STACK(stacktop));
1428  }  }
1429    
1430  static SLJIT_INLINE int get_localsize(compiler_common *common, uschar *cc, uschar *ccend)  static SLJIT_INLINE int get_private_data_copy_length(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend, BOOL needs_control_head)
1431  {  {
1432  int localsize = 2;  int private_data_length = needs_control_head ? 3 : 2;
1433  uschar *alternative;  int size;
1434  /* Calculate the sum of the local variables. */  pcre_uchar *alternative;
1435    /* Calculate the sum of the private machine words. */
1436  while (cc < ccend)  while (cc < ccend)
1437    {    {
1438      size = 0;
1439    switch(*cc)    switch(*cc)
1440      {      {
1441      case OP_ASSERT:      case OP_ASSERT:
# Line 872  while (cc < ccend) Line 1443  while (cc < ccend)
1443      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1444      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1445      case OP_ONCE:      case OP_ONCE:
1446        case OP_ONCE_NC:
1447      case OP_BRAPOS:      case OP_BRAPOS:
1448      case OP_SBRA:      case OP_SBRA:
1449      case OP_SBRAPOS:      case OP_SBRAPOS:
1450      case OP_SCOND:      case OP_SCOND:
1451      localsize++;      private_data_length++;
1452      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1453      break;      break;
1454    
1455      case OP_CBRA:      case OP_CBRA:
1456      case OP_SCBRA:      case OP_SCBRA:
1457      localsize++;      if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1458      cc += 1 + LINK_SIZE + 2;        private_data_length++;
1459        cc += 1 + LINK_SIZE + IMM2_SIZE;
1460      break;      break;
1461    
1462      case OP_CBRAPOS:      case OP_CBRAPOS:
1463      case OP_SCBRAPOS:      case OP_SCBRAPOS:
1464      localsize += 2;      private_data_length += 2;
1465      cc += 1 + LINK_SIZE + 2;      cc += 1 + LINK_SIZE + IMM2_SIZE;
1466      break;      break;
1467    
1468      case OP_COND:      case OP_COND:
1469      /* Might be a hidden SCOND. */      /* Might be a hidden SCOND. */
1470      alternative = cc + GET(cc, 1);      alternative = cc + GET(cc, 1);
1471      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)      if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1472        localsize++;        private_data_length++;
1473      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1474      break;      break;
1475    
1476        CASE_ITERATOR_PRIVATE_DATA_1
1477        if (PRIVATE_DATA(cc))
1478          private_data_length++;
1479        cc += 2;
1480    #ifdef SUPPORT_UTF
1481        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1482    #endif
1483        break;
1484    
1485        CASE_ITERATOR_PRIVATE_DATA_2A
1486        if (PRIVATE_DATA(cc))
1487          private_data_length += 2;
1488        cc += 2;
1489    #ifdef SUPPORT_UTF
1490        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1491    #endif
1492        break;
1493    
1494        CASE_ITERATOR_PRIVATE_DATA_2B
1495        if (PRIVATE_DATA(cc))
1496          private_data_length += 2;
1497        cc += 2 + IMM2_SIZE;
1498    #ifdef SUPPORT_UTF
1499        if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1500    #endif
1501        break;
1502    
1503        CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1504        if (PRIVATE_DATA(cc))
1505          private_data_length++;
1506        cc += 1;
1507        break;
1508    
1509        CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1510        if (PRIVATE_DATA(cc))
1511          private_data_length += 2;
1512        cc += 1;
1513        break;
1514    
1515        CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1516        if (PRIVATE_DATA(cc))
1517          private_data_length += 2;
1518        cc += 1 + IMM2_SIZE;
1519        break;
1520    
1521        case OP_CLASS:
1522        case OP_NCLASS:
1523    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1524        case OP_XCLASS:
1525        size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar);
1526    #else
1527        size = 1 + 32 / (int)sizeof(pcre_uchar);
1528    #endif
1529        if (PRIVATE_DATA(cc))
1530          private_data_length += get_class_iterator_size(cc + size);
1531        cc += size;
1532        break;
1533    
1534      default:      default:
1535      cc = next_opcode(common, cc);      cc = next_opcode(common, cc);
1536      SLJIT_ASSERT(cc != NULL);      SLJIT_ASSERT(cc != NULL);
# Line 907  while (cc < ccend) Line 1538  while (cc < ccend)
1538      }      }
1539    }    }
1540  SLJIT_ASSERT(cc == ccend);  SLJIT_ASSERT(cc == ccend);
1541  return localsize;  return private_data_length;
1542  }  }
1543    
1544  static void copy_locals(compiler_common *common, uschar *cc, uschar *ccend,  static void copy_private_data(compiler_common *common, pcre_uchar *cc, pcre_uchar *ccend,
1545    BOOL save, int stackptr, int stacktop)    BOOL save, int stackptr, int stacktop, BOOL needs_control_head)
1546  {  {
1547  DEFINE_COMPILER;  DEFINE_COMPILER;
1548  int srcw[2];  int srcw[2];
1549  int count;  int count, size;
1550  BOOL tmp1next = TRUE;  BOOL tmp1next = TRUE;
1551  BOOL tmp1empty = TRUE;  BOOL tmp1empty = TRUE;
1552  BOOL tmp2empty = TRUE;  BOOL tmp2empty = TRUE;
1553  uschar *alternative;  pcre_uchar *alternative;
1554  enum {  enum {
1555    start,    start,
1556    loop,    loop,
# Line 932  stacktop = STACK(stacktop - 1); Line 1563  stacktop = STACK(stacktop - 1);
1563    
1564  if (!save)  if (!save)
1565    {    {
1566    stackptr += sizeof(sljit_w);    stackptr += (needs_control_head ? 2 : 1) * sizeof(sljit_sw);
1567    if (stackptr < stacktop)    if (stackptr < stacktop)
1568      {      {
1569      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1570      stackptr += sizeof(sljit_w);      stackptr += sizeof(sljit_sw);
1571      tmp1empty = FALSE;      tmp1empty = FALSE;
1572      }      }
1573    if (stackptr < stacktop)    if (stackptr < stacktop)
1574      {      {
1575      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);      OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1576      stackptr += sizeof(sljit_w);      stackptr += sizeof(sljit_sw);
1577      tmp2empty = FALSE;      tmp2empty = FALSE;
1578      }      }
1579    /* The tmp1next must be TRUE in either way. */    /* The tmp1next must be TRUE in either way. */
1580    }    }
1581    
1582  while (status != end)  do
1583    {    {
1584    count = 0;    count = 0;
1585    switch(status)    switch(status)
1586      {      {
1587      case start:      case start:
1588      SLJIT_ASSERT(save);      SLJIT_ASSERT(save && common->recursive_head_ptr != 0);
1589      count = 1;      count = 1;
1590      srcw[0] = RECURSIVE_HEAD;      srcw[0] = common->recursive_head_ptr;
1591        if (needs_control_head)
1592          {
1593          SLJIT_ASSERT(common->control_head_ptr != 0);
1594          count = 2;
1595          srcw[1] = common->control_head_ptr;
1596          }
1597      status = loop;      status = loop;
1598      break;      break;
1599    
# Line 974  while (status != end) Line 1611  while (status != end)
1611        case OP_ASSERTBACK:        case OP_ASSERTBACK:
1612        case OP_ASSERTBACK_NOT:        case OP_ASSERTBACK_NOT:
1613        case OP_ONCE:        case OP_ONCE:
1614          case OP_ONCE_NC:
1615        case OP_BRAPOS:        case OP_BRAPOS:
1616        case OP_SBRA:        case OP_SBRA:
1617        case OP_SBRAPOS:        case OP_SBRAPOS:
1618        case OP_SCOND:        case OP_SCOND:
1619        count = 1;        count = 1;
1620        srcw[0] = PRIV(cc);        srcw[0] = PRIVATE_DATA(cc);
1621        SLJIT_ASSERT(srcw[0] != 0);        SLJIT_ASSERT(srcw[0] != 0);
1622        cc += 1 + LINK_SIZE;        cc += 1 + LINK_SIZE;
1623        break;        break;
1624    
1625        case OP_CBRA:        case OP_CBRA:
1626        case OP_SCBRA:        case OP_SCBRA:
1627        count = 1;        if (common->optimized_cbracket[GET2(cc, 1 + LINK_SIZE)] == 0)
1628        srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));          {
1629        cc += 1 + LINK_SIZE + 2;          count = 1;
1630            srcw[0] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1631            }
1632          cc += 1 + LINK_SIZE + IMM2_SIZE;
1633        break;        break;
1634    
1635        case OP_CBRAPOS:        case OP_CBRAPOS:
1636        case OP_SCBRAPOS:        case OP_SCBRAPOS:
1637        count = 2;        count = 2;
1638          srcw[0] = PRIVATE_DATA(cc);
1639        srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));        srcw[1] = OVECTOR_PRIV(GET2(cc, 1 + LINK_SIZE));
1640        srcw[0] = PRIV(cc);        SLJIT_ASSERT(srcw[0] != 0 && srcw[1] != 0);
1641        SLJIT_ASSERT(srcw[0] != 0);        cc += 1 + LINK_SIZE + IMM2_SIZE;
       cc += 1 + LINK_SIZE + 2;  
1642        break;        break;
1643    
1644        case OP_COND:        case OP_COND:
# Line 1006  while (status != end) Line 1647  while (status != end)
1647        if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)        if (*alternative == OP_KETRMAX || *alternative == OP_KETRMIN)
1648          {          {
1649          count = 1;          count = 1;
1650          srcw[0] = PRIV(cc);          srcw[0] = PRIVATE_DATA(cc);
1651          SLJIT_ASSERT(srcw[0] != 0);          SLJIT_ASSERT(srcw[0] != 0);
1652          }          }
1653        cc += 1 + LINK_SIZE;        cc += 1 + LINK_SIZE;
1654        break;        break;
1655    
1656          CASE_ITERATOR_PRIVATE_DATA_1
1657          if (PRIVATE_DATA(cc))
1658            {
1659            count = 1;
1660            srcw[0] = PRIVATE_DATA(cc);
1661            }
1662          cc += 2;
1663    #ifdef SUPPORT_UTF
1664          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1665    #endif
1666          break;
1667    
1668          CASE_ITERATOR_PRIVATE_DATA_2A
1669          if (PRIVATE_DATA(cc))
1670            {
1671            count = 2;
1672            srcw[0] = PRIVATE_DATA(cc);
1673            srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1674            }
1675          cc += 2;
1676    #ifdef SUPPORT_UTF
1677          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1678    #endif
1679          break;
1680    
1681          CASE_ITERATOR_PRIVATE_DATA_2B
1682          if (PRIVATE_DATA(cc))
1683            {
1684            count = 2;
1685            srcw[0] = PRIVATE_DATA(cc);
1686            srcw[1] = PRIVATE_DATA(cc) + sizeof(sljit_sw);
1687            }
1688          cc += 2 + IMM2_SIZE;
1689    #ifdef SUPPORT_UTF
1690          if (common->utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1691    #endif
1692          break;
1693    
1694          CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1695          if (PRIVATE_DATA(cc))
1696            {
1697            count = 1;
1698            srcw[0] = PRIVATE_DATA(cc);
1699            }
1700          cc += 1;
1701          break;
1702    
1703          CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1704          if (PRIVATE_DATA(cc))
1705            {
1706            count = 2;
1707            srcw[0] = PRIVATE_DATA(cc);
1708            srcw[1] = srcw[0] + sizeof(sljit_sw);
1709            }
1710          cc += 1;
1711          break;
1712    
1713          CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1714          if (PRIVATE_DATA(cc))
1715            {
1716            count = 2;
1717            srcw[0] = PRIVATE_DATA(cc);
1718            srcw[1] = srcw[0] + sizeof(sljit_sw);
1719            }
1720          cc += 1 + IMM2_SIZE;
1721          break;
1722    
1723          case OP_CLASS:
1724          case OP_NCLASS:
1725    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1726          case OP_XCLASS:
1727          size = (*cc == OP_XCLASS) ? GET(cc, 1) : 1 + 32 / (int)sizeof(pcre_uchar);
1728    #else
1729          size = 1 + 32 / (int)sizeof(pcre_uchar);
1730    #endif
1731          if (PRIVATE_DATA(cc))
1732            switch(get_class_iterator_size(cc + size))
1733              {
1734              case 1:
1735              count = 1;
1736              srcw[0] = PRIVATE_DATA(cc);
1737              break;
1738    
1739              case 2:
1740              count = 2;
1741              srcw[0] = PRIVATE_DATA(cc);
1742              srcw[1] = srcw[0] + sizeof(sljit_sw);
1743              break;
1744    
1745              default:
1746              SLJIT_ASSERT_STOP();
1747              break;
1748              }
1749          cc += size;
1750          break;
1751    
1752        default:        default:
1753        cc = next_opcode(common, cc);        cc = next_opcode(common, cc);
1754        SLJIT_ASSERT(cc != NULL);        SLJIT_ASSERT(cc != NULL);
# Line 1034  while (status != end) Line 1771  while (status != end)
1771          if (!tmp1empty)          if (!tmp1empty)
1772            {            {
1773            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1774            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1775            }            }
1776          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);          OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);
1777          tmp1empty = FALSE;          tmp1empty = FALSE;
# Line 1045  while (status != end) Line 1782  while (status != end)
1782          if (!tmp2empty)          if (!tmp2empty)
1783            {            {
1784            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);            OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1785            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1786            }            }
1787          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);          OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), srcw[count]);
1788          tmp2empty = FALSE;          tmp2empty = FALSE;
# Line 1062  while (status != end) Line 1799  while (status != end)
1799          if (!tmp1empty)          if (!tmp1empty)
1800            {            {
1801            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);            OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1802            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1803            }            }
1804          tmp1next = FALSE;          tmp1next = FALSE;
1805          }          }
# Line 1074  while (status != end) Line 1811  while (status != end)
1811          if (!tmp2empty)          if (!tmp2empty)
1812            {            {
1813            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);            OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(STACK_TOP), stackptr);
1814            stackptr += sizeof(sljit_w);            stackptr += sizeof(sljit_sw);
1815            }            }
1816          tmp1next = TRUE;          tmp1next = TRUE;
1817          }          }
1818        }        }
1819      }      }
1820    }    }
1821    while (status != end);
1822    
1823  if (save)  if (save)
1824    {    {
# Line 1089  if (save) Line 1827  if (save)
1827      if (!tmp1empty)      if (!tmp1empty)
1828        {        {
1829        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1830        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1831        }        }
1832      if (!tmp2empty)      if (!tmp2empty)
1833        {        {
1834        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1835        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1836        }        }
1837      }      }
1838    else    else
# Line 1102  if (save) Line 1840  if (save)
1840      if (!tmp2empty)      if (!tmp2empty)
1841        {        {
1842        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP2, 0);
1843        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1844        }        }
1845      if (!tmp1empty)      if (!tmp1empty)
1846        {        {
1847        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);        OP1(SLJIT_MOV, SLJIT_MEM1(STACK_TOP), stackptr, TMP1, 0);
1848        stackptr += sizeof(sljit_w);        stackptr += sizeof(sljit_sw);
1849        }        }
1850      }      }
1851    }    }
1852  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));  SLJIT_ASSERT(cc == ccend && stackptr == stacktop && (save || (tmp1empty && tmp2empty)));
1853  }  }
1854    
1855  static SLJIT_INLINE BOOL ispowerof2(unsigned int value)  #undef CASE_ITERATOR_PRIVATE_DATA_1
1856    #undef CASE_ITERATOR_PRIVATE_DATA_2A
1857    #undef CASE_ITERATOR_PRIVATE_DATA_2B
1858    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_1
1859    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2A
1860    #undef CASE_ITERATOR_TYPE_PRIVATE_DATA_2B
1861    
1862    static SLJIT_INLINE BOOL is_powerof2(unsigned int value)
1863  {  {
1864  return (value & (value - 1)) == 0;  return (value & (value - 1)) == 0;
1865  }  }
# Line 1124  static SLJIT_INLINE void set_jumps(jump_ Line 1869  static SLJIT_INLINE void set_jumps(jump_
1869  while (list)  while (list)
1870    {    {
1871    /* sljit_set_label is clever enough to do nothing    /* sljit_set_label is clever enough to do nothing
1872    if either the jump or the label is NULL */    if either the jump or the label is NULL. */
1873    sljit_set_label(list->jump, label);    SET_LABEL(list->jump, label);
1874    list = list->next;    list = list->next;
1875    }    }
1876  }  }
# Line 1141  if (list_item) Line 1886  if (list_item)
1886    }    }
1887  }  }
1888    
1889  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)
1890  {  {
1891  DEFINE_COMPILER;  DEFINE_COMPILER;
1892  stub_list* list_item = sljit_alloc_memory(compiler, sizeof(stub_list));  stub_list* list_item = sljit_alloc_memory(compiler, sizeof(stub_list));
1893    
1894  if (list_item)  if (list_item)
1895    {    {
   list_item->type = type;  
   list_item->data = data;  
1896    list_item->start = start;    list_item->start = start;
1897    list_item->leave = LABEL();    list_item->quit = LABEL();
1898    list_item->next = common->stubs;    list_item->next = common->stubs;
1899    common->stubs = list_item;    common->stubs = list_item;
1900    }    }
# Line 1165  stub_list* list_item = common->stubs; Line 1908  stub_list* list_item = common->stubs;
1908  while (list_item)  while (list_item)
1909    {    {
1910    JUMPHERE(list_item->start);    JUMPHERE(list_item->start);
1911    switch(list_item->type)    add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));
1912      {    JUMPTO(SLJIT_JUMP, list_item->quit);
     case stack_alloc:  
     add_jump(compiler, &common->stackalloc, JUMP(SLJIT_FAST_CALL));  
     break;  
   
     case max_index:  
     OP1(SLJIT_MOV, MAX_INDEX, 0, SLJIT_IMM, list_item->data);  
     break;  
     }  
   JUMPTO(SLJIT_JUMP, list_item->leave);  
1913    list_item = list_item->next;    list_item = list_item->next;
1914    }    }
1915  common->stubs = NULL;  common->stubs = NULL;
# Line 1185  static SLJIT_INLINE void decrease_call_c Line 1919  static SLJIT_INLINE void decrease_call_c
1919  {  {
1920  DEFINE_COMPILER;  DEFINE_COMPILER;
1921    
1922  OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_MEM1(SLJIT_LOCALS_REG), CALL_COUNT, SLJIT_MEM1(SLJIT_LOCALS_REG), CALL_COUNT, SLJIT_IMM, 1);  OP2(SLJIT_SUB | SLJIT_SET_E, CALL_COUNT, 0, CALL_COUNT, 0, SLJIT_IMM, 1);
1923  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));  add_jump(compiler, &common->calllimit, JUMP(SLJIT_C_ZERO));
1924  }  }
1925    
# Line 1194  static SLJIT_INLINE void allocate_stack( Line 1928  static SLJIT_INLINE void allocate_stack(
1928  /* May destroy all locals and registers except TMP2. */  /* May destroy all locals and registers except TMP2. */
1929  DEFINE_COMPILER;  DEFINE_COMPILER;
1930    
1931  OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_w));  OP2(SLJIT_ADD, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
1932  #ifdef DESTROY_REGISTERS  #ifdef DESTROY_REGISTERS
1933  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 12345);
1934  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);  OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
# Line 1202  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0); Line 1936  OP1(SLJIT_MOV, RETURN_ADDR, 0, TMP1, 0);
1936  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, TMP1, 0);
1937  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);
1938  #endif  #endif
1939  add_stub(common, stack_alloc, 0, CMP(SLJIT_C_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));  add_stub(common, CMP(SLJIT_C_GREATER, STACK_TOP, 0, STACK_LIMIT, 0));
1940  }  }
1941    
1942  static SLJIT_INLINE void free_stack(compiler_common *common, int size)  static SLJIT_INLINE void free_stack(compiler_common *common, int size)
1943  {  {
1944  DEFINE_COMPILER;  DEFINE_COMPILER;
1945  OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_w));  OP2(SLJIT_SUB, STACK_TOP, 0, STACK_TOP, 0, SLJIT_IMM, size * sizeof(sljit_sw));
1946  }  }
1947    
1948  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)  static SLJIT_INLINE void reset_ovector(compiler_common *common, int length)
# Line 1216  static SLJIT_INLINE void reset_ovector(c Line 1950  static SLJIT_INLINE void reset_ovector(c
1950  DEFINE_COMPILER;  DEFINE_COMPILER;
1951  struct sljit_label *loop;  struct sljit_label *loop;
1952  int i;  int i;
1953    
1954  /* At this point we can freely use all temporary registers. */  /* At this point we can freely use all temporary registers. */
1955    SLJIT_ASSERT(length > 1);
1956  /* TMP1 returns with begin - 1. */  /* TMP1 returns with begin - 1. */
1957  OP1(SLJIT_MOV, MAX_INDEX, 0, SLJIT_IMM, 1);  OP2(SLJIT_SUB, SLJIT_SCRATCH_REG1, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, IN_UCHARS(1));
1958  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_GENERAL_REG1), SLJIT_OFFSETOF(jit_arguments, begin), SLJIT_IMM, 1);  if (length < 8)
1959      {
1960      for (i = 1; i < length; i++)
1961        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_SCRATCH_REG1, 0);
1962      }
1963    else
1964      {
1965      GET_LOCAL_BASE(SLJIT_SCRATCH_REG2, 0, OVECTOR_START);
1966      OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, length - 1);
1967      loop = LABEL();
1968      OP1(SLJIT_MOVU, SLJIT_MEM1(SLJIT_SCRATCH_REG2), sizeof(sljit_sw), SLJIT_SCRATCH_REG1, 0);
1969      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 1);
1970      JUMPTO(SLJIT_C_NOT_ZERO, loop);
1971      }
1972    }
1973    
1974    static SLJIT_INLINE void do_reset_match(compiler_common *common, int length)
1975    {
1976    DEFINE_COMPILER;
1977    struct sljit_label *loop;
1978    int i;
1979    
1980    SLJIT_ASSERT(length > 1);
1981    /* OVECTOR(1) contains the "string begin - 1" constant. */
1982    if (length > 2)
1983      OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
1984  if (length < 8)  if (length < 8)
1985    {    {
1986    for (i = 0; i < length; i++)    for (i = 2; i < length; i++)
1987      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), SLJIT_TEMPORARY_REG1, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(i), TMP1, 0);
1988    }    }
1989  else  else
1990    {    {
1991    OP2(SLJIT_ADD, SLJIT_TEMPORARY_REG2, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, OVECTOR_START - sizeof(sljit_w));    GET_LOCAL_BASE(TMP2, 0, OVECTOR_START + sizeof(sljit_sw));
1992    OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG3, 0, SLJIT_IMM, length);    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_IMM, length - 2);
1993    loop = LABEL();    loop = LABEL();
1994    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);
1995    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);
1996    JUMPTO(SLJIT_C_NOT_ZERO, loop);    JUMPTO(SLJIT_C_NOT_ZERO, loop);
1997    }    }
1998    
1999    OP1(SLJIT_MOV, STACK_TOP, 0, ARGUMENTS, 0);
2000    if (common->mark_ptr != 0)
2001      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr, SLJIT_IMM, 0);
2002    SLJIT_ASSERT(common->control_head_ptr != 0);
2003    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->control_head_ptr, SLJIT_IMM, 0);
2004    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(jit_arguments, stack));
2005    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_ptr);
2006    OP1(SLJIT_MOV, STACK_TOP, 0, SLJIT_MEM1(STACK_TOP), SLJIT_OFFSETOF(struct sljit_stack, base));
2007    }
2008    
2009    static sljit_sw SLJIT_CALL do_check_control_chain(sljit_sw *current)
2010    {
2011    sljit_sw return_value = 0;
2012    
2013    SLJIT_ASSERT(current != NULL);
2014    do
2015      {
2016      switch (current[-2])
2017        {
2018        case type_commit:
2019        /* Commit overwrites all. */
2020        return -1;
2021    
2022        case type_prune:
2023        break;
2024    
2025        case type_skip:
2026        /* Overwrites prune, but not other skips. */
2027        if (return_value == 0)
2028          return_value = current[-3];
2029        break;
2030    
2031        default:
2032        SLJIT_ASSERT_STOP();
2033        break;
2034        }
2035      current = (sljit_sw*)current[-1];
2036      }
2037    while (current != NULL);
2038    return return_value;
2039  }  }
2040    
2041  static SLJIT_INLINE void copy_ovector(compiler_common *common)  static SLJIT_INLINE void copy_ovector(compiler_common *common, int topbracket)
2042  {  {
2043  DEFINE_COMPILER;  DEFINE_COMPILER;
2044  struct sljit_label *loop;  struct sljit_label *loop;
2045  struct sljit_jump *earlyexit;  struct sljit_jump *early_quit;
2046    
2047  /* At this point we can freely use all registers. */  /* At this point we can freely use all registers. */
2048  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, SLJIT_SAVED_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1));
2049  OP1(SLJIT_MOV_SI, SLJIT_TEMPORARY_REG2, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsetcount));  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(1), STR_PTR, 0);
2050  OP2(SLJIT_SUB, SLJIT_TEMPORARY_REG3, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));  
2051  OP1(SLJIT_MOV, SLJIT_TEMPORARY_REG1, 0, SLJIT_MEM1(SLJIT_TEMPORARY_REG1), SLJIT_OFFSETOF(jit_arguments, begin));  OP1(SLJIT_MOV, SLJIT_SCRATCH_REG1, 0, ARGUMENTS, 0);
2052  OP2(SLJIT_ADD, SLJIT_GENERAL_REG1, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, OVECTOR_START);  if (common->mark_ptr != 0)
2053      OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mark_ptr);
2054    OP1(SLJIT_MOV_SI, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, offset_count));
2055    if (common->mark_ptr != 0)
2056      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, mark_ptr), SLJIT_SCRATCH_REG3, 0);
2057    OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, offsets), SLJIT_IMM, sizeof(int));
2058    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG1, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), SLJIT_OFFSETOF(jit_arguments, begin));
2059    GET_LOCAL_BASE(SLJIT_SAVED_REG1, 0, OVECTOR_START);
2060  /* Unlikely, but possible */  /* Unlikely, but possible */
2061  earlyexit = CMP(SLJIT_C_EQUAL, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 0);  early_quit = CMP(SLJIT_C_EQUAL, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, 0);
2062  loop = LABEL();  loop = LABEL();
2063  OP2(SLJIT_SUB, SLJIT_GENERAL_REG2, 0, SLJIT_MEM1(SLJIT_GENERAL_REG1), 0, SLJIT_TEMPORARY_REG1, 0);  OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, SLJIT_MEM1(SLJIT_SAVED_REG1), 0, SLJIT_SCRATCH_REG1, 0);
2064  OP2(SLJIT_ADD, SLJIT_GENERAL_REG1, 0, SLJIT_GENERAL_REG1, 0, SLJIT_IMM, sizeof(sljit_w));  OP2(SLJIT_ADD, SLJIT_SAVED_REG1, 0, SLJIT_SAVED_REG1, 0, SLJIT_IMM, sizeof(sljit_sw));
2065  /* Copy the integer value to the output buffer */  /* Copy the integer value to the output buffer */
2066  OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_TEMPORARY_REG3), sizeof(int), SLJIT_GENERAL_REG2, 0);  #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2067  OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0, SLJIT_IMM, 1);  OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, UCHAR_SHIFT);
2068    #endif
2069    OP1(SLJIT_MOVU_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG3), sizeof(int), SLJIT_SAVED_REG2, 0);
2070    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_SCRATCH_REG2, 0, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, 1);
2071  JUMPTO(SLJIT_C_NOT_ZERO, loop);  JUMPTO(SLJIT_C_NOT_ZERO, loop);
2072  JUMPHERE(earlyexit);  JUMPHERE(early_quit);
2073    
2074    /* Calculate the return value, which is the maximum ovector value. */
2075    if (topbracket > 1)
2076      {
2077      GET_LOCAL_BASE(SLJIT_SCRATCH_REG1, 0, OVECTOR_START + topbracket * 2 * sizeof(sljit_sw));
2078      OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, topbracket + 1);
2079    
2080      /* OVECTOR(0) is never equal to SLJIT_SAVED_REG3. */
2081      loop = LABEL();
2082      OP1(SLJIT_MOVU, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG1), -(2 * (sljit_sw)sizeof(sljit_sw)));
2083      OP2(SLJIT_SUB, SLJIT_SCRATCH_REG2, 0, SLJIT_SCRATCH_REG2, 0, SLJIT_IMM, 1);
2084      CMPTO(SLJIT_C_EQUAL, SLJIT_SCRATCH_REG3, 0, SLJIT_SAVED_REG3, 0, loop);
2085      OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_SCRATCH_REG2, 0);
2086      }
2087    else
2088      OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, 1);
2089    }
2090    
2091    static SLJIT_INLINE void return_with_partial_match(compiler_common *common, struct sljit_label *quit)
2092    {
2093    DEFINE_COMPILER;
2094    struct sljit_jump *jump;
2095    
2096    SLJIT_COMPILE_ASSERT(STR_END == SLJIT_SAVED_REG2, str_end_must_be_saved_reg2);
2097    SLJIT_ASSERT(common->start_used_ptr != 0 && common->start_ptr != 0
2098      && (common->mode == JIT_PARTIAL_SOFT_COMPILE ? common->hit_start != 0 : common->hit_start == 0));
2099    
2100    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, ARGUMENTS, 0);
2101    OP1(SLJIT_MOV, SLJIT_RETURN_REG, 0, SLJIT_IMM, PCRE_ERROR_PARTIAL);
2102    OP1(SLJIT_MOV_SI, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, real_offset_count));
2103    CMPTO(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 2, quit);
2104    
2105    /* Store match begin and end. */
2106    OP1(SLJIT_MOV, SLJIT_SAVED_REG1, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, begin));
2107    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG2, 0, SLJIT_MEM1(SLJIT_SCRATCH_REG2), SLJIT_OFFSETOF(jit_arguments, offsets));
2108    
2109    jump = CMP(SLJIT_C_SIG_LESS, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, 3);
2110    OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_ptr : (common->hit_start + (int)sizeof(sljit_sw)), SLJIT_SAVED_REG1, 0);
2111    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2112    OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);
2113    #endif
2114    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG2), 2 * sizeof(int), SLJIT_SCRATCH_REG3, 0);
2115    JUMPHERE(jump);
2116    
2117    OP1(SLJIT_MOV, SLJIT_SCRATCH_REG3, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->mode == JIT_PARTIAL_HARD_COMPILE ? common->start_used_ptr : common->hit_start);
2118    OP2(SLJIT_SUB, SLJIT_SAVED_REG2, 0, STR_END, 0, SLJIT_SAVED_REG1, 0);
2119    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2120    OP2(SLJIT_ASHR, SLJIT_SAVED_REG2, 0, SLJIT_SAVED_REG2, 0, SLJIT_IMM, UCHAR_SHIFT);
2121    #endif
2122    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG2), sizeof(int), SLJIT_SAVED_REG2, 0);
2123    
2124    OP2(SLJIT_SUB, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_SAVED_REG1, 0);
2125    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2126    OP2(SLJIT_ASHR, SLJIT_SCRATCH_REG3, 0, SLJIT_SCRATCH_REG3, 0, SLJIT_IMM, UCHAR_SHIFT);
2127    #endif
2128    OP1(SLJIT_MOV_SI, SLJIT_MEM1(SLJIT_SCRATCH_REG2), 0, SLJIT_SCRATCH_REG3, 0);
2129    
2130    JUMPTO(SLJIT_JUMP, quit);
2131    }
2132    
2133    static SLJIT_INLINE void check_start_used_ptr(compiler_common *common)
2134    {
2135    /* May destroy TMP1. */
2136    DEFINE_COMPILER;
2137    struct sljit_jump *jump;
2138    
2139    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2140      {
2141      /* The value of -1 must be kept for start_used_ptr! */
2142      OP2(SLJIT_ADD, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, 1);
2143      /* Jumps if start_used_ptr < STR_PTR, or start_used_ptr == -1. Although overwriting
2144      is not necessary if start_used_ptr == STR_PTR, it does not hurt as well. */
2145      jump = CMP(SLJIT_C_LESS_EQUAL, TMP1, 0, STR_PTR, 0);
2146      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);
2147      JUMPHERE(jump);
2148      }
2149    else if (common->mode == JIT_PARTIAL_HARD_COMPILE)
2150      {
2151      jump = CMP(SLJIT_C_LESS_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);
2152      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);
2153      JUMPHERE(jump);
2154      }
2155  }  }
2156    
2157  static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, uschar* cc)  static SLJIT_INLINE BOOL char_has_othercase(compiler_common *common, pcre_uchar* cc)
2158  {  {
2159  /* Detects if the character has an othercase. */  /* Detects if the character has an othercase. */
2160  unsigned int c;  unsigned int c;
2161    
2162  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2163  if (common->utf8)  if (common->utf)
2164    {    {
2165    GETCHAR(c, cc);    GETCHAR(c, cc);
2166    if (c > 127)    if (c > 127)
# Line 1277  if (common->utf8) Line 2171  if (common->utf8)
2171      return FALSE;      return FALSE;
2172  #endif  #endif
2173      }      }
2174    #ifndef COMPILE_PCRE8
2175      return common->fcc[c] != c;
2176    #endif
2177    }    }
2178  else  else
2179  #endif  #endif
2180    c = *cc;    c = *cc;
2181  return common->fcc[c] != c;  return MAX_255(c) ? common->fcc[c] != c : FALSE;
2182  }  }
2183    
2184  static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)  static SLJIT_INLINE unsigned int char_othercase(compiler_common *common, unsigned int c)
2185  {  {
2186  /* Returns with the othercase. */  /* Returns with the othercase. */
2187  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2188  if (common->utf8 && c > 127)  if (common->utf && c > 127)
2189    {    {
2190  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2191    return UCD_OTHERCASE(c);    return UCD_OTHERCASE(c);
# Line 1297  if (common->utf8 && c > 127) Line 2194  if (common->utf8 && c > 127)
2194  #endif  #endif
2195    }    }
2196  #endif  #endif
2197  return common->fcc[c];  return TABLE_GET(c, common->fcc, c);
2198  }  }
2199    
2200  static unsigned int char_get_othercase_bit(compiler_common *common, uschar* cc)  static unsigned int char_get_othercase_bit(compiler_common *common, pcre_uchar* cc)
2201  {  {
2202  /* Detects if the character and its othercase has only 1 bit difference. */  /* Detects if the character and its othercase has only 1 bit difference. */
2203  unsigned int c, oc, bit;  unsigned int c, oc, bit;
2204  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
2205  int n;  int n;
2206  #endif  #endif
2207    
2208  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2209  if (common->utf8)  if (common->utf)
2210    {    {
2211    GETCHAR(c, cc);    GETCHAR(c, cc);
2212    if (c <= 127)    if (c <= 127)
# Line 1326  if (common->utf8) Line 2223  if (common->utf8)
2223  else  else
2224    {    {
2225    c = *cc;    c = *cc;
2226    oc = common->fcc[c];    oc = TABLE_GET(c, common->fcc, c);
2227    }    }
2228  #else  #else
2229  c = *cc;  c = *cc;
2230  oc = common->fcc[c];  oc = TABLE_GET(c, common->fcc, c);
2231  #endif  #endif
2232    
2233  SLJIT_ASSERT(c != oc);  SLJIT_ASSERT(c != oc);
# Line 1341  if (c <= 127 && bit == 0x20) Line 2238  if (c <= 127 && bit == 0x20)
2238    return (0 << 8) | 0x20;    return (0 << 8) | 0x20;
2239    
2240  /* Since c != oc, they must have at least 1 bit difference. */  /* Since c != oc, they must have at least 1 bit difference. */
2241  if (!ispowerof2(bit))  if (!is_powerof2(bit))
2242    return 0;    return 0;
2243    
2244  #ifdef SUPPORT_UTF8  #if defined COMPILE_PCRE8
2245  if (common->utf8 && c > 127)  
2246    #ifdef SUPPORT_UTF
2247    if (common->utf && c > 127)
2248    {    {
2249    n = _pcre_utf8_table4[*cc & 0x3f];    n = GET_EXTRALEN(*cc);
2250    while ((bit & 0x3f) == 0)    while ((bit & 0x3f) == 0)
2251      {      {
2252      n--;      n--;
# Line 1355  if (common->utf8 && c > 127) Line 2254  if (common->utf8 && c > 127)
2254      }      }
2255    return (n << 8) | bit;    return (n << 8) | bit;
2256    }    }
2257  #endif  #endif /* SUPPORT_UTF */
2258  return (0 << 8) | bit;  return (0 << 8) | bit;
2259    
2260    #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2261    
2262    #ifdef SUPPORT_UTF
2263    if (common->utf && c > 65535)
2264      {
2265      if (bit >= (1 << 10))
2266        bit >>= 10;
2267      else
2268        return (bit < 256) ? ((2 << 8) | bit) : ((3 << 8) | (bit >> 8));
2269      }
2270    #endif /* SUPPORT_UTF */
2271    return (bit < 256) ? ((0 << 8) | bit) : ((1 << 8) | (bit >> 8));
2272    
2273    #endif /* COMPILE_PCRE[8|16|32] */
2274    }
2275    
2276    static void check_partial(compiler_common *common, BOOL force)
2277    {
2278    /* Checks whether a partial matching is occured. Does not modify registers. */
2279    DEFINE_COMPILER;
2280    struct sljit_jump *jump = NULL;
2281    
2282    SLJIT_ASSERT(!force || common->mode != JIT_COMPILE);
2283    
2284    if (common->mode == JIT_COMPILE)
2285      return;
2286    
2287    if (!force)
2288      jump = CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0);
2289    else if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2290      jump = CMP(SLJIT_C_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, SLJIT_IMM, -1);
2291    
2292    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2293      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2294    else
2295      {
2296      if (common->partialmatchlabel != NULL)
2297        JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2298      else
2299        add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2300      }
2301    
2302    if (jump != NULL)
2303      JUMPHERE(jump);
2304    }
2305    
2306    static void check_str_end(compiler_common *common, jump_list **end_reached)
2307    {
2308    /* Does not affect registers. Usually used in a tight spot. */
2309    DEFINE_COMPILER;
2310    struct sljit_jump *jump;
2311    
2312    if (common->mode == JIT_COMPILE)
2313      {
2314      add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2315      return;
2316      }
2317    
2318    jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);
2319    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2320      {
2321      add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2322      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2323      add_jump(compiler, end_reached, JUMP(SLJIT_JUMP));
2324      }
2325    else
2326      {
2327      add_jump(compiler, end_reached, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2328      if (common->partialmatchlabel != NULL)
2329        JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2330      else
2331        add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2332      }
2333    JUMPHERE(jump);
2334  }  }
2335    
2336  static SLJIT_INLINE void check_input_end(compiler_common *common, jump_list **fallbacks)  static void detect_partial_match(compiler_common *common, jump_list **backtracks)
2337  {  {
2338  DEFINE_COMPILER;  DEFINE_COMPILER;
2339  add_jump(compiler, fallbacks, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));  struct sljit_jump *jump;
2340    
2341    if (common->mode == JIT_COMPILE)
2342      {
2343      add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0));
2344      return;
2345      }
2346    
2347    /* Partial matching mode. */
2348    jump = CMP(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0);
2349    add_jump(compiler, backtracks, CMP(SLJIT_C_GREATER_EQUAL, SLJIT_MEM1(SLJIT_LOCALS_REG), common->start_used_ptr, STR_PTR, 0));
2350    if (common->mode == JIT_PARTIAL_SOFT_COMPILE)
2351      {
2352      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->hit_start, SLJIT_IMM, 0);
2353      add_jump(compiler, backtracks, JUMP(SLJIT_JUMP));
2354      }
2355    else
2356      {
2357      if (common->partialmatchlabel != NULL)
2358        JUMPTO(SLJIT_JUMP, common->partialmatchlabel);
2359      else
2360        add_jump(compiler, &common->partialmatch, JUMP(SLJIT_JUMP));
2361      }
2362    JUMPHERE(jump);
2363  }  }
2364    
2365  static void read_char(compiler_common *common)  static void read_char(compiler_common *common)
# Line 1370  static void read_char(compiler_common *c Line 2367  static void read_char(compiler_common *c
2367  /* Reads the character into TMP1, updates STR_PTR.  /* Reads the character into TMP1, updates STR_PTR.
2368  Does not check STR_END. TMP2 Destroyed. */  Does not check STR_END. TMP2 Destroyed. */
2369  DEFINE_COMPILER;  DEFINE_COMPILER;
2370  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2371  struct sljit_jump *jump;  struct sljit_jump *jump;
2372  #endif  #endif
2373    
2374  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2375  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2376  if (common->utf8)  if (common->utf)
2377    {    {
2378    /* Should not found a value between 128 and 192 here. */  #if defined COMPILE_PCRE8
2379    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 192);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2380    add_jump(compiler, &common->utf8readchar, JUMP(SLJIT_FAST_CALL));  #elif defined COMPILE_PCRE16
2381      jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
2382    #endif /* COMPILE_PCRE[8|16] */
2383      add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2384    JUMPHERE(jump);    JUMPHERE(jump);
2385    }    }
2386  #endif  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2387  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2388  }  }
2389    
2390  static void peek_char(compiler_common *common)  static void peek_char(compiler_common *common)
# Line 1392  static void peek_char(compiler_common *c Line 2392  static void peek_char(compiler_common *c
2392  /* Reads the character into TMP1, keeps STR_PTR.  /* Reads the character into TMP1, keeps STR_PTR.
2393  Does not check STR_END. TMP2 Destroyed. */  Does not check STR_END. TMP2 Destroyed. */
2394  DEFINE_COMPILER;  DEFINE_COMPILER;
2395  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2396  struct sljit_jump *jump;  struct sljit_jump *jump;
2397  #endif  #endif
2398    
2399  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2400  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2401  if (common->utf8)  if (common->utf)
2402    {    {
2403    /* Should not found a value between 128 and 192 here. */  #if defined COMPILE_PCRE8
2404    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 192);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2405    add_jump(compiler, &common->utf8readchar, JUMP(SLJIT_FAST_CALL));  #elif defined COMPILE_PCRE16
2406      jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
2407    #endif /* COMPILE_PCRE[8|16] */
2408      add_jump(compiler, &common->utfreadchar, JUMP(SLJIT_FAST_CALL));
2409    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2410    JUMPHERE(jump);    JUMPHERE(jump);
2411    }    }
2412  #endif  #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2413  }  }
2414    
2415  static void read_char8_type(compiler_common *common)  static void read_char8_type(compiler_common *common)
2416  {  {
2417  /* 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. */
2418  DEFINE_COMPILER;  DEFINE_COMPILER;
2419  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2420  struct sljit_jump *jump;  struct sljit_jump *jump;
2421  #endif  #endif
2422    
2423  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2424  if (common->utf8)  if (common->utf)
2425    {    {
2426    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2427    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2428    #if defined COMPILE_PCRE8
2429    /* This can be an extra read in some situations, but hopefully    /* This can be an extra read in some situations, but hopefully
2430    it is a clever early read in most cases. */    it is needed in most cases. */
2431      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2432      jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 0xc0);
2433      add_jump(compiler, &common->utfreadtype8, JUMP(SLJIT_FAST_CALL));
2434      JUMPHERE(jump);
2435    #elif defined COMPILE_PCRE16
2436      OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2437      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2438      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2439      JUMPHERE(jump);
2440      /* Skip low surrogate if necessary. */
2441      OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0xfc00);
2442      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0xd800);
2443      OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2444      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
2445      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
2446    #elif defined COMPILE_PCRE32
2447      OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2448      jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2449    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
   /* Should not found a value between 128 and 192 here. */  
   jump = CMP(SLJIT_C_LESS, TMP2, 0, SLJIT_IMM, 192);  
   add_jump(compiler, &common->utf8readtype8, JUMP(SLJIT_FAST_CALL));  
2450    JUMPHERE(jump);    JUMPHERE(jump);
2451    #endif /* COMPILE_PCRE[8|16|32] */
2452    return;    return;
2453    }    }
2454    #endif /* SUPPORT_UTF */
2455    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);
2456    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2457    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2458    /* The ctypes array contains only 256 values. */
2459    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2460    jump = CMP(SLJIT_C_GREATER, TMP2, 0, SLJIT_IMM, 255);
2461    #endif
2462    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), common->ctypes);
2463    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2464    JUMPHERE(jump);
2465  #endif  #endif
 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  
 OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);  
2466  }  }
2467    
2468  static void skip_char_back(compiler_common *common)  static void skip_char_back(compiler_common *common)
2469  {  {
2470  /* Goes one character back. Only affects STR_PTR. Does not check begin. */  /* Goes one character back. Affects STR_PTR and TMP1. Does not check begin. */
2471  DEFINE_COMPILER;  DEFINE_COMPILER;
2472  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2473    #if defined COMPILE_PCRE8
2474  struct sljit_label *label;  struct sljit_label *label;
2475    
2476  if (common->utf8)  if (common->utf)
2477    {    {
2478    label = LABEL();    label = LABEL();
2479    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2480    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2481    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xc0);
2482    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, 0x80, label);
2483    return;    return;
2484    }    }
2485  #endif  #elif defined COMPILE_PCRE16
2486  OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  if (common->utf)
2487      {
2488      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), -IN_UCHARS(1));
2489      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2490      /* Skip low surrogate if necessary. */
2491      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
2492      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xdc00);
2493      OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2494      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
2495      OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2496      return;
2497      }
2498    #endif /* COMPILE_PCRE[8|16] */
2499    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2500    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2501  }  }
2502    
2503  static void check_newlinechar(compiler_common *common, int nltype, jump_list **fallbacks, BOOL jumpiftrue)  static void check_newlinechar(compiler_common *common, int nltype, jump_list **backtracks, BOOL jumpiftrue)
2504  {  {
2505  /* 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. */
2506  DEFINE_COMPILER;  DEFINE_COMPILER;
# Line 1465  DEFINE_COMPILER; Line 2508  DEFINE_COMPILER;
2508  if (nltype == NLTYPE_ANY)  if (nltype == NLTYPE_ANY)
2509    {    {
2510    add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->anynewline, JUMP(SLJIT_FAST_CALL));
2511    add_jump(compiler, fallbacks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
2512    }    }
2513  else if (nltype == NLTYPE_ANYCRLF)  else if (nltype == NLTYPE_ANYCRLF)
2514    {    {
2515    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_CR);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_CR);
2516    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2517    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
2518    COND_VALUE(SLJIT_OR | SLJIT_SET_E, TMP2, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
2519    add_jump(compiler, fallbacks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));    add_jump(compiler, backtracks, JUMP(jumpiftrue ? SLJIT_C_NOT_ZERO : SLJIT_C_ZERO));
2520    }    }
2521  else  else
2522    {    {
2523    SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline <= 255);    SLJIT_ASSERT(nltype == NLTYPE_FIXED && common->newline < 256);
2524    add_jump(compiler, fallbacks, CMP(jumpiftrue ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));    add_jump(compiler, backtracks, CMP(jumpiftrue ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, common->newline));
2525    }    }
2526  }  }
2527    
2528  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2529  static void do_utf8readchar(compiler_common *common)  
2530    #if defined COMPILE_PCRE8
2531    static void do_utfreadchar(compiler_common *common)
2532  {  {
2533  /* Fast decoding an utf8 character. TMP1 contains the first byte  /* Fast decoding a UTF-8 character. TMP1 contains the first byte
2534  of the character (>= 192). Return char value in TMP1, length - 1 in TMP2. */  of the character (>= 0xc0). Return char value in TMP1, length - 1 in TMP2. */
2535  DEFINE_COMPILER;  DEFINE_COMPILER;
2536  struct sljit_jump *jump;  struct sljit_jump *jump;
2537    
2538  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0, 1, 5, 5, common->localsize);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2539  /* Searching for the first zero. */  /* Searching for the first zero. */
2540  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x20);
2541  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2542  /* 2 byte sequence */  /* Two byte sequence. */
2543  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 1);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2544  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2545  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1f);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x1f);
2546  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 6);
2547  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2548  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2549  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
2550  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2551  JUMPHERE(jump);  JUMPHERE(jump);
2552    
2553  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x10);
2554  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2555  /* 3 byte sequence */  /* Three byte sequence. */
2556  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 1);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2557  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0f);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x0f);
2558  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 12);  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 12);
2559  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2560  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2561  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2562  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 2);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2563  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 2);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(2));
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 2);  
 sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  
 JUMPHERE(jump);  
   
 OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x08);  
 jump = JUMP(SLJIT_C_NOT_ZERO);  
 /* 4 byte sequence */  
 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 1);  
 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);  
 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 2);  
 OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  
 OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 3);  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 3);  
2564  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2565  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2566  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 3);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(2));
2567  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2568  JUMPHERE(jump);  JUMPHERE(jump);
2569    
2570  /* 5 byte sequence */  /* Four byte sequence. */
2571  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 1);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2572  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x03);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x07);
2573  OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 24);  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, 18);  
 OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  
 OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 2);  
2574  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2575  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 12);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 12);
2576  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2577  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 3);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(2));
2578  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2579  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2580  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2581  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 4);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(3));
2582  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 4);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(3));
2583  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3f);
2584  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2585  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 4);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(3));
2586  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2587  }  }
2588    
2589  static void do_utf8readtype8(compiler_common *common)  static void do_utfreadtype8(compiler_common *common)
2590  {  {
2591  /* Fast decoding an utf8 character type. TMP2 contains the first byte  /* Fast decoding a UTF-8 character type. TMP2 contains the first byte
2592  of the character (>= 192) and TMP1 is destroyed. Return value in TMP1. */  of the character (>= 0xc0). Return value in TMP1. */
2593  DEFINE_COMPILER;  DEFINE_COMPILER;
2594  struct sljit_jump *jump;  struct sljit_jump *jump;
2595  struct sljit_jump *compare;  struct sljit_jump *compare;
2596    
2597  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0, 1, 5, 5, common->localsize);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2598    
2599  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0x20);
2600  jump = JUMP(SLJIT_C_NOT_ZERO);  jump = JUMP(SLJIT_C_NOT_ZERO);
2601  /* 2 byte sequence */  /* Two byte sequence. */
2602  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2603  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2604  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);  OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x1f);
2605  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, 6);
2606  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3f);
# Line 1595  sljit_emit_fast_return(compiler, RETURN_ Line 2615  sljit_emit_fast_return(compiler, RETURN_
2615  JUMPHERE(jump);  JUMPHERE(jump);
2616    
2617  /* We only have types for characters less than 256. */  /* We only have types for characters less than 256. */
2618  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), (sljit_w)_pcre_utf8_char_sizes);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(utf8_table4) - 0xc0);
 OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);  
2619  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2620  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 0);
2621  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2622  }  }
2623    
2624  #endif  #elif defined COMPILE_PCRE16
2625    
2626    static void do_utfreadchar(compiler_common *common)
2627    {
2628    /* Fast decoding a UTF-16 character. TMP1 contains the first 16 bit char
2629    of the character (>= 0xd800). Return char value in TMP1, length - 1 in TMP2. */
2630    DEFINE_COMPILER;
2631    struct sljit_jump *jump;
2632    
2633    sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2634    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xdc00);
2635    /* Do nothing, only return. */
2636    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2637    
2638    JUMPHERE(jump);
2639    /* Combine two 16 bit characters. */
2640    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2641    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2642    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x3ff);
2643    OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 10);
2644    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 0x3ff);
2645    OP2(SLJIT_OR, TMP1, 0, TMP1, 0, TMP2, 0);
2646    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, IN_UCHARS(1));
2647    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 0x10000);
2648    sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2649    }
2650    
2651    #endif /* COMPILE_PCRE[8|16] */
2652    
2653    #endif /* SUPPORT_UTF */
2654    
2655  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2656    
# Line 1618  DEFINE_COMPILER; Line 2666  DEFINE_COMPILER;
2666    
2667  SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8);  SLJIT_ASSERT(UCD_BLOCK_SIZE == 128 && sizeof(ucd_record) == 8);
2668    
2669  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0, 1, 5, 5, common->localsize);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
2670  OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);  OP2(SLJIT_LSHR, TMP2, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
2671  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_w)_pcre_ucd_stage1);  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP2), (sljit_sw)PRIV(ucd_stage1));
2672  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);  OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, UCD_BLOCK_MASK);
2673  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);  OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCD_BLOCK_SHIFT);
2674  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, TMP2, 0);
2675  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_w)_pcre_ucd_stage2);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_stage2));
2676  OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);  OP1(SLJIT_MOV_UH, TMP2, 0, SLJIT_MEM2(TMP2, TMP1), 1);
2677  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_w)_pcre_ucd_records + SLJIT_OFFSETOF(ucd_record, chartype));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, (sljit_sw)PRIV(ucd_records) + SLJIT_OFFSETOF(ucd_record, chartype));
2678  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM2(TMP1, TMP2), 3);
2679  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
2680  }  }
# Line 1640  struct sljit_label *newlinelabel = NULL; Line 2688  struct sljit_label *newlinelabel = NULL;
2688  struct sljit_jump *start;  struct sljit_jump *start;
2689  struct sljit_jump *end = NULL;  struct sljit_jump *end = NULL;
2690  struct sljit_jump *nl = NULL;  struct sljit_jump *nl = NULL;
2691    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2692    struct sljit_jump *singlechar;
2693    #endif
2694  jump_list *newline = NULL;  jump_list *newline = NULL;
2695  BOOL newlinecheck = FALSE;  BOOL newlinecheck = FALSE;
2696  BOOL readbyte = FALSE;  BOOL readuchar = FALSE;
2697    
2698  if (!(hascrorlf || firstline) && (common->nltype == NLTYPE_ANY ||  if (!(hascrorlf || firstline) && (common->nltype == NLTYPE_ANY ||
2699      common->nltype == NLTYPE_ANYCRLF || common->newline > 255))      common->nltype == NLTYPE_ANYCRLF || common->newline > 255))
# Line 1651  if (!(hascrorlf || firstline) && (common Line 2702  if (!(hascrorlf || firstline) && (common
2702  if (firstline)  if (firstline)
2703    {    {
2704    /* Search for the end of the first line. */    /* Search for the end of the first line. */
2705    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, STR_PTR, 0);    SLJIT_ASSERT(common->first_line_end != 0);
2706    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END, STR_END, 0);    OP1(SLJIT_MOV, TMP3, 0, STR_PTR, 0);
2707    
2708    if (common->nltype == NLTYPE_FIXED && common->newline > 255)    if (common->nltype == NLTYPE_FIXED && common->newline > 255)
2709      {      {
2710      mainloop = LABEL();      mainloop = LABEL();
2711      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2712      end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);      end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
2713      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), -1);      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
2714      OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), 0);      OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2715      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, mainloop);
2716      CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);      CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, mainloop);
2717      OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END, STR_PTR, 0, SLJIT_IMM, 1);      JUMPHERE(end);
2718        OP2(SLJIT_SUB, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2719      }      }
2720    else    else
2721      {      {
2722      end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);      end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
2723      mainloop = LABEL();      mainloop = LABEL();
2724      /* Continual stores does not cause data dependency. */      /* Continual stores does not cause data dependency. */
2725      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END, STR_PTR, 0);      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0);
2726      read_char(common);      read_char(common);
2727      check_newlinechar(common, common->nltype, &newline, TRUE);      check_newlinechar(common, common->nltype, &newline, TRUE);
2728      CMPTO(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0, mainloop);      CMPTO(SLJIT_C_LESS, STR_PTR, 0, STR_END, 0, mainloop);
2729      OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END, STR_PTR, 0);      JUMPHERE(end);
2730        OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, STR_PTR, 0);
2731      set_jumps(newline, LABEL());      set_jumps(newline, LABEL());
2732      }      }
2733    
2734    JUMPHERE(end);    OP1(SLJIT_MOV, STR_PTR, 0, TMP3, 0);
   OP1(SLJIT_MOV, STR_PTR, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);  
2735    }    }
2736    
2737  start = JUMP(SLJIT_JUMP);  start = JUMP(SLJIT_JUMP);
# Line 1687  start = JUMP(SLJIT_JUMP); Line 2739  start = JUMP(SLJIT_JUMP);
2739  if (newlinecheck)  if (newlinecheck)
2740    {    {
2741    newlinelabel = LABEL();    newlinelabel = LABEL();
2742    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2743    end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    end = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
2744    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2745    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);
2746    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2747    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
2748      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
2749    #endif
2750    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2751    nl = JUMP(SLJIT_JUMP);    nl = JUMP(SLJIT_JUMP);
2752    }    }
# Line 1699  if (newlinecheck) Line 2754  if (newlinecheck)
2754  mainloop = LABEL();  mainloop = LABEL();
2755    
2756  /* Increasing the STR_PTR here requires one less jump in the most common case. */  /* Increasing the STR_PTR here requires one less jump in the most common case. */
2757  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2758  if (common->utf8) readbyte = TRUE;  if (common->utf) readuchar = TRUE;
2759  #endif  #endif
2760  if (newlinecheck) readbyte = TRUE;  if (newlinecheck) readuchar = TRUE;
2761    
2762  if (readbyte)  if (readuchar)
2763    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2764    
2765  if (newlinecheck)  if (newlinecheck)
2766    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);    CMPTO(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, newlinelabel);
2767    
2768  #ifdef SUPPORT_UTF8  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2769  if (common->utf8)  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2770    #if defined COMPILE_PCRE8
2771    if (common->utf)
2772    {    {
2773    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_w)_pcre_utf8_char_sizes);    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0);
2774      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
2775    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2776      JUMPHERE(singlechar);
2777    }    }
2778  else  #elif defined COMPILE_PCRE16
2779    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  if (common->utf)
2780  #else    {
2781  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    singlechar = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800);
2782  #endif    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
2783      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
2784      OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
2785      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
2786      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
2787      JUMPHERE(singlechar);
2788      }
2789    #endif /* COMPILE_PCRE[8|16] */
2790    #endif /* SUPPORT_UTF && !COMPILE_PCRE32 */
2791  JUMPHERE(start);  JUMPHERE(start);
2792    
2793  if (newlinecheck)  if (newlinecheck)
# Line 1732  if (newlinecheck) Line 2799  if (newlinecheck)
2799  return mainloop;  return mainloop;
2800  }  }
2801    
2802  static SLJIT_INLINE void fast_forward_first_byte(compiler_common *common, pcre_uint16 firstbyte, BOOL firstline)  #define MAX_N_CHARS 3
2803    
2804    static SLJIT_INLINE BOOL fast_forward_first_n_chars(compiler_common *common, BOOL firstline)
2805    {
2806    DEFINE_COMPILER;
2807    struct sljit_label *start;
2808    struct sljit_jump *quit;
2809    pcre_uint32 chars[MAX_N_CHARS * 2];
2810    pcre_uchar *cc = common->start + 1 + LINK_SIZE;
2811    int location = 0;
2812    pcre_int32 len, c, bit, caseless;
2813    int must_stop;
2814    
2815    /* We do not support alternatives now. */
2816    if (*(common->start + GET(common->start, 1)) == OP_ALT)
2817      return FALSE;
2818    
2819    while (TRUE)
2820      {
2821      caseless = 0;
2822      must_stop = 1;
2823      switch(*cc)
2824        {
2825        case OP_CHAR:
2826        must_stop = 0;
2827        cc++;
2828        break;
2829    
2830        case OP_CHARI:
2831        caseless = 1;
2832        must_stop = 0;
2833        cc++;
2834        break;
2835    
2836        case OP_SOD:
2837        case OP_SOM:
2838        case OP_SET_SOM:
2839        case OP_NOT_WORD_BOUNDARY:
2840        case OP_WORD_BOUNDARY:
2841        case OP_EODN:
2842        case OP_EOD:
2843        case OP_CIRC:
2844        case OP_CIRCM:
2845        case OP_DOLL:
2846        case OP_DOLLM:
2847        /* Zero width assertions. */
2848        cc++;
2849        continue;
2850    
2851        case OP_PLUS:
2852        case OP_MINPLUS:
2853        case OP_POSPLUS:
2854        cc++;
2855        break;
2856    
2857        case OP_EXACT:
2858        cc += 1 + IMM2_SIZE;
2859        break;
2860    
2861        case OP_PLUSI:
2862        case OP_MINPLUSI:
2863        case OP_POSPLUSI:
2864        caseless = 1;
2865        cc++;
2866        break;
2867    
2868        case OP_EXACTI:
2869        caseless = 1;
2870        cc += 1 + IMM2_SIZE;
2871        break;
2872    
2873        default:
2874        must_stop = 2;
2875        break;
2876        }
2877    
2878      if (must_stop == 2)
2879          break;
2880    
2881      len = 1;
2882    #ifdef SUPPORT_UTF
2883      if (common->utf && HAS_EXTRALEN(cc[0])) len += GET_EXTRALEN(cc[0]);
2884    #endif
2885    
2886      if (caseless && char_has_othercase(common, cc))
2887        {
2888        caseless = char_get_othercase_bit(common, cc);
2889        if (caseless == 0)
2890          return FALSE;
2891    #ifdef COMPILE_PCRE8
2892        caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 8));
2893    #else
2894        if ((caseless & 0x100) != 0)
2895          caseless = ((caseless & 0xff) << 16) | (len - (caseless >> 9));
2896        else
2897          caseless = ((caseless & 0xff) << 8) | (len - (caseless >> 9));
2898    #endif
2899        }
2900      else
2901        caseless = 0;
2902    
2903      while (len > 0 && location < MAX_N_CHARS * 2)
2904        {
2905        c = *cc;
2906        bit = 0;
2907        if (len == (caseless & 0xff))
2908          {
2909          bit = caseless >> 8;
2910          c |= bit;
2911          }
2912    
2913        chars[location] = c;
2914        chars[location + 1] = bit;
2915    
2916        len--;
2917        location += 2;
2918        cc++;
2919        }
2920    
2921      if (location >= MAX_N_CHARS * 2 || must_stop != 0)
2922        break;
2923      }
2924    
2925    /* At least two characters are required. */
2926    if (location < 2 * 2)
2927        return FALSE;
2928    
2929    if (firstline)
2930      {
2931      SLJIT_ASSERT(common->first_line_end != 0);
2932      OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
2933      OP2(SLJIT_SUB, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));
2934      }
2935    else
2936      OP2(SLJIT_SUB, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));
2937    
2938    start = LABEL();
2939    quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
2940    
2941    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(0));
2942    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2943    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2944    if (chars[1] != 0)
2945      OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[1]);
2946    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[0], start);
2947    if (location > 2 * 2)
2948      OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(1));
2949    if (chars[3] != 0)
2950      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, chars[3]);
2951    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, chars[2], start);
2952    if (location > 2 * 2)
2953      {
2954      if (chars[5] != 0)
2955        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, chars[5]);
2956      CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, chars[4], start);
2957      }
2958    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
2959    
2960    JUMPHERE(quit);
2961    
2962    if (firstline)
2963      OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
2964    else
2965      OP2(SLJIT_ADD, STR_END, 0, STR_END, 0, SLJIT_IMM, IN_UCHARS((location >> 1) - 1));
2966    return TRUE;
2967    }
2968    
2969    #undef MAX_N_CHARS
2970    
2971    static SLJIT_INLINE void fast_forward_first_char(compiler_common *common, pcre_uchar first_char, BOOL caseless, BOOL firstline)
2972  {  {
2973  DEFINE_COMPILER;  DEFINE_COMPILER;
2974  struct sljit_label *start;  struct sljit_label *start;
2975  struct sljit_jump *leave;  struct sljit_jump *quit;
2976  struct sljit_jump *found;  struct sljit_jump *found;
2977  pcre_uint16 oc, bit;  pcre_uchar oc, bit;
2978    
2979  if (firstline)  if (firstline)
2980    {    {
2981    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, STR_END, 0);    SLJIT_ASSERT(common->first_line_end != 0);
2982    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
2983      OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
2984    }    }
2985    
2986  start = LABEL();  start = LABEL();
2987  leave = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
2988  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
2989    
2990  if ((firstbyte & REQ_CASELESS) == 0)  oc = first_char;
2991    found = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, firstbyte & 0xff);  if (caseless)
2992      {
2993      oc = TABLE_GET(first_char, common->fcc, first_char);
2994    #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
2995      if (first_char > 127 && common->utf)
2996        oc = UCD_OTHERCASE(first_char);
2997    #endif
2998      }
2999    if (first_char == oc)
3000      found = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, first_char);
3001  else  else
3002    {    {
3003    firstbyte &= 0xff;    bit = first_char ^ oc;
3004    oc = common->fcc[firstbyte];    if (is_powerof2(bit))
   bit = firstbyte ^ oc;  
   if (ispowerof2(bit))  
3005      {      {
3006      OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, bit);      OP2(SLJIT_OR, TMP2, 0, TMP1, 0, SLJIT_IMM, bit);
3007      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, firstbyte | bit);      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, first_char | bit);
3008      }      }
3009    else    else
3010      {      {
3011      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, firstbyte);      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, first_char);
3012      COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_EQUAL);      OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3013      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, oc);
3014      COND_VALUE(SLJIT_OR | SLJIT_SET_E, TMP2, 0, SLJIT_C_EQUAL);      OP_FLAGS(SLJIT_OR | SLJIT_SET_E, TMP2, 0, TMP2, 0, SLJIT_C_EQUAL);
3015      found = JUMP(SLJIT_C_NOT_ZERO);      found = JUMP(SLJIT_C_NOT_ZERO);
3016      }      }
3017    }    }
3018    
3019  #ifdef SUPPORT_UTF8  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
 if (common->utf8)  
   {  
   OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_w)_pcre_utf8_char_sizes);  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);  
   }  
 else  
   OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  
 #else  
 OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);  
 #endif  
3020  JUMPTO(SLJIT_JUMP, start);  JUMPTO(SLJIT_JUMP, start);
3021  JUMPHERE(found);  JUMPHERE(found);
3022  JUMPHERE(leave);  JUMPHERE(quit);
3023    
3024  if (firstline)  if (firstline)
3025    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3026  }  }
3027    
3028  static SLJIT_INLINE void fast_forward_newline(compiler_common *common, BOOL firstline)  static SLJIT_INLINE void fast_forward_newline(compiler_common *common, BOOL firstline)
# Line 1797  DEFINE_COMPILER; Line 3031  DEFINE_COMPILER;
3031  struct sljit_label *loop;  struct sljit_label *loop;
3032  struct sljit_jump *lastchar;  struct sljit_jump *lastchar;
3033  struct sljit_jump *firstchar;  struct sljit_jump *firstchar;
3034  struct sljit_jump *leave;  struct sljit_jump *quit;
3035  struct sljit_jump *foundcr = NULL;  struct sljit_jump *foundcr = NULL;
3036  struct sljit_jump *notfoundnl;  struct sljit_jump *notfoundnl;
3037  jump_list *newline = NULL;  jump_list *newline = NULL;
3038    
3039  if (firstline)  if (firstline)
3040    {    {
3041    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, STR_END, 0);    SLJIT_ASSERT(common->first_line_end != 0);
3042    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END);    OP1(SLJIT_MOV, TMP3, 0, STR_END, 0);
3043      OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3044    }    }
3045    
3046  if (common->nltype == NLTYPE_FIXED && common->newline > 255)  if (common->nltype == NLTYPE_FIXED && common->newline > 255)
# Line 1816  if (common->nltype == NLTYPE_FIXED && co Line 3051  if (common->nltype == NLTYPE_FIXED && co
3051    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));    OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
3052    firstchar = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP2, 0);    firstchar = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP2, 0);
3053    
3054    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2);    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(2));
3055    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, STR_PTR, 0, TMP1, 0);
3056    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_GREATER_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_GREATER_EQUAL);
3057    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3058      OP2(SLJIT_SHL, TMP2, 0, TMP2, 0, SLJIT_IMM, UCHAR_SHIFT);
3059    #endif
3060    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);    OP2(SLJIT_SUB, STR_PTR, 0, STR_PTR, 0, TMP2, 0);
3061    
3062    loop = LABEL();    loop = LABEL();
3063    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3064    leave = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3065    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), -2);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-2));
3066    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(STR_PTR), -1);    OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(STR_PTR), IN_UCHARS(-1));
3067    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop);    CMPTO(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, (common->newline >> 8) & 0xff, loop);
3068    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop);    CMPTO(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, common->newline & 0xff, loop);
3069    
3070    JUMPHERE(leave);    JUMPHERE(quit);
3071    JUMPHERE(firstchar);    JUMPHERE(firstchar);
3072    JUMPHERE(lastchar);    JUMPHERE(lastchar);
3073    
# Line 1853  set_jumps(newline, loop); Line 3091  set_jumps(newline, loop);
3091    
3092  if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)  if (common->nltype == NLTYPE_ANY || common->nltype == NLTYPE_ANYCRLF)
3093    {    {
3094    leave = JUMP(SLJIT_JUMP);    quit = JUMP(SLJIT_JUMP);
3095    JUMPHERE(foundcr);    JUMPHERE(foundcr);
3096    notfoundnl = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);    notfoundnl = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3097    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);    OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3098    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);    OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, CHAR_NL);
3099    COND_VALUE(SLJIT_MOV, TMP1, 0, SLJIT_C_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3100    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
3101      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, UCHAR_SHIFT);
3102    #endif
3103    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3104    JUMPHERE(notfoundnl);    JUMPHERE(notfoundnl);
3105    JUMPHERE(leave);    JUMPHERE(quit);
3106    }    }
3107  JUMPHERE(lastchar);  JUMPHERE(lastchar);
3108  JUMPHERE(firstchar);  JUMPHERE(firstchar);
3109    
3110  if (firstline)  if (firstline)
3111    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);    OP1(SLJIT_MOV, STR_END, 0, TMP3, 0);
3112  }  }
3113    
3114    static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks);
3115    
3116  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, sljit_uw start_bits, BOOL firstline)  static SLJIT_INLINE void fast_forward_start_bits(compiler_common *common, sljit_uw start_bits, BOOL firstline)
3117  {  {
3118  DEFINE_COMPILER;  DEFINE_COMPILER;
3119  struct sljit_label *start;  struct sljit_label *start;
3120  struct sljit_jump *leave;  struct sljit_jump *quit;
3121  struct sljit_jump *found;  struct sljit_jump *found = NULL;
3122    jump_list *matches = NULL;
3123    pcre_uint8 inverted_start_bits[32];
3124    int i;
3125    #ifndef COMPILE_PCRE8
3126    struct sljit_jump *jump;
3127    #endif
3128    
3129    for (i = 0; i < 32; ++i)
3130      inverted_start_bits[i] = ~(((pcre_uint8*)start_bits)[i]);
3131    
3132  if (firstline)  if (firstline)
3133    {    {
3134    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0, STR_END, 0);    SLJIT_ASSERT(common->first_line_end != 0);
3135    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), FIRSTLINE_END);    OP1(SLJIT_MOV, RETURN_ADDR, 0, STR_END, 0);
3136      OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->first_line_end);
3137    }    }
3138    
3139  start = LABEL();  start = LABEL();
3140  leave = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  quit = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);
3141  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);  OP1(MOV_UCHAR, TMP1, 0, SLJIT_MEM1(STR_PTR), 0);
3142  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3143  if (common->utf8)  if (common->utf)
3144    OP1(SLJIT_MOV_UB, TMP3, 0, SLJIT_MEM1(TMP1), (sljit_w)_pcre_utf8_char_sizes);    OP1(SLJIT_MOV, TMP3, 0, TMP1, 0);
3145  #endif  #endif
3146  OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);  
3147  OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);  if (!check_class_ranges(common, inverted_start_bits, (inverted_start_bits[31] & 0x80) != 0, &matches))
3148  OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), start_bits);    {
3149  OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);  #ifndef COMPILE_PCRE8
3150  OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);    jump = CMP(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 255);
3151  found = JUMP(SLJIT_C_NOT_ZERO);    OP1(SLJIT_MOV, TMP1, 0, SLJIT_IMM, 255);
3152      JUMPHERE(jump);
3153  #ifdef SUPPORT_UTF8  #endif
3154  if (common->utf8)    OP2(SLJIT_AND, TMP2, 0, TMP1, 0, SLJIT_IMM, 0x7);
3155    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP3, 0);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 3);
3156  else    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), start_bits);
3157    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    OP2(SLJIT_SHL, TMP2, 0, SLJIT_IMM, 1, TMP2, 0);
3158  #else    OP2(SLJIT_AND | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, TMP2, 0);
3159  OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, 1);    found = JUMP(SLJIT_C_NOT_ZERO);
3160      }
3161    
3162    #ifdef SUPPORT_UTF
3163    if (common->utf)
3164      OP1(SLJIT_MOV, TMP1, 0, TMP3, 0);
3165  #endif  #endif
3166    OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3167    #ifdef SUPPORT_UTF
3168    #if defined COMPILE_PCRE8
3169    if (common->utf)
3170      {
3171      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xc0, start);
3172      OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), (sljit_sw)PRIV(utf8_table4) - 0xc0);
3173      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3174      }
3175    #elif defined COMPILE_PCRE16
3176    if (common->utf)
3177      {
3178      CMPTO(SLJIT_C_LESS, TMP1, 0, SLJIT_IMM, 0xd800, start);
3179      OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 0xfc00);
3180      OP2(SLJIT_SUB | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0xd800);
3181      OP_FLAGS(SLJIT_MOV, TMP1, 0, SLJIT_UNUSED, 0, SLJIT_C_EQUAL);
3182      OP2(SLJIT_SHL, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3183      OP2(SLJIT_ADD, STR_PTR, 0, STR_PTR, 0, TMP1, 0);
3184      }
3185    #endif /* COMPILE_PCRE[8|16] */
3186    #endif /* SUPPORT_UTF */
3187  JUMPTO(SLJIT_JUMP, start);  JUMPTO(SLJIT_JUMP, start);
3188  JUMPHERE(found);  if (found != NULL)
3189  JUMPHERE(leave);    JUMPHERE(found);
3190    if (matches != NULL)
3191      set_jumps(matches, LABEL());
3192    JUMPHERE(quit);
3193    
3194  if (firstline)  if (firstline)
3195    OP1(SLJIT_MOV, STR_END, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), POSSESSIVE0);    OP1(SLJIT_MOV, STR_END, 0, RETURN_ADDR, 0);
3196  }  }
3197    
3198  static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, pcre_uint16 reqbyte, BOOL has_firstbyte)  static SLJIT_INLINE struct sljit_jump *search_requested_char(compiler_common *common, pcre_uchar req_char, BOOL caseless, BOOL has_firstchar)
3199  {  {
3200  DEFINE_COMPILER;  DEFINE_COMPILER;
3201  struct sljit_label *loop;  struct sljit_label *loop;
# Line 1922  struct sljit_jump *alreadyfound; Line 3204  struct sljit_jump *alreadyfound;
3204  struct sljit_jump *found;  struct sljit_jump *found;
3205  struct sljit_jump *foundoc = NULL;  struct sljit_jump *foundoc = NULL;
3206  struct sljit_jump *notfound;  struct sljit_jump *notfound;
3207  pcre_uint16 oc, bit;  pcre_uint32 oc, bit;
3208    
3209  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), REQ_BYTE_PTR);  SLJIT_ASSERT(common->req_char_ptr != 0);
3210    OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), common->req_char_ptr);
3211  OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_BYTE_MAX);  OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, REQ_BYTE_MAX);
3212  toolong = CMP(SLJIT_C_LESS, TMP1, 0, STR_END, 0);  toolong = CMP(SLJIT_C_LESS, TMP1, 0, STR_END, 0);
3213  alreadyfound = CMP(SLJIT_C_LESS, STR_PTR, 0, TMP2, 0);  alreadyfound = CMP(SLJIT_C_LESS, STR_PTR, 0, TMP2, 0);
3214    
3215  if (has_firstbyte)  if (has_firstchar)
3216    OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, 1);    OP2(SLJIT_ADD, TMP1, 0, STR_PTR, 0, SLJIT_IMM, IN_UCHARS(1));
3217  else  else
3218    OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0);    OP1(SLJIT_MOV, TMP1, 0, STR_PTR, 0);
3219    
3220  loop = LABEL();  loop = LABEL();
3221  notfound = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, STR_END, 0);  notfound = CMP(SLJIT_C_GREATER_EQUAL, TMP1, 0, STR_END, 0);
3222    
3223  OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), 0);  OP1(MOV_UCHAR, TMP2, 0, SLJIT_MEM1(TMP1), 0);
3224  if ((reqbyte & REQ_CASELESS) == 0)  oc = req_char;
3225    found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, reqbyte & 0xff);  if (caseless)
3226      {
3227      oc = TABLE_GET(req_char, common->fcc, req_char);
3228    #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
3229      if (req_char > 127 && common->utf)
3230        oc = UCD_OTHERCASE(req_char);
3231    #endif
3232      }
3233    if (req_char == oc)
3234      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
3235  else  else
3236    {    {
3237    reqbyte &= 0xff;    bit = req_char ^ oc;
3238    oc = common->fcc[reqbyte];    if (is_powerof2(bit))
   bit = reqbyte ^ oc;  
   if (ispowerof2(bit))  
3239      {      {
3240      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);      OP2(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_IMM, bit);
3241      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, reqbyte | bit);      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char | bit);
3242      }      }
3243    else    else
3244      {      {
3245      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, reqbyte);      found = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, req_char);
3246      foundoc = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, oc);      foundoc = CMP(SLJIT_C_EQUAL, TMP2, 0, SLJIT_IMM, oc);
3247      }      }
3248    }    }
3249  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, IN_UCHARS(1));
3250  JUMPTO(SLJIT_JUMP, loop);  JUMPTO(SLJIT_JUMP, loop);
3251    
3252  JUMPHERE(found);  JUMPHERE(found);
3253  if (foundoc)  if (foundoc)
3254    JUMPHERE(foundoc);    JUMPHERE(foundoc);
3255  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), REQ_BYTE_PTR, TMP1, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), common->req_char_ptr, TMP1, 0);
3256  JUMPHERE(alreadyfound);  JUMPHERE(alreadyfound);
3257  JUMPHERE(toolong);  JUMPHERE(toolong);
3258  return notfound;  return notfound;
# Line 1971  return notfound; Line 3261  return notfound;
3261  static void do_revertframes(compiler_common *common)  static void do_revertframes(compiler_common *common)
3262  {  {
3263  DEFINE_COMPILER;  DEFINE_COMPILER;
 struct sljit_jump *earlyexit;  
3264  struct sljit_jump *jump;  struct sljit_jump *jump;
3265  struct sljit_label *mainloop;  struct sljit_label *mainloop;
3266    
3267  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0, 1, 5, 5, common->localsize);  sljit_emit_fast_enter(compiler, RETURN_ADDR, 0);
3268  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS_HEAD);  OP1(SLJIT_MOV, TMP1, 0, STACK_TOP, 0);
3269    GET_LOCAL_BASE(TMP3, 0, 0);
3270    
3271  /* Drop frames until we reach STACK_TOP. */  /* Drop frames until we reach STACK_TOP. */
 earlyexit = CMP(SLJIT_C_LESS, TMP1, 0, STACK_TOP, 0);  
3272  mainloop = LABEL();  mainloop = LABEL();
3273  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), 0);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), 0);
3274  jump = CMP(SLJIT_C_SIG_LESS_EQUAL, TMP2, 0, SLJIT_IMM, frame_end);  OP2(SLJIT_SUB | SLJIT_SET_S, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_IMM, 0);
3275  OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, SLJIT_LOCALS_REG, 0);  jump = JUMP(SLJIT_C_SIG_LESS_EQUAL);
3276  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  
3277  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_w), SLJIT_MEM1(TMP1), 2 * sizeof(sljit_w));  OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
3278  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_w));  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
3279    OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), sizeof(sljit_sw), SLJIT_MEM1(TMP1), 2 * sizeof(sljit_sw));
3280    OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 3 * sizeof(sljit_sw));
3281  JUMPTO(SLJIT_JUMP, mainloop);  JUMPTO(SLJIT_JUMP, mainloop);
3282    
3283  JUMPHERE(jump);  JUMPHERE(jump);
3284  jump = CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, frame_end);  jump = JUMP(SLJIT_C_SIG_LESS);
3285  /* End of dropping frames. */  /* End of dropping frames. */
 OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  
 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS_HEAD, TMP1, 0);  
 CMPTO(SLJIT_C_GREATER_EQUAL, TMP1, 0, STACK_TOP, 0, mainloop);  
 JUMPHERE(earlyexit);  
3286  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);  sljit_emit_fast_return(compiler, RETURN_ADDR, 0);
3287    
3288  JUMPHERE(jump);  JUMPHERE(jump);
3289  jump = CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, frame_setmaxindex);  OP1(SLJIT_NEG, TMP2, 0, TMP2, 0);
3290  /* Set max index. */  OP2(SLJIT_ADD, TMP2, 0, TMP2, 0, TMP3, 0);
3291  OP1(SLJIT_MOV, MAX_INDEX, 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  OP1(SLJIT_MOV, SLJIT_MEM1(TMP2), 0, SLJIT_MEM1(TMP1), sizeof(sljit_sw));
3292  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_w));  OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_sw));
 JUMPTO(SLJIT_JUMP, mainloop);  
   
 JUMPHERE(jump);  
 jump = CMP(SLJIT_C_NOT_EQUAL, TMP2, 0, SLJIT_IMM, frame_setstrbegin);  
 /* Set max index. */  
 OP1(SLJIT_MOV, TMP2, 0, SLJIT_MEM1(TMP1), sizeof(sljit_w));  
 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_w));  
 OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), OVECTOR(0), TMP2, 0);  
 JUMPTO(SLJIT_JUMP, mainloop);  
   
 JUMPHERE(jump);  
 /* Unknown command. */  
 OP2(SLJIT_ADD, TMP1, 0, TMP1, 0, SLJIT_IMM, 2 * sizeof(sljit_w));  
3293  JUMPTO(SLJIT_JUMP, mainloop);  JUMPTO(SLJIT_JUMP, mainloop);
3294  }  }
3295    
3296  static void check_wordboundary(compiler_common *common)  static void check_wordboundary(compiler_common *common)
3297  {  {
3298  DEFINE_COMPILER;  DEFINE_COMPILER;
3299  struct sljit_jump *beginend;  struct sljit_jump *skipread;
3300  #ifdef SUPPORT_UTF8  jump_list *skipread_list = NULL;
3301    #if !(defined COMPILE_PCRE8) || defined SUPPORT_UTF
3302  struct sljit_jump *jump;  struct sljit_jump *jump;
3303  #endif  #endif
3304    
3305  SLJIT_ASSERT(ctype_word == 0x10);  SLJIT_COMPILE_ASSERT(ctype_word == 0x10, ctype_word_must_be_16);
3306    
3307  sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0, 1, 5, 5, common->localsize);  sljit_emit_fast_enter(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
3308  /* Get type of the previous char, and put it to LOCALS1. */  /* Get type of the previous char, and put it to LOCALS1. */
3309  OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);  OP1(SLJIT_MOV, TMP1, 0, ARGUMENTS, 0);
3310  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));  OP1(SLJIT_MOV, TMP1, 0, SLJIT_MEM1(TMP1), SLJIT_OFFSETOF(jit_arguments, begin));
3311  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, SLJIT_IMM, 0);  OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, SLJIT_IMM, 0);
3312  beginend = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP1, 0);  skipread = CMP(SLJIT_C_LESS_EQUAL, STR_PTR, 0, TMP1, 0);
3313  skip_char_back(common);  skip_char_back(common);
3314    check_start_used_ptr(common);
3315  read_char(common);  read_char(common);
3316    
3317  /* Testing char type. */  /* Testing char type. */
3318  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3319  if (common->useucp)  if (common->use_ucp)
3320    {    {
3321    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
3322    jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);    jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
3323    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
3324    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
3325    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
3326    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_LESS_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
3327    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
3328    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
3329    COND_VALUE(SLJIT_OR, TMP2, 0, SLJIT_C_LESS_EQUAL);    OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
3330    JUMPHERE(jump);    JUMPHERE(jump);
3331    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP2, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP2, 0);
3332    }    }
3333  else  else
3334  #endif  #endif
3335    {    {
3336  #ifdef SUPPORT_UTF8  #ifndef COMPILE_PCRE8
3337      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
3338    #elif defined SUPPORT_UTF
3339    /* Here LOCALS1 has already been zeroed. */    /* Here LOCALS1 has already been zeroed. */
3340    jump = NULL;    jump = NULL;
3341    if (common->utf8)    if (common->utf)
3342      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
3343  #endif  #endif /* COMPILE_PCRE8 */
3344    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);    OP1(SLJIT_MOV_UB, TMP1, 0, SLJIT_MEM1(TMP1), common->ctypes);
3345    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */);    OP2(SLJIT_LSHR, TMP1, 0, TMP1, 0, SLJIT_IMM, 4 /* ctype_word */);
3346    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);    OP2(SLJIT_AND, TMP1, 0, TMP1, 0, SLJIT_IMM, 1);
3347    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);    OP1(SLJIT_MOV, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1, TMP1, 0);
3348  #ifdef SUPPORT_UTF8  #ifndef COMPILE_PCRE8
3349      JUMPHERE(jump);
3350    #elif defined SUPPORT_UTF
3351    if (jump != NULL)    if (jump != NULL)
3352      JUMPHERE(jump);      JUMPHERE(jump);
3353  #endif  #endif /* COMPILE_PCRE8 */
3354    }    }
3355  JUMPHERE(beginend);  JUMPHERE(skipread);
3356    
3357  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);  OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
3358  beginend = CMP(SLJIT_C_GREATER_EQUAL, STR_PTR, 0, STR_END, 0);  check_str_end(common, &skipread_list);
3359  peek_char(common);  peek_char(common);
3360    
3361  /* Testing char type. This is a code duplication. */  /* Testing char type. This is a code duplication. */
3362  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3363  if (common->useucp)  if (common->use_ucp)
3364    {    {
3365    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 1);
3366    jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);    jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, CHAR_UNDERSCORE);
3367    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));    add_jump(compiler, &common->getucd, JUMP(SLJIT_FAST_CALL));
3368    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Ll);
3369    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_Lu - ucp_Ll);
3370    COND_VALUE(SLJIT_MOV, TMP2, 0, SLJIT_C_LESS_EQUAL);    OP_FLAGS(SLJIT_MOV, TMP2, 0, SLJIT_UNUSED, 0, SLJIT_C_LESS_EQUAL);
3371    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);    OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ucp_Nd - ucp_Ll);
3372    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);    OP2(SLJIT_SUB | SLJIT_SET_U, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, ucp_No - ucp_Nd);
3373    COND_VALUE(SLJIT_OR, TMP2, 0, SLJIT_C_LESS_EQUAL);    OP_FLAGS(SLJIT_OR, TMP2, 0, TMP2, 0, SLJIT_C_LESS_EQUAL);
3374    JUMPHERE(jump);    JUMPHERE(jump);
3375    }    }
3376  else  else
3377  #endif  #endif
3378    {    {
3379  #ifdef SUPPORT_UTF8  #ifndef COMPILE_PCRE8
3380      /* TMP2 may be destroyed by peek_char. */
3381      OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
3382      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
3383    #elif defined SUPPORT_UTF
3384    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);    OP1(SLJIT_MOV, TMP2, 0, SLJIT_IMM, 0);
3385    jump = NULL;    jump = NULL;
3386    if (common->utf8)    if (common->utf)
3387      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);      jump = CMP(SLJIT_C_GREATER, TMP1, 0, SLJIT_IMM, 255);
3388  #endif  #endif
3389    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);    OP1(SLJIT_MOV_UB, TMP2, 0, SLJIT_MEM1(TMP1), common->ctypes);
3390    OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);    OP2(SLJIT_LSHR, TMP2, 0, TMP2, 0, SLJIT_IMM, 4 /* ctype_word */);
3391    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);    OP2(SLJIT_AND, TMP2, 0, TMP2, 0, SLJIT_IMM, 1);
3392  #ifdef SUPPORT_UTF8  #ifndef COMPILE_PCRE8
3393      JUMPHERE(jump);
3394    #elif defined SUPPORT_UTF
3395    if (jump != NULL)    if (jump != NULL)
3396      JUMPHERE(jump);      JUMPHERE(jump);
3397  #endif  #endif /* COMPILE_PCRE8 */
3398    }    }
3399  JUMPHERE(beginend);  set_jumps(skipread_list, LABEL());
3400    
3401  OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);  OP2(SLJIT_XOR | SLJIT_SET_E, SLJIT_UNUSED, 0, TMP2, 0, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS1);
3402  sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);  sljit_emit_fast_return(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), LOCALS0);
3403  }  }
3404    
3405    /*
3406      range format:
3407    
3408      ranges[0] = length of the range (max MAX_RANGE_SIZE, -1 means invalid range).
3409      ranges[1] = first bit (0 or 1)
3410      ranges[2-length] = position of the bit change (when the current bit is not equal to the previous)
3411    */
3412    
3413    static BOOL check_ranges(compiler_common *common, int *ranges, jump_list **backtracks, BOOL readch)
3414    {
3415    DEFINE_COMPILER;
3416    struct sljit_jump *jump;
3417    
3418    if (ranges[0] < 0)
3419      return FALSE;
3420    
3421    switch(ranges[0])
3422      {
3423      case 1:
3424      if (readch)
3425        read_char(common);
3426      add_jump(compiler, backtracks, CMP(ranges[1] == 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3427      return TRUE;
3428    
3429      case 2:
3430      if (readch)
3431        read_char(common);
3432      OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[2]);
3433      add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[3] - ranges[2]));
3434      return TRUE;
3435    
3436      case 4:
3437      if (ranges[2] + 1 == ranges[3] && ranges[4] + 1 == ranges[5])
3438        {
3439        if (readch)
3440          read_char(common);
3441        if (ranges[1] != 0)
3442          {
3443          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]));
3444          add_jump(compiler, backtracks, CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3445          }
3446        else
3447          {
3448          jump = CMP(SLJIT_C_EQUAL, TMP1, 0, SLJIT_IMM, ranges[2]);
3449          add_jump(compiler, backtracks, CMP(SLJIT_C_NOT_EQUAL, TMP1, 0, SLJIT_IMM, ranges[4]));
3450          JUMPHERE(jump);
3451          }
3452        return TRUE;
3453        }
3454      if ((ranges[3] - ranges[2]) == (ranges[5] - ranges[4]) && is_powerof2(ranges[4] - ranges[2]))
3455        {
3456        if (readch)
3457          read_char(common);
3458        OP2(SLJIT_OR, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4] - ranges[2]);
3459        OP2(SLJIT_SUB, TMP1, 0, TMP1, 0, SLJIT_IMM, ranges[4]);
3460        add_jump(compiler, backtracks, CMP(ranges[1] != 0 ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL, TMP1, 0, SLJIT_IMM, ranges[5] - ranges[4]));
3461        return TRUE;
3462        }
3463      return FALSE;
3464    
3465      default:
3466      return FALSE;
3467      }
3468    }
3469    
3470    static void get_ctype_ranges(compiler_common *common, int flag, int *ranges)
3471    {
3472    int i, bit, length;
3473    const pcre_uint8 *ctypes = (const pcre_uint8*)common->ctypes;
3474    
3475    bit = ctypes[0] & flag;
3476    ranges[0] = -1;
3477    ranges[1] = bit != 0 ? 1 : 0;
3478    length = 0;
3479    
3480    for (i = 1; i < 256; i++)
3481      if ((ctypes[i] & flag) != bit)
3482        {
3483        if (length >= MAX_RANGE_SIZE)
3484          return;
3485        ranges[2 + length] = i;
3486        length++;
3487        bit ^= flag;
3488        }
3489    
3490    if (bit != 0)
3491      {
3492      if (length >= MAX_RANGE_SIZE)
3493        return;
3494      ranges[2 + length] = 256;
3495      length++;
3496      }
3497    ranges[0] = length;
3498    }
3499    
3500    static BOOL check_class_ranges(compiler_common *common, const pcre_uint8 *bits, BOOL nclass, jump_list **backtracks)
3501    {
3502    int ranges[2 + MAX_RANGE_SIZE];
3503    pcre_uint8 bit, cbit, all;
3504    int i, byte, length = 0;
3505    
3506    bit = bits[0] & 0x1;
3507    ranges[1] = bit;