[perl #18154] Problem found in calculating offsets in regex
[p5sagit/p5-mst-13.2.git] / regcomp.c
index 43c46bb..6b35e39 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
 #endif
 
 typedef struct RExC_state_t {
-    U16                flags16;                /* are we folding, multilining? */
+    U32                flags;                  /* are we folding, multilining? */
     char       *precomp;               /* uncompiled string. */
     regexp     *rx;
     char       *start;                 /* Start of input for compile */
@@ -132,7 +132,7 @@ typedef struct RExC_state_t {
 #endif
 } RExC_state_t;
 
-#define RExC_flags16   (pRExC_state->flags16)
+#define RExC_flags     (pRExC_state->flags)
 #define RExC_precomp   (pRExC_state->precomp)
 #define RExC_rx                (pRExC_state->rx)
 #define RExC_start     (pRExC_state->start)
@@ -227,9 +227,9 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 #define SCF_DO_STCLASS         (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
 #define SCF_WHILEM_VISITED_POS 0x2000
 
-#define UTF RExC_utf8
-#define LOC (RExC_flags16 & PMf_LOCALE)
-#define FOLD (RExC_flags16 & PMf_FOLD)
+#define UTF (RExC_utf8 != 0)
+#define LOC ((RExC_flags & PMf_LOCALE) != 0)
+#define FOLD ((RExC_flags & PMf_FOLD) != 0)
 
 #define OOB_UNICODE            12345678
 #define OOB_NAMEDCLASS         -1
@@ -256,183 +256,159 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  * arg. Show regex, up to a maximum length. If it's too long, chop and add
  * "...".
  */
-#define        FAIL(msg)                                                             \
-    STMT_START {                                                             \
-        char *ellipses = "";                                                 \
-        IV len = RExC_end - RExC_precomp;                                \
-                                                                             \
-       if (!SIZE_ONLY)                                                      \
-           SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
-                                                                             \
-       if (len > RegexLengthToShowInErrorMessages) {                        \
-            /* chop 10 shorter than the max, to ensure meaning of "..." */   \
-           len = RegexLengthToShowInErrorMessages - 10;                     \
-           ellipses = "...";                                                \
-       }                                                                    \
-       Perl_croak(aTHX_ "%s in regex m/%.*s%s/",                            \
-                  msg, (int)len, RExC_precomp, ellipses);                  \
-    } STMT_END
+#define        FAIL(msg) STMT_START {                                          \
+    char *ellipses = "";                                               \
+    IV len = RExC_end - RExC_precomp;                                  \
+                                                                       \
+    if (!SIZE_ONLY)                                                    \
+       SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                      \
+    if (len > RegexLengthToShowInErrorMessages) {                      \
+       /* chop 10 shorter than the max, to ensure meaning of "..." */  \
+       len = RegexLengthToShowInErrorMessages - 10;                    \
+       ellipses = "...";                                               \
+    }                                                                  \
+    Perl_croak(aTHX_ "%s in regex m/%.*s%s/",                          \
+           msg, (int)len, RExC_precomp, ellipses);                     \
+} STMT_END
 
 /*
  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
  * args. Show regex, up to a maximum length. If it's too long, chop and add
  * "...".
  */
-#define        FAIL2(pat,msg)                                                        \
-    STMT_START {                                                             \
-        char *ellipses = "";                                                 \
-        IV len = RExC_end - RExC_precomp;                                \
-                                                                             \
-       if (!SIZE_ONLY)                                                      \
-           SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
-                                                                             \
-       if (len > RegexLengthToShowInErrorMessages) {                        \
-            /* chop 10 shorter than the max, to ensure meaning of "..." */   \
-           len = RegexLengthToShowInErrorMessages - 10;                     \
-           ellipses = "...";                                                \
-       }                                                                    \
-       S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/",                        \
-                   msg, (int)len, RExC_precomp, ellipses);                \
-    } STMT_END
+#define        FAIL2(pat,msg) STMT_START {                                     \
+    char *ellipses = "";                                               \
+    IV len = RExC_end - RExC_precomp;                                  \
+                                                                       \
+    if (!SIZE_ONLY)                                                    \
+       SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                      \
+    if (len > RegexLengthToShowInErrorMessages) {                      \
+       /* chop 10 shorter than the max, to ensure meaning of "..." */  \
+       len = RegexLengthToShowInErrorMessages - 10;                    \
+       ellipses = "...";                                               \
+    }                                                                  \
+    S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/",                      \
+           msg, (int)len, RExC_precomp, ellipses);                     \
+} STMT_END
 
 
 /*
  * Simple_vFAIL -- like FAIL, but marks the current location in the scan
  */
-#define        Simple_vFAIL(m)                                                      \
-    STMT_START {                                                             \
-      IV offset = RExC_parse - RExC_precomp; \
-                                                                             \
-      Perl_croak(aTHX_ "%s" REPORT_LOCATION,               \
-                m, (int)offset, RExC_precomp, RExC_precomp + offset);     \
-    } STMT_END
+#define        Simple_vFAIL(m) STMT_START {                                    \
+    IV offset = RExC_parse - RExC_precomp;                             \
+    Perl_croak(aTHX_ "%s" REPORT_LOCATION,                             \
+           m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
+} STMT_END
 
 /*
  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
  */
-#define        vFAIL(m)                                                             \
-    STMT_START {                                                             \
-      if (!SIZE_ONLY)                                                        \
-           SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
-      Simple_vFAIL(m);                                                       \
-    } STMT_END
+#define        vFAIL(m) STMT_START {                           \
+    if (!SIZE_ONLY)                                    \
+       SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);      \
+    Simple_vFAIL(m);                                   \
+} STMT_END
 
 /*
  * Like Simple_vFAIL(), but accepts two arguments.
  */
