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
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.
12 * For the fashion of Minas Tirith was such that it was built on seven
15 * [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"]
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
26 #define PERL_IN_SCOPE_C
31 Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
34 SSize_t current = (p - PL_stack_base);
36 PERL_ARGS_ASSERT_STACK_GROW;
40 "panic: stack_grow() negative count (%" IVdf ")", (IV)n);
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
52 if (UNLIKELY( current > Stack_off_t_MAX - extra
53 || current + extra > Stack_off_t_MAX - n
55 /* diag_listed_as: Out of memory during %s extend */
56 croak("Out of memory during stack extend");
58 av_extend(PL_curstack, current + n + extra);
60 PL_curstackinfo->si_stack_hwm = current + n + extra;
67 #define GROW(old) ((old) + 1)
69 #define GROW(old) ((old) * 3 / 2)
74 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
76 return new_stackinfo_flags(stitems, cxitems, 0);
79 /* current flag meanings:
80 * 1 make the new arg stack AvREAL
84 Perl_new_stackinfo_flags(pTHX_ I32 stitems, I32 cxitems, UV flags)
88 si->si_stack = newAV();
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;
95 si->si_stack_nonrc_base = 0;
99 si->si_cxmax = cxitems - 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);
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;
124 =for apidoc_section $callback
125 =for apidoc push_scope
127 Implements L<perlapi/C<ENTER>>
133 Perl_push_scope(pTHX)
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);
139 Renew(PL_scopestack_name, new_max, const char*);
141 PL_scopestack_max = new_max;
144 PL_scopestack_name[PL_scopestack_ix] = "unknown";
146 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
151 =for apidoc_section $callback
152 =for apidoc pop_scope
154 Implements L<perlapi/C<LEAVE>>
162 const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
163 LEAVE_SCOPE(oldsave);
167 Perl_markstack_grow(pTHX)
169 const I32 oldmax = PL_markstack_max - PL_markstack;
170 const I32 newmax = GROW(oldmax);
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;
182 Perl_savestack_grow(pTHX)
184 const I32 by = PL_savestack_max - PL_savestack_ix;
185 Perl_savestack_grow_cnt(aTHX_ by);
189 Perl_savestack_grow_cnt(pTHX_ I32 need)
191 /* NOTE: PL_savestack_max and PL_savestack_ix are I32.
193 * This makes sense when you consider that having I32_MAX items on
194 * the stack would be quite large.
196 * However, we use IV here so that we can detect if the new requested
197 * amount is larger than I32_MAX.
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 */
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");
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.
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;
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
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.
245 Perl_tmps_grow_p(pTHX_ SSize_t ix)
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;
254 extend_to += grow_size;
256 Renew(PL_tmps_stack, extend_to + 1, SV*);
257 PL_tmps_max = extend_to + 1;
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--];
270 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
274 SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
280 =for apidoc save_scalar_at
282 A helper function for localizing the SV referenced by C<*sptr>.
284 If C<SAVEf_KEEPOLDELEM> is set in in C<flags>, the function returns the input
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.
294 =for apidoc Amnh ||SAVEf_KEEPOLDELEM
295 =for apidoc Amnh ||SAVEf_SETMAGIC
301 S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
306 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
309 if (flags & SAVEf_KEEPOLDELEM)
312 sv = (*sptr = newSV_type(SVt_NULL));
313 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
314 mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
321 Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
331 Perl_save_scalar(pTHX_ GV *gv)
333 SV ** const sptr = &GvSVn(gv);
335 PERL_ARGS_ASSERT_SAVE_SCALAR;
337 if (UNLIKELY(SvGMAGICAL(*sptr))) {
342 save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
343 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
347 =for apidoc save_generic_svref
349 Implements C<SAVEGENERICSV>.
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.
358 Perl_save_generic_svref(pTHX_ SV **sptr)
360 PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
362 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
367 =for apidoc save_rcpv
369 Implements C<SAVERCPV>.
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.
380 Perl_save_rcpv(pTHX_ char **prcpv) {
381 PERL_ARGS_ASSERT_SAVE_RCPV;
382 save_pushptrptr(prcpv, rcpv_copy(*prcpv), SAVEt_RCPV);
386 =for apidoc save_freercpv
388 Implements C<SAVEFREERCPV>.
390 Saves and frees a refcounted string. Calls rcpv_free()
391 on the argument when the current pseudo block is finished.
396 Perl_save_freercpv(pTHX_ char *rcpv) {
397 PERL_ARGS_ASSERT_SAVE_FREERCPV;
398 save_pushptr(rcpv, SAVEt_FREERCPV);
403 =for apidoc_section $callback
404 =for apidoc save_generic_pvref
406 Implements C<SAVEGENERICPV>.
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.
416 Perl_save_generic_pvref(pTHX_ char **str)
418 PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
420 save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
424 =for apidoc_section $callback
425 =for apidoc save_shared_pvref
427 Implements C<SAVESHAREDPV>.
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.
437 Perl_save_shared_pvref(pTHX_ char **str)
439 PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
441 save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
446 =for apidoc_section $callback
447 =for apidoc save_set_svflags
449 Implements C<SAVESETSVFLAGS>.
451 Set the SvFLAGS specified by mask to the values in val
457 Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
461 PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
466 SS_ADD_UV(SAVEt_SET_SVFLAGS);
472 =for apidoc_section $GV
476 Saves the current GP of gv on the save stack to be restored on scope exit.
478 If C<empty> is true, replace the GP with a new GP.
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.
487 Perl_save_gp(pTHX_ GV *gv, I32 empty)
489 PERL_ARGS_ASSERT_SAVE_GP;
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.)
497 assert(isGV_or_RVCV(gv));
498 (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
502 save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
505 GP *gp = Perl_newGP(aTHX_ gv);
506 HV * const stash = GvSTASH(gv);
507 bool isa_changed = 0;
509 if (stash && HvHasENAME(stash)) {
510 if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
513 /* taking a method out of circulation ("local")*/
514 mro_method_changed_in(stash);
516 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
518 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
521 if (isa_changed) mro_isa_changed_in(stash);
530 Perl_save_ary(pTHX_ GV *gv)
532 AV * const oav = GvAVn(gv);
535 PERL_ARGS_ASSERT_SAVE_ARY;
537 if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
539 save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
543 if (UNLIKELY(SvMAGIC(oav)))
544 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
549 Perl_save_hash(pTHX_ GV *gv)
553 PERL_ARGS_ASSERT_SAVE_HASH;
556 SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
561 if (UNLIKELY(SvMAGIC(ohv)))
562 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
567 Perl_save_item(pTHX_ SV *item)
569 SV * const sv = newSVsv(item);
571 PERL_ARGS_ASSERT_SAVE_ITEM;
573 save_pushptrptr(item, /* remember the pointer */
574 sv, /* remember the value */
579 Perl_save_bool(pTHX_ bool *boolp)
583 PERL_ARGS_ASSERT_SAVE_BOOL;
586 SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
591 Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
602 Perl_save_int(pTHX_ int *intp)
605 UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
609 PERL_ARGS_ASSERT_SAVE_INT;
611 if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
622 Perl_save_I8(pTHX_ I8 *bytep)
626 PERL_ARGS_ASSERT_SAVE_I8;
629 SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
634 Perl_save_I16(pTHX_ I16 *intp)
638 PERL_ARGS_ASSERT_SAVE_I16;
641 SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
646 Perl_save_I32(pTHX_ I32 *intp)
649 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
653 PERL_ARGS_ASSERT_SAVE_I32;
655 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
666 Perl_save_strlen(pTHX_ STRLEN *ptr)
669 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL);
673 PERL_ARGS_ASSERT_SAVE_STRLEN;
675 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
687 Perl_save_iv(pTHX_ IV *ivp)
689 PERL_ARGS_ASSERT_SAVE_IV;
697 /* Cannot use save_sptr() to store a char* since the SV** cast will
698 * force word-alignment and we'll miss the pointer.
701 Perl_save_pptr(pTHX_ char **pptr)
703 PERL_ARGS_ASSERT_SAVE_PPTR;
705 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
709 =for apidoc_section $callback
710 =for apidoc save_vptr
712 Implements C<SAVEVPTR>.
718 Perl_save_vptr(pTHX_ void *ptr)
720 PERL_ARGS_ASSERT_SAVE_VPTR;
722 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
726 Perl_save_sptr(pTHX_ SV **sptr)
728 PERL_ARGS_ASSERT_SAVE_SPTR;
730 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
734 =for apidoc_section $callback
735 =for apidoc save_padsv_and_mortalize
737 Implements C<SAVEPADSVANDMORTALIZE>.
743 Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
747 ASSERT_CURPAD_ACTIVE("save_padsv");
748 SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
749 SS_ADD_PTR(PL_comppad);
751 SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
756 Perl_save_hptr(pTHX_ HV **hptr)
758 PERL_ARGS_ASSERT_SAVE_HPTR;
760 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
764 Perl_save_aptr(pTHX_ AV **aptr)
766 PERL_ARGS_ASSERT_SAVE_APTR;
768 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
772 =for apidoc_section $callback
773 =for apidoc save_pushptr
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_>.
779 This is the underlying implementation of several macros, like
786 Perl_save_pushptr(pTHX_ void *const ptr, const int type)
795 Perl_save_clearsv(pTHX_ SV **svp)
797 const UV offset = svp - PL_curpad;
798 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
800 PERL_ARGS_ASSERT_SAVE_CLEARSV;
802 ASSERT_CURPAD_ACTIVE("save_clearsv");
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);
812 SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
818 Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
820 PERL_ARGS_ASSERT_SAVE_DELETE;
822 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
826 =for apidoc_section $callback
827 =for apidoc save_hdelete
829 Implements C<SAVEHDELETE>.
835 Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
841 PERL_ARGS_ASSERT_SAVE_HDELETE;
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);
850 =for apidoc_section $callback
851 =for apidoc save_adelete
853 Implements C<SAVEADELETE>.
859 Perl_save_adelete(pTHX_ AV *av, SSize_t key)
863 PERL_ARGS_ASSERT_SAVE_ADELETE;
865 SvREFCNT_inc_void(av);
868 SS_ADD_IV(SAVEt_ADELETE);
873 Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
876 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
880 SS_ADD_UV(SAVEt_DESTRUCTOR);
885 Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
891 SS_ADD_UV(SAVEt_DESTRUCTOR_X);
896 =for apidoc_section $callback
897 =for apidoc save_hints
899 Implements C<SAVEHINTS>.
905 Perl_save_hints(pTHX)
907 COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
908 if (PL_hints & HINT_LOCALIZE_HH) {
909 HV *oldhh = GvHV(PL_hintgv);
912 SS_ADD_INT(PL_hints);
913 SS_ADD_PTR(save_cophh);
915 SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8));
918 GvHV(PL_hintgv) = NULL; /* in case copying dies */
919 GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
922 save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8));
927 S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
939 =for apidoc_section $callback
940 =for apidoc save_aelem
941 =for apidoc_item save_aelem_flags
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>.
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.
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>.
961 Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
967 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
970 SS_ADD_PTR(SvREFCNT_inc_simple(av));
972 SS_ADD_PTR(SvREFCNT_inc(*sptr));
973 SS_ADD_UV(SAVEt_AELEM);
975 /* The array needs to hold a reference count on its new element, so it
977 if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
979 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
980 if (flags & SAVEf_KEEPOLDELEM)
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)))
992 =for apidoc_section $callback
993 =for apidoc save_helem
994 =for apidoc_item save_helem_flags
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>.
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.
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>.
1014 Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
1018 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
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);
1029 save_scalar_at(sptr, flags);
1030 if (flags & SAVEf_KEEPOLDELEM)
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)))
1042 Perl_save_svref(pTHX_ SV **sptr)
1044 PERL_ARGS_ASSERT_SAVE_SVREF;
1047 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
1048 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
1056 SS_ADD_IV(PL_tmps_floor);
1057 PL_tmps_floor = PL_tmps_ix;
1058 SS_ADD_UV(SAVEt_TMPSFLOOR);
1063 =for apidoc_section $stack
1064 =for apidoc save_alloc
1066 Implements L<perlapi/C<SSNEW>> and kin, which should be used instead of this
1073 Perl_save_alloc(pTHX_ SSize_t size, I32 pad)
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;
1080 if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
1082 "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
1083 elems, (IV)size, (IV)pad);
1087 PL_savestack_ix += elems;
1088 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
1095 =for apidoc_section $callback
1096 =for apidoc leave_scope
1098 Implements C<LEAVE_SCOPE> which you should use instead.
1104 Perl_leave_scope(pTHX_ I32 base)
1106 /* Localise the effects of the TAINT_NOT inside the loop. */
1107 bool was = TAINT_get;
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) {
1116 ANY *ap; /* arg pointer */
1117 ANY a0, a1, a2; /* up to 3 args */
1123 I32 ix = PL_savestack_ix - 1;
1125 ap = &PL_savestack[ix];
1127 type = (U8)uv & SAVE_MASK;
1128 argcount = leave_scope_arg_counts[type];
1129 PL_savestack_ix = ix - argcount;
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))) {
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 */
1152 case SAVEt_SV: /* scalar reference */
1153 a0 = ap[0]; a1 = ap[1];
1154 a2.any_svp = &GvSV(a0.any_gv);
1157 /* do *a2.any_svp = a1 and free a0 */
1158 SV * const sv = *a2.any_svp;
1159 *a2.any_svp = a1.any_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 */
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);
1175 SvREFCNT_dec_NN(a1.any_sv);
1176 SvREFCNT_dec(a0.any_sv);
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;
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;
1196 case SAVEt_GVSV: /* scalar slot in GV */
1197 a0 = ap[0]; a1 = ap[1];
1198 a0.any_svp = &GvSV(a0.any_gv);
1202 case SAVEt_GENERIC_SVREF: /* generic sv */
1203 a0 = ap[0]; a1 = ap[1];
1206 /* do *a0.any_svp = a1 */
1207 SV * const sv = *a0.any_svp;
1208 *a0.any_svp = a1.any_sv;
1210 SvREFCNT_dec(a1.any_sv);
1214 case SAVEt_RCPV: /* like generic sv, but for struct rcpv */
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);
1224 case SAVEt_FREERCPV: /* like SAVEt_FREEPV but for a RCPV */
1227 char *rcpv = a0.any_pv;
1228 (void)rcpv_free(rcpv);
1232 case SAVEt_GVSLOT: /* any slot in GV */
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)
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);
1248 a0.any_svp = a1.any_svp;
1249 a1.any_sv = a2.any_sv;
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;
1258 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1259 /* mg_set might die, so make sure a0 isn't leaked */
1261 SS_ADD_PTR(a0.any_sv);
1262 SS_ADD_UV(SAVEt_FREESV);
1269 SvREFCNT_dec_NN(a0.any_sv);
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;
1278 case SAVEt_INT_SMALL:
1280 *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
1283 case SAVEt_INT: /* int reference */
1284 a0 = ap[0]; a1 = ap[1];
1285 *(int*)a1.any_ptr = (int)a0.any_i32;
1288 case SAVEt_STRLEN_SMALL:
1290 *(STRLEN*)a0.any_ptr = (STRLEN)(uv >> SAVE_TIGHT_SHIFT);
1293 case SAVEt_STRLEN: /* STRLEN/size_t ref */
1294 a0 = ap[0]; a1 = ap[1];
1295 *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
1298 case SAVEt_TMPSFLOOR: /* restore PL_tmps_floor */
1300 PL_tmps_floor = (SSize_t)a0.any_iv;
1303 case SAVEt_BOOL: /* bool reference */
1305 *(bool*)a0.any_ptr = cBOOL(uv >> 8);
1306 #ifdef NO_TAINT_SUPPORT
1307 PERL_UNUSED_VAR(was);
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,
1315 was = *(bool*)a0.any_ptr;
1320 case SAVEt_I32_SMALL:
1322 *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
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)
1330 *(I32*)a1.any_ptr = a0.any_i32;
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;
1342 case SAVEt_GP: /* scalar reference */
1347 a0 = ap[0]; a1 = ap[1];
1348 /* possibly taking a method out of circulation */
1349 had_method = cBOOL(GvCVu(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);
1359 SvREFCNT_dec_NN(a0.any_gv);
1365 SvREFCNT_dec(a0.any_sv);
1368 case SAVEt_FREEPADNAME:
1370 PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
1373 case SAVEt_FREECOPHH:
1375 cophh_free((COPHH *)a0.any_ptr);
1378 case SAVEt_MORTALIZESV:
1380 sv_2mortal(a0.any_sv);
1385 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
1391 Safefree(a0.any_ptr);
1394 case SAVEt_FREE_REXC_STATE:
1397 release_RExC_state(a0.any_ptr);
1400 case SAVEt_CLEARPADRANGE:
1404 i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
1405 svp = &PL_curpad[uv >>
1406 (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
1409 svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
1412 for (; i; i--, svp--) {
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"
1422 /* Can clear pad variable in place? */
1423 if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) {
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() */
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
1441 if (SvTYPE(sv) == SVt_PVHV && HvHasAUX(sv))
1442 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
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)
1453 if (SvTHINKFIRST(sv))
1454 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
1458 switch (SvTYPE(sv)) {
1462 av_clear(MUTABLE_AV(sv));
1465 hv_clear(MUTABLE_HV(sv));
1469 HEK *hek = CvGvNAME_HEK(sv);
1471 (void)share_hek_hek(hek);
1473 CvNAME_HEK_set(sv, hek);
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. */
1483 SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8);
1487 SvPADSTALE_on(sv); /* mark as no longer live */
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;
1495 HEK * const hek = CvGvNAME_HEK(sv);
1498 *svp = newSV_type(SVt_PVCV);
1501 CvNAME_HEK_set(*svp,
1502 share_hek_hek(hek));
1506 default: *svp = newSV_type(SVt_NULL); break;
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;
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
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);
1531 a0 = ap[0]; a1 = ap[1];
1532 /* av_delete could die, so SvREFCNT_dec the av by pushing a
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);
1541 case SAVEt_DESTRUCTOR_X:
1542 a0 = ap[0]; a1 = ap[1];
1543 (*a0.any_dxptr)(aTHX_ a1.any_ptr);
1546 case SAVEt_REGCONTEXT:
1547 /* regexp must have croaked */
1549 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
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.
1562 PL_stack_sp = PL_stack_base + a0.any_i32;
1565 case SAVEt_AELEM: /* array element */
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);
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;
1582 SvREFCNT_dec(a0.any_av);
1583 SvREFCNT_dec(a2.any_sv);
1587 case SAVEt_HELEM: /* hash element */
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);
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;
1605 SvREFCNT_dec(a0.any_hv);
1606 SvREFCNT_dec(a2.any_sv);
1612 PL_op = (OP*)a0.any_ptr;
1615 case SAVEt_HINTS_HH:
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));
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);
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;
1642 assert(GvHV(PL_hintgv));
1647 PL_comppad = (PAD*)a0.any_ptr;
1648 if (LIKELY(PL_comppad))
1649 PL_curpad = AvARRAY(PL_comppad);
1654 case SAVEt_PADSV_AND_MORTALIZE:
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
1671 case SAVEt_SAVESWITCHSTACK:
1675 a0 = ap[0]; a1 = ap[1];
1676 SWITCHSTACK(a1.any_av, a0.any_av);
1677 PL_curstackinfo->si_stack = a0.any_av;
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;
1687 /* These are only saved in mathoms.c */
1690 (void)sv_clear(a0.any_sv);
1693 case SAVEt_IV: /* IV reference */
1694 a0 = ap[0]; a1 = ap[1];
1695 *(IV*)a1.any_ptr = a0.any_iv;
1698 case SAVEt_I16: /* I16 reference */
1700 *(I16*)a0.any_ptr = (I16)(uv >> 8);
1703 case SAVEt_I8: /* I8 reference */
1705 *(I8*)a0.any_ptr = (I8)(uv >> 8);
1708 case SAVEt_DESTRUCTOR:
1709 a0 = ap[0]; a1 = ap[1];
1710 (*a0.any_dptr)(a1.any_ptr);
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
1720 free_and_set_cop_warnings(&PL_compiling, a0.any_pv);
1723 case SAVEt_CURCOP_WARNINGS:
1724 /* NOTE: see comment above about SAVEt_COMPILE_WARNINGS */
1726 free_and_set_cop_warnings(PL_curcop, a0.any_pv);
1731 parser_free((yy_parser *)a0.any_ptr);
1734 case SAVEt_READONLY_OFF:
1736 SvREADONLY_off(a0.any_sv);
1740 croak("panic: leave_scope inconsistency %u",
1741 (U8)uv & SAVE_MASK);
1749 Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
1751 PERL_ARGS_ASSERT_CX_DUMP;
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) {
1767 gimme_text = "VOID";
1770 gimme_text = "SCALAR";
1773 gimme_text = "LIST";
1776 gimme_text = "UNKNOWN";
1779 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
1781 switch (CxTYPE(cx)) {
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));
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));
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));
1824 case CXt_LOOP_PLAIN:
1825 case CXt_LOOP_LAZYIV:
1826 case CXt_LOOP_LAZYSV:
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));
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);
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",
1855 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
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",
1863 PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
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));
1872 PERL_UNUSED_CONTEXT;
1873 PERL_UNUSED_ARG(cx);
1874 #endif /* DEBUGGING */
1878 =for apidoc_section $callback
1879 =for apidoc mortal_destructor_sv
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>.
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.
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
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.
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.
1912 =for apidoc mortal_svfunc_x
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.
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.
1925 =for apidoc magic_freedestruct
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.
1935 Perl_mortal_destructor_sv(pTHX_ SV *coderef, SV *args) {
1936 PERL_ARGS_ASSERT_MORTAL_DESTRUCTOR_SV;
1938 (SvROK(coderef) && SvTYPE(SvRV(coderef)) == SVt_PVCV) /* perl coderef */
1940 (SvIOK(coderef) && !SvROK(coderef)) /* C function ref */
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);
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);
1957 Perl_magic_freedestruct(pTHX_ SV* sv, MAGIC* mg) {
1958 PERL_ARGS_ASSERT_MAGIC_FREEDESTRUCT;
1968 if (PL_phase == PERL_PHASE_DESTRUCT) {
1969 warn("Can't call destructor for 0x%p in global destruction\n", sv);
1973 args_any.pv = mg->mg_ptr;
1974 coderef = mg->mg_obj;
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);
1984 if (SvTYPE(args_any.sv) == SVt_PVAV) {
1985 nargs = av_len(args_any.av) + 1;
1990 PUSHSTACKi(PERLSI_MAGIC);
1991 ENTER_with_name("call_freedestruct");
1996 if (SvTYPE(args_any.sv) == SVt_PVAV) {
1998 for (n = 0 ; n < nargs ; n++ ) {
1999 SV **argp = av_fetch(args_any.av, n, 0);
2008 (void)call_sv(coderef, G_VOID | G_EVAL | G_KEEPERR);
2010 LEAVE_with_name("call_freedestruct");
2017 * ex: set ts=8 sts=4 sw=4 et: