Abstract all the accesses to cop_arybase (apart from ByteLoader)
[p5sagit/p5-mst-13.2.git] / regexec.c
index f705021..4010b34 100644 (file)
--- a/regexec.c
+++ b/regexec.c
@@ -78,7 +78,7 @@
  ****    Alterations to Henry's code are...
  ****
  ****    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- ****    2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
+ ****    2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
  ****
  ****    You may distribute under the terms of either the GNU General Public
  ****    License or the Artistic License, as specified in the README file.
 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
 
-#define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
-#define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
-#define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
-#define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
-#define HOPc(pos,off) ((char*)HOP(pos,off))
-#define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
-
-#define HOPBACK(pos, off) (            \
-    (PL_reg_match_utf8)                        \
-       ? reghopmaybe((U8*)pos, -off)   \
+#define HOPc(pos,off) ((char *)(PL_reg_match_utf8 \
+           ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
+           : (U8*)(pos + off)))
+#define HOPBACKc(pos, off) ((char*)    \
+    ((PL_reg_match_utf8)               \
+       ? reghopmaybe3((U8*)pos, -off, ((U8*)(off < 0 ? PL_regeol : PL_bostr))) \
     : (pos - off >= PL_bostr)          \
        ? (U8*)(pos - off)              \
-    : (U8*)NULL                                \
+    : (U8*)NULL)                       \
 )
-#define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
 
-#define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
-#define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
-#define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
 
 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
     if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
@@ -178,6 +170,7 @@ static void restore_pos(pTHX_ void *arg);
 STATIC CHECKPOINT
 S_regcppush(pTHX_ I32 parenfloor)
 {
+    dVAR;
     const int retval = PL_savestack_ix;
 #define REGCP_PAREN_ELEMS 4
     const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
@@ -222,6 +215,7 @@ S_regcppush(pTHX_ I32 parenfloor)
 STATIC char *
 S_regcppop(pTHX)
 {
+    dVAR;
     I32 i;
     U32 paren = 0;
     char *input;
@@ -283,17 +277,6 @@ S_regcppop(pTHX)
     return input;
 }
 
-STATIC char *
-S_regcp_set_to(pTHX_ I32 ss)
-{
-    const I32 tmp = PL_savestack_ix;
-
-    PL_savestack_ix = ss;
-    regcppop();
-    PL_savestack_ix = tmp;
-    return Nullch;
-}
-
 typedef struct re_cc_state
 {
     I32 ss;
@@ -305,7 +288,7 @@ typedef struct re_cc_state
 
 #define regcpblow(cp) LEAVE_SCOPE(cp)  /* Ignores regcppush()ed data. */
 
-#define TRYPAREN(paren, n, input) {                            \
+#define TRYPAREN(paren, n, input, where) {                     \
     if (paren) {                                               \
        if (n) {                                                \
            PL_regstartp[paren] = HOPc(input, -1) - PL_bostr;   \
@@ -314,7 +297,8 @@ typedef struct re_cc_state
        else                                                    \
            PL_regendp[paren] = -1;                             \
     }                                                          \
-    if (regmatch(next))                                                \
+    REGMATCH(next, where);                                     \
+    if (result)                                                        \
        sayYES;                                                 \
     if (paren && n)                                            \
        PL_regendp[paren] = -1;                                 \
@@ -344,6 +328,7 @@ Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *stren
 STATIC void
 S_cache_re(pTHX_ regexp *prog)
 {
+    dVAR;
     PL_regprecomp = prog->precomp;             /* Needed for FAIL. */
 #ifdef DEBUGGING
     PL_regprogram = prog->program;
@@ -403,6 +388,7 @@ char *
 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
                     char *strend, U32 flags, re_scream_pos_data *data)
 {
+    dVAR;
     register I32 start_shift = 0;
     /* Should be nonnegative! */
     register I32 end_shift   = 0;
@@ -412,12 +398,12 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
     char *t;
     const int do_utf8 = sv ? SvUTF8(sv) : 0;   /* if no sv we have to assume bytes */
     I32 ml_anch;
-    register char *other_last = Nullch;        /* other substr checked before this */
-    char *check_at = Nullch;           /* check substr found at this pos */
+    register char *other_last = NULL;  /* other substr checked before this */
+    char *check_at = NULL;             /* check substr found at this pos */
     const I32 multiline = prog->reganch & PMf_MULTILINE;
 #ifdef DEBUGGING
-    char *i_strpos = strpos;
-    SV *dsv = PERL_DEBUG_PAD_ZERO(0);
+    const char * const i_strpos = strpos;
+    SV * const dsv = PERL_DEBUG_PAD_ZERO(0);
 #endif
 
     GET_RE_DEBUG_FLAGS_DECL;
@@ -518,6 +504,7 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
                     || ((slen = SvCUR(check)) > 1
                         && memNE(SvPVX_const(check), s, slen)))
                goto report_neq;
+           check_at = s;
            goto success_at_start;
          }
        }
@@ -615,7 +602,8 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
        if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
          do_other_anchored:
            {
-               char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
+               char * const last = HOP3c(s, -start_shift, strbeg);
+               char *last1, *last2;
                char *s1 = s;
                SV* must;
 
@@ -837,9 +825,9 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
            SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
            if (do_utf8 ? prog->check_substr : prog->check_utf8)
                SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
-           prog->check_substr = prog->check_utf8 = Nullsv;     /* disable */
-           prog->float_substr = prog->float_utf8 = Nullsv;     /* clear */
-           check = Nullsv;                     /* abort */
+           prog->check_substr = prog->check_utf8 = NULL;       /* disable */
+           prog->float_substr = prog->float_utf8 = NULL;       /* clear */
+           check = NULL;                       /* abort */
            s = strpos;
            /* XXXX This is a remnant of the old implementation.  It
                    looks wasteful, since now INTUIT can use many
@@ -861,7 +849,7 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
           regstclass does not come from lookahead...  */
        /* If regstclass takes bytelength more than 1: If charlength==1, OK.
           This leaves EXACTF only, which is dealt with in find_byclass().  */
-        const U8* str = (U8*)STRING(prog->regstclass);
+        const U8* const str = (U8*)STRING(prog->regstclass);
         const int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
                    ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
                    : 1);
@@ -877,7 +865,7 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
         s = find_byclass(prog, prog->regstclass, s, endpos, 1);
        if (!s) {
 #ifdef DEBUGGING
-           const char *what = 0;
+           const char *what = NULL;
 #endif
            if (endpos == strend) {
                DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
@@ -962,7 +950,7 @@ Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
   fail:
     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
                          PL_colors[4], PL_colors[5]));
-    return Nullch;
+    return NULL;
 }
 
 /* We know what class REx starts with.  Try to find this position... */
@@ -1038,14 +1026,15 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32
                U8 *sm = (U8 *) m;
                U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
                U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
+               const U32 uniflags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
 
                to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
                to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
 
                c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE, 
-                                   0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
+                                   0, uniflags);
                c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
-                                   0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
+                                   0, uniflags);
                lnc = 0;
                while (sm < ((U8 *) m + ln)) {
                    lnc++;
@@ -1084,14 +1073,13 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32
                UV c, f;
                U8 tmpbuf [UTF8_MAXBYTES+1];
                STRLEN len, foldlen;
-               
+               const U32 uniflags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
                if (c1 == c2) {
                    /* Upper and lower of 1st char are equal -
                     * probably not a "letter". */
                    while (s <= e) {
                        c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
-                                          ckWARN(WARN_UTF8) ?
-                                          0 : UTF8_ALLOW_ANY);
+                                          uniflags);
                        if ( c == c1
                             && (ln == len ||
                                 ibcmp_utf8(s, (char **)0, 0,  do_utf8,
@@ -1118,8 +1106,7 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32
                else {
                    while (s <= e) {
                      c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
-                                          ckWARN(WARN_UTF8) ?
-                                          0 : UTF8_ALLOW_ANY);
+                                          uniflags);
 
                        /* Handle some of the three Greek sigmas cases.
                         * Note that not all the possible combinations
@@ -1187,8 +1174,7 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32
                if (s == PL_bostr)
                    tmp = '\n';
                else {
-                   U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
-               
+                   U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
                    tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
                }
                tmp = ((OP(c) == BOUND ?
@@ -1230,8 +1216,7 @@ S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32
                if (s == PL_bostr)
                    tmp = '\n';
                else {
-                   U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
-               
+                   U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
                    tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
                }
                tmp = ((OP(c) == NBOUND ?
@@ -1632,6 +1617,7 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
 /* data: May be used for some additional optimizations. */
 /* nosave: For optimizations. */
 {
+    dVAR;
     register char *s;
     register regnode *c;
     register char *startpos = stringarg;
@@ -1639,13 +1625,13 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
     I32 dontbother = 0;        /* how many characters not to try at end */
     I32 end_shift = 0;                 /* Same for the end. */         /* CC */
     I32 scream_pos = -1;               /* Internal iterator of scream. */
-    char *scream_olds;
+    char *scream_olds = NULL;
     SV* oreplsv = GvSV(PL_replgv);
     const bool do_utf8 = DO_UTF8(sv);
     const I32 multiline = prog->reganch & PMf_MULTILINE;
 #ifdef DEBUGGING
-    SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
-    SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
+    SV * const dsv0 = PERL_DEBUG_PAD_ZERO(0);
+    SV * const dsv1 = PERL_DEBUG_PAD_ZERO(1);
 #endif
 
     GET_RE_DEBUG_FLAGS_DECL;
@@ -1653,8 +1639,6 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
     PERL_UNUSED_ARG(data);
     RX_MATCH_UTF8_set(prog,do_utf8);
 
-    PL_regcc = 0;
-
     cache_re(prog);
 #ifdef DEBUGGING
     PL_regnarrate = DEBUG_r_TEST;
@@ -1722,7 +1706,7 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
            PL_reg_ganch = strbeg;
     }
 
-    if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
+    if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
        re_scream_pos_data d;
 
        d.scream_olds = &scream_olds;
@@ -1848,9 +1832,9 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
                                   "Did not find anchored character...\n")
                );
     }
-    else if (prog->anchored_substr != Nullsv
-             || prog->anchored_utf8 != Nullsv
-             || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
+    else if (prog->anchored_substr != NULL
+             || prog->anchored_utf8 != NULL
+             || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
                  && prog->float_max_offset < strend - s)) {
        SV *must;
        I32 back_max;
@@ -1906,10 +1890,10 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
                s = HOPc(s, -back_max);
            }
            else {
-               char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
+               char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
 
                last1 = HOPc(s, -back_min);
-               s = t;          
+               s = t;
            }
            if (do_utf8) {
                while (s <= last1) {
@@ -1972,7 +1956,7 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
     }
     else {
        dontbother = 0;
-       if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
+       if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
            /* Trim the end. */
            char *last;
            SV* float_real;
@@ -1999,7 +1983,7 @@ Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *
                        last = strend - len + 1;
                    else if (!multiline)
                        last = memEQ(strend - len, little, len)
-                           ? strend - len : Nullch;
+                           ? strend - len : NULL;
                    else
                        goto find_last;
                } else {
@@ -2101,6 +2085,7 @@ phooey:
 STATIC I32                     /* 0 failure, 1 success */
 S_regtry(pTHX_ regexp *prog, char *startpos)
 {
+    dVAR;
     register I32 i;
     register I32 *sp;
     register I32 *ep;
@@ -2136,9 +2121,12 @@ S_regtry(pTHX_ regexp *prog, char *startpos)
            if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
                  && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
                /* prepare for quick setting of pos */
-               sv_magic(PL_reg_sv, (SV*)0,
-                       PERL_MAGIC_regex_global, Nullch, 0);
-               mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
+#ifdef PERL_OLD_COPY_ON_WRITE
+               if (SvIsCOW(sv))
+                   sv_force_normal_flags(sv, 0);
+#endif
+               mg = sv_magicext(PL_reg_sv, (SV*)0, PERL_MAGIC_regex_global,
+                                &PL_vtbl_mglob, NULL, 0);
                mg->mg_len = -1;
            }
            PL_reg_magic    = mg;
@@ -2146,7 +2134,7 @@ S_regtry(pTHX_ regexp *prog, char *startpos)
            SAVEDESTRUCTOR_X(restore_pos, 0);
         }
         if (!PL_reg_curpm) {
-           Newz(22, PL_reg_curpm, 1, PMOP);
+           Newxz(PL_reg_curpm, 1, PMOP);
 #ifdef USE_ITHREADS
             {
                 SV* repointer = newSViv(0);
@@ -2173,7 +2161,7 @@ S_regtry(pTHX_ regexp *prog, char *startpos)
            RX_MATCH_COPIED_off(prog);
        }
        else
-           PL_reg_oldsaved = Nullch;
+           PL_reg_oldsaved = NULL;
        prog->subbeg = PL_bostr;
        prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
     }
@@ -2192,7 +2180,7 @@ S_regtry(pTHX_ regexp *prog, char *startpos)
         if(PL_reg_start_tmp)
             Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
         else
-            New(22, PL_reg_start_tmp, PL_reg_start_tmpl, char*);
+            Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
     }
 
     /* XXXX What this code is doing here?!!!  There should be no need
@@ -2315,57 +2303,70 @@ typedef union re_unwind_t {
 /* Make sure there is a test for this +1 options in re_tests */
 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
 
-#define TRIE_CHECK_STATE_IS_ACCEPTING STMT_START {                       \
-    if ( trie->states[ state ].wordnum ) {                               \
-       if ( !accepted ) {                                               \
-           ENTER;                                                       \
-           SAVETMPS;                                                    \
-           bufflen = TRIE_INITAL_ACCEPT_BUFFLEN ;                       \
-           sv_accept_buff=NEWSV( 1234,                                  \
-             bufflen * sizeof(reg_trie_accepted) - 1 );                 \
-           SvCUR_set( sv_accept_buff, sizeof(reg_trie_accepted) );      \
-           SvPOK_on( sv_accept_buff );                                  \
-           sv_2mortal( sv_accept_buff );                                \
-           accept_buff = (reg_trie_accepted*)SvPV_nolen( sv_accept_buff );\
-       } else {                                                         \
-           if ( accepted >= bufflen ) {                                 \
-               bufflen *= 2;                                            \
-               accept_buff =(reg_trie_accepted*)SvGROW( sv_accept_buff, \
-                   bufflen * sizeof(reg_trie_accepted) );               \
-           }                                                            \
-           SvCUR_set( sv_accept_buff,SvCUR( sv_accept_buff )            \
-               + sizeof( reg_trie_accepted ) );                         \
-       }                                                                \
-       accept_buff[ accepted ].wordnum = trie->states[ state ].wordnum; \
-       accept_buff[ accepted ].endpos = uc;                             \
-       ++accepted;                                                      \
-    } } STMT_END
-
-#define TRIE_HANDLE_CHAR STMT_START {                                   \
-        if ( uvc < 256 ) {                                              \
-            charid = trie->charmap[ uvc ];                              \
-        } else {                                                        \
-            charid = 0;                                                 \
-            if( trie->widecharmap ) {                                   \
-            SV** svpp = (SV**)NULL;                                     \
-            svpp = hv_fetch( trie->widecharmap, (char*)&uvc,            \
-                         sizeof( UV ), 0 );                            \
-            if ( svpp ) {                                               \
-               charid = (U16)SvIV( *svpp );                            \
-                }                                                       \
-            }                                                           \
-        }                                                               \
-        if ( charid &&                                                  \
-             ( base + charid > trie->uniquecharcount ) &&               \
-             ( base + charid - 1 - trie->uniquecharcount < trie->lasttrans) && \
-             trie->trans[ base + charid - 1 - trie->uniquecharcount ].check == state ) \
-        {                                                               \
-            state = trie->trans[ base + charid - 1 - trie->uniquecharcount ].next;     \
-        } else {                                                        \
-            state = 0;                                                  \
-        }                                                               \
-        uc += len;                                                      \
-    } STMT_END
+
+/* simulate a recursive call to regmatch */
+
+#define REGMATCH(ns, where) \
+    new_scan = (ns); \
+    resume_state = resume_##where; \
+    goto start_recurse; \
+    resume_point_##where:
+
+typedef enum {
+    resume_TRIE1,
+    resume_TRIE2,
+    resume_CURLYX,
+    resume_WHILEM1,
+    resume_WHILEM2,
+    resume_WHILEM3,
+    resume_WHILEM4,
+    resume_WHILEM5,
+    resume_WHILEM6,
+    resume_CURLYM1,
+    resume_CURLYM2,
+    resume_CURLYM3,
+    resume_CURLYM4,
+    resume_IFMATCH,
+    resume_PLUS1,
+    resume_PLUS2,
+    resume_PLUS3,
+    resume_PLUS4,
+    resume_END
+} resume_states;
+
+
+struct regmatch_state {
+    struct regmatch_state *prev_state;
+    resume_states resume_state;
+    regnode *scan;
+    regnode *next;
+    bool minmod;
+    bool sw;
+    int logical;
+    I32 unwind;
+    CURCUR *cc;
+    char *locinput;
+
+    I32 n;
+    I32 ln;
+    I32 c1, c2, paren;
+    CHECKPOINT cp, lastcp;
+    CURCUR *oldcc;
+    char *lastloc;
+    I32 cache_offset, cache_bit;
+    I32 curlym_l;
+    I32 matches;
+    I32 maxwanted;
+    char *e; 
+    char *old;
+    int count;
+    re_cc_state *cur_call_cc;
+    regexp *end_re;
+    reg_trie_accepted *accept_buff;
+    U32 accepted;
+
+    re_cc_state *reg_call_cc;  /* saved value of PL_reg_call_cc */
+};
 
 /*
  - regmatch - main matching routine
@@ -2380,56 +2381,161 @@ typedef union re_unwind_t {
 /* [lwall] I've hoisted the register declarations to the outer block in order to
  * maybe save a little bit of pushing and popping on the stack.  It also takes
  * advantage of machines that use a register save mask on subroutine entry.
+ *
+ * This function used to be heavily recursive, but since this had the
+ * effect of blowing the CPU stack on complex regexes, it has been
+ * restructured to be iterative, and to save state onto the heap rather
+ * than the stack. Essentially whereever regmatch() used to be called, it
+ * pushes the current state, notes where to return, then jumps back into
+ * the main loop.
+ *
+ * Originally the structure of this function used to look something like
+
+    S_regmatch() {
+       int a = 1, b = 2;
+       ...
+       while (scan != NULL) {
+           ...
+           switch (OP(scan)) {
+               case FOO: {
+                   int local = 3;
+                   ...
+                   if (regmatch(...))  // recurse
+                       goto yes;
+               }
+               ...
+           }
+       }
+       yes:
+       return 1;
+    }
+
+ * Now it looks something like this:
+
+    struct regmatch_state {
+       int a, b, local;
+       int resume_state;
+    };
+
+    S_regmatch() {
+       int a = 1, b = 2;
+       int local;
+       ...
+       while (scan != NULL) {
+           ...
+           switch (OP(scan)) {
+               case FOO: {
+                   local = 3;
+                   ...
+                   resume_state = resume_FOO;
+                   new_scan = ...;
+                   goto start_recurse;
+                   resume_point_FOO:
+
+                   if (result)  // recurse
+                       goto yes;
+               }
+               ...
+           }
+           start_recurse:
+           ...push a, b, local onto the heap
+           a = 1; b = 2;
+           scan = new_scan;
+       }
+       yes:
+       result = 1;
+       if (states pushed on heap) {
+           ... restore a, b, local from heap
+           switch (resume_state) {
+           case resume_FOO:
+               goto resume_point_FOO;
+           ...
+           }
+       }
+       return result
+    }
+           
+ * WARNING: this means that any line in this function that contains a
+ * REGMATCH() or TRYPAREN() is actually simulating a recursive call to
+ * regmatch() using gotos instead. Thus the values of any local variables
+ * not saved in the regmatch_state structure will have been lost when
+ * execution resumes on the next line .
  */