-#define        Simple_vFAIL2(m,a1)                                                  \
-    STMT_START {                                                             \
-      IV offset = RExC_parse - RExC_precomp; \
-                                                                             \
-      S_re_croak2(aTHX_ m, REPORT_LOCATION, a1,       \
-                 (int)offset, RExC_precomp, RExC_precomp + offset);       \
-    } STMT_END
+#define        Simple_vFAIL2(m,a1) STMT_START {                        \
+    IV offset = RExC_parse - RExC_precomp;                     \
+    S_re_croak2(aTHX_ m, REPORT_LOCATION, a1,                  \
+           (int)offset, RExC_precomp, RExC_precomp + offset);  \
+} STMT_END
 
 /*
  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
  */
-#define        vFAIL2(m,a1)                                                         \
-    STMT_START {                                                             \
-      if (!SIZE_ONLY)                                                        \
-           SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
-      Simple_vFAIL2(m, a1);                                                  \
-    } STMT_END
+#define        vFAIL2(m,a1) STMT_START {                       \
+    if (!SIZE_ONLY)                                    \
+       SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);      \
+    Simple_vFAIL2(m, a1);                              \
+} STMT_END
 
 
 /*
  * Like Simple_vFAIL(), but accepts three arguments.
  */
-#define        Simple_vFAIL3(m, a1, a2)                                             \
-    STMT_START {                                                             \
-      IV offset = RExC_parse - RExC_precomp; \
-                                                                             \
-      S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2,   \
-                 (int)offset, RExC_precomp, RExC_precomp + offset);       \
-    } STMT_END
+#define        Simple_vFAIL3(m, a1, a2) STMT_START {                   \
+    IV offset = RExC_parse - RExC_precomp;                     \
+    S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2,              \
+           (int)offset, RExC_precomp, RExC_precomp + offset);  \
+} STMT_END
 
 /*
  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
  */
-#define        vFAIL3(m,a1,a2)                                                      \
-    STMT_START {                                                             \
-      if (!SIZE_ONLY)                                                        \
-           SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
-      Simple_vFAIL3(m, a1, a2);                                              \
-    } STMT_END
+#define        vFAIL3(m,a1,a2) STMT_START {                    \
+    if (!SIZE_ONLY)                                    \
+       SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);      \
+    Simple_vFAIL3(m, a1, a2);                          \
+} STMT_END
 
 /*
  * Like Simple_vFAIL(), but accepts four arguments.
  */
-#define        Simple_vFAIL4(m, a1, a2, a3)                                         \
-    STMT_START {                                                             \
-      IV offset = RExC_parse - RExC_precomp; \
-                                                                             \
-      S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
-                 (int)offset, RExC_precomp, RExC_precomp + offset);       \
-    } STMT_END
+#define        Simple_vFAIL4(m, a1, a2, a3) STMT_START {               \
+    IV offset = RExC_parse - RExC_precomp;                     \
+    S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,          \
+           (int)offset, RExC_precomp, RExC_precomp + offset);  \
+} STMT_END
 
 /*
  * Like Simple_vFAIL(), but accepts five arguments.
  */
-#define        Simple_vFAIL5(m, a1, a2, a3, a4)                                     \
-    STMT_START {                                                             \
-      IV offset = RExC_parse - RExC_precomp; \
-      S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
-                 (int)offset, RExC_precomp, RExC_precomp + offset);       \
-    } STMT_END
-
-
-#define        vWARN(loc,m)                                                         \
-    STMT_START {                                                             \
-        IV offset = loc - RExC_precomp;          \
-       Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s" REPORT_LOCATION,\
-                m, (int)offset, RExC_precomp, RExC_precomp + offset);          \
-    } STMT_END                                                               \
-
-#define        vWARNdep(loc,m)                                                         \
-    STMT_START {                                                             \
-        IV offset = loc - RExC_precomp;          \
-       Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), "%s" REPORT_LOCATION,\
-                m, (int)offset, RExC_precomp, RExC_precomp + offset);          \
-    } STMT_END                                                               \
-
-
-#define        vWARN2(loc, m, a1)                                                   \
-    STMT_START {                                                             \
-        IV offset = loc - RExC_precomp;          \
-       Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,\
-                 a1,                                                         \
-                (int)offset, RExC_precomp, RExC_precomp + offset);        \
-    } STMT_END
-
-#define        vWARN3(loc, m, a1, a2)                                               \
-    STMT_START {                                                             \
-      IV offset = loc - RExC_precomp;        \
-       Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,                    \
-                 a1, a2,                                                     \
-                (int)offset, RExC_precomp, RExC_precomp + offset);        \
-    } STMT_END
-
-#define        vWARN4(loc, m, a1, a2, a3)                                           \
-    STMT_START {                                                             \
-      IV offset = loc - RExC_precomp;            \
-       Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,\
-                 a1, a2, a3,                                                 \
-                (int)offset, RExC_precomp, RExC_precomp + offset);        \
-    } STMT_END
-
-/* used for the parse_flags section for (?c) -- japhy */
-#define        vWARN5(loc, m, a1, a2, a3, a4)                                       \
-  STMT_START {                                                   \
-      IV offset = loc - RExC_precomp;   \
-        Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,      \
-                 a1, a2, a3, a4,                                 \
-                 (int)offset, RExC_precomp, RExC_precomp + offset);  \
-    } STMT_END
+#define        Simple_vFAIL5(m, a1, a2, a3, a4) STMT_START {           \
+    IV offset = RExC_parse - RExC_precomp;                     \
+    S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,      \
+           (int)offset, RExC_precomp, RExC_precomp + offset);  \
+} STMT_END
+
+
+#define        vWARN(loc,m) STMT_START {                                       \
+    IV offset = loc - RExC_precomp;                                    \
+    Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s" REPORT_LOCATION,     \
+           m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
+} STMT_END
+
+#define        vWARNdep(loc,m) STMT_START {                                    \
+    IV offset = loc - RExC_precomp;                                    \
+    Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP),         \
+           "%s" REPORT_LOCATION,                                       \
+           m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
+} STMT_END
+
+
+#define        vWARN2(loc, m, a1) STMT_START {                                 \
+    IV offset = loc - RExC_precomp;                                    \
+    Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,                \
+           a1, (int)offset, RExC_precomp, RExC_precomp + offset);      \
+} STMT_END
+
+#define        vWARN3(loc, m, a1, a2) STMT_START {                             \
+    IV offset = loc - RExC_precomp;                                    \
+    Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,                \
+           a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset);  \
+} STMT_END
+
+#define        vWARN4(loc, m, a1, a2, a3) STMT_START {                         \
+    IV offset = loc - RExC_precomp;                                    \
+    Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,                \
+           a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
+} STMT_END
+
+#define        vWARN5(loc, m, a1, a2, a3, a4) STMT_START {                     \
+    IV offset = loc - RExC_precomp;                                    \
+    Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,                \
+           a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \
+} STMT_END
 
 
 /* Allow for side effects in s */
-#define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (void)(s);} STMT_END
+#define REGC(c,s) STMT_START {                 \
+    if (!SIZE_ONLY) *(s) = (c); else (void)(s);        \
+} STMT_END
 
 /* Macros for recording node offsets.   20001227 mjd@plover.com 
  * Nodes are numbered 1, 2, 3, 4.  Node #n's position is recorded in
@@ -441,38 +417,42 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  */
 
 #define MJD_OFFSET_DEBUG(x)
-/* #define MJD_OFFSET_DEBUG(x) fprintf x */
-
-
-#  define Set_Node_Offset_To_R(node,byte)                           \
-   STMT_START {                                        \
-     if (! SIZE_ONLY) {                                  \
-       if((node) < 0) {                   \
-         Perl_croak(aTHX_ "value of node is %d in Offset macro", node); \
-       } else {                                                        \
-         RExC_offsets[2*(node)-1] = (byte);                               \
-       }                                                               \
-     }                                                                 \
-   } STMT_END
-
-#  define Set_Node_Offset(node,byte) Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
-#  define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
-
-#  define Set_Node_Length_To_R(node,len)                            \
-   STMT_START {                                        \
-     if (! SIZE_ONLY) {                                  \
-       MJD_OFFSET_DEBUG((stderr, "** (%d) size of node %d is %d.\n", __LINE__, (node), (len))); \
-       if((node) < 0) {                   \
-         Perl_croak(aTHX_ "value of node is %d in Length macro", node); \
-       } else {                                                        \
-         RExC_offsets[2*(node)] = (len);                               \
-       }                                                               \
-     }                                                                 \
-   } STMT_END
-
-#  define Set_Node_Length(node,len) Set_Node_Length_To_R((node)-RExC_emit_start, len)
-#  define Set_Cur_Node_Length(len)  Set_Node_Length(RExC_emit, len)
-#  define Set_Node_Cur_Length(node)   Set_Node_Length(node, RExC_parse - parse_start)
+/* #define MJD_OFFSET_DEBUG(x) Perl_warn_nocontext x */
+
+
+#define Set_Node_Offset_To_R(node,byte) STMT_START {                   \
+    if (! SIZE_ONLY) {                                                 \
+       MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n",         \
+               __LINE__, (node), (byte)));                             \
+       if((node) < 0) {                                                \
+           Perl_croak(aTHX_ "value of node is %d in Offset macro", node); \
+       } else {                                                        \
+           RExC_offsets[2*(node)-1] = (byte);                          \
+       }                                                               \
+    }                                                                  \
+} STMT_END
+
+#define Set_Node_Offset(node,byte) \
+    Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
+#define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
+
+#define Set_Node_Length_To_R(node,len) STMT_START {                    \
+    if (! SIZE_ONLY) {                                                 \
+       MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n",           \
+               __LINE__, (node), (len)));                              \
+       if((node) < 0) {                                                \
+           Perl_croak(aTHX_ "value of node is %d in Length macro", node); \
+       } else {                                                        \
+           RExC_offsets[2*(node)] = (len);                             \
+       }                                                               \
+    }                                                                  \
+} STMT_END
+
+#define Set_Node_Length(node,len) \
+    Set_Node_Length_To_R((node)-RExC_emit_start, len)
+#define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
+#define Set_Node_Cur_Length(node) \
+    Set_Node_Length(node, RExC_parse - parse_start)
 
 /* Get offsets and lengths */
 #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
@@ -505,6 +485,8 @@ S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
            data->offset_float_max = (l
                                      ? data->last_start_max
                                      : data->pos_min + data->pos_delta);
+           if ((U32)data->offset_float_max > (U32)I32_MAX)
+               data->offset_float_max = I32_MAX;
            if (data->flags & SF_BEFORE_EOL)
                data->flags
                    |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
@@ -1169,7 +1151,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
                    if (OP(nxt) != CLOSE)
                        goto nogo;
                    /* Now we know that nxt2 is the only contents: */
-                   oscan->flags = ARG(nxt);
+                   oscan->flags = (U8)ARG(nxt);
                    OP(oscan) = CURLYN;
                    OP(nxt1) = NOTHING; /* was OPEN. */
 #ifdef DEBUGGING
@@ -1205,7 +1187,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
 
                        if (OP(nxt) != CLOSE)
                            FAIL("Panic opt close");
-                       oscan->flags = ARG(nxt);
+                       oscan->flags = (U8)ARG(nxt);
                        OP(nxt1) = OPTIMIZED;   /* was OPEN. */
                        OP(nxt) = OPTIMIZED;    /* was CLOSE. */
 #ifdef DEBUGGING
@@ -1249,8 +1231,8 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
 
                    if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
                        nxt += ARG(nxt);
-                   PREVOPER(nxt)->flags = data->whilem_c
-                       | (RExC_whilem_seen << 4); /* On WHILEM */
+                   PREVOPER(nxt)->flags = (U8)(data->whilem_c
+                       | (RExC_whilem_seen << 4)); /* On WHILEM */
                }
                if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
                    pars++;
@@ -1304,8 +1286,8 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
                        } else {
                            /* start offset must point into the last copy */
                            data->last_start_min += minnext * (mincount - 1);
-                           data->last_start_max += is_inf ? 0 : (maxcount - 1)
-                               * (minnext + data->pos_delta);
+                           data->last_start_max += is_inf ? I32_MAX
+                               : (maxcount - 1) * (minnext + data->pos_delta);
                        }
                    }
                    /* It is counted once already... */
@@ -1591,7 +1573,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
                else if (minnext > U8_MAX) {
                    vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
                }
-               scan->flags = minnext;
+               scan->flags = (U8)minnext;
            }
            if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
                pars++;
@@ -1611,7 +1593,7 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, reg
            pars++;
        }
        else if (OP(scan) == CLOSE) {
-           if (ARG(scan) == is_par) {
+           if ((I32)ARG(scan) == is_par) {
                next = regnext(scan);
 
                if ( next && (OP(next) != WHILEM) && next < last)
@@ -1744,7 +1726,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
                       PL_colors[4],PL_colors[5],PL_colors[0],
                       (int)(xend - exp), RExC_precomp, PL_colors[1]);
     });
-    RExC_flags16 = pm->op_pmflags;
+    RExC_flags = pm->op_pmflags;
     RExC_sawback = 0;
 
     RExC_seen = 0;
@@ -1813,7 +1795,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
     RExC_rx = r;
 
     /* Second pass: emit code. */
-    RExC_flags16 = pm->op_pmflags;     /* don't let top level (?i) bleed */
+    RExC_flags = pm->op_pmflags;       /* don't let top level (?i) bleed */
     RExC_parse = exp;
     RExC_end = xend;
     RExC_naughty = 0;
@@ -1821,7 +1803,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
     RExC_emit_start = r->program;
     RExC_emit = r->program;
     /* Store the count of eval-groups for security checks: */
-    RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
+    RExC_emit->next_off = (U16)((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
     REGC((U8)REG_MAGIC, (char*) RExC_emit++);
     r->data = 0;
     if (reg(pRExC_state, 0, &flags) == NULL)
@@ -1829,7 +1811,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
 
     /* Dig out information for optimizations. */
     r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
-    pm->op_pmflags = RExC_flags16;
+    pm->op_pmflags = RExC_flags;
     if (UTF)
         r->reganch |= ROPT_UTF8;       /* Unicode in it? */
     r->regstclass = NULL;
@@ -1957,7 +1939,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
        if (longest_float_length
            || (data.flags & SF_FL_BEFORE_EOL
                && (!(data.flags & SF_FL_BEFORE_MEOL)
-                   || (RExC_flags16 & PMf_MULTILINE)))) {
+                   || (RExC_flags & PMf_MULTILINE)))) {
            int t;
 
            if (SvCUR(data.longest_fixed)                       /* ok to leave SvCUR */
@@ -1976,7 +1958,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
            r->float_max_offset = data.offset_float_max;
            t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
                       && (!(data.flags & SF_FL_BEFORE_MEOL)
-                          || (RExC_flags16 & PMf_MULTILINE)));
+                          || (RExC_flags & PMf_MULTILINE)));
            fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0);
        }
        else {
@@ -1990,7 +1972,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
        if (longest_fixed_length
            || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
                && (!(data.flags & SF_FIX_BEFORE_MEOL)
-                   || (RExC_flags16 & PMf_MULTILINE)))) {
+                   || (RExC_flags & PMf_MULTILINE)))) {
            int t;
 
            if (SvUTF8(data.longest_fixed)) {
@@ -2003,7 +1985,7 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
            r->anchored_offset = data.offset_fixed;
            t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
                 && (!(data.flags & SF_FIX_BEFORE_MEOL)
-                    || (RExC_flags16 & PMf_MULTILINE)));
+                    || (RExC_flags & PMf_MULTILINE)));
            fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0);
        }
        else {
@@ -2017,7 +1999,8 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
        if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset)
            && stclass_flag
            && !(data.start_class->flags & ANYOF_EOS)
