This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / scope.c
diff --git a/scope.c b/scope.c
index dfaab80..f5e1984 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -25,6 +25,7 @@
 #include "EXTERN.h"
 #define PERL_IN_SCOPE_C
 #include "perl.h"
 #include "EXTERN.h"
 #define PERL_IN_SCOPE_C
 #include "perl.h"
+#include "feature.h"
 
 SV**
 Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
 
 SV**
 Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
@@ -46,10 +47,10 @@ Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
         128;
 #endif
     /* If the total might wrap, panic instead. This is really testing
         128;
 #endif
     /* If the total might wrap, panic instead. This is really testing
-     * that (current + n + extra < SSize_t_MAX), but done in a way that
+     * that (current + n + extra < Stack_off_t_MAX), but done in a way that
      * can't wrap */
      * can't wrap */
-    if (UNLIKELY(   current         > SSize_t_MAX - extra
-                 || current + extra > SSize_t_MAX - n
+    if (UNLIKELY(   current         > Stack_off_t_MAX - extra
+                 || current + extra > Stack_off_t_MAX - n
     ))
         /* diag_listed_as: Out of memory during %s extend */
         Perl_croak(aTHX_ "Out of memory during stack extend");
     ))
         /* diag_listed_as: Out of memory during %s extend */
         Perl_croak(aTHX_ "Out of memory during stack extend");
@@ -62,26 +63,42 @@ Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
     return PL_stack_sp;
 }
 
     return PL_stack_sp;
 }
 
-#ifndef STRESS_REALLOC
-#define GROW(old) ((old) * 3 / 2)
-#else
+#ifdef STRESS_REALLOC
 #define GROW(old) ((old) + 1)
 #define GROW(old) ((old) + 1)
+#else
+#define GROW(old) ((old) * 3 / 2)
 #endif
 
 #endif
 
+/* for backcomp */
 PERL_SI *
 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
 {
 PERL_SI *
 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
 {
+    return new_stackinfo_flags(stitems, cxitems, 0);
+}
+
+/* current flag meanings:
+ *   1 make the new arg stack AvREAL
+ */
+
+PERL_SI *
+Perl_new_stackinfo_flags(pTHX_ I32 stitems, I32 cxitems, UV flags)
+{
     PERL_SI *si;
     Newx(si, 1, PERL_SI);
     si->si_stack = newAV();
     PERL_SI *si;
     Newx(si, 1, PERL_SI);
     si->si_stack = newAV();
-    AvREAL_off(si->si_stack);
+    if (!(flags & 1))
+        AvREAL_off(si->si_stack);
     av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
     AvALLOC(si->si_stack)[0] = &PL_sv_undef;
     AvFILLp(si->si_stack) = 0;
     av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
     AvALLOC(si->si_stack)[0] = &PL_sv_undef;
     AvFILLp(si->si_stack) = 0;
+#ifdef PERL_RC_STACK
+    si->si_stack_nonrc_base = 0;
+#endif
     si->si_prev = 0;
     si->si_next = 0;
     si->si_cxmax = cxitems - 1;
     si->si_cxix = -1;
     si->si_prev = 0;
     si->si_next = 0;
     si->si_cxmax = cxitems - 1;
     si->si_cxix = -1;
+    si->si_cxsubix = -1;
     si->si_type = PERLSI_UNDEF;
     Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
     /* Without any kind of initialising CX_PUSHSUBST()
     si->si_type = PERLSI_UNDEF;
     Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
     /* Without any kind of initialising CX_PUSHSUBST()
@@ -103,16 +120,25 @@ Perl_cxinc(pTHX)
     return cxstack_ix + 1;
 }
 
     return cxstack_ix + 1;
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc push_scope
+
+Implements L<perlapi/C<ENTER>>
+
+=cut
+*/
+
 void
 Perl_push_scope(pTHX)
 {
     if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
         const IV new_max = GROW(PL_scopestack_max);
 void
 Perl_push_scope(pTHX)
 {
     if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
         const IV new_max = GROW(PL_scopestack_max);
-       Renew(PL_scopestack, new_max, I32);
+        Renew(PL_scopestack, new_max, I32);
 #ifdef DEBUGGING
 #ifdef DEBUGGING
-       Renew(PL_scopestack_name, new_max, const char*);
+        Renew(PL_scopestack_name, new_max, const char*);
 #endif
 #endif
-       PL_scopestack_max = new_max;
+        PL_scopestack_max = new_max;
     }
 #ifdef DEBUGGING
     PL_scopestack_name[PL_scopestack_ix] = "unknown";
     }
 #ifdef DEBUGGING
     PL_scopestack_name[PL_scopestack_ix] = "unknown";
@@ -121,6 +147,15 @@ Perl_push_scope(pTHX)
 
 }
 
 
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc pop_scope
+
+Implements L<perlapi/C<LEAVE>>
+
+=cut
+*/
+
 void
 Perl_pop_scope(pTHX)
 {
 void
 Perl_pop_scope(pTHX)
 {
@@ -128,13 +163,13 @@ Perl_pop_scope(pTHX)
     LEAVE_SCOPE(oldsave);
 }
 
     LEAVE_SCOPE(oldsave);
 }
 
-I32 *
+Stack_off_t *
 Perl_markstack_grow(pTHX)
 {
     const I32 oldmax = PL_markstack_max - PL_markstack;
     const I32 newmax = GROW(oldmax);
 
 Perl_markstack_grow(pTHX)
 {
     const I32 oldmax = PL_markstack_max - PL_markstack;
     const I32 newmax = GROW(oldmax);
 
-    Renew(PL_markstack, newmax, I32);
+    Renew(PL_markstack, newmax, Stack_off_t);
     PL_markstack_max = PL_markstack + newmax;
     PL_markstack_ptr = PL_markstack + oldmax;
     DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
     PL_markstack_max = PL_markstack + newmax;
     PL_markstack_ptr = PL_markstack + oldmax;
     DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
@@ -146,25 +181,44 @@ Perl_markstack_grow(pTHX)
 void
 Perl_savestack_grow(pTHX)
 {
 void
 Perl_savestack_grow(pTHX)
 {
-    IV new_max;
-#ifdef STRESS_REALLOC
-    new_max = PL_savestack_max + SS_MAXPUSH;
-#else
-    new_max = GROW(PL_savestack_max);
-#endif
-    /* Note that we allocate SS_MAXPUSH slots higher than ss_max
-     * so that SS_ADD_END(), SSGROW() etc can do a simper check */
-    Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
-    PL_savestack_max = new_max;
+    const I32 by = PL_savestack_max - PL_savestack_ix;
+    Perl_savestack_grow_cnt(aTHX_ by);
 }
 
 void
 Perl_savestack_grow_cnt(pTHX_ I32 need)
 {
 }
 
 void
 Perl_savestack_grow_cnt(pTHX_ I32 need)
 {
-    const IV new_max = PL_savestack_ix + need;
-    /* Note that we allocate SS_MAXPUSH slots higher than ss_max
-     * so that SS_ADD_END(), SSGROW() etc can do a simper check */
-    Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
+    /* NOTE: PL_savestack_max and PL_savestack_ix are I32.
+     *
+     * This makes sense when you consider that having I32_MAX items on
+     * the stack would be quite large.
+     *
+     * However, we use IV here so that we can detect if the new requested
+     * amount is larger than I32_MAX.
+     */
+    const IV new_floor = PL_savestack_max + need; /* what we need */
+    /* the GROW() macro normally does scales by 1.5 but under
+     * STRESS_REALLOC it simply adds 1 */
+    IV new_max         = GROW(new_floor); /* and some extra */
+
+    /* the new_max < PL_savestack_max is for cases where IV is I32
+     * and we have rolled over from I32_MAX to a small value */
+    if (new_max > I32_MAX || new_max < PL_savestack_max) {
+        if (new_floor > I32_MAX || new_floor < PL_savestack_max) {
+            Perl_croak(aTHX_ "panic: savestack overflows I32_MAX");
+        }
+        new_max = new_floor;
+    }
+
+    /* Note that we add an additional SS_MAXPUSH slots on top of
+     * PL_savestack_max so that SS_ADD_END(), SSGROW() etc can do
+     * a simper check and if necessary realloc *after* apparently
+     * overwriting the current PL_savestack_max. See scope.h.
+     *
+     * The +1 is because new_max/PL_savestack_max is the highest
+     * index, by Renew needs the number of items, which is one
+     * larger than the highest index. */
+    Renew(PL_savestack, new_max + SS_MAXPUSH + 1, ANY);
     PL_savestack_max = new_max;
 }
 
     PL_savestack_max = new_max;
 }
 
@@ -192,8 +246,12 @@ Perl_tmps_grow_p(pTHX_ SSize_t ix)
 {
     SSize_t extend_to = ix;
 #ifndef STRESS_REALLOC
 {
     SSize_t extend_to = ix;
 #ifndef STRESS_REALLOC
-    if (ix - PL_tmps_max < 128)
-       extend_to += (PL_tmps_max < 512) ? 128 : 512;
+    SSize_t grow_size = PL_tmps_max < 512 ? 128 : PL_tmps_max / 2;
+    if (extend_to > SSize_t_MAX - grow_size - 1)
+        /* trigger memwrap message or fail allocation */
+        extend_to = SSize_t_MAX-1;
+    else
+        extend_to += grow_size;
 #endif
     Renew(PL_tmps_stack, extend_to + 1, SV*);
     PL_tmps_max = extend_to + 1;
 #endif
     Renew(PL_tmps_stack, extend_to + 1, SV*);
     PL_tmps_max = extend_to + 1;
@@ -207,17 +265,38 @@ Perl_free_tmps(pTHX)
     /* XXX should tmps_floor live in cxstack? */
     const SSize_t myfloor = PL_tmps_floor;
     while (PL_tmps_ix > myfloor) {      /* clean up after last statement */
     /* XXX should tmps_floor live in cxstack? */
     const SSize_t myfloor = PL_tmps_floor;
     while (PL_tmps_ix > myfloor) {      /* clean up after last statement */
-       SV* const sv = PL_tmps_stack[PL_tmps_ix--];
+        SV* const sv = PL_tmps_stack[PL_tmps_ix--];
 #ifdef PERL_POISON
 #ifdef PERL_POISON
-       PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
+        PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
 #endif
 #endif
-       if (LIKELY(sv)) {
-           SvTEMP_off(sv);
-           SvREFCNT_dec_NN(sv);                /* note, can modify tmps_ix!!! */
-       }
+        if (LIKELY(sv)) {
+            SvTEMP_off(sv);
+            SvREFCNT_dec_NN(sv);               /* note, can modify tmps_ix!!! */
+        }
     }
 }
 
     }
 }
 
+/*
+=for apidoc save_scalar_at
+
+A helper function for localizing the SV referenced by C<*sptr>.
+
+If C<SAVEf_KEEPOLDELEM> is set in in C<flags>, the function returns the input
+scalar untouched.
+
+Otherwise it replaces C<*sptr> with a new C<undef> scalar, and returns that.
+The new scalar will have the old one's magic (if any) copied to it.
+If there is such magic, and C<SAVEf_SETMAGIC> is set in in C<flags>, 'set'
+magic will be processed on the new scalar.  If unset, 'set' magic will be
+skipped.  The latter typically means that assignment will soon follow (I<e.g.>,
+S<C<'local $x = $y'>>), and that will handle the magic.
+
+=for apidoc Amnh ||SAVEf_KEEPOLDELEM
+=for apidoc Amnh ||SAVEf_SETMAGIC
+
+=cut
+*/
+
 STATIC SV *
 S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
 {
 STATIC SV *
 S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
 {
@@ -230,7 +309,7 @@ S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
     if (flags & SAVEf_KEEPOLDELEM)
         sv = osv;
     else {
     if (flags & SAVEf_KEEPOLDELEM)
         sv = osv;
     else {
-        sv  = (*sptr = newSV(0));
+        sv  = (*sptr = newSV_type(SVt_NULL));
         if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
             mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
     }
         if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
             mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
     }
@@ -264,8 +343,17 @@ Perl_save_scalar(pTHX_ GV *gv)
     return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
 }
 
     return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
 }
 
-/* Like save_sptr(), but also SvREFCNT_dec()s the new value.  Can be used to
- * restore a global SV to its prior contents, freeing new value. */
+/*
+=for apidoc save_generic_svref
+
+Implements C<SAVEGENERICSV>.
+
+Like save_sptr(), but also SvREFCNT_dec()s the new value.  Can be used to
+restore a global SV to its prior contents, freeing new value.
+
+=cut
+ */
+
 void
 Perl_save_generic_svref(pTHX_ SV **sptr)
 {
 void
 Perl_save_generic_svref(pTHX_ SV **sptr)
 {
@@ -274,9 +362,56 @@ Perl_save_generic_svref(pTHX_ SV **sptr)
     save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
 }
 
     save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
 }
 
-/* Like save_pptr(), but also Safefree()s the new value if it is different
- * from the old one.  Can be used to restore a global char* to its prior
- * contents, freeing new value. */
+
+/*
+=for apidoc save_rcpv
+
+Implements C<SAVERCPV>.
+
+Saves and restores a refcounted string, similar to what
+save_generic_svref would do for a SV*. Can be used to restore
+a refcounted string to its previous state. Performs the 
+appropriate refcount counting so that nothing should leak
+or be prematurely freed.
+
+=cut
+ */
+void
+Perl_save_rcpv(pTHX_ char **prcpv) {
+    PERL_ARGS_ASSERT_SAVE_RCPV;
+    save_pushptrptr(prcpv, rcpv_copy(*prcpv), SAVEt_RCPV);
+}
+
+/*
+=for apidoc save_freercpv
+
+Implements C<SAVEFREERCPV>.
+
+Saves and frees a refcounted string. Calls rcpv_free()
+on the argument when the current pseudo block is finished.
+
+=cut
+ */
+void
+Perl_save_freercpv(pTHX_ char *rcpv) {
+    PERL_ARGS_ASSERT_SAVE_FREERCPV;
+    save_pushptr(rcpv, SAVEt_FREERCPV);
+}
+
+
+/*
+=for apidoc_section $callback
+=for apidoc save_generic_pvref
+
+Implements C<SAVEGENERICPV>.
+
+Like save_pptr(), but also Safefree()s the new value if it is different
+from the old one.  Can be used to restore a global char* to its prior
+contents, freeing new value.
+
+=cut
+ */
+
 void
 Perl_save_generic_pvref(pTHX_ char **str)
 {
 void
 Perl_save_generic_pvref(pTHX_ char **str)
 {
@@ -285,9 +420,19 @@ Perl_save_generic_pvref(pTHX_ char **str)
     save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
 }
 
     save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
 }
 
-/* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
- * Can be used to restore a shared global char* to its prior
- * contents, freeing new value. */
+/*
+=for apidoc_section $callback
+=for apidoc save_shared_pvref
+
+Implements C<SAVESHAREDPV>.
+
+Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
+Can be used to restore a shared global char* to its prior
+contents, freeing new value.
+
+=cut
+ */
+
 void
 Perl_save_shared_pvref(pTHX_ char **str)
 {
 void
 Perl_save_shared_pvref(pTHX_ char **str)
 {
@@ -296,7 +441,17 @@ Perl_save_shared_pvref(pTHX_ char **str)
     save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
 }
 
     save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
 }
 
-/* set the SvFLAGS specified by mask to the values in val */
+
+/*
+=for apidoc_section $callback
+=for apidoc save_set_svflags
+
+Implements C<SAVESETSVFLAGS>.
+
+Set the SvFLAGS specified by mask to the values in val
+
+=cut
+ */
 
 void
 Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
 
 void
 Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
@@ -313,14 +468,17 @@ Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
 }
 
 /*
 }
 
 /*
+
+=for apidoc_section $GV
+
 =for apidoc save_gp
 
 Saves the current GP of gv on the save stack to be restored on scope exit.
 
 =for apidoc save_gp
 
 Saves the current GP of gv on the save stack to be restored on scope exit.
 
-If empty is true, replace the GP with a new GP.
+If C<empty> is true, replace the GP with a new GP.
 
 
-If empty is false, mark gv with GVf_INTRO so the next reference
-assigned is localized, which is how C< local *foo = $someref; > works.
+If C<empty> is false, mark C<gv> with C<GVf_INTRO> so the next reference
+assigned is localized, which is how S<C< local *foo = $someref; >> works.
 
 =cut
 */
 
 =cut
 */
@@ -330,30 +488,41 @@ Perl_save_gp(pTHX_ GV *gv, I32 empty)
 {
     PERL_ARGS_ASSERT_SAVE_GP;
 
 {
     PERL_ARGS_ASSERT_SAVE_GP;
 
+    /* XXX For now, we just upgrade any coderef in the stash to a full GV
+           during localisation.  Maybe at some point we could make localis-
+           ation work without needing the upgrade.  (In which case our
+           callers should probably call a different function, not save_gp.)
+     */
+    if (!isGV(gv)) {
+        assert(isGV_or_RVCV(gv));
+        (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
+        assert(isGV(gv));
+    }
+
     save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
 
     if (empty) {
     save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
 
     if (empty) {
-       GP *gp = Perl_newGP(aTHX_ gv);
-       HV * const stash = GvSTASH(gv);
-       bool isa_changed = 0;
-
-       if (stash && HvENAME(stash)) {
-           if (GvNAMELEN(gv) == 3 && strnEQ(GvNAME(gv), "ISA", 3))
-               isa_changed = TRUE;
-           else if (GvCVu(gv))
-               /* taking a method out of circulation ("local")*/
+        GP *gp = Perl_newGP(aTHX_ gv);
+        HV * const stash = GvSTASH(gv);
+        bool isa_changed = 0;
+
+        if (stash && HvHasENAME(stash)) {
+            if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
+                isa_changed = TRUE;
+            else if (GvCVu(gv))
+                /* taking a method out of circulation ("local")*/
                 mro_method_changed_in(stash);
                 mro_method_changed_in(stash);
-       }
-       if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
-           gp->gp_io = newIO();
-           IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
-       }
-       GvGP_set(gv,gp);
-       if (isa_changed) mro_isa_changed_in(stash);
+        }
+        if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
+            gp->gp_io = newIO();
+            IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
+        }
+        GvGP_set(gv,gp);
+        if (isa_changed) mro_isa_changed_in(stash);
     }
     else {
     }
     else {
-       gp_ref(GvGP(gv));
-       GvINTRO_on(gv);
+        gp_ref(GvGP(gv));
+        GvINTRO_on(gv);
     }
 }
 
     }
 }
 
@@ -366,13 +535,13 @@ Perl_save_ary(pTHX_ GV *gv)
     PERL_ARGS_ASSERT_SAVE_ARY;
 
     if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
     PERL_ARGS_ASSERT_SAVE_ARY;
 
     if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
-       av_reify(oav);
+        av_reify(oav);
     save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
 
     GvAV(gv) = NULL;
     av = GvAVn(gv);
     if (UNLIKELY(SvMAGIC(oav)))
     save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
 
     GvAV(gv) = NULL;
     av = GvAVn(gv);
     if (UNLIKELY(SvMAGIC(oav)))
-       mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
+        mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
     return av;
 }
 
     return av;
 }
 
@@ -384,13 +553,13 @@ Perl_save_hash(pTHX_ GV *gv)
     PERL_ARGS_ASSERT_SAVE_HASH;
 
     save_pushptrptr(
     PERL_ARGS_ASSERT_SAVE_HASH;
 
     save_pushptrptr(
-       SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
+        SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
     );
 
     GvHV(gv) = NULL;
     hv = GvHVn(gv);
     if (UNLIKELY(SvMAGIC(ohv)))
     );
 
     GvHV(gv) = NULL;
     hv = GvHVn(gv);
     if (UNLIKELY(SvMAGIC(ohv)))
-       mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
+        mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
     return hv;
 }
 
     return hv;
 }
 
