This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
only call leave_common in non-void context
[perl5.git] / pp_ctl.c
index 7b1a068..85ae4d3 100644 (file)
--- a/pp_ctl.c
+++ b/pp_ctl.c
@@ -1519,7 +1519,8 @@ Perl_dounwind(pTHX_ I32 cxix)
            break;
        case CXt_EVAL:
            POPEVAL(cx);
-            LEAVE_SCOPE(PL_scopestack[cx->blk_oldscopesp-1]);
+            LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+            PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
            break;
        case CXt_LOOP_LAZYIV:
        case CXt_LOOP_LAZYSV:
@@ -1650,7 +1651,8 @@ Perl_die_unwind(pTHX_ SV *msv)
                *++newsp = &PL_sv_undef;
            PL_stack_sp = newsp;
 
-           LEAVE;
+            LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+            PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
 
            if (optype == OP_REQUIRE) {
                 assert (PL_curcop == oldcop);
@@ -1942,7 +1944,6 @@ PP(pp_dbstate)
        dSP;
        PERL_CONTEXT *cx;
        const I32 gimme = G_ARRAY;
-       U8 hasargs;
        GV * const gv = PL_DBgv;
        CV * cv = NULL;
 
@@ -1956,16 +1957,12 @@ PP(pp_dbstate)
            /* don't do recursive DB::DB call */
            return NORMAL;
 
-       ENTER;
-       SAVETMPS;
-
-       SAVEI32(PL_debug);
-       SAVESTACK_POS();
-       PL_debug = 0;
-       hasargs = 0;
-       SPAGAIN;
-
        if (CvISXSUB(cv)) {
+            ENTER;
+            SAVEI32(PL_debug);
+            PL_debug = 0;
+            SAVESTACK_POS();
+            SAVETMPS;
            PUSHMARK(SP);
            (void)(*CvXSUB(cv))(aTHX_ cv);
            FREETMPS;
@@ -1973,9 +1970,15 @@ PP(pp_dbstate)
            return NORMAL;
        }
        else {
+            U8 hasargs = 0;
            PUSHBLOCK(cx, CXt_SUB, SP);
            PUSHSUB_DB(cx);
            cx->blk_sub.retop = PL_op->op_next;
+            cx->blk_sub.old_savestack_ix = PL_savestack_ix;
+
+            SAVEI32(PL_debug);
+            PL_debug = 0;
+            SAVESTACK_POS();
            CvDEPTH(cv)++;
            if (CvDEPTH(cv) >= 2) {
                PERL_STACK_OVERFLOW_CHECK();
@@ -2076,8 +2079,10 @@ PP(pp_leave)
 
     gimme = OP_GIMME(PL_op, (cxstack_ix >= 0) ? gimme : G_SCALAR);
 
-    SP = leave_common(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP,
-                              PL_op->op_private & OPpLVALUE);
+    SP = (gimme == G_VOID)
+        ? newsp
+        : leave_common(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP,
+                               PL_op->op_private & OPpLVALUE);
     PL_curpm = newpm;  /* Don't pop $1 et al till now */
 
     LEAVE_with_name("block");
@@ -2248,7 +2253,9 @@ PP(pp_leaveloop)
     mark = newsp;
     newsp = PL_stack_base + cx->blk_loop.resetsp;
 
-    SP = leave_common(newsp, SP, MARK, gimme, 0,
+    SP = (gimme == G_VOID)
+        ? newsp
+        : leave_common(newsp, SP, MARK, gimme, 0,
                               PL_op->op_private & OPpLVALUE);
     PUTBACK;
 
@@ -2314,7 +2321,6 @@ PP(pp_leavesublv)
                what = "undef";
            }
           croak:
-           LEAVE;
            POPSUB(cx,sv);
            cxstack_ix--;
            PL_curpm = newpm;
@@ -2385,7 +2391,6 @@ PP(pp_leavesublv)
     }
     PUTBACK;
 
-    LEAVE;
     POPSUB(cx,sv);     /* Stack values are safe: release CV and @_ ... */
     cxstack_ix--;
     PL_curpm = newpm;  /* ... and pop $1 et al */
@@ -2675,7 +2680,6 @@ PP(pp_goto)
            PERL_CONTEXT *cx;
            CV *cv = MUTABLE_CV(SvRV(sv));
            AV *arg = GvAV(PL_defgv);
-           I32 oldsave;
 
            while (!CvROOT(cv) && !CvXSUB(cv)) {
                const GV * const gv = CvGV(cv);
@@ -2726,6 +2730,13 @@ PP(pp_goto)
 
             /* partial unrolled POPSUB(): */
 
+            /* protect @_ during save stack unwind. */
+            if (arg)
+                SvREFCNT_inc_NN(sv_2mortal(MUTABLE_SV(arg)));
+
+           assert(PL_scopestack_ix == cx->blk_oldscopesp);
+            LEAVE_SCOPE(cx->blk_sub.old_savestack_ix);
+
            if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
                AV* av = MUTABLE_AV(PAD_SVl(0));
                 assert(AvARRAY(MUTABLE_AV(
@@ -2738,23 +2749,11 @@ PP(pp_goto)
                  * unless pad[0] and @_ differ (e.g. if the old sub did
                  * local *_ = []); in which case clear the old pad[0]
                  * array in the usual way */
-               if (av == arg || AvREAL(av)) {
-                   SvREFCNT_dec(av);
-                   av = newAV();
-                   AvREIFY_only(av);
-                   PAD_SVl(0) = (SV*)av;
-               }
+               if (av == arg || AvREAL(av))
+                    clear_defarray(av, av == arg);
                else CLEAR_ARGARRAY(av);
            }
 
-            /* protect @_ during save stack unwind. */
-            if (arg)
-                SvREFCNT_inc_NN(sv_2mortal(MUTABLE_SV(arg)));
-
-           assert(PL_scopestack_ix == cx->blk_oldscopesp);
-           oldsave = PL_scopestack[cx->blk_oldscopesp - 1];
-           LEAVE_SCOPE(oldsave);
-
             /* don't restore PL_comppad here. It won't be needed if the
              * sub we're going to is non-XS, but restoring it early then
              * croaking (e.g. the "Goto undefined subroutine" below)
@@ -2780,8 +2779,6 @@ PP(pp_goto)
             }
 
            /* Now do some callish stuff. */
-           SAVETMPS;
-           SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
            if (CvISXSUB(cv)) {
                SV **newsp;
                I32 gimme;
@@ -2792,6 +2789,10 @@ PP(pp_goto)
                 PERL_UNUSED_VAR(newsp);
                 PERL_UNUSED_VAR(gimme);
 
+                ENTER;
+                SAVETMPS;
+                SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
+
                /* put GvAV(defgv) back onto stack */
                if (items) {
                    EXTEND(SP, items+1); /* @_ could have been extended. */
@@ -2822,8 +2823,13 @@ PP(pp_goto)
                retop = cx->blk_sub.retop;
                 PL_comppad = cx->blk_sub.prevcomppad;
                 PL_curpad = LIKELY(PL_comppad) ? AvARRAY(PL_comppad) : NULL;
-               /* XS subs don't have a CxSUB, so pop it */
-               POPBLOCK(cx, PL_curpm);
+
+               /* XS subs don't have a CXt_SUB, so pop it;
+                 * this is a POPBLOCK(), less all the stuff we already did
+                 * for TOPBLOCK() earlier */
+                PL_curcop = cx->blk_oldcop;
+               cxstack_ix--;
+
                /* Push a mark for the start of arglist */
                PUSHMARK(mark);
                PUTBACK;
@@ -2834,6 +2840,8 @@ PP(pp_goto)
            else {
                PADLIST * const padlist = CvPADLIST(cv);
 
+                SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
+
                 /* partial unrolled PUSHSUB(): */
 
                cx->blk_sub.cv = cv;
@@ -3414,8 +3422,9 @@ S_doeval(pTHX_ int gimme, CV* outside, U32 seq, HV *hh)
            POPBLOCK(cx,PL_curpm);
            POPEVAL(cx);
            namesv = cx->blk_eval.old_namesv;
-           /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
-           LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE.  */
+           /* POPBLOCK has rendered LEAVE_with_name("evalcomp") unnecessary */
+            LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+            PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
        }
 
        errsv = ERRSV;
@@ -3629,6 +3638,7 @@ PP(pp_require)
     OP *op;
     int saved_errno;
     bool path_searchable;
+    I32 old_savestack_ix;
 
     sv = POPs;
     SvGETMAGIC(sv);
@@ -4043,8 +4053,7 @@ PP(pp_require)
                           unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
     }
 
-    ENTER_with_name("eval");
-    SAVETMPS;
+    old_savestack_ix = PL_savestack_ix;
     SAVECOPFILE_FREE(&PL_compiling);
     CopFILE_set(&PL_compiling, tryname);
     lex_start(NULL, tryrsfp, 0);
@@ -4066,6 +4075,7 @@ PP(pp_require)
     /* switch to eval mode */
     PUSHBLOCK(cx, CXt_EVAL, SP);
     PUSHEVAL(cx, name);
+    cx->blk_eval.old_savestack_ix = old_savestack_ix;
     cx->blk_eval.retop = PL_op->op_next;
 
     SAVECOPLINE(&PL_compiling);
@@ -4110,6 +4120,7 @@ PP(pp_entereval)
     U32 seq, lex_flags = 0;
     HV *saved_hh = NULL;
     const bool bytes = PL_op->op_private & OPpEVAL_BYTES;
+    I32 old_savestack_ix;
 
     if (PL_op->op_private & OPpEVAL_HAS_HH) {
        saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
@@ -4147,13 +4158,13 @@ PP(pp_entereval)
     TAINT_IF(SvTAINTED(sv));
     TAINT_PROPER("eval");
 
-    ENTER_with_name("eval");
+    old_savestack_ix = PL_savestack_ix;
+
     lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE
                           ? LEX_IGNORE_UTF8_HINTS
                           : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER
                        )
             );
-    SAVETMPS;
 
     /* switch to eval mode */
 
@@ -4180,6 +4191,7 @@ PP(pp_entereval)
 
     PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
     PUSHEVAL(cx, 0);
+    cx->blk_eval.old_savestack_ix = old_savestack_ix;
     cx->blk_eval.retop = PL_op->op_next;
 
     /* prepare to compile string */
@@ -4244,8 +4256,8 @@ PP(pp_leaveeval)
     retop = cx->blk_eval.retop;
     evalcv = cx->blk_eval.cv;
 
-    SP = leave_common((gimme == G_VOID) ? SP : newsp, SP, newsp,
-                               gimme, SVs_TEMP, FALSE);
+    if (gimme != G_VOID)
+        SP = leave_common(newsp, SP, newsp, gimme, SVs_TEMP, FALSE);
     PL_curpm = newpm;  /* Don't pop $1 et al till now */
 
 #ifdef DEBUGGING
@@ -4261,13 +4273,15 @@ PP(pp_leaveeval)
                        SvPVX_const(namesv),
                         SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
                        G_DISCARD);
-       LEAVE_with_name("eval");
+        LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+        PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
        Perl_die(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
         NOT_REACHED; /* NOTREACHED */
        /* die_unwind() did LEAVE, or we won't be here */
     }
     else {
-       LEAVE_with_name("eval");
+        LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+        PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
         if (!keep)
            CLEAR_ERRSV();
     }
@@ -4289,7 +4303,8 @@ Perl_delete_eval_scope(pTHX)
     POPBLOCK(cx,newpm);
     POPEVAL(cx);
     PL_curpm = newpm;
-    LEAVE_with_name("eval_scope");
+    LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+    PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
     PERL_UNUSED_VAR(newsp);
     PERL_UNUSED_VAR(gimme);
     PERL_UNUSED_VAR(optype);
@@ -4303,11 +4318,9 @@ Perl_create_eval_scope(pTHX_ U32 flags)
     PERL_CONTEXT *cx;
     const I32 gimme = GIMME_V;
        
-    ENTER_with_name("eval_scope");
-    SAVETMPS;
-
     PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
     PUSHEVAL(cx, 0);
+    cx->blk_eval.old_savestack_ix = PL_savestack_ix;
 
     PL_in_eval = EVAL_INEVAL;
     if (flags & G_KEEPERR)
@@ -4343,11 +4356,15 @@ PP(pp_leavetry)
     POPEVAL(cx);
     PERL_UNUSED_VAR(optype);
 
-    SP = leave_common(newsp, SP, newsp, gimme,
+    SP = (gimme == G_VOID)
+        ? newsp
+        : leave_common(newsp, SP, newsp, gimme,
                               SVs_PADTMP|SVs_TEMP, FALSE);
     PL_curpm = newpm;  /* Don't pop $1 et al till now */
 
-    LEAVE_with_name("eval_scope");
+    LEAVE_SCOPE(cx->blk_eval.old_savestack_ix);
+    PL_tmps_floor = cx->blk_eval.old_tmpsfloor;
+
     CLEAR_ERRSV();
     RETURNOP(retop);
 }
@@ -4383,7 +4400,9 @@ PP(pp_leavegiven)
     POPBLOCK(cx,newpm);
     assert(CxTYPE(cx) == CXt_GIVEN);
 
-    SP = leave_common(newsp, SP, newsp, gimme,
+    SP = (gimme == G_VOID)
+        ? newsp
+        : leave_common(newsp, SP, newsp, gimme,
                               SVs_PADTMP|SVs_TEMP, FALSE);
     PL_curpm = newpm;  /* Don't pop $1 et al till now */
 
@@ -4967,7 +4986,9 @@ PP(pp_leavewhen)
     POPBLOCK(cx,newpm);
     assert(CxTYPE(cx) == CXt_WHEN);
 
-    SP = leave_common(newsp, SP, newsp, gimme,
+    SP = (gimme == G_VOID)
+        ? newsp
+        : leave_common(newsp, SP, newsp, gimme,
                               SVs_PADTMP|SVs_TEMP, FALSE);
     PL_curpm = newpm;   /* pop $1 et al */