-           && !cl_is_anything(data.start_class)) {
+           && !cl_is_anything(data.start_class))
+       {
            I32 n = add_data(pRExC_state, 1, "f");
 
            New(1006, RExC_rx->data->data[n], 1,
@@ -2072,7 +2055,8 @@ Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
        r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8
                = r->float_substr = r->float_utf8 = Nullsv;
        if (!(data.start_class->flags & ANYOF_EOS)
-           && !cl_is_anything(data.start_class)) {
+           && !cl_is_anything(data.start_class))
+       {
            I32 n = add_data(pRExC_state, 1, "f");
 
            New(1006, RExC_rx->data->data[n], 1,
@@ -2124,7 +2108,7 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
     register regnode *lastbr;
     register regnode *ender = 0;
     register I32 parno = 0;
-    I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
+    I32 flags, oregflags = RExC_flags, have_branch = 0, open = 0;
 
     /* for (?g), (?gc), and (?o) warnings; warning
        about (?c) will warn about (?g) -- japhy    */
@@ -2145,8 +2129,8 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
     /* Make an OPEN node, if parenthesized. */
     if (paren) {
        if (*RExC_parse == '?') { /* (?...) */
-           U16 posflags = 0, negflags = 0;
-           U16 *flagsp = &posflags;
+           U32 posflags = 0, negflags = 0;
+           U32 *flagsp = &posflags;
            int logical = 0;
            char *seqstart = RExC_parse;
 
@@ -2214,7 +2198,7 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
                    vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
                }
                if (!SIZE_ONLY) {
-                   AV *av;
+                   PAD *pad;
                
                    if (RExC_parse - 1 - s)
                        sv = newSVpvn(s, RExC_parse - 1 - s);
@@ -2223,7 +2207,7 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
 
                    ENTER;
                    Perl_save_re_context(aTHX);
-                   rop = sv_compile_2op(sv, &sop, "re", &av);
+                   rop = sv_compile_2op(sv, &sop, "re", &pad);
                    sop->op_private |= OPpREFCOUNTED;
                    /* re_dup will OpREFCNT_inc */
                    OpREFCNT_set(sop, 1);
@@ -2232,7 +2216,7 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
                    n = add_data(pRExC_state, 3, "nop");
                    RExC_rx->data->data[n] = (void*)rop;
                    RExC_rx->data->data[n+1] = (void*)sop;
-                   RExC_rx->data->data[n+2] = (void*)av;
+                   RExC_rx->data->data[n+2] = (void*)pad;
                    SvREFCNT_dec(sv);
                }
                else {                                          /* First pass */
@@ -2254,7 +2238,10 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
                     /* deal with the length of this later - MJD */
                    return ret;
                }
-               return reganode(pRExC_state, EVAL, n);
+               ret = reganode(pRExC_state, EVAL, n);
+               Set_Node_Length(ret, RExC_parse - parse_start + 1);
+               Set_Node_Offset(ret, parse_start);
+               return ret;
            }
            case '(':           /* (?(?{...})...) and (?(?=...)...) */
            {
@@ -2367,8 +2354,8 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
                    ++RExC_parse;
                    goto parse_flags;
                }
-               RExC_flags16 |= posflags;
-               RExC_flags16 &= ~negflags;
+               RExC_flags |= posflags;
+               RExC_flags &= ~negflags;
                if (*RExC_parse == ':') {
                    RExC_parse++;
                    paren = ':';
@@ -2484,12 +2471,14 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
        static char parens[] = "=!<,>";
 
        if (paren && (p = strchr(parens, paren))) {
-           int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
+           U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
            int flag = (p - parens) > 1;
 
            if (paren == '>')
                node = SUSPEND, flag = 0;
            reginsert(pRExC_state, node,ret);
+           Set_Node_Offset(ret, oregcomp_parse);
+           Set_Node_Length(ret,  RExC_parse - oregcomp_parse + 2);
            ret->flags = flag;
            regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
        }
@@ -2497,7 +2486,7 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
 
     /* Check for proper termination. */
     if (paren) {
-       RExC_flags16 = oregflags;
+       RExC_flags = oregflags;
        if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
            RExC_parse = oregcomp_parse;
            vFAIL("Unmatched (");
@@ -2679,8 +2668,8 @@ S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
            if (max && max < min)
                vFAIL("Can't do {n,m} with n > m");
            if (!SIZE_ONLY) {
-               ARG1_SET(ret, min);
-               ARG2_SET(ret, max);
+               ARG1_SET(ret, (U16)min);
+               ARG2_SET(ret, (U16)max);
            }
 
            goto nest_check;
@@ -2779,9 +2768,9 @@ tryagain:
     case '^':
        RExC_seen_zerolen++;
        nextchar(pRExC_state);
-       if (RExC_flags16 & PMf_MULTILINE)
+       if (RExC_flags & PMf_MULTILINE)
            ret = reg_node(pRExC_state, MBOL);
-       else if (RExC_flags16 & PMf_SINGLELINE)
+       else if (RExC_flags & PMf_SINGLELINE)
            ret = reg_node(pRExC_state, SBOL);
        else
            ret = reg_node(pRExC_state, BOL);
@@ -2791,9 +2780,9 @@ tryagain:
        nextchar(pRExC_state);
        if (*RExC_parse)
            RExC_seen_zerolen++;
-       if (RExC_flags16 & PMf_MULTILINE)
+       if (RExC_flags & PMf_MULTILINE)
            ret = reg_node(pRExC_state, MEOL);
-       else if (RExC_flags16 & PMf_SINGLELINE)
+       else if (RExC_flags & PMf_SINGLELINE)
            ret = reg_node(pRExC_state, SEOL);
        else
            ret = reg_node(pRExC_state, EOL);
@@ -2801,7 +2790,7 @@ tryagain:
        break;
     case '.':
        nextchar(pRExC_state);
-       if (RExC_flags16 & PMf_SINGLELINE)
+       if (RExC_flags & PMf_SINGLELINE)
            ret = reg_node(pRExC_state, SANY);
        else
            ret = reg_node(pRExC_state, REG_ANY);
@@ -2902,13 +2891,13 @@ tryagain:
             Set_Node_Length(ret, 2); /* MJD */
            break;
        case 'w':
-           ret = reg_node(pRExC_state, LOC ? ALNUML     : ALNUM);
+           ret = reg_node(pRExC_state, (U8)(LOC ? ALNUML     : ALNUM));
            *flagp |= HASWIDTH|SIMPLE;
            nextchar(pRExC_state);
             Set_Node_Length(ret, 2); /* MJD */
            break;
        case 'W':
-           ret = reg_node(pRExC_state, LOC ? NALNUML     : NALNUM);
+           ret = reg_node(pRExC_state, (U8)(LOC ? NALNUML    : NALNUM));
            *flagp |= HASWIDTH|SIMPLE;
            nextchar(pRExC_state);
             Set_Node_Length(ret, 2); /* MJD */
@@ -2916,7 +2905,7 @@ tryagain:
        case 'b':
            RExC_seen_zerolen++;
            RExC_seen |= REG_SEEN_LOOKBEHIND;
-           ret = reg_node(pRExC_state, LOC ? BOUNDL     : BOUND);
+           ret = reg_node(pRExC_state, (U8)(LOC ? BOUNDL     : BOUND));
            *flagp |= SIMPLE;
            nextchar(pRExC_state);
             Set_Node_Length(ret, 2); /* MJD */
@@ -2924,19 +2913,19 @@ tryagain:
        case 'B':
            RExC_seen_zerolen++;
            RExC_seen |= REG_SEEN_LOOKBEHIND;
-           ret = reg_node(pRExC_state, LOC ? NBOUNDL     : NBOUND);
+           ret = reg_node(pRExC_state, (U8)(LOC ? NBOUNDL    : NBOUND));
            *flagp |= SIMPLE;
            nextchar(pRExC_state);
             Set_Node_Length(ret, 2); /* MJD */
            break;
        case 's':
-           ret = reg_node(pRExC_state, LOC ? SPACEL     : SPACE);
+           ret = reg_node(pRExC_state, (U8)(LOC ? SPACEL     : SPACE));
            *flagp |= HASWIDTH|SIMPLE;
            nextchar(pRExC_state);
             Set_Node_Length(ret, 2); /* MJD */
            break;
        case 'S':
-           ret = reg_node(pRExC_state, LOC ? NSPACEL     : NSPACE);
+           ret = reg_node(pRExC_state, (U8)(LOC ? NSPACEL    : NSPACE));
            *flagp |= HASWIDTH|SIMPLE;
            nextchar(pRExC_state);
             Set_Node_Length(ret, 2); /* MJD */
@@ -2957,7 +2946,7 @@ tryagain:
        case 'P':
            {   
                char* oldregxend = RExC_end;
-                char* parse_start = RExC_parse;
+               char* parse_start = RExC_parse - 2;
 
                if (RExC_parse[1] == '{') {
                  /* a lovely hack--pretend we saw [\pX] instead */
@@ -2981,7 +2970,9 @@ tryagain:
 
                RExC_end = oldregxend;
                RExC_parse--;
-                Set_Node_Cur_Length(ret); /* MJD */
+
+               Set_Node_Offset(ret, parse_start + 2);
+               Set_Node_Cur_Length(ret);
                nextchar(pRExC_state);
                *flagp |= HASWIDTH|SIMPLE;
            }
@@ -3008,12 +2999,12 @@ tryagain:
                    while (isDIGIT(*RExC_parse))
                        RExC_parse++;
 
-                   if (!SIZE_ONLY && num > RExC_rx->nparens)
+                   if (!SIZE_ONLY && num > (I32)RExC_rx->nparens)
                        vFAIL("Reference to nonexistent group");
                    RExC_sawback = 1;
-                   ret = reganode(pRExC_state, FOLD
-                                  ? (LOC ? REFFL : REFF)
-                                  : REF, num);
+                   ret = reganode(pRExC_state,
+                                  (U8)(FOLD ? (LOC ? REFFL : REFF) : REF),
+                                  num);
                    *flagp |= HASWIDTH;
                     
                     /* override incorrect value set in reganode MJD */
@@ -3036,7 +3027,7 @@ tryagain:
        break;
 
     case '#':
-       if (RExC_flags16 & PMf_EXTENDED) {
+       if (RExC_flags & PMf_EXTENDED) {
            while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
            if (RExC_parse < RExC_end)
                goto tryagain;
@@ -3058,9 +3049,8 @@ tryagain:
 
        defchar:
            ender = 0;
-           ret = reg_node(pRExC_state, FOLD
-                         ? (LOC ? EXACTFL : EXACTF)
-                         : EXACT);
+           ret = reg_node(pRExC_state,
+                          (U8)(FOLD ? (LOC ? EXACTFL : EXACTF) : EXACT));
            s = STRING(ret);
            for (len = 0, p = RExC_parse - 1;
              len < 127 && p < RExC_end;
@@ -3068,7 +3058,7 @@ tryagain:
            {
                oldp = p;
 
-               if (RExC_flags16 & PMf_EXTENDED)
+               if (RExC_flags & PMf_EXTENDED)
                    p = regwhite(p, RExC_end);
                switch (*p) {
                case '^':
@@ -3193,7 +3183,7 @@ tryagain:
                        ender = *p++;
                    break;
                }
-               if (RExC_flags16 & PMf_EXTENDED)
+               if (RExC_flags & PMf_EXTENDED)
                    p = regwhite(p, RExC_end);
                if (UTF && FOLD) {
                    /* Prime the casefolded buffer. */
@@ -3235,7 +3225,7 @@ tryagain:
                    }
                    else {
                        len++;
-                       REGC(ender, s++);
+                       REGC((char)ender, s++);
                    }
                    break;
                }
@@ -3272,7 +3262,7 @@ tryagain:
                     len--;
                }
                else
-                   REGC(ender, s++);
+                   REGC((char)ender, s++);
            }
        loopdone:
            RExC_parse = p - 1;
@@ -3301,25 +3291,27 @@ tryagain:
     /* If the encoding pragma is in effect recode the text of
      * any EXACT-kind nodes. */
     if (PL_encoding && PL_regkind[(U8)OP(ret)] == EXACT) {
-        STRLEN oldlen = STR_LEN(ret);
-        SV *sv        = sv_2mortal(newSVpvn(STRING(ret), oldlen));
-
-        if (RExC_utf8)
-             SvUTF8_on(sv);
-        if (sv_utf8_downgrade(sv, TRUE)) {
-             char *s       = sv_recode_to_utf8(sv, PL_encoding);
-             STRLEN newlen = SvCUR(sv);
-        
-             if (!SIZE_ONLY) {
-                  DEBUG_r(PerlIO_printf(Perl_debug_log, "recode %*s to %*s\n",
-                                        (int)oldlen, STRING(ret),
-                                        (int)newlen, s));
-                  Copy(s, STRING(ret), newlen, char);
-                  STR_LEN(ret) += newlen - oldlen;
-                  RExC_emit += STR_SZ(newlen) - STR_SZ(oldlen);
-             } else
-                  RExC_size += STR_SZ(newlen) - STR_SZ(oldlen);
-        }
+       STRLEN oldlen = STR_LEN(ret);
+       SV *sv        = sv_2mortal(newSVpvn(STRING(ret), oldlen));
+
+       if (RExC_utf8)
+           SvUTF8_on(sv);
+       if (sv_utf8_downgrade(sv, TRUE)) {
+           char *s       = sv_recode_to_utf8(sv, PL_encoding);
+           STRLEN newlen = SvCUR(sv);
+
+           if (SvUTF8(sv))
+               RExC_utf8 = 1;
+           if (!SIZE_ONLY) {
+               DEBUG_r(PerlIO_printf(Perl_debug_log, "recode %*s to %*s\n",
+                                     (int)oldlen, STRING(ret),
+                                     (int)newlen, s));
+               Copy(s, STRING(ret), newlen, char);
+               STR_LEN(ret) += newlen - oldlen;
+               RExC_emit += STR_SZ(newlen) - STR_SZ(oldlen);
+           } else
+               RExC_size += STR_SZ(newlen) - STR_SZ(oldlen);
+       }
     }
 
     return(ret);
@@ -3520,6 +3512,9 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
     UV n;
     bool optimize_invert   = TRUE;
     AV* unicode_alternate  = 0;
+#ifdef EBCDIC
+    UV literal_endpoint = 0;
+#endif
 
     ret = reganode(pRExC_state, ANYOF, 0);
 
@@ -3682,6 +3677,10 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
                break;
            }
        } /* end of \blah */
+#ifdef EBCDIC
+       else
+           literal_endpoint++;
+#endif
 
        if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
 
@@ -4046,7 +4045,7 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
        } /* end of namedclass \blah */
 
        if (range) {
-           if (prevvalue > value) /* b-a */ {
+           if (prevvalue > (IV)value) /* b-a */ {
                Simple_vFAIL4("Invalid [] range \"%*.*s\"",
                              RExC_parse - rangebegin,
                              RExC_parse - rangebegin,
@@ -4084,8 +4083,11 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
                IV ceilvalue = value < 256 ? value : 255;
 
 #ifdef EBCDIC
-               if ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
-                   (isUPPER(prevvalue) && isUPPER(ceilvalue)))
+               /* In EBCDIC [\x89-\x91] should include
+                * the \x8e but [i-j] should not. */
+               if (literal_endpoint == 2 &&
+                   ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
+                    (isUPPER(prevvalue) && isUPPER(ceilvalue))))
                {
                    if (isLOWER(prevvalue)) {
                        for (i = prevvalue; i <= ceilvalue; i++)
@@ -4122,7 +4124,7 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
                          * character, insert also the folded version
                          * to the charclass. */
                         if (f != value) {
-                             if (foldlen == UNISKIP(f))
+                             if (foldlen == (STRLEN)UNISKIP(f))
                                  Perl_sv_catpvf(aTHX_ listsv,
                                                 "%04"UVxf"\n", f);
                              else {
@@ -4165,6 +4167,9 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
                    }
                }
            }
+#ifdef EBCDIC
+           literal_endpoint = 0;
+#endif
         }
 
        range = 0; /* this range (if it was one) is done now */
@@ -4185,7 +4190,7 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
        ) {
        for (value = 0; value < 256; ++value) {
            if (ANYOF_BITMAP_TEST(ret, value)) {
-               IV fold = PL_fold[value];
+               UV fold = PL_fold[value];
 
                if (fold != value)
                    ANYOF_BITMAP_SET(ret, fold);
@@ -4208,11 +4213,11 @@ S_regclass(pTHX_ RExC_state_t *pRExC_state)
        SV *rv;
 
        /* The 0th element stores the character class description
-        * in its textual form: used later (regexec.c:Perl_regclass_swatch())
+        * in its textual form: used later (regexec.c:Perl_regclass_swash())
         * to initialize the appropriate swash (which gets stored in
         * the 1st element), and also useful for dumping the regnode.
         * The 2nd element stores the multicharacter foldings,
-        * used later (regexec.c:s_reginclasslen()). */
+        * used later (regexec.c:S_reginclass()). */
        av_store(av, 0, listsv);
        av_store(av, 1, NULL);
        av_store(av, 2, (SV*)unicode_alternate);
@@ -4238,7 +4243,7 @@ S_nextchar(pTHX_ RExC_state_t *pRExC_state)
            RExC_parse++;
            continue;
        }
-       if (RExC_flags16 & PMf_EXTENDED) {
+       if (RExC_flags & PMf_EXTENDED) {
            if (isSPACE(*RExC_parse)) {
                RExC_parse++;
                continue;
@@ -4274,7 +4279,7 @@ S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
     ptr = ret;
     FILL_ADVANCE_NODE(ptr, op);
     if (RExC_offsets) {         /* MJD */
-      MJD_OFFSET_DEBUG((stderr, "%s:%u: (op %s) %s %u <- %u (len %u) (max %u).\n", 
+       MJD_OFFSET_DEBUG(("%s:%u: (op %s) %s %u <- %u (len %u) (max %u).\n", 
               "reg_node", __LINE__, 
               reg_name[op],
               RExC_emit - RExC_emit_start > RExC_offsets[0] 
@@ -4282,7 +4287,7 @@ S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
               RExC_emit - RExC_emit_start,
               RExC_parse - RExC_start,
               RExC_offsets[0])); 
-      Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
+       Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
     }
             
     RExC_emit = ptr;
@@ -4310,14 +4315,16 @@ S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
     ptr = ret;
     FILL_ADVANCE_NODE_ARG(ptr, op, arg);
     if (RExC_offsets) {         /* MJD */
-      MJD_OFFSET_DEBUG((stderr, "%s: %s %u <- %u (max %u).\n", 
+       MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %u <- %u (max %u).\n", 
               "reganode",
+             __LINE__,
+             reg_name[op],
               RExC_emit - RExC_emit_start > RExC_offsets[0] ? 
               "Overwriting end of array!\n" : "OK",
               RExC_emit - RExC_emit_start,
               RExC_parse - RExC_start,
               RExC_offsets[0])); 
-      Set_Cur_Node_Offset;
+       Set_Cur_Node_Offset;
     }
             
     RExC_emit = ptr;
@@ -4360,29 +4367,33 @@ S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
     while (src > opnd) {
        StructCopy(--src, --dst, regnode);
         if (RExC_offsets) {     /* MJD 20010112 */
-          MJD_OFFSET_DEBUG((stderr, "%s: %s copy %u -> %u (max %u).\n", 
+           MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %u -> %u (max %u).\n",
                   "reg_insert",
+                 __LINE__,
+                 reg_name[op],
                   dst - RExC_emit_start > RExC_offsets[0] 
                   ? "Overwriting end of array!\n" : "OK",
                   src - RExC_emit_start,
                   dst - RExC_emit_start,
                   RExC_offsets[0])); 
-          Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
-          Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
+           Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
+           Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
         }
     }
     
 
     place = opnd;              /* Op node, where operand used to be. */
     if (RExC_offsets) {         /* MJD */
-      MJD_OFFSET_DEBUG((stderr, "%s: %s %u <- %u (max %u).\n", 
+       MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %u <- %u (max %u).\n", 
               "reginsert",
+             __LINE__,
+             reg_name[op],
               place - RExC_emit_start > RExC_offsets[0] 
               ? "Overwriting end of array!\n" : "OK",
               place - RExC_emit_start,
               RExC_parse - RExC_start,
               RExC_offsets[0])); 
-      Set_Node_Offset(place, RExC_parse);
+       Set_Node_Offset(place, RExC_parse);
     }
     src = NEXTOPER(place);
     FILL_ADVANCE_NODE(place, op);
@@ -4772,7 +4783,6 @@ Perl_regprop(pTHX_ SV *sv, regnode *o)
        
            if (lv) {
                if (sw) {
-                   UV i;
                    U8 s[UTF8_MAXLEN+1];
                
                    for (i = 0; i <= 256; i++) { /* just the first 256 */
@@ -4867,9 +4877,13 @@ Perl_pregfree(pTHX_ struct regexp *r)
     if (!r || (--r->refcnt > 0))
        return;
     DEBUG_r({
-         char *s = pv_uni_display(dsv, (U8*)r->precomp, r->prelen, 60,
-                                 UNI_DISPLAY_REGEX);
-        int len = SvCUR(dsv);
+        int len;
+         char *s;
+
+        s = (r->reganch & ROPT_UTF8) ? pv_uni_display(dsv, (U8*)r->precomp,
+               r->prelen, 60, UNI_DISPLAY_REGEX)
+            : pv_display(dsv, r->precomp, r->prelen, 0, 60);
+        len = SvCUR(dsv);
         if (!PL_colorset)
              reginitcolors();
         PerlIO_printf(Perl_debug_log,
@@ -4899,9 +4913,8 @@ Perl_pregfree(pTHX_ struct regexp *r)
     }
     if (r->data) {
        int n = r->data->count;
-       AV* new_comppad = NULL;
-       AV* old_comppad;
-       SV** old_curpad;
+       PAD* new_comppad = NULL;
+       PAD* old_comppad;
 
        while (--n >= 0) {
           /* If you add a ->what type here, update the comment in regcomp.h */
@@ -4918,22 +4931,16 @@ Perl_pregfree(pTHX_ struct regexp *r)
            case 'o':
                if (new_comppad == NULL)
                    Perl_croak(aTHX_ "panic: pregfree comppad");
-               old_comppad = PL_comppad;
-               old_curpad = PL_curpad;
-               /* Watch out for global destruction's random ordering. */
-               if (SvTYPE(new_comppad) == SVt_PVAV) {
-                   PL_comppad = new_comppad;
-                   PL_curpad = AvARRAY(new_comppad);
-               }
-               else
-                   PL_curpad = NULL;
-
+               PAD_SAVE_LOCAL(old_comppad,
+                   /* Watch out for global destruction's random ordering. */
+                   (SvTYPE(new_comppad) == SVt_PVAV) ?
+                               new_comppad : Null(PAD *)
+               );
                if (!OpREFCNT_dec((OP_4tree*)r->data->data[n])) {
                     op_free((OP_4tree*)r->data->data[n]);
                }
 
-               PL_comppad = old_comppad;
-               PL_curpad = old_curpad;
+               PAD_RESTORE_LOCAL(old_comppad);
                SvREFCNT_dec((SV*)new_comppad);
                new_comppad = NULL;
                break;
@@ -5015,7 +5022,7 @@ Perl_save_re_context(pTHX)
     SAVEPPTR(RExC_precomp);            /* uncompiled string. */
     SAVEI32(RExC_npar);                /* () count. */
     SAVEI32(RExC_size);                /* Code size. */
-    SAVEI16(RExC_flags16);             /* are we folding, multilining? */
+    SAVEI32(RExC_flags);               /* are we folding, multilining? */
     SAVEVPTR(RExC_rx);         /* from regcomp.c */
     SAVEI32(RExC_seen);                /* from regcomp.c */
     SAVEI32(RExC_sawback);             /* Did we see \1, ...? */