@@ -402,8 +571,8 @@ Perl_save_item(pTHX_ SV *item)
     PERL_ARGS_ASSERT_SAVE_ITEM;
 
     save_pushptrptr(item, /* remember the pointer */
     PERL_ARGS_ASSERT_SAVE_ITEM;
 
     save_pushptrptr(item, /* remember the pointer */
-                   sv,   /* remember the value */
-                   SAVEt_ITEM);
+                    sv,   /* remember the value */
+                    SAVEt_ITEM);
 }
 
 void
 }
 
 void
@@ -496,14 +665,22 @@ Perl_save_I32(pTHX_ I32 *intp)
 void
 Perl_save_strlen(pTHX_ STRLEN *ptr)
 {
 void
 Perl_save_strlen(pTHX_ STRLEN *ptr)
 {
+    const IV i = *ptr;
+    UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL);
+    int size = 2;
     dSS_ADD;
 
     PERL_ARGS_ASSERT_SAVE_STRLEN;
 
     dSS_ADD;
 
     PERL_ARGS_ASSERT_SAVE_STRLEN;
 
-    SS_ADD_IV(*ptr);
+    if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
+        SS_ADD_IV(*ptr);
+        type = SAVEt_STRLEN;
+        size++;
+    }
+
     SS_ADD_PTR(ptr);
     SS_ADD_PTR(ptr);
-    SS_ADD_UV(SAVEt_STRLEN);
-    SS_ADD_END(3);
+    SS_ADD_UV(type);
+    SS_ADD_END(size);
 }
 
 void
 }
 
 void
@@ -511,7 +688,7 @@ Perl_save_iv(pTHX_ IV *ivp)
 {
     PERL_ARGS_ASSERT_SAVE_IV;
 
 {
     PERL_ARGS_ASSERT_SAVE_IV;
 
-    SSCHECK(3);
+    SSGROW(3);
     SSPUSHIV(*ivp);
     SSPUSHPTR(ivp);
     SSPUSHUV(SAVEt_IV);
     SSPUSHIV(*ivp);
     SSPUSHPTR(ivp);
     SSPUSHUV(SAVEt_IV);
@@ -528,6 +705,15 @@ Perl_save_pptr(pTHX_ char **pptr)
     save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
 }
 
     save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc save_vptr
+
+Implements C<SAVEVPTR>.
+
+=cut
+ */
+
 void
 Perl_save_vptr(pTHX_ void *ptr)
 {
 void
 Perl_save_vptr(pTHX_ void *ptr)
 {
@@ -544,6 +730,15 @@ Perl_save_sptr(pTHX_ SV **sptr)
     save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
 }
 
     save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc save_padsv_and_mortalize
+
+Implements C<SAVEPADSVANDMORTALIZE>.
+
+=cut
+ */
+
 void
 Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
 {
 void
 Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
 {
@@ -573,6 +768,20 @@ Perl_save_aptr(pTHX_ AV **aptr)
     save_pushptrptr(*aptr, aptr, SAVEt_APTR);
 }
 
     save_pushptrptr(*aptr, aptr, SAVEt_APTR);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc save_pushptr
+
+The refcnt of object C<ptr> will be decremented at the end of the current
+I<pseudo-block>.  C<type> gives the type of C<ptr>, expressed as one of the
+constants in F<scope.h> whose name begins with C<SAVEt_>.
+
+This is the underlying implementation of several macros, like
+C<SAVEFREESV>.
+
+=cut
+*/
+
 void
 Perl_save_pushptr(pTHX_ void *const ptr, const int type)
 {
 void
 Perl_save_pushptr(pTHX_ void *const ptr, const int type)
 {
@@ -591,10 +800,11 @@ Perl_save_clearsv(pTHX_ SV **svp)
     PERL_ARGS_ASSERT_SAVE_CLEARSV;
 
     ASSERT_CURPAD_ACTIVE("save_clearsv");
     PERL_ARGS_ASSERT_SAVE_CLEARSV;
 
     ASSERT_CURPAD_ACTIVE("save_clearsv");
+    assert(*svp);
     SvPADSTALE_off(*svp); /* mark lexical as active */
     if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
     SvPADSTALE_off(*svp); /* mark lexical as active */
     if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
-       Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
-                  offset, svp, PL_curpad);
+        Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
+                   offset, svp, PL_curpad);
     }
 
     {
     }
 
     {
@@ -612,6 +822,15 @@ Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
     save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
 }
 
     save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc save_hdelete
+
+Implements C<SAVEHDELETE>.
+
+=cut
+*/
+
 void
 Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
 {
 void
 Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
 {
@@ -627,6 +846,15 @@ Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
     save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
 }
 
     save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc save_adelete
+
+Implements C<SAVEADELETE>.
+
+=cut
+*/
+
 void
 Perl_save_adelete(pTHX_ AV *av, SSize_t key)
 {
 void
 Perl_save_adelete(pTHX_ AV *av, SSize_t key)
 {
@@ -664,23 +892,40 @@ Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
     SS_ADD_END(3);
 }
 
     SS_ADD_END(3);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc save_hints
+
+Implements C<SAVEHINTS>.
+
+=cut
+ */
+
 void
 Perl_save_hints(pTHX)
 {
     COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
     if (PL_hints & HINT_LOCALIZE_HH) {
 void
 Perl_save_hints(pTHX)
 {
     COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
     if (PL_hints & HINT_LOCALIZE_HH) {
-       HV *oldhh = GvHV(PL_hintgv);
-       save_pushptri32ptr(oldhh, PL_hints, save_cophh, SAVEt_HINTS);
-       GvHV(PL_hintgv) = NULL; /* in case copying dies */
-       GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
+        HV *oldhh = GvHV(PL_hintgv);
+        {
+            dSS_ADD;
+            SS_ADD_INT(PL_hints);
+            SS_ADD_PTR(save_cophh);
+            SS_ADD_PTR(oldhh);
+            SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8));
+            SS_ADD_END(4);
+        }
+        GvHV(PL_hintgv) = NULL; /* in case copying dies */
+        GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
+        SAVEFEATUREBITS();
     } else {
     } else {
-       save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS);
+        save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8));
     }
 }
 
 static void
 S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
     }
 }
 
 static void
 S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
-                       const int type)
+                        const int type)
 {
     dSS_ADD;
     SS_ADD_PTR(ptr1);
 {
     dSS_ADD;
     SS_ADD_PTR(ptr1);
@@ -690,9 +935,31 @@ S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
     SS_ADD_END(4);
 }
 
     SS_ADD_END(4);
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc      save_aelem
+=for apidoc_item save_aelem_flags
+
+These each arrange for the value of the array element C<av[idx]> to be restored
+at the end of the enclosing I<pseudo-block>.
+
+In C<save_aelem>, the SV at C**sptr> will be replaced by a new C<undef>
+scalar.  That scalar will inherit any magic from the original C<**sptr>,
+and any 'set' magic will be processed.
+
+In C<save_aelem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
+the function to forgo all that:  the scalar at C<**sptr> is untouched.
+If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
+new C<undef> scalar.  That scalar will inherit any magic from the original
+C<**sptr>.  Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
+is set in in C<flags>.
+
+=cut
+*/
+
 void
 Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
 void
 Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
-                           const U32 flags)
+                            const U32 flags)
 {
     dSS_ADD;
     SV *sv;
 {
     dSS_ADD;
     SV *sv;
@@ -708,19 +975,41 @@ Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
     /* The array needs to hold a reference count on its new element, so it
        must be AvREAL. */
     if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
     /* The array needs to hold a reference count on its new element, so it
        must be AvREAL. */
     if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
-       av_reify(av);
+        av_reify(av);
     save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
     if (flags & SAVEf_KEEPOLDELEM)
     save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
     if (flags & SAVEf_KEEPOLDELEM)
-       return;
+        return;
     sv = *sptr;
     /* If we're localizing a tied array element, this new sv
      * won't actually be stored in the array - so it won't get
      * reaped when the localize ends. Ensure it gets reaped by
      * mortifying it instead. DAPM */
     if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
     sv = *sptr;
     /* If we're localizing a tied array element, this new sv
      * won't actually be stored in the array - so it won't get
      * reaped when the localize ends. Ensure it gets reaped by
      * mortifying it instead. DAPM */
     if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
-       sv_2mortal(sv);
+        sv_2mortal(sv);
 }
 
 }
 
+/*
+=for apidoc_section $callback
+=for apidoc      save_helem
+=for apidoc_item save_helem_flags
+
+These each arrange for the value of the hash element (in Perlish terms)
+C<$hv{key}]> to be restored at the end of the enclosing I<pseudo-block>.
+
+In C<save_helem>, the SV at C**sptr> will be replaced by a new C<undef>
+scalar.  That scalar will inherit any magic from the original C<**sptr>,
+and any 'set' magic will be processed.
+
+In C<save_helem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
+the function to forgo all that:  the scalar at C<**sptr> is untouched.
+If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
+new C<undef> scalar.  That scalar will inherit any magic from the original
+C<**sptr>.  Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
+is set in in C<flags>.
+
+=cut
+*/
+
 void
 Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
 {
 void
 Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
 {
@@ -739,14 +1028,14 @@ Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
     }
     save_scalar_at(sptr, flags);
     if (flags & SAVEf_KEEPOLDELEM)
     }
     save_scalar_at(sptr, flags);
     if (flags & SAVEf_KEEPOLDELEM)
-       return;
+        return;
     sv = *sptr;
     /* If we're localizing a tied hash element, this new sv
      * won't actually be stored in the hash - so it won't get
      * reaped when the localize ends. Ensure it gets reaped by
      * mortifying it instead. DAPM */
     if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
     sv = *sptr;
     /* If we're localizing a tied hash element, this new sv
      * won't actually be stored in the hash - so it won't get
      * reaped when the localize ends. Ensure it gets reaped by
      * mortifying it instead. DAPM */
     if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
-       sv_2mortal(sv);
+        sv_2mortal(sv);
 }
 
 SV*
 }
 
 SV*
