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