+
 STATIC I32                     /* 0 failure, 1 success */
 S_regmatch(pTHX_ regnode *prog)
 {
     dVAR;
+    register const bool do_utf8 = PL_reg_match_utf8;
+    const U32 uniflags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
+
+    /************************************************************
+     * the following variabes are saved and restored on each fake
+     * recursive call to regmatch:
+     *
+     * The first ones contain state that needs to be maintained
+     * across the main while loop: */
+
+    struct regmatch_state *prev_state = NULL; /* stack of pushed states */
+    resume_states resume_state; /* where to jump to on return */
     register regnode *scan;    /* Current node. */
     regnode *next;             /* Next node. */
-    regnode *inner;            /* Next node in internal branch. */
-    register I32 nextchr;      /* renamed nextchr - nextchar colides with
-                                  function of same name */
-    register I32 n;            /* no or next */
-    register I32 ln = 0;       /* len or last */
-    register char *s = Nullch; /* operand or save */
+    bool minmod = 0;           /* the next {n.m} is a {n,m}? */
+    bool sw = 0;               /* the condition value in (?(cond)a|b) */
+    int logical = 0;
+    I32 unwind = 0;            /* savestack index of current unwind block */
+    CURCUR *cc = NULL;         /* current innermost curly struct */
     register char *locinput = PL_reginput;
-    register I32 c1 = 0, c2 = 0, paren;        /* case fold search, parenth */
-    int minmod = 0, sw = 0, logical = 0;
-    I32 unwind = 0;
-
-    /* used by the trie code */
-    SV                 *sv_accept_buff = 0;  /* accepting states we have traversed */
-    reg_trie_accepted  *accept_buff = 0;     /* "" */
-    reg_trie_data      *trie;                /* what trie are we using right now */
-    U32 accepted = 0;                        /* how many accepting states we have seen*/
-
-#if 0
-    I32 firstcp = PL_savestack_ix;
-#endif
-    const register bool do_utf8 = PL_reg_match_utf8;
-#ifdef DEBUGGING
-    SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
-    SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
-    SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
 
-    SV *re_debug_flags = NULL;
-#endif
-
-    GET_RE_DEBUG_FLAGS;
+    /* while the rest of these are local to an individual branch, and
+     * have only been hoisted into this outer scope to allow for saving and
+     * restoration - thus they can be safely reused in other branches.
+     * Note that they are only initialized here to silence compiler
+     * warnings :-( */
 
+    register I32 n = 0;                /* no or next */
+    register I32 ln = 0;       /* len or last */
+    register I32 c1 = 0, c2 = 0, paren = 0; /* case fold search, parenth */
+    CHECKPOINT cp = 0;         /* remember current savestack indexes */
+    CHECKPOINT lastcp = 0;
+    CURCUR *oldcc = NULL;      /* tmp copy of cc */
+    char *lastloc = NULL;      /* Detection of 0-len. */
+    I32 cache_offset = 0;
+    I32 cache_bit = 0;
+    I32 curlym_l = 0;
+    I32 matches = 0;
+    I32 maxwanted = 0;
+    char *e = NULL;
+    char *old = NULL;
+    int count = 0;
+    re_cc_state *cur_call_cc = NULL;
+    regexp *end_re = NULL;
+    reg_trie_accepted *accept_buff = NULL;
+    U32 accepted = 0; /* how many accepting states we have seen */
+
+    /************************************************************
+     * these variables are NOT saved: */
+
+    register I32 nextchr;   /* is always set to UCHARAT(locinput) */
+    regnode *new_scan; /* node to begin exedcution at when recursing */
+    bool result;       /* return value of S_regmatch */
+
+    regnode *inner;    /* Next node in internal branch. */
+    
 #ifdef DEBUGGING
+    SV *re_debug_flags = NULL;
+    GET_RE_DEBUG_FLAGS;
     PL_regindent++;
 #endif
 
-
     /* Note that nextchr is a byte even in UTF */
     nextchr = UCHARAT(locinput);
     scan = prog;
     while (scan != NULL) {
 
         DEBUG_EXECUTE_r( {
-           SV *prop = sv_newmortal();
+           SV * const prop = sv_newmortal();
            const int docolor = *PL_colors[0];
            const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
            int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
@@ -2459,17 +2565,18 @@ S_regmatch(pTHX_ regnode *prog)
            {
              const char * const s0 =
                do_utf8 && OP(scan) != CANY ?
-               pv_uni_display(dsv0, (U8*)(locinput - pref_len),
+               pv_uni_display(PERL_DEBUG_PAD(0), (U8*)(locinput - pref_len),
                               pref0_len, 60, UNI_DISPLAY_REGEX) :
                locinput - pref_len;
              const int len0 = do_utf8 ? strlen(s0) : pref0_len;
              const char * const s1 = do_utf8 && OP(scan) != CANY ?
-               pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
+               pv_uni_display(PERL_DEBUG_PAD(1),
+                              (U8*)(locinput - pref_len + pref0_len),
                               pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
                locinput - pref_len + pref0_len;
              const int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
              const char * const s2 = do_utf8 && OP(scan) != CANY ?
-               pv_uni_display(dsv2, (U8*)locinput,
+               pv_uni_display(PERL_DEBUG_PAD(2), (U8*)locinput,
                               PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
                locinput;
              const int len2 = do_utf8 ? strlen(s2) : l;
@@ -2573,17 +2680,12 @@ S_regmatch(pTHX_ regnode *prog)
           traverse the TRIE keeping track of all accepting states
           we transition through until we get to a failing node.
 
-          we use two slightly different pieces of code to handle
-          the traversal depending on whether its case sensitive or
-          not. we reuse the accept code however. (this should probably
-          be turned into a macro.)
 
        */
+       case TRIE:
        case TRIEF:
        case TRIEFL:
            {
-
-               const U32 uniflags = ckWARN( WARN_UTF8 ) ? 0 : UTF8_ALLOW_ANY;
                U8 *uc = ( U8* )locinput;
                U32 state = 1;
                U16 charid = 0;
@@ -2593,13 +2695,48 @@ S_regmatch(pTHX_ regnode *prog)
                STRLEN foldlen = 0;
                U8 *uscan = (U8*)NULL;
                STRLEN bufflen=0;
-               accepted = 0;
-
-               trie = (reg_trie_data*)PL_regdata->data[ ARG( scan ) ];
+               SV *sv_accept_buff = NULL;
+               const enum { trie_plain, trie_utf8, trie_uft8_fold }
+                   trie_type = do_utf8 ?
+                         (OP(scan) == TRIE ? trie_utf8 : trie_uft8_fold)
+                       : trie_plain;
+
+               /* what trie are we using right now */
+               reg_trie_data *trie
+                   = (reg_trie_data*)PL_regdata->data[ ARG( scan ) ];
+               accepted = 0; /* how many accepting states we have seen */
+               result = 0;
 
                while ( state && uc <= (U8*)PL_regeol ) {
 
-                   TRIE_CHECK_STATE_IS_ACCEPTING;
+                   if (trie->states[ state ].wordnum) {
+                       if (!accepted ) {
+                           ENTER;
+                           SAVETMPS;
+                           bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
+                           sv_accept_buff=newSV(bufflen *
+                                           sizeof(reg_trie_accepted) - 1);
+                           SvCUR_set(sv_accept_buff,
+                                               sizeof(reg_trie_accepted));
+                           SvPOK_on(sv_accept_buff);
+                           sv_2mortal(sv_accept_buff);
+                           accept_buff =
+                               (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
+                       }
+                       else {
+                           if (accepted >= bufflen) {
+                               bufflen *= 2;
+                               accept_buff =(reg_trie_accepted*)
+                                   SvGROW(sv_accept_buff,
+                                       bufflen * sizeof(reg_trie_accepted));
+                           }
+                           SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
+                               + sizeof(reg_trie_accepted));
+                       }
+                       accept_buff[accepted].wordnum = trie->states[state].wordnum;
+                       accept_buff[accepted].endpos = uc;
+                       ++accepted;
+                   }
 
                    base = trie->states[ state ].trans.base;
 
@@ -2611,8 +2748,8 @@ S_regmatch(pTHX_ regnode *prog)
                    );
 
                    if ( base ) {
-
-                       if ( do_utf8 || UTF ) {
+                       switch (trie_type) {
+                       case trie_uft8_fold:
                            if ( foldlen>0 ) {
                                uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags );
                                foldlen -= len;
@@ -2625,14 +2762,47 @@ S_regmatch(pTHX_ regnode *prog)
                                foldlen -= UNISKIP( uvc );
                                uscan = foldbuf + UNISKIP( uvc );
                            }
-                       } else {
+                           break;
+                       case trie_utf8:
+                           uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN,
+                                                           &len, uniflags );
+                           break;
+                       case trie_plain:
                            uvc = (UV)*uc;
                            len = 1;
                        }
 
-                       TRIE_HANDLE_CHAR;
+                       if (uvc < 256) {
+                           charid = trie->charmap[ uvc ];
+                       }
+                       else {
+                           charid = 0;
+                           if (trie->widecharmap) {
+                               SV** svpp = (SV**)NULL;
+                               svpp = hv_fetch(trie->widecharmap,
+                                           (char*)&uvc, sizeof(UV), 0);
+                               if (svpp)
+                                   charid = (U16)SvIV(*svpp);
+                           }
+                       }
+
+                       if (charid &&
+                            (base + charid > trie->uniquecharcount )
+                            && (base + charid - 1 - trie->uniquecharcount
+                                   < trie->lasttrans)
+                            && trie->trans[base + charid - 1 -
+                                   trie->uniquecharcount].check == state)
+                       {
+                           state = trie->trans[base + charid - 1 -
+                               trie->uniquecharcount ].next;
+                       }
+                       else {
+                           state = 0;
+                       }
+                       uc += len;
 
-                   } else {
+                   }
+                   else {
                        state = 0;
                    }
                    DEBUG_TRIE_EXECUTE_r(
@@ -2641,66 +2811,8 @@ S_regmatch(pTHX_ regnode *prog)
                            charid, uvc, (UV)state, PL_colors[5] );
                    );
                }
-               if ( !accepted ) {
-                  sayNO;
-               } else {
-                   goto TrieAccept;
-               }
-           }
-           /* unreached codepoint: we jump into the middle of the next case
-              from previous if blocks */
-       case TRIE:
-           {
-               const U32 uniflags = ckWARN( WARN_UTF8 ) ? 0 : UTF8_ALLOW_ANY;
-               U8 *uc = (U8*)locinput;
-               U32 state = 1;
-               U16 charid = 0;
-               U32 base = 0;
-               UV uvc = 0;
-               STRLEN len = 0;
-               STRLEN bufflen = 0;
-               accepted = 0;
-
-               trie = (reg_trie_data*)PL_regdata->data[ ARG( scan ) ];
-
-               while ( state && uc <= (U8*)PL_regeol ) {
-
-                   TRIE_CHECK_STATE_IS_ACCEPTING;
-
-                   base = trie->states[ state ].trans.base;
-
-                   DEBUG_TRIE_EXECUTE_r(
-                           PerlIO_printf( Perl_debug_log,
-                               "%*s  %sState: %4"UVxf", Base: %4"UVxf", Accepted: %4"UVxf" ",
-                               REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
-                               (UV)state, (UV)base, (UV)accepted );
-                   );
-
-                   if ( base ) {
-
-                       if ( do_utf8 || UTF ) {
-                           uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags );
-                       } else {
-                           uvc = (U32)*uc;
-                           len = 1;
-                       }
-
-                        TRIE_HANDLE_CHAR;
-
-                   } else {
-                       state = 0;
-                   }
-                   DEBUG_TRIE_EXECUTE_r(
-                           PerlIO_printf( Perl_debug_log,
-                               "Charid:%3x CV:%4"UVxf" After State: %4"UVxf"%s\n",
-                               charid, uvc, (UV)state, PL_colors[5] );
-                   );
-               }
-               if ( !accepted ) {
+               if (!accepted )
                   sayNO;
-               }
-           }
-
 
            /*
               There was at least one accepting state that we
@@ -2714,9 +2826,6 @@ S_regmatch(pTHX_ regnode *prog)
               eventually terminates once all of the accepting states
               have been tried.
            */
-       TrieAccept:
-           {
-               int gotit = 0;
 
                if ( accepted == 1 ) {
                    DEBUG_EXECUTE_r({
@@ -2733,14 +2842,15 @@ S_regmatch(pTHX_ regnode *prog)
                       as we wont be using accept_buff again. */
                    FREETMPS;
                    LEAVE;
-                   gotit = regmatch( scan + NEXT_OFF( scan ) );
+                   REGMATCH(scan + NEXT_OFF(scan), TRIE1);
+                   /*** all unsaved local vars undefined at this point */
                } else {
                     DEBUG_EXECUTE_r(
                         PerlIO_printf( Perl_debug_log,"%*s  %sgot %"IVdf" possible matches%s\n",
                             REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4], (IV)accepted,
                             PL_colors[5] );
                     );
-                   while ( !gotit && accepted-- ) {
+                   while ( !result && accepted-- ) {
                        U32 best = 0;
                        U32 cur;
                        for( cur = 1 ; cur <= accepted ; cur++ ) {
@@ -2756,7 +2866,7 @@ S_regmatch(pTHX_ regnode *prog)
                                    best = cur;
                        }
                        DEBUG_EXECUTE_r({
-                           SV **tmp = av_fetch( trie->words, accept_buff[ best ].wordnum - 1, 0 );
+                           SV ** const tmp = av_fetch( trie->words, accept_buff[ best ].wordnum - 1, 0 );
                            PerlIO_printf( Perl_debug_log, "%*s  %strying alternation #%d <%s> at 0x%p%s\n",
                                REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
                                accept_buff[best].wordnum,
@@ -2777,22 +2887,23 @@ S_regmatch(pTHX_ regnode *prog)
                            all until I can be sure.
                          */
                        SAVETMPS;
-                       gotit = regmatch( scan + NEXT_OFF( scan ) ) ;
+                       REGMATCH(scan + NEXT_OFF(scan), TRIE2);
+                       /*** all unsaved local vars undefined at this point */
                        FREETMPS;
                    }
                    FREETMPS;
                    LEAVE;
                }
                
-               if ( gotit ) {
+               if (result) {
                    sayYES;
                } else {
                    sayNO;
                }
            }
            /* unreached codepoint */
-       case EXACT:
-           s = STRING(scan);
+       case EXACT: {
+           char *s = STRING(scan);
            ln = STR_LEN(scan);
            if (do_utf8 != UTF) {
                /* The target and the pattern have differing utf8ness. */
@@ -2807,8 +2918,7 @@ S_regmatch(pTHX_ regnode *prog)
                             sayNO;
                        if (NATIVE_TO_UNI(*(U8*)s) !=
                            utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
-                                          ckWARN(WARN_UTF8) ?
-                                          0 : UTF8_ALLOW_ANY))
+                                           uniflags))
                             sayNO;
                        l += ulen;
                        s ++;
@@ -2822,8 +2932,7 @@ S_regmatch(pTHX_ regnode *prog)
                            sayNO;
                        if (NATIVE_TO_UNI(*((U8*)l)) !=
                            utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
-                                          ckWARN(WARN_UTF8) ?
-                                          0 : UTF8_ALLOW_ANY))
+                                          uniflags))
                            sayNO;
                        s += ulen;
                        l ++;
@@ -2844,11 +2953,12 @@ S_regmatch(pTHX_ regnode *prog)
            locinput += ln;
            nextchr = UCHARAT(locinput);
            break;
+           }
        case EXACTFL:
            PL_reg_flags |= RF_tainted;
            /* FALL THROUGH */
-       case EXACTF:
-           s = STRING(scan);
+       case EXACTF: {
+           char *s = STRING(scan);
            ln = STR_LEN(scan);
 
            if (do_utf8 || UTF) {
@@ -2892,6 +3002,7 @@ S_regmatch(pTHX_ regnode *prog)
            locinput += ln;
            nextchr = UCHARAT(locinput);
            break;
+           }
        case ANYOF:
            if (do_utf8) {
                STRLEN inclasslen = PL_regeol - locinput;
@@ -3134,7 +3245,8 @@ S_regmatch(pTHX_ regnode *prog)
            PL_reg_flags |= RF_tainted;
            /* FALL THROUGH */
         case REF:
-       case REFF:
+       case REFF: {
+           char *s;
            n = ARG(scan);  /* which paren pair */
            ln = PL_regstartp[n];
            PL_reg_leftiter = PL_reg_maxiter;           /* Void cache */
@@ -3191,6 +3303,7 @@ S_regmatch(pTHX_ regnode *prog)
            locinput += ln;
            nextchr = UCHARAT(locinput);
            break;
+           }
 
        case NOTHING:
        case TAIL:
@@ -3200,11 +3313,11 @@ S_regmatch(pTHX_ regnode *prog)
        case EVAL:
        {
            dSP;
-           OP_4tree *oop = PL_op;
-           COP *ocurcop = PL_curcop;
+           OP_4tree * const oop = PL_op;
+           COP * const ocurcop = PL_curcop;
            PAD *old_comppad;
            SV *ret;
-           struct regexp *oreg = PL_reg_re;
+           struct regexp * const oreg = PL_reg_re;
        
            n = ARG(scan);
            PL_op = (OP_4tree*)PL_regdata->data[n];
@@ -3213,7 +3326,7 @@ S_regmatch(pTHX_ regnode *prog)
            PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
 
            {
-               SV **before = SP;
+               SV ** const before = SP;
                CALLRUNOPS(aTHX);                       /* Scalar context. */
                SPAGAIN;
                if (SP == before)
@@ -3230,9 +3343,8 @@ S_regmatch(pTHX_ regnode *prog)
            if (logical) {
                if (logical == 2) {     /* Postponed subexpression. */
                    regexp *re;
-                   MAGIC *mg = Null(MAGIC*);
+                   MAGIC *mg = NULL;
                    re_cc_state state;
-                   CHECKPOINT cp, lastcp;
                     int toggleutf;
                    register SV *sv;
 
@@ -3251,7 +3363,7 @@ S_regmatch(pTHX_ regnode *prog)
                    }
                    else {
                        STRLEN len;
-                       const char *t = SvPV_const(ret, len);
+                       const char * const t = SvPV_const(ret, len);
                        PMOP pm;
                        char * const oprecomp = PL_regprecomp;
                        const I32 osize = PL_regsize;
@@ -3279,10 +3391,10 @@ S_regmatch(pTHX_ regnode *prog)
                        );
                    state.node = next;
                    state.prev = PL_reg_call_cc;
-                   state.cc = PL_regcc;
+                   state.cc = cc;
                    state.re = PL_reg_re;
 
-                   PL_regcc = 0;
+                   cc = 0;
                
                    cp = regcppush(0);  /* Save *all* the positions. */
                    REGCP_SET(lastcp);
@@ -3299,6 +3411,7 @@ S_regmatch(pTHX_ regnode *prog)
                    /* XXXX This is too dramatic a measure... */
                    PL_reg_maxiter = 0;
 
+                   /* XXX the only recursion left in regmatch() */
                    if (regmatch(re->program + 1)) {
                        /* Even though we succeeded, we need to restore
                           global variables, since we may be wrapped inside
@@ -3306,7 +3419,7 @@ S_regmatch(pTHX_ regnode *prog)
 
                        /* XXXX Do this only if SUSPENDed? */
                        PL_reg_call_cc = state.prev;
-                       PL_regcc = state.cc;
+                       cc = state.cc;
                        PL_reg_re = state.re;
                        cache_re(PL_reg_re);
                        if (toggleutf) PL_reg_flags ^= RF_utf8;
@@ -3323,7 +3436,7 @@ S_regmatch(pTHX_ regnode *prog)
                    REGCP_UNWIND(lastcp);
                    regcppop();
                    PL_reg_call_cc = state.prev;
-                   PL_regcc = state.cc;
+                   cc = state.cc;
                    PL_reg_re = state.re;
                    cache_re(PL_reg_re);
                    if (toggleutf) PL_reg_flags ^= RF_utf8;
@@ -3375,15 +3488,15 @@ S_regmatch(pTHX_ regnode *prog)
            logical = scan->flags;
            break;
 /*******************************************************************
- PL_regcc contains infoblock about the innermost (...)* loop, and
+ cc contains infoblock about the innermost (...)* loop, and
  a pointer to the next outer infoblock.
 
  Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
 
-   1) After matching X, regnode for CURLYX is processed;
+   1) After matching Y, regnode for CURLYX is processed;
 
-   2) This regnode creates infoblock on the stack, and calls
-      regmatch() recursively with the starting point at WHILEM node;
+   2) This regnode mallocs an infoblock, and calls regmatch() recursively
+      with the starting point at WHILEM node;
 
    3) Each hit of WHILEM node tries to match A and Z (in the order
       depending on the current iteration, min/max of {min,max} and
@@ -3393,9 +3506,9 @@ S_regmatch(pTHX_ regnode *prog)
 
    4) After A matches, the same WHILEM node is hit again.
 
-   5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
+   5) Each time WHILEM is hit, cc is the infoblock created by CURLYX
       of the same pair.  Thus when WHILEM tries to match Z, it temporarily
-      resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
+      resets cc, since this Y(A)*Z can be a part of some other loop:
       as in (Y(A)*Z)*.  If Z matches, the automaton will hit the WHILEM node
       of the external loop.
 
@@ -3403,7 +3516,7 @@ S_regmatch(pTHX_ regnode *prog)
  and whatever it mentions via ->next, and additional attached trees
  corresponding to temporarily unset infoblocks as in "5" above.
 
- In the following picture infoblocks for outer loop of
+ In the following picture, infoblocks for outer loop of
  (Y(A)*?Z)*?T are denoted O, for inner I.  NULL starting block
  is denoted by x.  The matched string is YAAZYAZT.  Temporarily postponed
  infoblocks are drawn below the "reset" infoblock.
@@ -3449,35 +3562,42 @@ S_regmatch(pTHX_ regnode *prog)
                                O
                                I,I
  *******************************************************************/
+
        case CURLYX: {
-               CURCUR cc;
-               CHECKPOINT cp = PL_savestack_ix;
                /* No need to save/restore up to this paren */
                I32 parenfloor = scan->flags;
 
+               {
+                   CURCUR *newcc;
+                   Newx(newcc, 1, CURCUR);
+                   oldcc = cc;
+                   newcc->oldcc = cc;
+                   cc = newcc;
+               }
+               cp = PL_savestack_ix;
                if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
                    next += ARG(next);
-               cc.oldcc = PL_regcc;
-               PL_regcc = &cc;
                /* XXXX Probably it is better to teach regpush to support
                   parenfloor > PL_regsize... */
                if (parenfloor > (I32)*PL_reglastparen)
                    parenfloor = *PL_reglastparen; /* Pessimization... */
-               cc.parenfloor = parenfloor;
-               cc.cur = -1;
-               cc.min = ARG1(scan);
-               cc.max  = ARG2(scan);
-               cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
-               cc.next = next;
-               cc.minmod = minmod;
-               cc.lastloc = 0;
+               cc->parenfloor = parenfloor;
+               cc->cur = -1;
+               cc->min = ARG1(scan);
+               cc->max  = ARG2(scan);
+               cc->scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
+               cc->next = next;
+               cc->minmod = minmod;
+               cc->lastloc = 0;
                PL_reginput = locinput;
-               n = regmatch(PREVOPER(next));   /* start on the WHILEM */
+               REGMATCH(PREVOPER(next), CURLYX); /* start on the WHILEM */
+               /*** all unsaved local vars undefined at this point */
                regcpblow(cp);
-               PL_regcc = cc.oldcc;
-               saySAME(n);
+               Safefree(cc);
+               cc = oldcc;
+               saySAME(result);
            }
-           /* NOT REACHED */
+           /* NOTREACHED */
        case WHILEM: {
                /*
                 * This is really hard to understand, because after we match
@@ -3488,10 +3608,9 @@ S_regmatch(pTHX_ regnode *prog)
                 * that we can try again after backing off.
                 */
 
-               CHECKPOINT cp, lastcp;
-               CURCUR* cc = PL_regcc;
-               char *lastloc = cc->lastloc; /* Detection of 0-len. */
-               I32 cache_offset = 0, cache_bit = 0;
+               lastloc = cc->lastloc; /* Detection of 0-len. */
+               cache_offset = 0;
+               cache_bit = 0;
                
                n = cc->cur + 1;        /* how many we know we matched */
                PL_reginput = locinput;
@@ -3507,19 +3626,22 @@ S_regmatch(pTHX_ regnode *prog)
                /* If degenerate scan matches "", assume scan done. */
 
                if (locinput == cc->lastloc && n >= cc->min) {
-                   PL_regcc = cc->oldcc;
-                   if (PL_regcc)
-                       ln = PL_regcc->cur;
+                   oldcc = cc;
+                   cc = cc->oldcc;
+                   if (cc)
+                       ln = cc->cur;
                    DEBUG_EXECUTE_r(
                        PerlIO_printf(Perl_debug_log,
                           "%*s  empty match detected, try continuation...\n",
                           REPORT_CODE_OFF+PL_regindent*2, "")
                        );
-                   if (regmatch(cc->next))
+                   REGMATCH(oldcc->next, WHILEM1);
+                   /*** all unsaved local vars undefined at this point */
+                   cc = oldcc;
+                   if (result)
                        sayYES;
-                   if (PL_regcc)
-                       PL_regcc->cur = ln;
-                   PL_regcc = cc;
+                   if (cc->oldcc)
+                       cc->oldcc->cur = ln;
                    sayNO;
                }
 
@@ -3528,7 +3650,9 @@ S_regmatch(pTHX_ regnode *prog)
                if (n < cc->min) {
                    cc->cur = n;
                    cc->lastloc = locinput;
-                   if (regmatch(cc->scan))
+                   REGMATCH(cc->scan, WHILEM2);
+                   /*** all unsaved local vars undefined at this point */
+                   if (result)
                        sayYES;
                    cc->cur = n - 1;
                    cc->lastloc = lastloc;
@@ -3554,7 +3678,7 @@ S_regmatch(pTHX_ regnode *prog)
                    }
                    else {
                        PL_reg_poscache_size = size;
-                       Newz(29, PL_reg_poscache, size, char);
+                       Newxz(PL_reg_poscache, size, char);
                    }
                    DEBUG_EXECUTE_r(
                        PerlIO_printf(Perl_debug_log,
@@ -3589,20 +3713,23 @@ S_regmatch(pTHX_ regnode *prog)
                /* Prefer next over scan for minimal matching. */
 
                if (cc->minmod) {
-                   PL_regcc = cc->oldcc;
-                   if (PL_regcc)
-                       ln = PL_regcc->cur;
-                   cp = regcppush(cc->parenfloor);
+                   oldcc = cc;
+                   cc = cc->oldcc;
+                   if (cc)
+                       ln = cc->cur;
+                   cp = regcppush(oldcc->parenfloor);
                    REGCP_SET(lastcp);
-                   if (regmatch(cc->next)) {
+                   REGMATCH(oldcc->next, WHILEM3);
+                   /*** all unsaved local vars undefined at this point */
+                   cc = oldcc;
+                   if (result) {
                        regcpblow(cp);
                        CACHEsayYES;    /* All done. */
                    }
                    REGCP_UNWIND(lastcp);
                    regcppop();
-                   if (PL_regcc)
-                       PL_regcc->cur = ln;
-                   PL_regcc = cc;
+                   if (cc->oldcc)
+                       cc->oldcc->cur = ln;
 
                    if (n >= cc->max) { /* Maximum greed exceeded? */
                        if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
@@ -3626,7 +3753,9 @@ S_regmatch(pTHX_ regnode *prog)
                    cc->lastloc = locinput;
                    cp = regcppush(cc->parenfloor);
                    REGCP_SET(lastcp);
-                   if (regmatch(cc->scan)) {
+                   REGMATCH(cc->scan, WHILEM4);
+                   /*** all unsaved local vars undefined at this point */
+                   if (result) {
                        regcpblow(cp);
                        CACHEsayYES;
                    }
@@ -3644,7 +3773,9 @@ S_regmatch(pTHX_ regnode *prog)
                    cc->cur = n;
                    cc->lastloc = locinput;
                    REGCP_SET(lastcp);
-                   if (regmatch(cc->scan)) {
+                   REGMATCH(cc->scan, WHILEM5);
+                   /*** all unsaved local vars undefined at this point */
+                   if (result) {
                        regcpblow(cp);
                        CACHEsayYES;
                    }
@@ -3666,19 +3797,22 @@ S_regmatch(pTHX_ regnode *prog)
                }
 
                /* Failed deeper matches of scan, so see if this one works. */
-               PL_regcc = cc->oldcc;
-               if (PL_regcc)
-                   ln = PL_regcc->cur;
-               if (regmatch(cc->next))
+               oldcc = cc;
+               cc = cc->oldcc;
+               if (cc)
+                   ln = cc->cur;
+               REGMATCH(oldcc->next, WHILEM6);
+               /*** all unsaved local vars undefined at this point */
+               cc = oldcc;
+               if (result)
                    CACHEsayYES;
-               if (PL_regcc)
-                   PL_regcc->cur = ln;
-               PL_regcc = cc;
+               if (cc->oldcc)
+                   cc->oldcc->cur = ln;
                cc->cur = n - 1;
                cc->lastloc = lastloc;
                CACHEsayNO;
            }
-           /* NOT REACHED */
+           /* NOTREACHED */
        case BRANCHJ:
            next = scan + ARG(scan);
            if (next == scan)
@@ -3694,12 +3828,10 @@ S_regmatch(pTHX_ regnode *prog)
                    next = inner;       /* Avoid recursion. */
                else {
                    const I32 lastparen = *PL_reglastparen;
-                   I32 unwind1;
-                   re_unwind_branch_t *uw;
-
                    /* Put unwinding data on stack */
-                   unwind1 = SSNEWt(1,re_unwind_branch_t);
-                   uw = SSPTRt(unwind1,re_unwind_branch_t);
+                   const I32 unwind1 = SSNEWt(1,re_unwind_branch_t);
+                   re_unwind_branch_t * const uw = SSPTRt(unwind1,re_unwind_branch_t);
+
                    uw->prev = unwind;
                    unwind = unwind1;
                    uw->type = ((c1 == BRANCH)
@@ -3725,8 +3857,7 @@ S_regmatch(pTHX_ regnode *prog)
            break;
        case CURLYM:
        {
-           I32 l = 0;
-           CHECKPOINT lastcp;
+           curlym_l = matches = 0;
        
            /* We suppose that the next guy does not need
               backtracking: in particular, it is of constant non-zero length,
@@ -3744,11 +3875,40 @@ S_regmatch(pTHX_ regnode *prog)
            if (paren)
                scan += NEXT_OFF(scan); /* Skip former OPEN. */
            PL_reginput = locinput;
+           maxwanted = minmod ? ln : n;
+           if (maxwanted) {
+               while (PL_reginput < PL_regeol && matches < maxwanted) {
+                   REGMATCH(scan, CURLYM1);
+                   /*** all unsaved local vars undefined at this point */
+                   if (!result)
+                       break;
+                   /* on first match, determine length, curlym_l */
+                   if (!matches++) {
+                       if (PL_reg_match_utf8) {
+                           char *s = locinput;
+                           while (s < PL_reginput) {
+                               curlym_l++;
+                               s += UTF8SKIP(s);
+                           }
+                       }
+                       else {
+                           curlym_l = PL_reginput - locinput;
+                       }
+                       if (curlym_l == 0) {
+                           matches = maxwanted;
+                           break;
+                       }
+                   }
+                   locinput = PL_reginput;
+               }
+           }
+
+           PL_reginput = locinput;
+
            if (minmod) {
                minmod = 0;
-               if (ln && regrepeat_hard(scan, ln, &l) < ln)
+               if (ln && matches < ln)
                    sayNO;
-               locinput = PL_reginput;
                if (HAS_TEXT(next) || JUMPABLE(next)) {
                    regnode *text_node = next;
 
@@ -3782,19 +3942,23 @@ S_regmatch(pTHX_ regnode *prog)
                        if (paren) {
                            if (ln) {
                                PL_regstartp[paren] =
-                                   HOPc(PL_reginput, -l) - PL_bostr;
+                                   HOPc(PL_reginput, -curlym_l) - PL_bostr;
                                PL_regendp[paren] = PL_reginput - PL_bostr;
                            }
                            else
                                PL_regendp[paren] = -1;
                        }
-                       if (regmatch(next))
+                       REGMATCH(next, CURLYM2);
+                       /*** all unsaved local vars undefined at this point */
+                       if (result)
                            sayYES;
                        REGCP_UNWIND(lastcp);
                    }
                    /* Couldn't or didn't -- move forward. */
                    PL_reginput = locinput;
-                   if (regrepeat_hard(scan, 1, &l)) {
+                   REGMATCH(scan, CURLYM3);
+                   /*** all unsaved local vars undefined at this point */
+                   if (result) {
                        ln++;
                        locinput = PL_reginput;
                    }
@@ -3803,15 +3967,13 @@ S_regmatch(pTHX_ regnode *prog)
                }
            }
            else {
-               n = regrepeat_hard(scan, n, &l);
-               locinput = PL_reginput;
                DEBUG_EXECUTE_r(
                    PerlIO_printf(Perl_debug_log,
-                                 "%*s  matched %"IVdf" times, len=%"IVdf"...\n",
-                                 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
-                                 (IV) n, (IV)l)
+                             "%*s  matched %"IVdf" times, len=%"IVdf"...\n",
+                             (int)(REPORT_CODE_OFF+PL_regindent*2), "",
+                             (IV) matches, (IV)curlym_l)
                    );
-               if (n >= ln) {
+               if (matches >= ln) {
                    if (HAS_TEXT(next) || JUMPABLE(next)) {
                        regnode *text_node = next;
 
@@ -3838,36 +4000,41 @@ S_regmatch(pTHX_ regnode *prog)
                }
            assume_ok_REG:
                REGCP_SET(lastcp);
-               while (n >= ln) {
+               while (matches >= ln) {
                    /* If it could work, try it. */
                    if (c1 == -1000 ||
                        UCHARAT(PL_reginput) == c1 ||
                        UCHARAT(PL_reginput) == c2)
                    {
                        DEBUG_EXECUTE_r(
-                               PerlIO_printf(Perl_debug_log,
-                                             "%*s  trying tail with n=%"IVdf"...\n",
-                                             (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
+                           PerlIO_printf(Perl_debug_log,
+                               "%*s  trying tail with matches=%"IVdf"...\n",
+                               (int)(REPORT_CODE_OFF+PL_regindent*2),
+                               "", (IV)matches)
                            );
                        if (paren) {
-                           if (n) {
-                               PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
+                           if (matches) {
+                               PL_regstartp[paren]
+                                   = HOPc(PL_reginput, -curlym_l) - PL_bostr;
                                PL_regendp[paren] = PL_reginput - PL_bostr;
                            }
                            else
                                PL_regendp[paren] = -1;
                        }
-                       if (regmatch(next))
+                       REGMATCH(next, CURLYM4);
+                       /*** all unsaved local vars undefined at this point */
+                       if (result)
                            sayYES;
                        REGCP_UNWIND(lastcp);
                    }
                    /* Couldn't or didn't -- back up. */
-                   n--;
-                   locinput = HOPc(locinput, -l);
+                   matches--;
+                   locinput = HOPc(locinput, -curlym_l);
                    PL_reginput = locinput;
                }
            }
            sayNO;
+           /* NOTREACHED */
            break;
        }
        case CURLYN:
@@ -3940,16 +4107,13 @@ S_regmatch(pTHX_ regnode *prog)
                             to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
 
                             c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
-                                                ckWARN(WARN_UTF8) ?
-                                                0 : UTF8_ALLOW_ANY);
+                                                uniflags);
                             c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
-                                                ckWARN(WARN_UTF8) ?
-                                                0 : UTF8_ALLOW_ANY);
+                                                uniflags);
                        }
                        else {
                            c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
-                                                    ckWARN(WARN_UTF8) ?
-                                                    0 : UTF8_ALLOW_ANY);
+                                                    uniflags);
                        }
                    }
                }
@@ -3959,16 +4123,14 @@ S_regmatch(pTHX_ regnode *prog)
        assume_ok_easy:
            PL_reginput = locinput;
            if (minmod) {
-               CHECKPOINT lastcp;
                minmod = 0;
                if (ln && regrepeat(scan, ln) < ln)
                    sayNO;
                locinput = PL_reginput;
                REGCP_SET(lastcp);
                if (c1 != -1000) {
-                   char *e; /* Should not check after this */
-                   char *old = locinput;
-                   int count = 0;
+                   old = locinput;
+                   count = 0;
 
                    if  (n == REG_INFTY) {
                        e = PL_regeol - 1;
@@ -4010,8 +4172,7 @@ S_regmatch(pTHX_ regnode *prog)
                                while (locinput <= e &&
                                       utf8n_to_uvchr((U8*)locinput,
                                                      UTF8_MAXBYTES, &len,
-                                                     ckWARN(WARN_UTF8) ?
-                                                     0 : UTF8_ALLOW_ANY) != (UV)c1) {
+                                                     uniflags) != (UV)c1) {
                                    locinput += len;
                                    count++;
                                }
@@ -4022,8 +4183,7 @@ S_regmatch(pTHX_ regnode *prog)
                                while (locinput <= e) {
                                    UV c = utf8n_to_uvchr((U8*)locinput,
                                                          UTF8_MAXBYTES, &len,
-                                                         ckWARN(WARN_UTF8) ?
-                                                         0 : UTF8_ALLOW_ANY);
+                                                         uniflags);
                                    if (c == (UV)c1 || c == (UV)c2)
                                        break;
                                    locinput += len;
@@ -4040,7 +4200,8 @@ S_regmatch(pTHX_ regnode *prog)
                                sayNO;
                        }
                        /* PL_reginput == locinput now */
-                       TRYPAREN(paren, ln, locinput);
+                       TRYPAREN(paren, ln, locinput, PLUS1);
+                       /*** all unsaved local vars undefined at this point */
                        PL_reginput = locinput; /* Could be reset... */
                        REGCP_UNWIND(lastcp);
                        /* Couldn't or didn't -- move forward. */
@@ -4059,21 +4220,22 @@ S_regmatch(pTHX_ regnode *prog)
                        if (do_utf8)
                            c = utf8n_to_uvchr((U8*)PL_reginput,
                                               UTF8_MAXBYTES, 0,
-                                              ckWARN(WARN_UTF8) ?
-                                              0 : UTF8_ALLOW_ANY);
+                                              uniflags);
                        else
                            c = UCHARAT(PL_reginput);
                        /* If it could work, try it. */
                        if (c == (UV)c1 || c == (UV)c2)
                        {
-                           TRYPAREN(paren, ln, PL_reginput);
+                           TRYPAREN(paren, ln, PL_reginput, PLUS2);
+                           /*** all unsaved local vars undefined at this point */
                            REGCP_UNWIND(lastcp);
                        }
                    }
                    /* If it could work, try it. */
                    else if (c1 == -1000)
                    {
-                       TRYPAREN(paren, ln, PL_reginput);
+                       TRYPAREN(paren, ln, PL_reginput, PLUS3);
+                       /*** all unsaved local vars undefined at this point */
                        REGCP_UNWIND(lastcp);
                    }
                    /* Couldn't or didn't -- move forward. */
@@ -4087,7 +4249,6 @@ S_regmatch(pTHX_ regnode *prog)
                }
            }
            else {
-               CHECKPOINT lastcp;
                n = regrepeat(scan, n);
                locinput = PL_reginput;
                if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
@@ -4102,45 +4263,22 @@ S_regmatch(pTHX_ regnode *prog)
                        ln--;
                }
                REGCP_SET(lastcp);
-               if (paren) {
-                   UV c = 0;
-                   while (n >= ln) {
-                       if (c1 != -1000) {
-                           if (do_utf8)
-                               c = utf8n_to_uvchr((U8*)PL_reginput,
-                                                  UTF8_MAXBYTES, 0,
-                                                  ckWARN(WARN_UTF8) ?
-                                                  0 : UTF8_ALLOW_ANY);
-                           else
-                               c = UCHARAT(PL_reginput);
-                       }
-                       /* If it could work, try it. */
-                       if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
-                           {
-                               TRYPAREN(paren, n, PL_reginput);
-                               REGCP_UNWIND(lastcp);
-                           }
-                       /* Couldn't or didn't -- back up. */
-                       n--;
-                       PL_reginput = locinput = HOPc(locinput, -1);
-                   }
-               }
-               else {
+               {
                    UV c = 0;
                    while (n >= ln) {
                        if (c1 != -1000) {
                            if (do_utf8)
                                c = utf8n_to_uvchr((U8*)PL_reginput,
                                                   UTF8_MAXBYTES, 0,
-                                                  ckWARN(WARN_UTF8) ?
-                                                  0 : UTF8_ALLOW_ANY);
+                                                  uniflags);
                            else
                                c = UCHARAT(PL_reginput);
                        }
                        /* If it could work, try it. */
                        if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
                            {
-                               TRYPAREN(paren, n, PL_reginput);
+                               TRYPAREN(paren, n, PL_reginput, PLUS4);
+                               /*** all unsaved local vars undefined at this point */
                                REGCP_UNWIND(lastcp);
                            }
                        /* Couldn't or didn't -- back up. */
@@ -4153,21 +4291,31 @@ S_regmatch(pTHX_ regnode *prog)
            break;
        case END:
            if (PL_reg_call_cc) {
-               re_cc_state *cur_call_cc = PL_reg_call_cc;
-               CURCUR *cctmp = PL_regcc;
-               regexp *re = PL_reg_re;
-               CHECKPOINT cp, lastcp;
-               
-               cp = regcppush(0);      /* Save *all* the positions. */
+               cur_call_cc = PL_reg_call_cc;
+               end_re = PL_reg_re;
+
+               /* Save *all* the positions. */
+               cp = regcppush(0);
                REGCP_SET(lastcp);
-               regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
-                                                   the caller. */
-               PL_reginput = locinput; /* Make position available to
-                                          the callcc. */
+
+               /* Restore parens of the caller. */
+               {
+                   I32 tmp = PL_savestack_ix;
+                   PL_savestack_ix = PL_reg_call_cc->ss;
+                   regcppop();
+                   PL_savestack_ix = tmp;
+               }
+
+               /* Make position available to the callcc. */
+               PL_reginput = locinput;
+
                cache_re(PL_reg_call_cc->re);
-               PL_regcc = PL_reg_call_cc->cc;
+               oldcc = cc;
+               cc = PL_reg_call_cc->cc;
                PL_reg_call_cc = PL_reg_call_cc->prev;
-               if (regmatch(cur_call_cc->node)) {
+               REGMATCH(cur_call_cc->node, END);
+               /*** all unsaved local vars undefined at this point */
+               if (result) {
                    PL_reg_call_cc = cur_call_cc;
                    regcpblow(cp);
                    sayYES;
@@ -4175,9 +4323,9 @@ S_regmatch(pTHX_ regnode *prog)
                REGCP_UNWIND(lastcp);
                regcppop();
                PL_reg_call_cc = cur_call_cc;
-               PL_regcc = cctmp;
-               PL_reg_re = re;
-               cache_re(re);
+               cc = oldcc;
+               PL_reg_re = end_re;
+               cache_re(end_re);
 
                DEBUG_EXECUTE_r(
                    PerlIO_printf(Perl_debug_log,
@@ -4207,7 +4355,7 @@ S_regmatch(pTHX_ regnode *prog)
        case UNLESSM:
            n = 0;
            if (scan->flags) {
-               s = HOPBACKc(locinput, scan->flags);
+               char * const s = HOPBACKc(locinput, scan->flags);
                if (!s)
                    goto say_yes;
                PL_reginput = s;
@@ -4218,7 +4366,7 @@ S_regmatch(pTHX_ regnode *prog)
        case IFMATCH:
            n = 1;
            if (scan->flags) {
-               s = HOPBACKc(locinput, scan->flags);
+               char * const s = HOPBACKc(locinput, scan->flags);
                if (!s)
                    goto say_no;
                PL_reginput = s;
@@ -4227,8 +4375,9 @@ S_regmatch(pTHX_ regnode *prog)
                PL_reginput = locinput;
 
          do_ifmatch:
-           inner = NEXTOPER(NEXTOPER(scan));
-           if (regmatch(inner) != n) {
+           REGMATCH(NEXTOPER(NEXTOPER(scan)), IFMATCH);
+           /*** all unsaved local vars undefined at this point */
+           if (result != n) {
              say_no:
                if (logical) {
                    logical = 0;
@@ -4259,8 +4408,68 @@ S_regmatch(pTHX_ regnode *prog)
                          PTR2UV(scan), OP(scan));
            Perl_croak(aTHX_ "regexp memory corruption");
        }
+
       reenter:
        scan = next;
+       continue;
+       /* NOTREACHED */
+
+       /* simulate recursively calling regmatch(), but without actually
+        * recursing - ie save the current state on the heap rather than on
+        * the stack, then re-enter the loop. This avoids complex regexes
+        * blowing the processor stack */
+
+      start_recurse:
+       {
+           /* save existing local variables */
+           struct regmatch_state *p;
+
+           Newx(p, 1, struct regmatch_state);
+           p->prev_state = prev_state;
+           prev_state = p;
+           p->resume_state = resume_state;
+           p->scan = scan;
+           p->next = next;
+           p->minmod = minmod;
+           p->sw = sw;
+           p->logical = logical;
+           p->unwind = unwind;
+           p->cc = cc;
+           p->locinput = locinput;
+           p->n = n;
+           p->ln = ln;
+           p->c1 = c1;
+           p->c2 = c2;
+           p->paren = paren;
+           p->cp = cp;
+           p->lastcp = lastcp;
+           p->oldcc = oldcc;
+           p->lastloc = lastloc;
+           p->cache_offset = cache_offset;
+           p->cache_bit = cache_bit;
+           p->curlym_l = curlym_l;
+           p->matches = matches;
+           p->maxwanted = maxwanted;
+           p->e = e;
+           p->old = old;
+           p->count = count;
+           p->cur_call_cc = cur_call_cc;
+           p->end_re = end_re;
+           p->accept_buff = accept_buff;
+           p->accepted = accepted;
+           p->reg_call_cc = PL_reg_call_cc;
+
+           scan = new_scan;
+           locinput = PL_reginput;
+           nextchr = UCHARAT(locinput);
+           minmod = 0;
+           sw = 0;
+           logical = 0;
+           unwind = 0;
+#ifdef DEBUGGING
+           PL_regindent++;
+#endif
+       }
     }
 
     /*
@@ -4286,11 +4495,8 @@ yes:
     PL_regindent--;
 #endif
 
-#if 0                                  /* Breaks $^R */
-    if (unwind)
-       regcpblow(firstcp);
-#endif
-    return 1;
+    result = 1;
+    goto exit_level;
 
 no:
     DEBUG_EXECUTE_r(
@@ -4302,13 +4508,13 @@ no:
 no_final:
 do_no:
     if (unwind) {
-       re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
+       re_unwind_t * const uw = SSPTRt(unwind,re_unwind_t);
 
        switch (uw->type) {
        case RE_UNWIND_BRANCH:
        case RE_UNWIND_BRANCHJ:
        {
-           re_unwind_branch_t *uwb = &(uw->branch);
+           re_unwind_branch_t * const uwb = &(uw->branch);
            const I32 lastparen = uwb->lastparen;
        
            REGCP_UNWIND(uwb->lastcp);
@@ -4343,16 +4549,102 @@ do_no:
 
            goto reenter;
        }
-       /* NOT REACHED */
+       /* NOTREACHED */
        default:
            Perl_croak(aTHX_ "regexp unwind memory corruption");
        }
-       /* NOT REACHED */
+       /* NOTREACHED */
     }
 #ifdef DEBUGGING
     PL_regindent--;
 #endif
-    return 0;
+    result = 0;
+exit_level:
+    if (prev_state) {
+       /* restore previous state and re-enter */
+
+       struct regmatch_state *p = prev_state;
+       resume_state = p->resume_state;
+       scan = p->scan;
+       next = p->next;
+       minmod = p->minmod;
+       sw = p->sw;
+       logical = p->logical;
+       unwind = p->unwind;
+       cc = p->cc;
+       locinput = p->locinput;
+       nextchr = UCHARAT(locinput);
+       n = p->n;
+       ln = p->ln;
+       c1 = p->c1;
+       c2 = p->c2;
+       paren = p->paren;
+       cp = p->cp;
+       lastcp = p->lastcp;
+       oldcc = p->oldcc;
+       lastloc = p->lastloc;
+       cache_offset = p->cache_offset;
+       cache_bit = p->cache_bit;
+       curlym_l = p->curlym_l;
+       matches = p->matches;
+       maxwanted = p->maxwanted;
+       e = p->e;
+       old = p->old;
+       count = p->count;
+       cur_call_cc = p->cur_call_cc;
+       end_re = p->end_re;
+       accept_buff = p->accept_buff;
+       accepted = p->accepted;
+       PL_reg_call_cc = p->reg_call_cc;
+       prev_state = p->prev_state;
+       Safefree(p);
+
+       switch (resume_state) {
+       case resume_TRIE1:
+           goto resume_point_TRIE1;
+       case resume_TRIE2:
+           goto resume_point_TRIE2;
+       case resume_CURLYX:
+           goto resume_point_CURLYX;
+       case resume_WHILEM1:
+           goto resume_point_WHILEM1;
+       case resume_WHILEM2:
+           goto resume_point_WHILEM2;
+       case resume_WHILEM3:
+           goto resume_point_WHILEM3;
+       case resume_WHILEM4:
+           goto resume_point_WHILEM4;
+       case resume_WHILEM5:
+           goto resume_point_WHILEM5;
+       case resume_WHILEM6:
+           goto resume_point_WHILEM6;
+       case resume_CURLYM1:
+           goto resume_point_CURLYM1;
+       case resume_CURLYM2:
+           goto resume_point_CURLYM2;
+       case resume_CURLYM3:
+           goto resume_point_CURLYM3;
+       case resume_CURLYM4:
+           goto resume_point_CURLYM4;
+       case resume_IFMATCH:
+           goto resume_point_IFMATCH;
+       case resume_PLUS1:
+           goto resume_point_PLUS1;
+       case resume_PLUS2:
+           goto resume_point_PLUS2;
+       case resume_PLUS3:
+           goto resume_point_PLUS3;
+       case resume_PLUS4:
+           goto resume_point_PLUS4;
+       case resume_END:
+           goto resume_point_END;
+       default:
+           Perl_croak(aTHX_ "regexp resume memory corruption");
+       }
+       /* NOTREACHED */
+    }
+    return result;
+
 }
 
 /*
@@ -4590,7 +4882,7 @@ S_regrepeat(pTHX_ const regnode *p, I32 max)
 
     DEBUG_r({
                SV *re_debug_flags = NULL;
-               SV *prop = sv_newmortal();
+               SV * const prop = sv_newmortal();
                 GET_RE_DEBUG_FLAGS;
                 DEBUG_EXECUTE_r({
                regprop(prop, p);
@@ -4603,57 +4895,6 @@ S_regrepeat(pTHX_ const regnode *p, I32 max)
     return(c);
 }
 
-/*
- - regrepeat_hard - repeatedly match something, report total lenth and length
- *
- * The repeater is supposed to have constant non-zero length.
- */
-
-STATIC I32
-S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
-{
-    register char *scan = Nullch;
-    register char *start;
-    register char *loceol = PL_regeol;
-    I32 l = 0;
-    I32 count = 0, res = 1;
-
-    if (!max)
-       return 0;
-
-    start = PL_reginput;
-    if (PL_reg_match_utf8) {
-       while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
-           if (!count++) {
-               l = 0;
-               while (start < PL_reginput) {
-                   l++;
-                   start += UTF8SKIP(start);
-               }
-               *lp = l;
-               if (l == 0)
-                   return max;
-           }
-           if (count == max)
-               return count;
-       }
-    }
-    else {
-       while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
-           if (!count++) {
-               *lp = l = PL_reginput - start;
-               if (max != REG_INFTY && l*max < loceol - scan)
-                   loceol = scan + l*max;
-               if (l == 0)
-                   return max;
-           }
-       }
-    }
-    if (!res)
-       PL_reginput = scan;
-
-    return count;
-}
 
 /*
 - regclass_swash - prepare the utf8 swash
@@ -4662,6 +4903,7 @@ S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
 SV *
 Perl_regclass_swash(pTHX_ register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
 {
+    dVAR;
     SV *sw  = NULL;
     SV *si  = NULL;
     SV *alt = NULL;
@@ -4670,16 +4912,16 @@ Perl_regclass_swash(pTHX_ register const regnode* node, bool doinit, SV** listsv
        const U32 n = ARG(node);
 
        if (PL_regdata->what[n] == 's') {
-           SV *rv = (SV*)PL_regdata->data[n];
-           AV *av = (AV*)SvRV((SV*)rv);
-           SV **ary = AvARRAY(av);
+           SV * const rv = (SV*)PL_regdata->data[n];
+           AV * const av = (AV*)SvRV((SV*)rv);
+           SV **const ary = AvARRAY(av);
            SV **a, **b;
        
-           /* See the end of regcomp.c:S_reglass() for
+           /* See the end of regcomp.c:S_regclass() for
             * documentation of these array elements. */
 
            si = *ary;
-           a  = SvTYPE(ary[1]) == SVt_RV   ? &ary[1] : 0;
+           a  = SvROK(ary[1]) ? &ary[1] : 0;
            b  = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
 
            if (a)
@@ -4721,9 +4963,13 @@ S_reginclass(pTHX_ register const regnode *n, register const U8* p, STRLEN* lenp
     STRLEN len = 0;
     STRLEN plen;
 
-    if (do_utf8 && !UTF8_IS_INVARIANT(c))
-        c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
-                           ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
+    if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
+       c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
+                           ckWARN(WARN_UTF8) ? UTF8_CHECK_ONLY :
+                                       UTF8_ALLOW_ANYUV|UTF8_CHECK_ONLY);
+       if (len == (STRLEN)-1)
+           Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
+    }
 
     plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
     if (do_utf8 || (flags & ANYOF_UNICODE)) {
@@ -4737,7 +4983,7 @@ S_reginclass(pTHX_ register const regnode *n, register const U8* p, STRLEN* lenp
            match = TRUE;
        if (!match) {
            AV *av;
-           SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
+           SV * const sw = regclass_swash(n, TRUE, 0, (SV**)&av);
        
            if (sw) {
                if (swash_fetch(sw, p, do_utf8))
@@ -4745,11 +4991,10 @@ S_reginclass(pTHX_ register const regnode *n, register const U8* p, STRLEN* lenp
                else if (flags & ANYOF_FOLD) {
                    if (!match && lenp && av) {
                        I32 i;
-                     
                        for (i = 0; i <= av_len(av); i++) {
-                           SV* sv = *av_fetch(av, i, FALSE);
+                           SV* const sv = *av_fetch(av, i, FALSE);
                            STRLEN len;
-                           const char *s = SvPV_const(sv, len);
+                           const char * const s = SvPV_const(sv, len);
                        
                            if (len <= plen && memEQ(s, (char*)p, len)) {
                                *lenp = len;
@@ -4832,14 +5077,9 @@ S_reginclass(pTHX_ register const regnode *n, register const U8* p, STRLEN* lenp
 }
 
 STATIC U8 *
-S_reghop(pTHX_ U8 *s, I32 off)
-{
-    return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
-}
-
-STATIC U8 *
-S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
+S_reghop3(U8 *s, I32 off, U8* lim)
 {
+    dVAR;
     if (off >= 0) {
        while (off-- && s < lim) {
            /* XXX could check well-formedness here */
@@ -4862,14 +5102,9 @@ S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
 }
 
 STATIC U8 *
-S_reghopmaybe(pTHX_ U8 *s, I32 off)
-{
-    return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
-}
-
-STATIC U8 *
-S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
+S_reghopmaybe3(U8* s, I32 off, U8* lim)
 {
+    dVAR;
     if (off >= 0) {
        while (off-- && s < lim) {
            /* XXX could check well-formedness here */
@@ -4900,6 +5135,7 @@ S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
 static void
 restore_pos(pTHX_ void *arg)
 {
+    dVAR;
     PERL_UNUSED_ARG(arg);
     if (PL_reg_eval_set) {
        if (PL_reg_oldsaved) {
@@ -4942,6 +5178,7 @@ S_to_utf8_substr(pTHX_ register regexp *prog)
 STATIC void
 S_to_byte_substr(pTHX_ register regexp *prog)
 {
+    dVAR;
     if (prog->float_utf8 && !prog->float_substr) {
        SV* sv;
        prog->float_substr = sv = newSVsv(prog->float_utf8);