@@ -770,19 +1059,28 @@ Perl_savetmps(pTHX)
     SS_ADD_END(2);
 }
 
     SS_ADD_END(2);
 }
 
+/*
+=for apidoc_section $stack
+=for apidoc save_alloc
 
 
-I32
-Perl_save_alloc(pTHX_ I32 size, I32 pad)
+Implements L<perlapi/C<SSNEW>> and kin, which should be used instead of this
+function.
+
+=cut
+*/
+
+SSize_t
+Perl_save_alloc(pTHX_ SSize_t size, I32 pad)
 {
 {
-    const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
+    const SSize_t start = pad + ((char*)&PL_savestack[PL_savestack_ix]
                           - (char*)PL_savestack);
     const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
     const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
 
     if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
                           - (char*)PL_savestack);
     const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
     const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
 
     if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
-       Perl_croak(aTHX_
+        Perl_croak(aTHX_
             "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
             "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
-                  elems, (IV)size, (IV)pad);
+                   elems, (IV)size, (IV)pad);
 
     SSGROW(elems + 1);
 
 
     SSGROW(elems + 1);
 
@@ -792,63 +1090,15 @@ Perl_save_alloc(pTHX_ I32 size, I32 pad)
 }
 
 
 }
 
 
-static const U8 arg_counts[] = {
-    0, /* SAVEt_ALLOC              */
-    0, /* SAVEt_CLEARPADRANGE      */
-    0, /* SAVEt_CLEARSV            */
-    0, /* SAVEt_REGCONTEXT         */
-    1, /* SAVEt_TMPSFLOOR          */
-    1, /* SAVEt_BOOL               */
-    1, /* SAVEt_COMPILE_WARNINGS   */
-    1, /* SAVEt_COMPPAD            */
-    1, /* SAVEt_FREECOPHH          */
-    1, /* SAVEt_FREEOP             */
-    1, /* SAVEt_FREEPV             */
-    1, /* SAVEt_FREESV             */
-    1, /* SAVEt_I16                */
-    1, /* SAVEt_I32_SMALL          */
-    1, /* SAVEt_I8                 */
-    1, /* SAVEt_INT_SMALL          */
-    1, /* SAVEt_MORTALIZESV        */
-    1, /* SAVEt_NSTAB              */
-    1, /* SAVEt_OP                 */
-    1, /* SAVEt_PARSER             */
-    1, /* SAVEt_STACK_POS          */
-    1, /* SAVEt_READONLY_OFF       */
-    1, /* SAVEt_FREEPADNAME        */
-    2, /* SAVEt_AV                 */
-    2, /* SAVEt_DESTRUCTOR         */
-    2, /* SAVEt_DESTRUCTOR_X       */
-    2, /* SAVEt_GENERIC_PVREF      */
-    2, /* SAVEt_GENERIC_SVREF      */
-    2, /* SAVEt_GP                 */
-    2, /* SAVEt_GVSV               */
-    2, /* SAVEt_HINTS              */
-    2, /* SAVEt_HPTR               */
-    2, /* SAVEt_HV                 */
-    2, /* SAVEt_I32                */
-    2, /* SAVEt_INT                */
-    2, /* SAVEt_ITEM               */
-    2, /* SAVEt_IV                 */
-    2, /* SAVEt_LONG               */
-    2, /* SAVEt_PPTR               */
-    2, /* SAVEt_SAVESWITCHSTACK    */
-    2, /* SAVEt_SHARED_PVREF       */
-    2, /* SAVEt_SPTR               */
-    2, /* SAVEt_STRLEN             */
-    2, /* SAVEt_SV                 */
-    2, /* SAVEt_SVREF              */
-    2, /* SAVEt_VPTR               */
-    2, /* SAVEt_ADELETE            */
-    2, /* SAVEt_APTR               */
-    3, /* SAVEt_HELEM              */
-    3, /* SAVEt_PADSV_AND_MORTALIZE*/
-    3, /* SAVEt_SET_SVFLAGS        */
-    3, /* SAVEt_GVSLOT             */
-    3, /* SAVEt_AELEM              */
-    3  /* SAVEt_DELETE             */
-};
 
 
+/*
+=for apidoc_section $callback
+=for apidoc leave_scope
+
+Implements C<LEAVE_SCOPE> which you should use instead.
+
+=cut
+ */
 
 void
 Perl_leave_scope(pTHX_ I32 base)
 
 void
 Perl_leave_scope(pTHX_ I32 base)
@@ -857,16 +1107,16 @@ Perl_leave_scope(pTHX_ I32 base)
     bool was = TAINT_get;
 
     if (UNLIKELY(base < -1))
     bool was = TAINT_get;
 
     if (UNLIKELY(base < -1))
-       Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
+        Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
     DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
     DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
-                       (long)PL_savestack_ix, (long)base));
+                        (long)PL_savestack_ix, (long)base));
     while (PL_savestack_ix > base) {
     while (PL_savestack_ix > base) {
-       UV uv;
-       U8 type;
+        UV uv;
+        U8 type;
         ANY *ap; /* arg pointer */
         ANY a0, a1, a2; /* up to 3 args */
 
         ANY *ap; /* arg pointer */
         ANY a0, a1, a2; /* up to 3 args */
 
-       TAINT_NOT;
+        TAINT_NOT;
 
         {
             U8  argcount;
 
         {
             U8  argcount;
@@ -875,39 +1125,39 @@ Perl_leave_scope(pTHX_ I32 base)
             ap = &PL_savestack[ix];
             uv = ap->any_uv;
             type = (U8)uv & SAVE_MASK;
             ap = &PL_savestack[ix];
             uv = ap->any_uv;
             type = (U8)uv & SAVE_MASK;
-            argcount = arg_counts[type];
+            argcount = leave_scope_arg_counts[type];
             PL_savestack_ix = ix - argcount;
             ap -= argcount;
         }
 
             PL_savestack_ix = ix - argcount;
             ap -= argcount;
         }
 
-       switch (type) {
-       case SAVEt_ITEM:                        /* normal string */
+        switch (type) {
+        case SAVEt_ITEM:                       /* normal string */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           sv_replace(a0.any_sv, a1.any_sv);
+            sv_replace(a0.any_sv, a1.any_sv);
             if (UNLIKELY(SvSMAGICAL(a0.any_sv))) {
                 PL_localizing = 2;
                 mg_set(a0.any_sv);
                 PL_localizing = 0;
             }
             if (UNLIKELY(SvSMAGICAL(a0.any_sv))) {
                 PL_localizing = 2;
                 mg_set(a0.any_sv);
                 PL_localizing = 0;
             }
-           break;
+            break;
 
 
-           /* This would be a mathom, but Perl_save_svref() calls a static
-              function, S_save_scalar_at(), so has to stay in this file.  */
-       case SAVEt_SVREF:                       /* scalar reference */
+            /* This would be a mathom, but Perl_save_svref() calls a static
+               function, S_save_scalar_at(), so has to stay in this file.  */
+        case SAVEt_SVREF:                      /* scalar reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           a2.any_svp = a0.any_svp;
-           a0.any_sv = NULL; /* what to refcnt_dec */
-           goto restore_sv;
+            a2.any_svp = a0.any_svp;
+            a0.any_sv = NULL; /* what to refcnt_dec */
+            goto restore_sv;
 
 
-       case SAVEt_SV:                          /* scalar reference */
+        case SAVEt_SV:                         /* scalar reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           a2.any_svp = &GvSV(a0.any_gv);
-       restore_sv:
+            a2.any_svp = &GvSV(a0.any_gv);
+        restore_sv:
         {
             /* do *a2.any_svp = a1 and free a0 */
         {
             /* do *a2.any_svp = a1 and free a0 */
-           SV * const sv = *a2.any_svp;
-           *a2.any_svp = a1.any_sv;
-           SvREFCNT_dec(sv);
+            SV * const sv = *a2.any_svp;
+            *a2.any_svp = a1.any_sv;
+            SvREFCNT_dec(sv);
             if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
                 /* mg_set could die, skipping the freeing of a0 and
                  * a1; Ensure that they're always freed in that case */
             if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
                 /* mg_set could die, skipping the freeing of a0 and
                  * a1; Ensure that they're always freed in that case */
@@ -922,73 +1172,88 @@ Perl_leave_scope(pTHX_ I32 base)
                 PL_localizing = 0;
                 break;
             }
                 PL_localizing = 0;
                 break;
             }
-           SvREFCNT_dec_NN(a1.any_sv);
-           SvREFCNT_dec(a0.any_sv);
-           break;
+            SvREFCNT_dec_NN(a1.any_sv);
+            SvREFCNT_dec(a0.any_sv);
+            break;
         }
 
         }
 
-       case SAVEt_GENERIC_PVREF:               /* generic pv */
+        case SAVEt_GENERIC_PVREF:              /* generic pv */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           if (*a1.any_pvp != a0.any_pv) {
-               Safefree(*a1.any_pvp);
-               *a1.any_pvp = a0.any_pv;
-           }
-           break;
+            if (*a1.any_pvp != a0.any_pv) {
+                Safefree(*a1.any_pvp);
+                *a1.any_pvp = a0.any_pv;
+            }
+            break;
 
 
-       case SAVEt_SHARED_PVREF:                /* shared pv */
+        case SAVEt_SHARED_PVREF:               /* shared pv */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           if (*a0.any_pvp != a1.any_pv) {
-#ifdef NETWARE
-               PerlMem_free(*a0.any_pvp);
-#else
-               PerlMemShared_free(*a0.any_pvp);
-#endif
-               *a0.any_pvp = a1.any_pv;
-           }
-           break;
+            if (*a0.any_pvp != a1.any_pv) {
+                PerlMemShared_free(*a0.any_pvp);
+                *a0.any_pvp = a1.any_pv;
+            }
+            break;
 
 
-       case SAVEt_GVSV:                        /* scalar slot in GV */
+        case SAVEt_GVSV:                       /* scalar slot in GV */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           a0.any_svp = &GvSV(a0.any_gv);
-           goto restore_svp;
+            a0.any_svp = &GvSV(a0.any_gv);
+            goto restore_svp;
+
 
 
-       case SAVEt_GENERIC_SVREF:               /* generic sv */
+        case SAVEt_GENERIC_SVREF:              /* generic sv */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-       restore_svp:
+        restore_svp:
         {
             /* do *a0.any_svp = a1 */
         {
             /* do *a0.any_svp = a1 */
-           SV * const sv = *a0.any_svp;
-           *a0.any_svp = a1.any_sv;
-           SvREFCNT_dec(sv);
-           SvREFCNT_dec(a1.any_sv);
-           break;
+            SV * const sv = *a0.any_svp;
+            *a0.any_svp = a1.any_sv;
+            SvREFCNT_dec(sv);
+            SvREFCNT_dec(a1.any_sv);
+            break;
         }
 
         }
 
-       case SAVEt_GVSLOT:                      /* any slot in GV */
+        case SAVEt_RCPV:           /* like generic sv, but for struct rcpv */
+        {
+            a0 = ap[0]; a1 = ap[1];
+            char *old = *a0.any_pvp;
+            *a0.any_pvp = a1.any_pv;
+            (void)rcpv_free(old);
+            (void)rcpv_free(a1.any_pv);
+            break;
+        }
+
+        case SAVEt_FREERCPV:           /* like SAVEt_FREEPV but for a RCPV */
+        {
+            a0 = ap[0];
+            char *rcpv = a0.any_pv;
+            (void)rcpv_free(rcpv);
+            break;
+        }
+
+        case SAVEt_GVSLOT:                     /* any slot in GV */
         {
             HV * hv;
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
             hv = GvSTASH(a0.any_gv);
         {
             HV * hv;
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
             hv = GvSTASH(a0.any_gv);
-           if (hv && HvENAME(hv) && (
-                   (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV)
-                || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV)
-              ))
-           {
-               if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv)
-                || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp)
-                || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */
-                   PL_sub_generation++;
-               else mro_method_changed_in(hv);
-           }
+            if (hv && HvHasENAME(hv) && (
+                    (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV)
+                 || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV)
+               ))
+            {
+                if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv)
+                 || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp)
+                 || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */
+                    PL_sub_generation++;
+                else mro_method_changed_in(hv);
+            }
             a0.any_svp = a1.any_svp;
             a1.any_sv  = a2.any_sv;
             a0.any_svp = a1.any_svp;
             a1.any_sv  = a2.any_sv;
-           goto restore_svp;
+            goto restore_svp;
         }
 
         }
 
-       case SAVEt_AV:                          /* array reference */
+        case SAVEt_AV:                         /* array reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           SvREFCNT_dec(GvAV(a0.any_gv));
-           GvAV(a0.any_gv) = a1.any_av;
+            SvREFCNT_dec(GvAV(a0.any_gv));
+            GvAV(a0.any_gv) = a1.any_av;
           avhv_common:
             if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
                 /* mg_set might die, so make sure a0 isn't leaked */
           avhv_common:
             if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
                 /* mg_set might die, so make sure a0 isn't leaked */
@@ -1001,138 +1266,141 @@ Perl_leave_scope(pTHX_ I32 base)
                 PL_localizing = 0;
                 break;
             }
                 PL_localizing = 0;
                 break;
             }
-           SvREFCNT_dec_NN(a0.any_sv);
-           break;
+            SvREFCNT_dec_NN(a0.any_sv);
+            break;
 
 
-       case SAVEt_HV:                          /* hash reference */
+        case SAVEt_HV:                         /* hash reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           SvREFCNT_dec(GvHV(a0.any_gv));
-           GvHV(a0.any_gv) = a1.any_hv;
+            SvREFCNT_dec(GvHV(a0.any_gv));
+            GvHV(a0.any_gv) = a1.any_hv;
             goto avhv_common;
 
             goto avhv_common;
 
-       case SAVEt_INT_SMALL:
+        case SAVEt_INT_SMALL:
             a0 = ap[0];
             a0 = ap[0];
-           *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
-           break;
+            *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
+            break;
 
 
-       case SAVEt_INT:                         /* int reference */
+        case SAVEt_INT:                                /* int reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           *(int*)a1.any_ptr = (int)a0.any_i32;
-           break;
+            *(int*)a1.any_ptr = (int)a0.any_i32;
+            break;
 
 
-       case SAVEt_STRLEN:                      /* STRLEN/size_t ref */
+        case SAVEt_STRLEN_SMALL:
+            a0 = ap[0];
+            *(STRLEN*)a0.any_ptr = (STRLEN)(uv >> SAVE_TIGHT_SHIFT);
+            break;
+
+        case SAVEt_STRLEN:                     /* STRLEN/size_t ref */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
-           break;
+            *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
+            break;
 
 
-       case SAVEt_TMPSFLOOR:                   /* restore PL_tmps_floor */
+        case SAVEt_TMPSFLOOR:                  /* restore PL_tmps_floor */
             a0 = ap[0];
             a0 = ap[0];
-           PL_tmps_floor = (SSize_t)a0.any_iv;
-           break;
+            PL_tmps_floor = (SSize_t)a0.any_iv;
+            break;
 
 
-       case SAVEt_BOOL:                        /* bool reference */
+        case SAVEt_BOOL:                       /* bool reference */
             a0 = ap[0];
             a0 = ap[0];
-           *(bool*)a0.any_ptr = cBOOL(uv >> 8);
+            *(bool*)a0.any_ptr = cBOOL(uv >> 8);
 #ifdef NO_TAINT_SUPPORT
             PERL_UNUSED_VAR(was);
 #else
 #ifdef NO_TAINT_SUPPORT
             PERL_UNUSED_VAR(was);
 #else
-           if (UNLIKELY(a0.any_ptr == &(TAINT_get))) {
-               /* If we don't update <was>, to reflect what was saved on the
-                * stack for PL_tainted, then we will overwrite this attempt to
-                * restore it when we exit this routine.  Note that this won't
-                * work if this value was saved in a wider-than necessary type,
-                * such as I32 */
-               was = *(bool*)a0.any_ptr;
-           }
+            if (UNLIKELY(a0.any_ptr == &(PL_tainted))) {
+                /* If we don't update <was>, to reflect what was saved on the
+                 * stack for PL_tainted, then we will overwrite this attempt to
+                 * restore it when we exit this routine.  Note that this won't
+                 * work if this value was saved in a wider-than necessary type,
+                 * such as I32 */
+                was = *(bool*)a0.any_ptr;
+            }
 #endif
 #endif
-           break;
+            break;
 
 
-       case SAVEt_I32_SMALL:
+        case SAVEt_I32_SMALL:
             a0 = ap[0];
             a0 = ap[0];
-           *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
-           break;
+            *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
+            break;
 
 
-       case SAVEt_I32:                         /* I32 reference */
+        case SAVEt_I32:                                /* I32 reference */
             a0 = ap[0]; a1 = ap[1];
 #ifdef PERL_DEBUG_READONLY_OPS
             if (*(I32*)a1.any_ptr != a0.any_i32)
 #endif
                 *(I32*)a1.any_ptr = a0.any_i32;
             a0 = ap[0]; a1 = ap[1];
 #ifdef PERL_DEBUG_READONLY_OPS
             if (*(I32*)a1.any_ptr != a0.any_i32)
 #endif
                 *(I32*)a1.any_ptr = a0.any_i32;
-           break;
+            break;
 
 
-       case SAVEt_SPTR:                        /* SV* reference */
-       case SAVEt_VPTR:                        /* random* reference */
-       case SAVEt_PPTR:                        /* char* reference */
-       case SAVEt_HPTR:                        /* HV* reference */
-       case SAVEt_APTR:                        /* AV* reference */
+        case SAVEt_SPTR:                       /* SV* reference */
+        case SAVEt_VPTR:                       /* random* reference */
+        case SAVEt_PPTR:                       /* char* reference */
+        case SAVEt_HPTR:                       /* HV* reference */
+        case SAVEt_APTR:                       /* AV* reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           *a1.any_svp= a0.any_sv;
-           break;
+            *a1.any_svp= a0.any_sv;
+            break;
 
 
-       case SAVEt_GP:                          /* scalar reference */
+        case SAVEt_GP:                         /* scalar reference */
         {
             HV *hv;
         {
             HV *hv;
-           bool had_method;
+            bool had_method;
 
             a0 = ap[0]; a1 = ap[1];
             /* possibly taking a method out of circulation */  
 
             a0 = ap[0]; a1 = ap[1];
             /* possibly taking a method out of circulation */  
-           had_method = !!GvCVu(a0.any_gv);
-           gp_free(a0.any_gv);
-           GvGP_set(a0.any_gv, (GP*)a1.any_ptr);
-           if ((hv=GvSTASH(a0.any_gv)) && HvENAME_get(hv)) {
-               if (   GvNAMELEN(a0.any_gv) == 3
-                    && strnEQ(GvNAME(a0.any_gv), "ISA", 3)
-                )
-                   mro_isa_changed_in(hv);
+            had_method = cBOOL(GvCVu(a0.any_gv));
+            gp_free(a0.any_gv);
+            GvGP_set(a0.any_gv, (GP*)a1.any_ptr);
+            if ((hv=GvSTASH(a0.any_gv)) && HvHasENAME(hv)) {
+                if (memEQs(GvNAME(a0.any_gv), GvNAMELEN(a0.any_gv), "ISA"))
+                    mro_isa_changed_in(hv);
                 else if (had_method || GvCVu(a0.any_gv))
                     /* putting a method back into circulation ("local")*/      
                     gv_method_changed(a0.any_gv);
                 else if (had_method || GvCVu(a0.any_gv))
                     /* putting a method back into circulation ("local")*/      
                     gv_method_changed(a0.any_gv);
-           }
-           SvREFCNT_dec_NN(a0.any_gv);
-           break;
+            }
+            SvREFCNT_dec_NN(a0.any_gv);
+            break;
         }
 
         }
 
-       case SAVEt_FREESV:
+        case SAVEt_FREESV:
             a0 = ap[0];
             a0 = ap[0];
