This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove full stop in the 'try' feature heading
[perl5.git] / scope.c
1 /*    scope.c
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  * For the fashion of Minas Tirith was such that it was built on seven
13  * levels...
14  *
15  *     [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"]
16  */
17
18 /* This file contains functions to manipulate several of Perl's stacks;
19  * in particular it contains code to push various types of things onto
20  * the savestack, then to pop them off and perform the correct restorative
21  * action for each one. This corresponds to the cleanup Perl does at
22  * each scope exit.
23  */
24
25 #include "EXTERN.h"
26 #define PERL_IN_SCOPE_C
27 #include "perl.h"
28 #include "feature.h"
29
30 SV**
31 Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
32 {
33     SSize_t extra;
34     SSize_t current = (p - PL_stack_base);
35
36     PERL_ARGS_ASSERT_STACK_GROW;
37
38     if (UNLIKELY(n < 0))
39         Perl_croak(aTHX_
40             "panic: stack_grow() negative count (%" IVdf ")", (IV)n);
41
42     PL_stack_sp = sp;
43     extra =
44 #ifdef STRESS_REALLOC
45         1;
46 #else
47         128;
48 #endif
49     /* If the total might wrap, panic instead. This is really testing
50      * that (current + n + extra < SSize_t_MAX), but done in a way that
51      * can't wrap */
52     if (UNLIKELY(   current         > SSize_t_MAX - extra
53                  || current + extra > SSize_t_MAX - n
54     ))
55         /* diag_listed_as: Out of memory during %s extend */
56         Perl_croak(aTHX_ "Out of memory during stack extend");
57
58     av_extend(PL_curstack, current + n + extra);
59 #ifdef DEBUGGING
60         PL_curstackinfo->si_stack_hwm = current + n + extra;
61 #endif
62
63     return PL_stack_sp;
64 }
65
66 #ifndef STRESS_REALLOC
67 #define GROW(old) ((old) * 3 / 2)
68 #else
69 #define GROW(old) ((old) + 1)
70 #endif
71
72 PERL_SI *
73 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
74 {
75     PERL_SI *si;
76     Newx(si, 1, PERL_SI);
77     si->si_stack = newAV();
78     AvREAL_off(si->si_stack);
79     av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
80     AvALLOC(si->si_stack)[0] = &PL_sv_undef;
81     AvFILLp(si->si_stack) = 0;
82     si->si_prev = 0;
83     si->si_next = 0;
84     si->si_cxmax = cxitems - 1;
85     si->si_cxix = -1;
86     si->si_cxsubix = -1;
87     si->si_type = PERLSI_UNDEF;
88     Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
89     /* Without any kind of initialising CX_PUSHSUBST()
90      * in pp_subst() will read uninitialised heap. */
91     PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT);
92     return si;
93 }
94
95 I32
96 Perl_cxinc(pTHX)
97 {
98     const IV old_max = cxstack_max;
99     const IV new_max = GROW(cxstack_max);
100     Renew(cxstack, new_max + 1, PERL_CONTEXT);
101     cxstack_max = new_max;
102     /* Without any kind of initialising deep enough recursion
103      * will end up reading uninitialised PERL_CONTEXTs. */
104     PoisonNew(cxstack + old_max + 1, new_max - old_max, PERL_CONTEXT);
105     return cxstack_ix + 1;
106 }
107
108 /*
109 =for apidoc_section $callback
110 =for apidoc push_scope
111
112 Implements L<perlapi/C<ENTER>>
113
114 =cut
115 */
116
117 void
118 Perl_push_scope(pTHX)
119 {
120     if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
121         const IV new_max = GROW(PL_scopestack_max);
122         Renew(PL_scopestack, new_max, I32);
123 #ifdef DEBUGGING
124         Renew(PL_scopestack_name, new_max, const char*);
125 #endif
126         PL_scopestack_max = new_max;
127     }
128 #ifdef DEBUGGING
129     PL_scopestack_name[PL_scopestack_ix] = "unknown";
130 #endif
131     PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
132
133 }
134
135 /*
136 =for apidoc_section $callback
137 =for apidoc pop_scope
138
139 Implements L<perlapi/C<LEAVE>>
140
141 =cut
142 */
143
144 void
145 Perl_pop_scope(pTHX)
146 {
147     const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
148     LEAVE_SCOPE(oldsave);
149 }
150
151 I32 *
152 Perl_markstack_grow(pTHX)
153 {
154     const I32 oldmax = PL_markstack_max - PL_markstack;
155     const I32 newmax = GROW(oldmax);
156
157     Renew(PL_markstack, newmax, I32);
158     PL_markstack_max = PL_markstack + newmax;
159     PL_markstack_ptr = PL_markstack + oldmax;
160     DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
161             "MARK grow %p %" IVdf " by %" IVdf "\n",
162             PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax)));
163     return PL_markstack_ptr;
164 }
165
166 void
167 Perl_savestack_grow(pTHX)
168 {
169     IV new_max;
170 #ifdef STRESS_REALLOC
171     new_max = PL_savestack_max + SS_MAXPUSH;
172 #else
173     new_max = GROW(PL_savestack_max);
174 #endif
175     /* Note that we allocate SS_MAXPUSH slots higher than ss_max
176      * so that SS_ADD_END(), SSGROW() etc can do a simper check */
177     Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
178     PL_savestack_max = new_max;
179 }
180
181 void
182 Perl_savestack_grow_cnt(pTHX_ I32 need)
183 {
184     const IV new_max = PL_savestack_ix + need;
185     /* Note that we allocate SS_MAXPUSH slots higher than ss_max
186      * so that SS_ADD_END(), SSGROW() etc can do a simper check */
187     Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
188     PL_savestack_max = new_max;
189 }
190
191 #undef GROW
192
193 /*  The original function was called Perl_tmps_grow and was removed from public
194     API, Perl_tmps_grow_p is the replacement and it used in public macros but
195     isn't public itself.
196
197     Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by,
198     where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max
199     Upon return, PL_tmps_stack[ix] will be a valid address. For machine code
200     optimization and register usage reasons, the proposed ix passed into
201     tmps_grow is returned to the caller which the caller can then use to write
202     an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in
203     pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of
204     tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller
205     must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is
206     appropriate. The assignment to PL_temps_ix can happen before or after
207     tmps_grow call since tmps_grow doesn't look at PL_tmps_ix.
208  */
209
210 SSize_t
211 Perl_tmps_grow_p(pTHX_ SSize_t ix)
212 {
213     SSize_t extend_to = ix;
214 #ifndef STRESS_REALLOC
215     if (ix - PL_tmps_max < 128)
216         extend_to += (PL_tmps_max < 512) ? 128 : 512;
217 #endif
218     Renew(PL_tmps_stack, extend_to + 1, SV*);
219     PL_tmps_max = extend_to + 1;
220     return ix;
221 }
222
223
224 void
225 Perl_free_tmps(pTHX)
226 {
227     /* XXX should tmps_floor live in cxstack? */
228     const SSize_t myfloor = PL_tmps_floor;
229     while (PL_tmps_ix > myfloor) {      /* clean up after last statement */
230         SV* const sv = PL_tmps_stack[PL_tmps_ix--];
231 #ifdef PERL_POISON
232         PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
233 #endif
234         if (LIKELY(sv)) {
235             SvTEMP_off(sv);
236             SvREFCNT_dec_NN(sv);                /* note, can modify tmps_ix!!! */
237         }
238     }
239 }
240
241 /*
242 =for apidoc save_scalar_at
243
244 A helper function for localizing the SV referenced by C<*sptr>.
245
246 If C<SAVEf_KEEPOLDELEM> is set in in C<flags>, the function returns the input
247 scalar untouched.
248
249 Otherwise it replaces C<*sptr> with a new C<undef> scalar, and returns that.
250 The new scalar will have the old one's magic (if any) copied to it.
251 If there is such magic, and C<SAVEf_SETMAGIC> is set in in C<flags>, 'set'
252 magic will be processed on the new scalar.  If unset, 'set' magic will be
253 skipped.  The latter typically means that assignment will soon follow (I<e.g.>,
254 S<C<'local $x = $y'>>), and that will handle the magic.
255
256 =for apidoc Amnh ||SAVEf_KEEPOLDELEM
257 =for apidoc Amnh ||SAVEf_SETMAGIC
258
259 =cut
260 */
261
262 STATIC SV *
263 S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
264 {
265     SV * osv;
266     SV *sv;
267
268     PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
269
270     osv = *sptr;
271     if (flags & SAVEf_KEEPOLDELEM)
272         sv = osv;
273     else {
274         sv  = (*sptr = newSV_type(SVt_NULL));
275         if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
276             mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
277     }
278
279     return sv;
280 }
281
282 void
283 Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
284 {
285     dSS_ADD;
286     SS_ADD_PTR(ptr1);
287     SS_ADD_PTR(ptr2);
288     SS_ADD_UV(type);
289     SS_ADD_END(3);
290 }
291
292 SV *
293 Perl_save_scalar(pTHX_ GV *gv)
294 {
295     SV ** const sptr = &GvSVn(gv);
296
297     PERL_ARGS_ASSERT_SAVE_SCALAR;
298
299     if (UNLIKELY(SvGMAGICAL(*sptr))) {
300         PL_localizing = 1;
301         (void)mg_get(*sptr);
302         PL_localizing = 0;
303     }
304     save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
305     return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
306 }
307
308 /*
309 =for apidoc save_generic_svref
310
311 Implements C<SAVEGENERICSV>.
312
313 Like save_sptr(), but also SvREFCNT_dec()s the new value.  Can be used to
314 restore a global SV to its prior contents, freeing new value.
315
316 =cut
317  */
318
319 void
320 Perl_save_generic_svref(pTHX_ SV **sptr)
321 {
322     PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
323
324     save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
325 }
326
327
328 /*
329 =for apidoc save_rcpv_free
330
331 Implements C<SAVERCPVFREE>.
332
333 Saves and restores a refcounted string, similar to what
334 save_generic_svref would do for a SV*. Can be used to restore
335 a refcounted string to its previous state. Performs the 
336 appropriate refcount counting so that nothing should leak
337 or be prematurel freed.
338
339 =cut
340  */
341 void
342 Perl_save_rcpv_free(pTHX_ char **ppv) {
343     PERL_ARGS_ASSERT_SAVE_RCPV_FREE;
344     save_pushptrptr(ppv, rcpv_copy(*ppv), SAVEt_RCPV_FREE);
345 }
346
347 /*
348 =for apidoc_section $callback
349 =for apidoc save_generic_pvref
350
351 Implements C<SAVEGENERICPV>.
352
353 Like save_pptr(), but also Safefree()s the new value if it is different
354 from the old one.  Can be used to restore a global char* to its prior
355 contents, freeing new value.
356
357 =cut
358  */
359
360 void
361 Perl_save_generic_pvref(pTHX_ char **str)
362 {
363     PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
364
365     save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
366 }
367
368 /*
369 =for apidoc_section $callback
370 =for apidoc save_shared_pvref
371
372 Implements C<SAVESHAREDPV>.
373
374 Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
375 Can be used to restore a shared global char* to its prior
376 contents, freeing new value.
377
378 =cut
379  */
380
381 void
382 Perl_save_shared_pvref(pTHX_ char **str)
383 {
384     PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
385
386     save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
387 }
388
389
390 /*
391 =for apidoc_section $callback
392 =for apidoc save_set_svflags
393
394 Implements C<SAVESETSVFLAGS>.
395
396 Set the SvFLAGS specified by mask to the values in val
397
398 =cut
399  */
400
401 void
402 Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
403 {
404     dSS_ADD;
405
406     PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
407
408     SS_ADD_PTR(sv);
409     SS_ADD_INT(mask);
410     SS_ADD_INT(val);
411     SS_ADD_UV(SAVEt_SET_SVFLAGS);
412     SS_ADD_END(4);
413 }
414
415 /*
416
417 =for apidoc_section $GV
418
419 =for apidoc save_gp
420
421 Saves the current GP of gv on the save stack to be restored on scope exit.
422
423 If C<empty> is true, replace the GP with a new GP.
424
425 If C<empty> is false, mark C<gv> with C<GVf_INTRO> so the next reference
426 assigned is localized, which is how S<C< local *foo = $someref; >> works.
427
428 =cut
429 */
430
431 void
432 Perl_save_gp(pTHX_ GV *gv, I32 empty)
433 {
434     PERL_ARGS_ASSERT_SAVE_GP;
435
436     /* XXX For now, we just upgrade any coderef in the stash to a full GV
437            during localisation.  Maybe at some point we could make localis-
438            ation work without needing the upgrade.  (In which case our
439            callers should probably call a different function, not save_gp.)
440      */
441     if (!isGV(gv)) {
442         assert(isGV_or_RVCV(gv));
443         (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
444         assert(isGV(gv));
445     }
446
447     save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
448
449     if (empty) {
450         GP *gp = Perl_newGP(aTHX_ gv);
451         HV * const stash = GvSTASH(gv);
452         bool isa_changed = 0;
453
454         if (stash && HvHasENAME(stash)) {
455             if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
456                 isa_changed = TRUE;
457             else if (GvCVu(gv))
458                 /* taking a method out of circulation ("local")*/
459                 mro_method_changed_in(stash);
460         }
461         if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
462             gp->gp_io = newIO();
463             IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
464         }
465         GvGP_set(gv,gp);
466         if (isa_changed) mro_isa_changed_in(stash);
467     }
468     else {
469         gp_ref(GvGP(gv));
470         GvINTRO_on(gv);
471     }
472 }
473
474 AV *
475 Perl_save_ary(pTHX_ GV *gv)
476 {
477     AV * const oav = GvAVn(gv);
478     AV *av;
479
480     PERL_ARGS_ASSERT_SAVE_ARY;
481
482     if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
483         av_reify(oav);
484     save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
485
486     GvAV(gv) = NULL;
487     av = GvAVn(gv);
488     if (UNLIKELY(SvMAGIC(oav)))
489         mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
490     return av;
491 }
492
493 HV *
494 Perl_save_hash(pTHX_ GV *gv)
495 {
496     HV *ohv, *hv;
497
498     PERL_ARGS_ASSERT_SAVE_HASH;
499
500     save_pushptrptr(
501         SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
502     );
503
504     GvHV(gv) = NULL;
505     hv = GvHVn(gv);
506     if (UNLIKELY(SvMAGIC(ohv)))
507         mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
508     return hv;
509 }
510
511 void
512 Perl_save_item(pTHX_ SV *item)
513 {
514     SV * const sv = newSVsv(item);
515
516     PERL_ARGS_ASSERT_SAVE_ITEM;
517
518     save_pushptrptr(item, /* remember the pointer */
519                     sv,   /* remember the value */
520                     SAVEt_ITEM);
521 }
522
523 void
524 Perl_save_bool(pTHX_ bool *boolp)
525 {
526     dSS_ADD;
527
528     PERL_ARGS_ASSERT_SAVE_BOOL;
529
530     SS_ADD_PTR(boolp);
531     SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
532     SS_ADD_END(2);
533 }
534
535 void
536 Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
537 {
538     dSS_ADD;
539
540     SS_ADD_INT(i);
541     SS_ADD_PTR(ptr);
542     SS_ADD_UV(type);
543     SS_ADD_END(3);
544 }
545
546 void
547 Perl_save_int(pTHX_ int *intp)
548 {
549     const int i = *intp;
550     UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
551     int size = 2;
552     dSS_ADD;
553
554     PERL_ARGS_ASSERT_SAVE_INT;
555
556     if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
557         SS_ADD_INT(i);
558         type = SAVEt_INT;
559         size++;
560     }
561     SS_ADD_PTR(intp);
562     SS_ADD_UV(type);
563     SS_ADD_END(size);
564 }
565
566 void
567 Perl_save_I8(pTHX_ I8 *bytep)
568 {
569     dSS_ADD;
570
571     PERL_ARGS_ASSERT_SAVE_I8;
572
573     SS_ADD_PTR(bytep);
574     SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
575     SS_ADD_END(2);
576 }
577
578 void
579 Perl_save_I16(pTHX_ I16 *intp)
580 {
581     dSS_ADD;
582
583     PERL_ARGS_ASSERT_SAVE_I16;
584
585     SS_ADD_PTR(intp);
586     SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
587     SS_ADD_END(2);
588 }
589
590 void
591 Perl_save_I32(pTHX_ I32 *intp)
592 {
593     const I32 i = *intp;
594     UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
595     int size = 2;
596     dSS_ADD;
597
598     PERL_ARGS_ASSERT_SAVE_I32;
599
600     if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
601         SS_ADD_INT(i);
602         type = SAVEt_I32;
603         size++;
604     }
605     SS_ADD_PTR(intp);
606     SS_ADD_UV(type);
607     SS_ADD_END(size);
608 }
609
610 void
611 Perl_save_strlen(pTHX_ STRLEN *ptr)
612 {
613     const IV i = *ptr;
614     UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL);
615     int size = 2;
616     dSS_ADD;
617
618     PERL_ARGS_ASSERT_SAVE_STRLEN;
619
620     if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
621         SS_ADD_IV(*ptr);
622         type = SAVEt_STRLEN;
623         size++;
624     }
625
626     SS_ADD_PTR(ptr);
627     SS_ADD_UV(type);
628     SS_ADD_END(size);
629 }
630
631 void
632 Perl_save_iv(pTHX_ IV *ivp)
633 {
634     PERL_ARGS_ASSERT_SAVE_IV;
635
636     SSCHECK(3);
637     SSPUSHIV(*ivp);
638     SSPUSHPTR(ivp);
639     SSPUSHUV(SAVEt_IV);
640 }
641
642 /* Cannot use save_sptr() to store a char* since the SV** cast will
643  * force word-alignment and we'll miss the pointer.
644  */
645 void
646 Perl_save_pptr(pTHX_ char **pptr)
647 {
648     PERL_ARGS_ASSERT_SAVE_PPTR;
649
650     save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
651 }
652
653 /*
654 =for apidoc_section $callback
655 =for apidoc save_vptr
656
657 Implements C<SAVEVPTR>.
658
659 =cut
660  */
661
662 void
663 Perl_save_vptr(pTHX_ void *ptr)
664 {
665     PERL_ARGS_ASSERT_SAVE_VPTR;
666
667     save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
668 }
669
670 void
671 Perl_save_sptr(pTHX_ SV **sptr)
672 {
673     PERL_ARGS_ASSERT_SAVE_SPTR;
674
675     save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
676 }
677
678 /*
679 =for apidoc_section $callback
680 =for apidoc save_padsv_and_mortalize
681
682 Implements C<SAVEPADSVANDMORTALIZE>.
683
684 =cut
685  */
686
687 void
688 Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
689 {
690     dSS_ADD;
691
692     ASSERT_CURPAD_ACTIVE("save_padsv");
693     SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
694     SS_ADD_PTR(PL_comppad);
695     SS_ADD_UV((UV)off);
696     SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
697     SS_ADD_END(4);
698 }
699
700 void
701 Perl_save_hptr(pTHX_ HV **hptr)
702 {
703     PERL_ARGS_ASSERT_SAVE_HPTR;
704
705     save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
706 }
707
708 void
709 Perl_save_aptr(pTHX_ AV **aptr)
710 {
711     PERL_ARGS_ASSERT_SAVE_APTR;
712
713     save_pushptrptr(*aptr, aptr, SAVEt_APTR);
714 }
715
716 /*
717 =for apidoc_section $callback
718 =for apidoc save_pushptr
719
720 The refcnt of object C<ptr> will be decremented at the end of the current
721 I<pseudo-block>.  C<type> gives the type of C<ptr>, expressed as one of the
722 constants in F<scope.h> whose name begins with C<SAVEt_>.
723
724 This is the underlying implementation of several macros, like
725 C<SAVEFREESV>.
726
727 =cut
728 */
729
730 void
731 Perl_save_pushptr(pTHX_ void *const ptr, const int type)
732 {
733     dSS_ADD;
734     SS_ADD_PTR(ptr);
735     SS_ADD_UV(type);
736     SS_ADD_END(2);
737 }
738
739 void
740 Perl_save_clearsv(pTHX_ SV **svp)
741 {
742     const UV offset = svp - PL_curpad;
743     const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
744
745     PERL_ARGS_ASSERT_SAVE_CLEARSV;
746
747     ASSERT_CURPAD_ACTIVE("save_clearsv");
748     assert(*svp);
749     SvPADSTALE_off(*svp); /* mark lexical as active */
750     if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
751         Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
752                    offset, svp, PL_curpad);
753     }
754
755     {
756         dSS_ADD;
757         SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
758         SS_ADD_END(1);
759     }
760 }
761
762 void
763 Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
764 {
765     PERL_ARGS_ASSERT_SAVE_DELETE;
766
767     save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
768 }
769
770 /*
771 =for apidoc_section $callback
772 =for apidoc save_hdelete
773
774 Implements C<SAVEHDELETE>.
775
776 =cut
777 */
778
779 void
780 Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
781 {
782     STRLEN len;
783     I32 klen;
784     const char *key;
785
786     PERL_ARGS_ASSERT_SAVE_HDELETE;
787
788     key  = SvPV_const(keysv, len);
789     klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
790     SvREFCNT_inc_simple_void_NN(hv);
791     save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
792 }
793
794 /*
795 =for apidoc_section $callback
796 =for apidoc save_adelete
797
798 Implements C<SAVEADELETE>.
799
800 =cut
801 */
802
803 void
804 Perl_save_adelete(pTHX_ AV *av, SSize_t key)
805 {
806     dSS_ADD;
807
808     PERL_ARGS_ASSERT_SAVE_ADELETE;
809
810     SvREFCNT_inc_void(av);
811     SS_ADD_UV(key);
812     SS_ADD_PTR(av);
813     SS_ADD_IV(SAVEt_ADELETE);
814     SS_ADD_END(3);
815 }
816
817 void
818 Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
819 {
820     dSS_ADD;
821     PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
822
823     SS_ADD_DPTR(f);
824     SS_ADD_PTR(p);
825     SS_ADD_UV(SAVEt_DESTRUCTOR);
826     SS_ADD_END(3);
827 }
828
829 void
830 Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
831 {
832     dSS_ADD;
833
834     SS_ADD_DXPTR(f);
835     SS_ADD_PTR(p);
836     SS_ADD_UV(SAVEt_DESTRUCTOR_X);
837     SS_ADD_END(3);
838 }
839
840 /*
841 =for apidoc_section $callback
842 =for apidoc save_hints
843
844 Implements C<SAVEHINTS>.
845
846 =cut
847  */
848
849 void
850 Perl_save_hints(pTHX)
851 {
852     COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
853     if (PL_hints & HINT_LOCALIZE_HH) {
854         HV *oldhh = GvHV(PL_hintgv);
855         {
856             dSS_ADD;
857             SS_ADD_INT(PL_hints);
858             SS_ADD_PTR(save_cophh);
859             SS_ADD_PTR(oldhh);
860             SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8));
861             SS_ADD_END(4);
862         }
863         GvHV(PL_hintgv) = NULL; /* in case copying dies */
864         GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
865         SAVEFEATUREBITS();
866     } else {
867         save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8));
868     }
869 }
870
871 static void
872 S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
873                         const int type)
874 {
875     dSS_ADD;
876     SS_ADD_PTR(ptr1);
877     SS_ADD_INT(i);
878     SS_ADD_PTR(ptr2);
879     SS_ADD_UV(type);
880     SS_ADD_END(4);
881 }
882
883 /*
884 =for apidoc_section $callback
885 =for apidoc      save_aelem
886 =for apidoc_item save_aelem_flags
887
888 These each arrange for the value of the array element C<av[idx]> to be restored
889 at the end of the enclosing I<pseudo-block>.
890
891 In C<save_aelem>, the SV at C**sptr> will be replaced by a new C<undef>
892 scalar.  That scalar will inherit any magic from the original C<**sptr>,
893 and any 'set' magic will be processed.
894
895 In C<save_aelem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
896 the function to forgo all that:  the scalar at C<**sptr> is untouched.
897 If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
898 new C<undef> scalar.  That scalar will inherit any magic from the original
899 C<**sptr>.  Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
900 is set in in C<flags>.
901
902 =cut
903 */
904
905 void
906 Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
907                             const U32 flags)
908 {
909     dSS_ADD;
910     SV *sv;
911
912     PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
913
914     SvGETMAGIC(*sptr);
915     SS_ADD_PTR(SvREFCNT_inc_simple(av));
916     SS_ADD_IV(idx);
917     SS_ADD_PTR(SvREFCNT_inc(*sptr));
918     SS_ADD_UV(SAVEt_AELEM);
919     SS_ADD_END(4);
920     /* The array needs to hold a reference count on its new element, so it
921        must be AvREAL. */
922     if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
923         av_reify(av);
924     save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
925     if (flags & SAVEf_KEEPOLDELEM)
926         return;
927     sv = *sptr;
928     /* If we're localizing a tied array element, this new sv
929      * won't actually be stored in the array - so it won't get
930      * reaped when the localize ends. Ensure it gets reaped by
931      * mortifying it instead. DAPM */
932     if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
933         sv_2mortal(sv);
934 }
935
936 /*
937 =for apidoc_section $callback
938 =for apidoc      save_helem
939 =for apidoc_item save_helem_flags
940
941 These each arrange for the value of the hash element (in Perlish terms)
942 C<$hv{key}]> to be restored at the end of the enclosing I<pseudo-block>.
943
944 In C<save_helem>, the SV at C**sptr> will be replaced by a new C<undef>
945 scalar.  That scalar will inherit any magic from the original C<**sptr>,
946 and any 'set' magic will be processed.
947
948 In C<save_helem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
949 the function to forgo all that:  the scalar at C<**sptr> is untouched.
950 If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
951 new C<undef> scalar.  That scalar will inherit any magic from the original
952 C<**sptr>.  Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
953 is set in in C<flags>.
954
955 =cut
956 */
957
958 void
959 Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
960 {
961     SV *sv;
962
963     PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
964
965     SvGETMAGIC(*sptr);
966     {
967         dSS_ADD;
968         SS_ADD_PTR(SvREFCNT_inc_simple(hv));
969         SS_ADD_PTR(newSVsv(key));
970         SS_ADD_PTR(SvREFCNT_inc(*sptr));
971         SS_ADD_UV(SAVEt_HELEM);
972         SS_ADD_END(4);
973     }
974     save_scalar_at(sptr, flags);
975     if (flags & SAVEf_KEEPOLDELEM)
976         return;
977     sv = *sptr;
978     /* If we're localizing a tied hash element, this new sv
979      * won't actually be stored in the hash - so it won't get
980      * reaped when the localize ends. Ensure it gets reaped by
981      * mortifying it instead. DAPM */
982     if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
983         sv_2mortal(sv);
984 }
985
986 SV*
987 Perl_save_svref(pTHX_ SV **sptr)
988 {
989     PERL_ARGS_ASSERT_SAVE_SVREF;
990
991     SvGETMAGIC(*sptr);
992     save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
993     return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
994 }
995
996
997 void
998 Perl_savetmps(pTHX)
999 {
1000     dSS_ADD;
1001     SS_ADD_IV(PL_tmps_floor);
1002     PL_tmps_floor = PL_tmps_ix;
1003     SS_ADD_UV(SAVEt_TMPSFLOOR);
1004     SS_ADD_END(2);
1005 }
1006
1007 /*
1008 =for apidoc_section $stack
1009 =for apidoc save_alloc
1010
1011 Implements L<perlapi/C<SSNEW>> and kin, which should be used instead of this
1012 function.
1013
1014 =cut
1015 */
1016
1017 SSize_t
1018 Perl_save_alloc(pTHX_ SSize_t size, I32 pad)
1019 {
1020     const SSize_t start = pad + ((char*)&PL_savestack[PL_savestack_ix]
1021                           - (char*)PL_savestack);
1022     const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
1023     const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
1024
1025     if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
1026         Perl_croak(aTHX_
1027             "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
1028                    elems, (IV)size, (IV)pad);
1029
1030     SSGROW(elems + 1);
1031
1032     PL_savestack_ix += elems;
1033     SSPUSHUV(SAVEt_ALLOC | elems_shifted);
1034     return start;
1035 }
1036
1037
1038
1039 /*
1040 =for apidoc_section $callback
1041 =for apidoc leave_scope
1042
1043 Implements C<LEAVE_SCOPE> which you should use instead.
1044
1045 =cut
1046  */
1047
1048 void
1049 Perl_leave_scope(pTHX_ I32 base)
1050 {
1051     /* Localise the effects of the TAINT_NOT inside the loop.  */
1052     bool was = TAINT_get;
1053
1054     if (UNLIKELY(base < -1))
1055         Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
1056     DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
1057                         (long)PL_savestack_ix, (long)base));
1058     while (PL_savestack_ix > base) {
1059         UV uv;
1060         U8 type;
1061         ANY *ap; /* arg pointer */
1062         ANY a0, a1, a2; /* up to 3 args */
1063
1064         TAINT_NOT;
1065
1066         {
1067             U8  argcount;
1068             I32 ix = PL_savestack_ix - 1;
1069
1070             ap = &PL_savestack[ix];
1071             uv = ap->any_uv;
1072             type = (U8)uv & SAVE_MASK;
1073             argcount = leave_scope_arg_counts[type];
1074             PL_savestack_ix = ix - argcount;
1075             ap -= argcount;
1076         }
1077
1078         switch (type) {
1079         case SAVEt_ITEM:                        /* normal string */
1080             a0 = ap[0]; a1 = ap[1];
1081             sv_replace(a0.any_sv, a1.any_sv);
1082             if (UNLIKELY(SvSMAGICAL(a0.any_sv))) {
1083                 PL_localizing = 2;
1084                 mg_set(a0.any_sv);
1085                 PL_localizing = 0;
1086             }
1087             break;
1088
1089             /* This would be a mathom, but Perl_save_svref() calls a static
1090                function, S_save_scalar_at(), so has to stay in this file.  */
1091         case SAVEt_SVREF:                       /* scalar reference */
1092             a0 = ap[0]; a1 = ap[1];
1093             a2.any_svp = a0.any_svp;
1094             a0.any_sv = NULL; /* what to refcnt_dec */
1095             goto restore_sv;
1096
1097         case SAVEt_SV:                          /* scalar reference */
1098             a0 = ap[0]; a1 = ap[1];
1099             a2.any_svp = &GvSV(a0.any_gv);
1100         restore_sv:
1101         {
1102             /* do *a2.any_svp = a1 and free a0 */
1103             SV * const sv = *a2.any_svp;
1104             *a2.any_svp = a1.any_sv;
1105             SvREFCNT_dec(sv);
1106             if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1107                 /* mg_set could die, skipping the freeing of a0 and
1108                  * a1; Ensure that they're always freed in that case */
1109                 dSS_ADD;
1110                 SS_ADD_PTR(a1.any_sv);
1111                 SS_ADD_UV(SAVEt_FREESV);
1112                 SS_ADD_PTR(a0.any_sv);
1113                 SS_ADD_UV(SAVEt_FREESV);
1114                 SS_ADD_END(4);
1115                 PL_localizing = 2;
1116                 mg_set(a1.any_sv);
1117                 PL_localizing = 0;
1118                 break;
1119             }
1120             SvREFCNT_dec_NN(a1.any_sv);
1121             SvREFCNT_dec(a0.any_sv);
1122             break;
1123         }
1124
1125         case SAVEt_GENERIC_PVREF:               /* generic pv */
1126             a0 = ap[0]; a1 = ap[1];
1127             if (*a1.any_pvp != a0.any_pv) {
1128                 Safefree(*a1.any_pvp);
1129                 *a1.any_pvp = a0.any_pv;
1130             }
1131             break;
1132
1133         case SAVEt_SHARED_PVREF:                /* shared pv */
1134             a0 = ap[0]; a1 = ap[1];
1135             if (*a0.any_pvp != a1.any_pv) {
1136                 PerlMemShared_free(*a0.any_pvp);
1137                 *a0.any_pvp = a1.any_pv;
1138             }
1139             break;
1140
1141         case SAVEt_GVSV:                        /* scalar slot in GV */
1142             a0 = ap[0]; a1 = ap[1];
1143             a0.any_svp = &GvSV(a0.any_gv);
1144             goto restore_svp;
1145
1146
1147         case SAVEt_GENERIC_SVREF:               /* generic sv */
1148             a0 = ap[0]; a1 = ap[1];
1149         restore_svp:
1150         {
1151             /* do *a0.any_svp = a1 */
1152             SV * const sv = *a0.any_svp;
1153             *a0.any_svp = a1.any_sv;
1154             SvREFCNT_dec(sv);
1155             SvREFCNT_dec(a1.any_sv);
1156             break;
1157         }
1158
1159         case SAVEt_RCPV_FREE:           /* like generic sv, but for struct rcpv */
1160         {
1161             a0 = ap[0]; a1 = ap[1];
1162             char *old = *a0.any_pvp;
1163             *a0.any_pvp = a1.any_pv;
1164             (void)rcpv_free(old);
1165             (void)rcpv_free(a1.any_pv);
1166             break;
1167         }
1168
1169         case SAVEt_GVSLOT:                      /* any slot in GV */
1170         {
1171             HV * hv;
1172             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1173             hv = GvSTASH(a0.any_gv);
1174             if (hv && HvHasENAME(hv) && (
1175                     (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV)
1176                  || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV)
1177                ))
1178             {
1179                 if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv)
1180                  || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp)
1181                  || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */
1182                     PL_sub_generation++;
1183                 else mro_method_changed_in(hv);
1184             }
1185             a0.any_svp = a1.any_svp;
1186             a1.any_sv  = a2.any_sv;
1187             goto restore_svp;
1188         }
1189
1190         case SAVEt_AV:                          /* array reference */
1191             a0 = ap[0]; a1 = ap[1];
1192             SvREFCNT_dec(GvAV(a0.any_gv));
1193             GvAV(a0.any_gv) = a1.any_av;
1194           avhv_common:
1195             if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1196                 /* mg_set might die, so make sure a0 isn't leaked */
1197                 dSS_ADD;
1198                 SS_ADD_PTR(a0.any_sv);
1199                 SS_ADD_UV(SAVEt_FREESV);
1200                 SS_ADD_END(2);
1201                 PL_localizing = 2;
1202                 mg_set(a1.any_sv);
1203                 PL_localizing = 0;
1204                 break;
1205             }
1206             SvREFCNT_dec_NN(a0.any_sv);
1207             break;
1208
1209         case SAVEt_HV:                          /* hash reference */
1210             a0 = ap[0]; a1 = ap[1];
1211             SvREFCNT_dec(GvHV(a0.any_gv));
1212             GvHV(a0.any_gv) = a1.any_hv;
1213             goto avhv_common;
1214
1215         case SAVEt_INT_SMALL:
1216             a0 = ap[0];
1217             *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
1218             break;
1219
1220         case SAVEt_INT:                         /* int reference */
1221             a0 = ap[0]; a1 = ap[1];
1222             *(int*)a1.any_ptr = (int)a0.any_i32;
1223             break;
1224
1225         case SAVEt_STRLEN_SMALL:
1226             a0 = ap[0];
1227             *(STRLEN*)a0.any_ptr = (STRLEN)(uv >> SAVE_TIGHT_SHIFT);
1228             break;
1229
1230         case SAVEt_STRLEN:                      /* STRLEN/size_t ref */
1231             a0 = ap[0]; a1 = ap[1];
1232             *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
1233             break;
1234
1235         case SAVEt_TMPSFLOOR:                   /* restore PL_tmps_floor */
1236             a0 = ap[0];
1237             PL_tmps_floor = (SSize_t)a0.any_iv;
1238             break;
1239
1240         case SAVEt_BOOL:                        /* bool reference */
1241             a0 = ap[0];
1242             *(bool*)a0.any_ptr = cBOOL(uv >> 8);
1243 #ifdef NO_TAINT_SUPPORT
1244             PERL_UNUSED_VAR(was);
1245 #else
1246             if (UNLIKELY(a0.any_ptr == &(PL_tainted))) {
1247                 /* If we don't update <was>, to reflect what was saved on the
1248                  * stack for PL_tainted, then we will overwrite this attempt to
1249                  * restore it when we exit this routine.  Note that this won't
1250                  * work if this value was saved in a wider-than necessary type,
1251                  * such as I32 */
1252                 was = *(bool*)a0.any_ptr;
1253             }
1254 #endif
1255             break;
1256
1257         case SAVEt_I32_SMALL:
1258             a0 = ap[0];
1259             *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
1260             break;
1261
1262         case SAVEt_I32:                         /* I32 reference */
1263             a0 = ap[0]; a1 = ap[1];
1264 #ifdef PERL_DEBUG_READONLY_OPS
1265             if (*(I32*)a1.any_ptr != a0.any_i32)
1266 #endif
1267                 *(I32*)a1.any_ptr = a0.any_i32;
1268             break;
1269
1270         case SAVEt_SPTR:                        /* SV* reference */
1271         case SAVEt_VPTR:                        /* random* reference */
1272         case SAVEt_PPTR:                        /* char* reference */
1273         case SAVEt_HPTR:                        /* HV* reference */
1274         case SAVEt_APTR:                        /* AV* reference */
1275             a0 = ap[0]; a1 = ap[1];
1276             *a1.any_svp= a0.any_sv;
1277             break;
1278
1279         case SAVEt_GP:                          /* scalar reference */
1280         {
1281             HV *hv;
1282             bool had_method;
1283
1284             a0 = ap[0]; a1 = ap[1];
1285             /* possibly taking a method out of circulation */   
1286             had_method = cBOOL(GvCVu(a0.any_gv));
1287             gp_free(a0.any_gv);
1288             GvGP_set(a0.any_gv, (GP*)a1.any_ptr);
1289             if ((hv=GvSTASH(a0.any_gv)) && HvHasENAME(hv)) {
1290                 if (memEQs(GvNAME(a0.any_gv), GvNAMELEN(a0.any_gv), "ISA"))
1291                     mro_isa_changed_in(hv);
1292                 else if (had_method || GvCVu(a0.any_gv))
1293                     /* putting a method back into circulation ("local")*/       
1294                     gv_method_changed(a0.any_gv);
1295             }
1296             SvREFCNT_dec_NN(a0.any_gv);
1297             break;
1298         }
1299
1300         case SAVEt_FREESV:
1301             a0 = ap[0];
1302             SvREFCNT_dec(a0.any_sv);
1303             break;
1304
1305         case SAVEt_FREEPADNAME:
1306             a0 = ap[0];
1307             PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
1308             break;
1309
1310         case SAVEt_FREECOPHH:
1311             a0 = ap[0];
1312             cophh_free((COPHH *)a0.any_ptr);
1313             break;
1314
1315         case SAVEt_MORTALIZESV:
1316             a0 = ap[0];
1317             sv_2mortal(a0.any_sv);
1318             break;
1319
1320         case SAVEt_FREEOP:
1321             a0 = ap[0];
1322             ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
1323             op_free(a0.any_op);
1324             break;
1325
1326         case SAVEt_FREEPV:
1327             a0 = ap[0];
1328             Safefree(a0.any_ptr);
1329             break;
1330
1331         case SAVEt_CLEARPADRANGE:
1332         {
1333             I32 i;
1334             SV **svp;
1335             i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
1336             svp = &PL_curpad[uv >>
1337                     (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
1338             goto clearsv;
1339         case SAVEt_CLEARSV:
1340             svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
1341             i = 1;
1342           clearsv:
1343             for (; i; i--, svp--) {
1344                 SV *sv = *svp;
1345
1346                 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
1347              "Pad 0x%" UVxf "[0x%" UVxf "] clearsv: %ld sv=0x%" UVxf "<%" IVdf "> %s\n",
1348                     PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1349                     (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
1350                     (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
1351                 ));
1352
1353                 /* Can clear pad variable in place? */
1354                 if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) {
1355
1356                     /* these flags are the union of all the relevant flags
1357                      * in the individual conditions within */
1358                     if (UNLIKELY(SvFLAGS(sv) & (
1359                             SVf_READONLY|SVf_PROTECT /*for SvREADONLY_off*/
1360                           | (SVs_GMG|SVs_SMG|SVs_RMG) /* SvMAGICAL() */
1361                           | SVf_OOK
1362                           | SVf_THINKFIRST)))
1363                     {
1364                         /* if a my variable that was made readonly is
1365                          * going out of scope, we want to remove the
1366                          * readonlyness so that it can go out of scope
1367                          * quietly
1368                          */
1369                         if (SvREADONLY(sv))
1370                             SvREADONLY_off(sv);
1371
1372                         if (SvTYPE(sv) == SVt_PVHV && HvHasAUX(sv))
1373                             Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
1374                         else if(SvOOK(sv))
1375                             sv_backoff(sv);
1376
1377                         if (SvMAGICAL(sv)) {
1378                             /* note that backrefs (either in HvAUX or magic)
1379                              * must be removed before other magic */
1380                             sv_unmagic(sv, PERL_MAGIC_backref);
1381                             if (SvTYPE(sv) != SVt_PVCV)
1382                                 mg_free(sv);
1383                         }
1384                         if (SvTHINKFIRST(sv))
1385                             sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
1386                                                      |SV_COW_DROP_PV);
1387
1388                     }
1389                     switch (SvTYPE(sv)) {
1390                     case SVt_NULL:
1391                         break;
1392                     case SVt_PVAV:
1393                         av_clear(MUTABLE_AV(sv));
1394                         break;
1395                     case SVt_PVHV:
1396                         hv_clear(MUTABLE_HV(sv));
1397                         break;
1398                     case SVt_PVCV:
1399                     {
1400                         HEK *hek = CvGvNAME_HEK(sv);
1401                         assert(hek);
1402                         (void)share_hek_hek(hek);
1403                         cv_undef((CV *)sv);
1404                         CvNAME_HEK_set(sv, hek);
1405                         CvLEXICAL_on(sv);
1406                         break;
1407                     }
1408                     default:
1409                         /* This looks odd, but these two macros are for use in
1410                            expressions and finish with a trailing comma, so
1411                            adding a ; after them would be wrong. */
1412                         assert_not_ROK(sv)
1413                         assert_not_glob(sv)
1414                         SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8);
1415                         break;
1416                     }
1417                     SvPADTMP_off(sv);
1418                     SvPADSTALE_on(sv); /* mark as no longer live */
1419                 }
1420                 else {  /* Someone has a claim on this, so abandon it. */
1421                     switch (SvTYPE(sv)) {       /* Console ourselves with a new value */
1422                     case SVt_PVAV:      *svp = MUTABLE_SV(newAV());     break;
1423                     case SVt_PVHV:      *svp = MUTABLE_SV(newHV());     break;
1424                     case SVt_PVCV:
1425                     {
1426                         HEK * const hek = CvGvNAME_HEK(sv);
1427
1428                         /* Create a stub */
1429                         *svp = newSV_type(SVt_PVCV);
1430
1431                         /* Share name */
1432                         CvNAME_HEK_set(*svp,
1433                                        share_hek_hek(hek));
1434                         CvLEXICAL_on(*svp);
1435                         break;
1436                     }
1437                     default:    *svp = newSV_type(SVt_NULL);            break;
1438                     }
1439                     SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
1440                     /* preserve pad nature, but also mark as not live
1441                      * for any closure capturing */
1442                     SvFLAGS(*svp) |= SVs_PADSTALE;
1443                 }
1444             }
1445             break;
1446         }
1447
1448         case SAVEt_DELETE:
1449             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1450             /* hv_delete could die, so free the key and SvREFCNT_dec the
1451              * hv by pushing new save actions
1452              */
1453             /* ap[0] is the key */
1454             ap[1].any_uv = SAVEt_FREEPV; /* was len */
1455             /* ap[2] is the hv */
1456             ap[3].any_uv = SAVEt_FREESV; /* was SAVEt_DELETE */
1457             PL_savestack_ix += 4;
1458             (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD);
1459             break;
1460
1461         case SAVEt_ADELETE:
1462             a0 = ap[0]; a1 = ap[1];
1463             /* av_delete could die, so SvREFCNT_dec the av by pushing a
1464              * new save action
1465              */
1466             ap[0].any_av = a1.any_av;
1467             ap[1].any_uv = SAVEt_FREESV;
1468             PL_savestack_ix += 2;
1469             (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD);
1470             break;
1471
1472         case SAVEt_DESTRUCTOR_X:
1473             a0 = ap[0]; a1 = ap[1];
1474             (*a0.any_dxptr)(aTHX_ a1.any_ptr);
1475             break;
1476
1477         case SAVEt_REGCONTEXT:
1478             /* regexp must have croaked */
1479         case SAVEt_ALLOC:
1480             PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
1481             break;
1482
1483         case SAVEt_STACK_POS:           /* Position on Perl stack */
1484             a0 = ap[0];
1485             PL_stack_sp = PL_stack_base + a0.any_i32;
1486             break;
1487
1488         case SAVEt_AELEM:               /* array element */
1489         {
1490             SV **svp;
1491             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1492             svp = av_fetch(a0.any_av, a1.any_iv, 1);
1493             if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */
1494                 SvREFCNT_dec(a2.any_sv);
1495             if (LIKELY(svp)) {
1496                 SV * const sv = *svp;
1497                 if (LIKELY(sv && sv != &PL_sv_undef)) {
1498                     if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied)))
1499                         SvREFCNT_inc_void_NN(sv);
1500                     a1.any_sv  = a2.any_sv;
1501                     a2.any_svp = svp;
1502                     goto restore_sv;
1503                 }
1504             }
1505             SvREFCNT_dec(a0.any_av);
1506             SvREFCNT_dec(a2.any_sv);
1507             break;
1508         }
1509
1510         case SAVEt_HELEM:               /* hash element */
1511         {
1512             HE *he;
1513
1514             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1515             he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0);
1516             SvREFCNT_dec(a1.any_sv);
1517             if (LIKELY(he)) {
1518                 const SV * const oval = HeVAL(he);
1519                 if (LIKELY(oval && oval != &PL_sv_undef)) {
1520                     SV **svp = &HeVAL(he);
1521                     if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied)))
1522                         SvREFCNT_inc_void(*svp);
1523                     a1.any_sv  = a2.any_sv;
1524                     a2.any_svp = svp;
1525                     goto restore_sv;
1526                 }
1527             }
1528             SvREFCNT_dec(a0.any_hv);
1529             SvREFCNT_dec(a2.any_sv);
1530             break;
1531         }
1532
1533         case SAVEt_OP:
1534             a0 = ap[0];
1535             PL_op = (OP*)a0.any_ptr;
1536             break;
1537
1538         case SAVEt_HINTS_HH:
1539             a2 = ap[2];
1540             /* FALLTHROUGH */
1541         case SAVEt_HINTS:
1542             a0 = ap[0]; a1 = ap[1];
1543             if ((PL_hints & HINT_LOCALIZE_HH)) {
1544               while (GvHV(PL_hintgv)) {
1545                 HV *hv = GvHV(PL_hintgv);
1546                 GvHV(PL_hintgv) = NULL;
1547                 SvREFCNT_dec(MUTABLE_SV(hv));
1548               }
1549             }
1550             cophh_free(CopHINTHASH_get(&PL_compiling));
1551             CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr);
1552             *(I32*)&PL_hints = a0.any_i32;
1553             PL_prevailing_version = (U16)(uv >> 8);
1554             if (type == SAVEt_HINTS_HH) {
1555                 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
1556                 GvHV(PL_hintgv) = MUTABLE_HV(a2.any_ptr);
1557             }
1558             if (!GvHV(PL_hintgv)) {
1559                 /* Need to add a new one manually, else rv2hv can
1560                    add one via GvHVn and it won't have the magic set.  */
1561                 HV *const hv = newHV();
1562                 hv_magic(hv, NULL, PERL_MAGIC_hints);
1563                 GvHV(PL_hintgv) = hv;
1564             }
1565             assert(GvHV(PL_hintgv));
1566             break;
1567
1568         case SAVEt_COMPPAD:
1569             a0 = ap[0];
1570             PL_comppad = (PAD*)a0.any_ptr;
1571             if (LIKELY(PL_comppad))
1572                 PL_curpad = AvARRAY(PL_comppad);
1573             else
1574                 PL_curpad = NULL;
1575             break;
1576
1577         case SAVEt_PADSV_AND_MORTALIZE:
1578             {
1579                 SV **svp;
1580
1581                 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1582                 assert (a1.any_ptr);
1583                 svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv;
1584                 /* This mortalizing used to be done by CX_POOPLOOP() via
1585                    itersave.  But as we have all the information here, we
1586                    can do it here, save even having to have itersave in
1587                    the struct.
1588                    */
1589                 sv_2mortal(*svp);
1590                 *svp = a0.any_sv;
1591             }
1592             break;
1593
1594         case SAVEt_SAVESWITCHSTACK:
1595             {
1596                 dSP;
1597
1598                 a0 = ap[0]; a1 = ap[1];
1599                 SWITCHSTACK(a1.any_av, a0.any_av);
1600                 PL_curstackinfo->si_stack = a0.any_av;
1601             }
1602             break;
1603
1604         case SAVEt_SET_SVFLAGS:
1605             a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1606             SvFLAGS(a0.any_sv) &= ~(a1.any_u32);
1607             SvFLAGS(a0.any_sv) |= a2.any_u32;
1608             break;
1609
1610             /* These are only saved in mathoms.c */
1611         case SAVEt_NSTAB:
1612             a0 = ap[0];
1613             (void)sv_clear(a0.any_sv);
1614             break;
1615
1616         case SAVEt_LONG:                        /* long reference */
1617             a0 = ap[0]; a1 = ap[1];
1618             *(long*)a1.any_ptr = a0.any_long;
1619             break;
1620
1621         case SAVEt_IV:                          /* IV reference */
1622             a0 = ap[0]; a1 = ap[1];
1623             *(IV*)a1.any_ptr = a0.any_iv;
1624             break;
1625
1626         case SAVEt_I16:                         /* I16 reference */
1627             a0 = ap[0];
1628             *(I16*)a0.any_ptr = (I16)(uv >> 8);
1629             break;
1630
1631         case SAVEt_I8:                          /* I8 reference */
1632             a0 = ap[0];
1633             *(I8*)a0.any_ptr = (I8)(uv >> 8);
1634             break;
1635
1636         case SAVEt_DESTRUCTOR:
1637             a0 = ap[0]; a1 = ap[1];
1638             (*a0.any_dptr)(a1.any_ptr);
1639             break;
1640
1641         case SAVEt_COMPILE_WARNINGS:
1642             /* NOTE: we can't put &PL_compiling or PL_curcop on the save
1643              *       stack directly, as we currently cannot translate
1644              *       them to the correct addresses after a thread start
1645              *       or win32 fork start. - Yves
1646              */
1647             a0 = ap[0];
1648             free_and_set_cop_warnings(&PL_compiling, a0.any_pv);
1649             break;
1650
1651         case SAVEt_CURCOP_WARNINGS:
1652             /* NOTE: see comment above about SAVEt_COMPILE_WARNINGS */
1653             a0 = ap[0];
1654             free_and_set_cop_warnings(PL_curcop, a0.any_pv);
1655             break;
1656
1657         case SAVEt_PARSER:
1658             a0 = ap[0];
1659             parser_free((yy_parser *)a0.any_ptr);
1660             break;
1661
1662         case SAVEt_READONLY_OFF:
1663             a0 = ap[0];
1664             SvREADONLY_off(a0.any_sv);
1665             break;
1666
1667         default:
1668             Perl_croak(aTHX_ "panic: leave_scope inconsistency %u",
1669                     (U8)uv & SAVE_MASK);
1670         }
1671     }
1672
1673     TAINT_set(was);
1674 }
1675
1676 void
1677 Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
1678 {
1679     PERL_ARGS_ASSERT_CX_DUMP;
1680
1681 #ifdef DEBUGGING
1682     PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
1683     if (CxTYPE(cx) != CXt_SUBST) {
1684         const char *gimme_text;
1685         PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
1686         PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n",
1687                       PTR2UV(cx->blk_oldcop));
1688         PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1689         PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
1690         PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
1691         PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n",
1692                       PTR2UV(cx->blk_oldpm));
1693         switch (cx->blk_gimme) {
1694             case G_VOID:
1695                 gimme_text = "VOID";
1696                 break;
1697             case G_SCALAR:
1698                 gimme_text = "SCALAR";
1699                 break;
1700             case G_LIST:
1701                 gimme_text = "LIST";
1702                 break;
1703             default:
1704                 gimme_text = "UNKNOWN";
1705                 break;
1706         }
1707         PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
1708     }
1709     switch (CxTYPE(cx)) {
1710     case CXt_NULL:
1711     case CXt_BLOCK:
1712     case CXt_DEFER:
1713         break;
1714     case CXt_FORMAT:
1715         PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n",
1716                 PTR2UV(cx->blk_format.cv));
1717         PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n",
1718                 PTR2UV(cx->blk_format.gv));
1719         PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n",
1720                 PTR2UV(cx->blk_format.dfoutgv));
1721         PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
1722                       (int)CxHASARGS(cx));
1723         PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n",
1724                 PTR2UV(cx->blk_format.retop));
1725         break;
1726     case CXt_SUB:
1727         PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n",
1728                 PTR2UV(cx->blk_sub.cv));
1729         PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
1730                 (long)cx->blk_sub.olddepth);
1731         PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
1732                 (int)CxHASARGS(cx));
1733         PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
1734         PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n",
1735                 PTR2UV(cx->blk_sub.retop));
1736         break;
1737     case CXt_EVAL:
1738         PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
1739                 (long)CxOLD_IN_EVAL(cx));
1740         PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
1741                 PL_op_name[CxOLD_OP_TYPE(cx)],
1742                 PL_op_desc[CxOLD_OP_TYPE(cx)]);
1743         if (cx->blk_eval.old_namesv)
1744             PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
1745                           SvPVX_const(cx->blk_eval.old_namesv));
1746         PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n",
1747                 PTR2UV(cx->blk_eval.old_eval_root));
1748         PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n",
1749                 PTR2UV(cx->blk_eval.retop));
1750         break;
1751
1752     case CXt_LOOP_PLAIN:
1753     case CXt_LOOP_LAZYIV:
1754     case CXt_LOOP_LAZYSV:
1755     case CXt_LOOP_LIST:
1756     case CXt_LOOP_ARY:
1757         PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
1758         PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n",
1759                 PTR2UV(cx->blk_loop.my_op));
1760         if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
1761             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n",
1762                     PTR2UV(CxITERVAR(cx)));
1763             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n",
1764                     PTR2UV(cx->blk_loop.itersave));
1765         }
1766         if (CxTYPE(cx) == CXt_LOOP_ARY) {
1767             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n",
1768                     PTR2UV(cx->blk_loop.state_u.ary.ary));
1769             PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1770                     (long)cx->blk_loop.state_u.ary.ix);
1771         }
1772         break;
1773
1774     case CXt_SUBST:
1775         PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
1776                 (long)cx->sb_iters);
1777         PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
1778                 (long)cx->sb_maxiters);
1779         PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1780                 (long)cx->sb_rflags);
1781         PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
1782                 (long)CxONCE(cx));
1783         PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
1784                 cx->sb_orig);
1785         PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n",
1786                 PTR2UV(cx->sb_dstr));
1787         PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n",
1788                 PTR2UV(cx->sb_targ));
1789         PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n",
1790                 PTR2UV(cx->sb_s));
1791         PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
1792                 PTR2UV(cx->sb_m));
1793         PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n",
1794                 PTR2UV(cx->sb_strend));
1795         PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n",
1796                 PTR2UV(cx->sb_rxres));
1797         break;
1798     }
1799 #else
1800     PERL_UNUSED_CONTEXT;
1801     PERL_UNUSED_ARG(cx);
1802 #endif  /* DEBUGGING */
1803 }
1804
1805 /*
1806  * ex: set ts=8 sts=4 sw=4 et:
1807  */