-           SvREFCNT_dec(a0.any_sv);
-           break;
+            SvREFCNT_dec(a0.any_sv);
+            break;
 
 
-       case SAVEt_FREEPADNAME:
+        case SAVEt_FREEPADNAME:
             a0 = ap[0];
             a0 = ap[0];
-           PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
-           break;
+            PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
+            break;
 
 
-       case SAVEt_FREECOPHH:
+        case SAVEt_FREECOPHH:
             a0 = ap[0];
             a0 = ap[0];
-           cophh_free((COPHH *)a0.any_ptr);
-           break;
+            cophh_free((COPHH *)a0.any_ptr);
+            break;
 
 
-       case SAVEt_MORTALIZESV:
+        case SAVEt_MORTALIZESV:
             a0 = ap[0];
             a0 = ap[0];
-           sv_2mortal(a0.any_sv);
-           break;
+            sv_2mortal(a0.any_sv);
+            break;
 
 
-       case SAVEt_FREEOP:
+        case SAVEt_FREEOP:
             a0 = ap[0];
             a0 = ap[0];
-           ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
-           op_free(a0.any_op);
-           break;
+            ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
+            op_free(a0.any_op);
+            break;
 
 
-       case SAVEt_FREEPV:
+        case SAVEt_FREEPV:
             a0 = ap[0];
             a0 = ap[0];
-           Safefree(a0.any_ptr);
-           break;
+            Safefree(a0.any_ptr);
+            break;
 
         case SAVEt_CLEARPADRANGE:
         {
             I32 i;
 
         case SAVEt_CLEARPADRANGE:
         {
             I32 i;
-           SV **svp;
+            SV **svp;
             i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
             svp = &PL_curpad[uv >>
                     (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
             goto clearsv;
             i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
             svp = &PL_curpad[uv >>
                     (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
             goto clearsv;
-       case SAVEt_CLEARSV:
-           svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
+        case SAVEt_CLEARSV:
+            svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
             i = 1;
           clearsv:
             for (; i; i--, svp--) {
             i = 1;
           clearsv:
             for (; i; i--, svp--) {
@@ -1164,12 +1432,10 @@ Perl_leave_scope(pTHX_ I32 base)
                         if (SvREADONLY(sv))
                             SvREADONLY_off(sv);
 
                         if (SvREADONLY(sv))
                             SvREADONLY_off(sv);
 
-                        if (SvOOK(sv)) { /* OOK or HvAUX */
-                            if (SvTYPE(sv) == SVt_PVHV)
-                                Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
-                            else
-                                sv_backoff(sv);
-                        }
+                        if (SvTYPE(sv) == SVt_PVHV && HvHasAUX(sv))
+                            Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
+                        else if(SvOOK(sv))
+                            sv_backoff(sv);
 
                         if (SvMAGICAL(sv)) {
                             /* note that backrefs (either in HvAUX or magic)
 
                         if (SvMAGICAL(sv)) {
                             /* note that backrefs (either in HvAUX or magic)
@@ -1231,7 +1497,7 @@ Perl_leave_scope(pTHX_ I32 base)
                         CvLEXICAL_on(*svp);
                         break;
                     }
                         CvLEXICAL_on(*svp);
                         break;
                     }
-                    default:   *svp = newSV(0);                break;
+                    default:   *svp = newSV_type(SVt_NULL);            break;
                     }
                     SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
                     /* preserve pad nature, but also mark as not live
                     }
                     SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
                     /* preserve pad nature, but also mark as not live
@@ -1239,208 +1505,240 @@ Perl_leave_scope(pTHX_ I32 base)
                     SvFLAGS(*svp) |= SVs_PADSTALE;
                 }
             }
                     SvFLAGS(*svp) |= SVs_PADSTALE;
                 }
             }
-           break;
+            break;
         }
 
         }
 
-       case SAVEt_DELETE:
+        case SAVEt_DELETE:
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
-           (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD);
-           SvREFCNT_dec(a2.any_hv);
-           Safefree(a0.any_ptr);
-           break;
-
-       case SAVEt_ADELETE:
+            /* hv_delete could die, so free the key and SvREFCNT_dec the
+             * hv by pushing new save actions
+             */
+            /* ap[0] is the key */
+            ap[1].any_uv = SAVEt_FREEPV; /* was len */
+            /* ap[2] is the hv */
+            ap[3].any_uv = SAVEt_FREESV; /* was SAVEt_DELETE */
+            PL_savestack_ix += 4;
+            (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD);
+            break;
+
+        case SAVEt_ADELETE:
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD);
-           SvREFCNT_dec(a1.any_av);
-           break;
-
-       case SAVEt_DESTRUCTOR_X:
+            /* av_delete could die, so SvREFCNT_dec the av by pushing a
+             * new save action
+             */
+            ap[0].any_av = a1.any_av;
+            ap[1].any_uv = SAVEt_FREESV;
+            PL_savestack_ix += 2;
+            (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD);
+            break;
+
+        case SAVEt_DESTRUCTOR_X:
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           (*a0.any_dxptr)(aTHX_ a1.any_ptr);
-           break;
-
-       case SAVEt_REGCONTEXT:
-           /* regexp must have croaked */
-       case SAVEt_ALLOC:
-           PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
-           break;
-
-       case SAVEt_STACK_POS:           /* Position on Perl stack */
+            (*a0.any_dxptr)(aTHX_ a1.any_ptr);
+            break;
+
+        case SAVEt_REGCONTEXT:
+            /* regexp must have croaked */
+        case SAVEt_ALLOC:
+            PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
+            break;
+
+        case SAVEt_STACK_POS:          /* Position on Perl stack */
+#ifdef PERL_RC_STACK
+            /* DAPM Jan 2023. I don't think this save type is used any
+             * more, but if some XS code uses it, fail it for now, as
+             * it's not clear to me what perl should be doing to stack ref
+             * counts when arbitrarily resetting the stack pointer.
+             */
+            assert(0);
+#endif
             a0 = ap[0];
             a0 = ap[0];
-           PL_stack_sp = PL_stack_base + a0.any_i32;
-           break;
+            PL_stack_sp = PL_stack_base + a0.any_i32;
+            break;
 
 
-       case SAVEt_AELEM:               /* array element */
+        case SAVEt_AELEM:              /* array element */
         {
             SV **svp;
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
         {
             SV **svp;
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
-           svp = av_fetch(a0.any_av, a1.any_iv, 1);
-           if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */
-               SvREFCNT_dec(a2.any_sv);
-           if (LIKELY(svp)) {
-               SV * const sv = *svp;
-               if (LIKELY(sv && sv != &PL_sv_undef)) {
-                   if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied)))
-                       SvREFCNT_inc_void_NN(sv);
+            svp = av_fetch(a0.any_av, a1.any_iv, 1);
+            if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */
+                SvREFCNT_dec(a2.any_sv);
+            if (LIKELY(svp)) {
+                SV * const sv = *svp;
+                if (LIKELY(sv && sv != &PL_sv_undef)) {
+                    if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied)))
+                        SvREFCNT_inc_void_NN(sv);
                     a1.any_sv  = a2.any_sv;
                     a2.any_svp = svp;
                     a1.any_sv  = a2.any_sv;
                     a2.any_svp = svp;
-                   goto restore_sv;
-               }
-           }
-           SvREFCNT_dec(a0.any_av);
-           SvREFCNT_dec(a2.any_sv);
-           break;
+                    goto restore_sv;
+                }
+            }
+            SvREFCNT_dec(a0.any_av);
+            SvREFCNT_dec(a2.any_sv);
+            break;
         }
 
         }
 
-       case SAVEt_HELEM:               /* hash element */
+        case SAVEt_HELEM:              /* hash element */
         {
         {
-           HE *he;
+            HE *he;
 
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
 
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
-           he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0);
-           SvREFCNT_dec(a1.any_sv);
-           if (LIKELY(he)) {
-               const SV * const oval = HeVAL(he);
-               if (LIKELY(oval && oval != &PL_sv_undef)) {
+            he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0);
+            SvREFCNT_dec(a1.any_sv);
+            if (LIKELY(he)) {
+                const SV * const oval = HeVAL(he);
+                if (LIKELY(oval && oval != &PL_sv_undef)) {
                     SV **svp = &HeVAL(he);
                     SV **svp = &HeVAL(he);
-                   if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied)))
-                       SvREFCNT_inc_void(*svp);
+                    if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied)))
+                        SvREFCNT_inc_void(*svp);
                     a1.any_sv  = a2.any_sv;
                     a2.any_svp = svp;
                     a1.any_sv  = a2.any_sv;
                     a2.any_svp = svp;
-                   goto restore_sv;
-               }
-           }
-           SvREFCNT_dec(a0.any_hv);
-           SvREFCNT_dec(a2.any_sv);
-           break;
+                    goto restore_sv;
+                }
+            }
+            SvREFCNT_dec(a0.any_hv);
+            SvREFCNT_dec(a2.any_sv);
+            break;
         }
 
         }
 
-       case SAVEt_OP:
+        case SAVEt_OP:
             a0 = ap[0];
             a0 = ap[0];
-           PL_op = (OP*)a0.any_ptr;
-           break;
+            PL_op = (OP*)a0.any_ptr;
+            break;
 
 
-       case SAVEt_HINTS:
+        case SAVEt_HINTS_HH:
+            a2 = ap[2];
+            /* FALLTHROUGH */
+        case SAVEt_HINTS:
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           if ((PL_hints & HINT_LOCALIZE_HH)) {
-             while (GvHV(PL_hintgv)) {
-               HV *hv = GvHV(PL_hintgv);
-               GvHV(PL_hintgv) = NULL;
-               SvREFCNT_dec(MUTABLE_SV(hv));
-             }
-           }
-           cophh_free(CopHINTHASH_get(&PL_compiling));
-           CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr);
-           *(I32*)&PL_hints = a0.any_i32;
-           if (PL_hints & HINT_LOCALIZE_HH) {
-               SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
-               GvHV(PL_hintgv) = MUTABLE_HV(SSPOPPTR);
-           }
-           if (!GvHV(PL_hintgv)) {
-               /* Need to add a new one manually, else rv2hv can
-                  add one via GvHVn and it won't have the magic set.  */
-               HV *const hv = newHV();
-               hv_magic(hv, NULL, PERL_MAGIC_hints);
-               GvHV(PL_hintgv) = hv;
-           }
-           assert(GvHV(PL_hintgv));
-           break;
-
-       case SAVEt_COMPPAD:
+            if ((PL_hints & HINT_LOCALIZE_HH)) {
+              while (GvHV(PL_hintgv)) {
+                HV *hv = GvHV(PL_hintgv);
+                GvHV(PL_hintgv) = NULL;
+                SvREFCNT_dec(MUTABLE_SV(hv));
+              }
+            }
+            cophh_free(CopHINTHASH_get(&PL_compiling));
+            CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr);
+            *(I32*)&PL_hints = a0.any_i32;
+            PL_prevailing_version = (U16)(uv >> 8);
+            if (type == SAVEt_HINTS_HH) {
+                SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
+                GvHV(PL_hintgv) = MUTABLE_HV(a2.any_ptr);
+            }
+            if (!GvHV(PL_hintgv)) {
+                /* Need to add a new one manually, else rv2hv can
+                   add one via GvHVn and it won't have the magic set.  */
+                HV *const hv = newHV();
+                hv_magic(hv, NULL, PERL_MAGIC_hints);
+                GvHV(PL_hintgv) = hv;
+            }
+            assert(GvHV(PL_hintgv));
+            break;
+
+        case SAVEt_COMPPAD:
             a0 = ap[0];
             a0 = ap[0];
-           PL_comppad = (PAD*)a0.any_ptr;
-           if (LIKELY(PL_comppad))
-               PL_curpad = AvARRAY(PL_comppad);
-           else
-               PL_curpad = NULL;
-           break;
+            PL_comppad = (PAD*)a0.any_ptr;
+            if (LIKELY(PL_comppad))
+                PL_curpad = AvARRAY(PL_comppad);
+            else
+                PL_curpad = NULL;
+            break;
 
 
-       case SAVEt_PADSV_AND_MORTALIZE:
-           {
-               SV **svp;
+        case SAVEt_PADSV_AND_MORTALIZE:
+            {
+                SV **svp;
 
                 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
 
                 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
-               assert (a1.any_ptr);
-               svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv;
+                assert (a1.any_ptr);
+                svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv;
                 /* This mortalizing used to be done by CX_POOPLOOP() via
                    itersave.  But as we have all the information here, we
                    can do it here, save even having to have itersave in
                    the struct.
                    */
                 /* This mortalizing used to be done by CX_POOPLOOP() via
                    itersave.  But as we have all the information here, we
                    can do it here, save even having to have itersave in
                    the struct.
                    */
-               sv_2mortal(*svp);
-               *svp = a0.any_sv;
-           }
-           break;
+                sv_2mortal(*svp);
+                *svp = a0.any_sv;
+            }
+            break;
 
 
-       case SAVEt_SAVESWITCHSTACK:
-           {
-               dSP;
+        case SAVEt_SAVESWITCHSTACK:
+            {
+                dSP;
 
                 a0 = ap[0]; a1 = ap[1];
 
                 a0 = ap[0]; a1 = ap[1];
-               SWITCHSTACK(a1.any_av, a0.any_av);
-               PL_curstackinfo->si_stack = a0.any_av;
-           }
-           break;
+                SWITCHSTACK(a1.any_av, a0.any_av);
+                PL_curstackinfo->si_stack = a0.any_av;
+            }
+            break;
 
 
-       case SAVEt_SET_SVFLAGS:
+        case SAVEt_SET_SVFLAGS:
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
             SvFLAGS(a0.any_sv) &= ~(a1.any_u32);
             SvFLAGS(a0.any_sv) |= a2.any_u32;
             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
             SvFLAGS(a0.any_sv) &= ~(a1.any_u32);
             SvFLAGS(a0.any_sv) |= a2.any_u32;
-           break;
+            break;
 
 
-           /* These are only saved in mathoms.c */
-       case SAVEt_NSTAB:
+            /* These are only saved in mathoms.c */
+        case SAVEt_NSTAB:
             a0 = ap[0];
             a0 = ap[0];
-           (void)sv_clear(a0.any_sv);
-           break;
+            (void)sv_clear(a0.any_sv);
+            break;
 
 
-       case SAVEt_LONG:                        /* long reference */
+        case SAVEt_LONG:                       /* long reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           *(long*)a1.any_ptr = a0.any_long;
-           break;
+            *(long*)a1.any_ptr = a0.any_long;
+            break;
 
 
-       case SAVEt_IV:                          /* IV reference */
+        case SAVEt_IV:                         /* IV reference */
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           *(IV*)a1.any_ptr = a0.any_iv;
-           break;
+            *(IV*)a1.any_ptr = a0.any_iv;
+            break;
 
 
-       case SAVEt_I16:                         /* I16 reference */
+        case SAVEt_I16:                                /* I16 reference */
             a0 = ap[0];
             a0 = ap[0];
-           *(I16*)a0.any_ptr = (I16)(uv >> 8);
-           break;
+            *(I16*)a0.any_ptr = (I16)(uv >> 8);
+            break;
 
 
-       case SAVEt_I8:                          /* I8 reference */
+        case SAVEt_I8:                         /* I8 reference */
             a0 = ap[0];
             a0 = ap[0];
-           *(I8*)a0.any_ptr = (I8)(uv >> 8);
-           break;
+            *(I8*)a0.any_ptr = (I8)(uv >> 8);
+            break;
 
 
-       case SAVEt_DESTRUCTOR:
+        case SAVEt_DESTRUCTOR:
             a0 = ap[0]; a1 = ap[1];
             a0 = ap[0]; a1 = ap[1];
-           (*a0.any_dptr)(a1.any_ptr);
-           break;
+            (*a0.any_dptr)(a1.any_ptr);
+            break;
+
+        case SAVEt_COMPILE_WARNINGS:
+            /* NOTE: we can't put &PL_compiling or PL_curcop on the save
+             *       stack directly, as we currently cannot translate
+             *       them to the correct addresses after a thread start
+             *       or win32 fork start. - Yves
+             */
+            a0 = ap[0];
+            free_and_set_cop_warnings(&PL_compiling, a0.any_pv);
+            break;
 
 
-       case SAVEt_COMPILE_WARNINGS:
+        case SAVEt_CURCOP_WARNINGS:
+            /* NOTE: see comment above about SAVEt_COMPILE_WARNINGS */
             a0 = ap[0];
             a0 = ap[0];
-           if (!specialWARN(PL_compiling.cop_warnings))
-               PerlMemShared_free(PL_compiling.cop_warnings);
-           PL_compiling.cop_warnings = (STRLEN*)a0.any_ptr;
-           break;
+            free_and_set_cop_warnings(PL_curcop, a0.any_pv);
+            break;
 
 
-       case SAVEt_PARSER:
+        case SAVEt_PARSER:
             a0 = ap[0];
             a0 = ap[0];
-           parser_free((yy_parser *)a0.any_ptr);
-           break;
+            parser_free((yy_parser *)a0.any_ptr);
+            break;
 
 
-       case SAVEt_READONLY_OFF:
+        case SAVEt_READONLY_OFF:
             a0 = ap[0];
             a0 = ap[0];
-           SvREADONLY_off(a0.any_sv);
-           break;
+            SvREADONLY_off(a0.any_sv);
+            break;
 
 
-       default:
-           Perl_croak(aTHX_ "panic: leave_scope inconsistency %u",
+        default:
+            Perl_croak(aTHX_ "panic: leave_scope inconsistency %u",
                     (U8)uv & SAVE_MASK);
                     (U8)uv & SAVE_MASK);
-       }
+        }
     }
 
     TAINT_set(was);
     }
 
     TAINT_set(was);
@@ -1454,118 +1752,120 @@ Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
 #ifdef DEBUGGING
     PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
     if (CxTYPE(cx) != CXt_SUBST) {
 #ifdef DEBUGGING
     PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
     if (CxTYPE(cx) != CXt_SUBST) {
-       const char *gimme_text;
-       PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
-       PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n",
-                     PTR2UV(cx->blk_oldcop));
-       PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
-       PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
-       PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
-       PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n",
-                     PTR2UV(cx->blk_oldpm));
-       switch (cx->blk_gimme) {
-           case G_VOID:
-               gimme_text = "VOID";
-               break;
-           case G_SCALAR:
-               gimme_text = "SCALAR";
-               break;
-           case G_ARRAY:
-               gimme_text = "LIST";
-               break;
-           default:
-               gimme_text = "UNKNOWN";
-               break;
-       }
-       PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
+        const char *gimme_text;
+        PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
+        PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n",
+                      PTR2UV(cx->blk_oldcop));
+        PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
+        PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
+        PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
+        PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n",
+                      PTR2UV(cx->blk_oldpm));
+        switch (cx->blk_gimme) {
+            case G_VOID:
+                gimme_text = "VOID";
+                break;
+            case G_SCALAR:
+                gimme_text = "SCALAR";
+                break;
+            case G_LIST:
+                gimme_text = "LIST";
+                break;
+            default:
+                gimme_text = "UNKNOWN";
+                break;
+        }
+        PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
     }
     switch (CxTYPE(cx)) {
     case CXt_NULL:
     case CXt_BLOCK:
     }
     switch (CxTYPE(cx)) {
     case CXt_NULL:
     case CXt_BLOCK:
-       break;
+    case CXt_DEFER:
+        break;
     case CXt_FORMAT:
     case CXt_FORMAT:
-       PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_format.cv));
-       PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_format.gv));
-       PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_format.dfoutgv));
-       PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
-                     (int)CxHASARGS(cx));
-       PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_format.retop));
-       break;
+        PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_format.cv));
+        PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_format.gv));
+        PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_format.dfoutgv));
+        PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
+                      (int)CxHASARGS(cx));
+        PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_format.retop));
+        break;
     case CXt_SUB:
     case CXt_SUB:
-       PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_sub.cv));
-       PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
-               (long)cx->blk_sub.olddepth);
-       PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
-               (int)CxHASARGS(cx));
-       PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
-       PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_sub.retop));
-       break;
+        PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_sub.cv));
+        PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
+                (long)cx->blk_sub.olddepth);
+        PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
+                (int)CxHASARGS(cx));
+        PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
+        PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_sub.retop));
+        break;
     case CXt_EVAL:
     case CXt_EVAL:
-       PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
-               (long)CxOLD_IN_EVAL(cx));
-       PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
-               PL_op_name[CxOLD_OP_TYPE(cx)],
-               PL_op_desc[CxOLD_OP_TYPE(cx)]);
-       if (cx->blk_eval.old_namesv)
-           PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
-                         SvPVX_const(cx->blk_eval.old_namesv));
-       PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_eval.old_eval_root));
-       PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_eval.retop));
-       break;
+        PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
+                (long)CxOLD_IN_EVAL(cx));
+        PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
+                PL_op_name[CxOLD_OP_TYPE(cx)],
+                PL_op_desc[CxOLD_OP_TYPE(cx)]);
+        if (cx->blk_eval.old_namesv)
+            PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
+                          SvPVX_const(cx->blk_eval.old_namesv));
+        PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_eval.old_eval_root));
+        PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_eval.retop));
+        break;
 
     case CXt_LOOP_PLAIN:
     case CXt_LOOP_LAZYIV:
     case CXt_LOOP_LAZYSV:
     case CXt_LOOP_LIST:
     case CXt_LOOP_ARY:
 
     case CXt_LOOP_PLAIN:
     case CXt_LOOP_LAZYIV:
     case CXt_LOOP_LAZYSV:
     case CXt_LOOP_LIST:
     case CXt_LOOP_ARY:
-       PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
-       PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n",
-               PTR2UV(cx->blk_loop.my_op));
+        PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
+        PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n",
+                PTR2UV(cx->blk_loop.my_op));
         if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n",
                     PTR2UV(CxITERVAR(cx)));
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n",
                     PTR2UV(cx->blk_loop.itersave));
         if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n",
                     PTR2UV(CxITERVAR(cx)));
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n",
                     PTR2UV(cx->blk_loop.itersave));
-            /* XXX: not accurate for LAZYSV/IV/LIST */
+        }
+        if (CxTYPE(cx) == CXt_LOOP_ARY) {
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n",
                     PTR2UV(cx->blk_loop.state_u.ary.ary));
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
                     (long)cx->blk_loop.state_u.ary.ix);
         }
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n",
                     PTR2UV(cx->blk_loop.state_u.ary.ary));
             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
                     (long)cx->blk_loop.state_u.ary.ix);
         }
-       break;
+        break;
 
     case CXt_SUBST:
 
     case CXt_SUBST:
-       PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
-               (long)cx->sb_iters);
-       PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
-               (long)cx->sb_maxiters);
-       PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
-               (long)cx->sb_rflags);
-       PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
-               (long)CxONCE(cx));
-       PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
-               cx->sb_orig);
-       PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n",
-               PTR2UV(cx->sb_dstr));
-       PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n",
-               PTR2UV(cx->sb_targ));
-       PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n",
-               PTR2UV(cx->sb_s));
-       PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
-               PTR2UV(cx->sb_m));
-       PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n",
-               PTR2UV(cx->sb_strend));
-       PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n",
-               PTR2UV(cx->sb_rxres));
-       break;
+        PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
+                (long)cx->sb_iters);
+        PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
+                (long)cx->sb_maxiters);
+        PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
+                (long)cx->sb_rflags);
+        PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
+                (long)CxONCE(cx));
+        PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
+                cx->sb_orig);
+        PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n",
+                PTR2UV(cx->sb_dstr));
+        PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n",
+                PTR2UV(cx->sb_targ));
+        PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n",
+                PTR2UV(cx->sb_s));
+        PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
+                PTR2UV(cx->sb_m));
+        PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n",
+                PTR2UV(cx->sb_strend));
+        PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n",
+                PTR2UV(cx->sb_rxres));
+        break;
     }
 #else
     PERL_UNUSED_CONTEXT;
     }
 #else
     PERL_UNUSED_CONTEXT;
@@ -1574,5 +1874,144 @@ Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
 }
 
 /*
 }
 
 /*
+=for apidoc_section $callback
+=for apidoc mortal_destructor_sv
+
+This function arranges for either a Perl code reference, or a C function
+reference to be called at the B<end of the current statement>.
+
+The C<coderef> argument determines the type of function that will be
+called. If it is C<SvROK()> it is assumed to be a reference to a CV and
+will arrange for the coderef to be called. If it is not SvROK() then it
+is assumed to be a C<SvIV()> which is C<SvIOK()> whose value is a pointer
+to a C function of type C<DESTRUCTORFUNC_t> created using C<PTR2INT()>.
+Either way the C<args> parameter will be provided to the callback as a
+parameter, although the rules for doing so differ between the Perl and
+C mode. Normally this function is only used directly for the Perl case
+and the wrapper C<mortal_destructor_x()> is used for the C function case.
+
+When operating in Perl callback mode the C<args> parameter may be NULL
+in which case the code reference is called with no arguments, otherwise
+if it is an AV (SvTYPE(args) == SVt_PVAV) then the contents of the AV
+will be used as the arguments to the code reference, and if it is any
+other type then the C<args> SV will be provided as a single argument to
+the code reference.
+
+When operating in a C callback mode the C<args> parameter will be passed
+directly to the C function as a C<void *> pointer. No additional
+processing of the argument will be peformed, and it is the callers
+responsibility to free the C<args> parameter if necessary.
+
+Be aware that there is a signficant difference in timing between the
+I<end of the current statement> and the I<end of the current pseudo
+block>. If you are looking for a mechanism to trigger a function at the
+end of the B<current pseudo block> you should look at
+C<SAVEDESTRUCTORX()> instead of this function.
+
+=for apidoc mortal_svfunc_x
+
+This function arranges for a C function reference to be called at the
+B<end of the current statement> with the arguments provided. It is a
+wrapper around C<mortal_destructor_sv()> which ensures that the latter
+function is called appropriately.
+
+Be aware that there is a signficant difference in timing between the
+I<end of the current statement> and the I<end of the current pseudo
+block>. If you are looking for a mechanism to trigger a function at the
+end of the B<current pseudo block> you should look at
+C<SAVEDESTRUCTORX()> instead of this function.
+
+=for apidoc magic_freedestruct
+
+This function is called via magic to implement the
+C<mortal_destructor_sv()> and C<mortal_destructor_x()> functions. It
+should not be called directly and has no user servicable parts.
+
+=cut
+*/
+
+void
+Perl_mortal_destructor_sv(pTHX_ SV *coderef, SV *args) {
+    PERL_ARGS_ASSERT_MORTAL_DESTRUCTOR_SV;
+    assert(
+        (SvROK(coderef) && SvTYPE(SvRV(coderef)) == SVt_PVCV) /* perl coderef */
+         ||
+        (SvIOK(coderef) && !SvROK(coderef))                  /* C function ref */
+    );
+    SV *variable = newSV_type_mortal(SVt_IV);
+    (void)sv_magicext(variable, coderef, PERL_MAGIC_destruct,
+                      &PL_vtbl_destruct, (char *)args, args ? HEf_SVKEY : 0);
+}
+
+
+void
+Perl_mortal_svfunc_x(pTHX_ SVFUNC_t f, SV *sv) {
+    PERL_ARGS_ASSERT_MORTAL_SVFUNC_X;
+    SV *sviv = newSViv(PTR2IV(f));
+    mortal_destructor_sv(sviv,sv);
+}
+
+
+int
+Perl_magic_freedestruct(pTHX_ SV* sv, MAGIC* mg) {
+    PERL_ARGS_ASSERT_MAGIC_FREEDESTRUCT;
+    dSP;
+    union {
+        SV   *sv;
+        AV   *av;
+        char *pv;
+    } args_any;
+    SV *coderef;
+
+    IV nargs = 0;
+    if (PL_phase == PERL_PHASE_DESTRUCT) {
+        Perl_warn(aTHX_ "Can't call destructor for 0x%p in global destruction\n", sv);
+        return 1;
+    }
+
+    args_any.pv = mg->mg_ptr;
+    coderef = mg->mg_obj;
+
+    /* Deal with C function destructor */
+    if (SvTYPE(coderef) == SVt_IV && !SvROK(coderef)) {
+        SVFUNC_t f = INT2PTR(SVFUNC_t, SvIV(coderef));
+        (f)(aTHX_ args_any.sv);
+        return 0;
+    }
+
+    if (args_any.sv) {
+        if (SvTYPE(args_any.sv) == SVt_PVAV) {
+            nargs = av_len(args_any.av) + 1;
+        } else {
+            nargs = 1;
+        }
+    }
+    PUSHSTACKi(PERLSI_MAGIC);
+    ENTER_with_name("call_freedestruct");
+    SAVETMPS;
+    EXTEND(SP, nargs);
+    PUSHMARK(SP);
+    if (args_any.sv) {
+        if (SvTYPE(args_any.sv) == SVt_PVAV) {
+            IV n;
+            for (n = 0 ; n < nargs ; n++ ) {
+                SV **argp = av_fetch(args_any.av, n, 0);
+                if (argp && *argp)
+                    PUSHs(*argp);
+            }
+        } else {
+            PUSHs(args_any.sv);
+        }
+    }
+    PUTBACK;
+    (void)call_sv(coderef, G_VOID | G_EVAL | G_KEEPERR);
+    FREETMPS;
+    LEAVE_with_name("call_freedestruct");
+    POPSTACK;
+    return 0;
+}
+
+
+/*
  * ex: set ts=8 sts=4 sw=4 et:
  */
  * ex: set ts=8 sts=4 sw=4 et:
  */