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 < SSize_t_MAX), but done in a way that
52 if (UNLIKELY( current > SSize_t_MAX - extra
53 || current + extra > SSize_t_MAX - n
55 /* diag_listed_as: Out of memory during %s extend */
56 Perl_croak(aTHX_ "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)
73 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
77 si->si_stack = newAV();
78 AvREAL_off(si->si_stack);
79 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
80 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
81 AvFILLp(si->si_stack) = 0;
84 si->si_cxmax = cxitems - 1;
87 si->si_type = PERLSI_UNDEF;
88 Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
89 /* Without any kind of initialising CX_PUSHSUBST()
90 * in pp_subst() will read uninitialised heap. */
91 PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT);
98 const IV old_max = cxstack_max;
99 const IV new_max = GROW(cxstack_max);
100 Renew(cxstack, new_max + 1, PERL_CONTEXT);
101 cxstack_max = new_max;
102 /* Without any kind of initialising deep enough recursion
103 * will end up reading uninitialised PERL_CONTEXTs. */
104 PoisonNew(cxstack + old_max + 1, new_max - old_max, PERL_CONTEXT);
105 return cxstack_ix + 1;
109 =for apidoc_section $callback
110 =for apidoc push_scope
112 Implements L<perlapi/C<ENTER>>
118 Perl_push_scope(pTHX)
120 if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
121 const IV new_max = GROW(PL_scopestack_max);
122 Renew(PL_scopestack, new_max, I32);
124 Renew(PL_scopestack_name, new_max, const char*);
126 PL_scopestack_max = new_max;
129 PL_scopestack_name[PL_scopestack_ix] = "unknown";
131 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
136 =for apidoc_section $callback
137 =for apidoc pop_scope
139 Implements L<perlapi/C<LEAVE>>
147 const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
148 LEAVE_SCOPE(oldsave);
152 Perl_markstack_grow(pTHX)
154 const I32 oldmax = PL_markstack_max - PL_markstack;
155 const I32 newmax = GROW(oldmax);
157 Renew(PL_markstack, newmax, I32);
158 PL_markstack_max = PL_markstack + newmax;
159 PL_markstack_ptr = PL_markstack + oldmax;
160 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
161 "MARK grow %p %" IVdf " by %" IVdf "\n",
162 PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax)));
163 return PL_markstack_ptr;
167 Perl_savestack_grow(pTHX)
169 const I32 by = PL_savestack_max - PL_savestack_ix;
170 Perl_savestack_grow_cnt(aTHX_ by);
174 Perl_savestack_grow_cnt(pTHX_ I32 need)
176 /* NOTE: PL_savestack_max and PL_savestack_ix are I32.
178 * This makes sense when you consider that having I32_MAX items on
179 * the stack would be quite large.
181 * However, we use IV here so that we can detect if the new requested
182 * amount is larger than I32_MAX.
184 const IV new_floor = PL_savestack_max + need; /* what we need */
185 /* the GROW() macro normally does scales by 1.5 but under
186 * STRESS_REALLOC it simply adds 1 */
187 IV new_max = GROW(new_floor); /* and some extra */
189 /* the new_max < PL_savestack_max is for cases where IV is I32
190 * and we have rolled over from I32_MAX to a small value */
191 if (new_max > I32_MAX || new_max < PL_savestack_max) {
192 if (new_floor > I32_MAX || new_floor < PL_savestack_max) {
193 Perl_croak(aTHX_ "panic: savestack overflows I32_MAX");
198 /* Note that we add an additional SS_MAXPUSH slots on top of
199 * PL_savestack_max so that SS_ADD_END(), SSGROW() etc can do
200 * a simper check and if necessary realloc *after* apparently
201 * overwriting the current PL_savestack_max. See scope.h.
203 * The +1 is because new_max/PL_savestack_max is the highest
204 * index, by Renew needs the number of items, which is one
205 * larger than the highest index. */
206 Renew(PL_savestack, new_max + SS_MAXPUSH + 1, ANY);
207 PL_savestack_max = new_max;
212 /* The original function was called Perl_tmps_grow and was removed from public
213 API, Perl_tmps_grow_p is the replacement and it used in public macros but
216 Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by,
217 where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max
218 Upon return, PL_tmps_stack[ix] will be a valid address. For machine code
219 optimization and register usage reasons, the proposed ix passed into
220 tmps_grow is returned to the caller which the caller can then use to write
221 an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in
222 pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of
223 tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller
224 must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is
225 appropriate. The assignment to PL_temps_ix can happen before or after
226 tmps_grow call since tmps_grow doesn't look at PL_tmps_ix.
230 Perl_tmps_grow_p(pTHX_ SSize_t ix)
232 SSize_t extend_to = ix;
233 #ifndef STRESS_REALLOC
234 if (ix - PL_tmps_max < 128)
235 extend_to += (PL_tmps_max < 512) ? 128 : 512;
237 Renew(PL_tmps_stack, extend_to + 1, SV*);
238 PL_tmps_max = extend_to + 1;
246 /* XXX should tmps_floor live in cxstack? */
247 const SSize_t myfloor = PL_tmps_floor;
248 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
249 SV* const sv = PL_tmps_stack[PL_tmps_ix--];
251 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
255 SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
261 =for apidoc save_scalar_at
263 A helper function for localizing the SV referenced by C<*sptr>.
265 If C<SAVEf_KEEPOLDELEM> is set in in C<flags>, the function returns the input
268 Otherwise it replaces C<*sptr> with a new C<undef> scalar, and returns that.
269 The new scalar will have the old one's magic (if any) copied to it.
270 If there is such magic, and C<SAVEf_SETMAGIC> is set in in C<flags>, 'set'
271 magic will be processed on the new scalar. If unset, 'set' magic will be
272 skipped. The latter typically means that assignment will soon follow (I<e.g.>,
273 S<C<'local $x = $y'>>), and that will handle the magic.
275 =for apidoc Amnh ||SAVEf_KEEPOLDELEM
276 =for apidoc Amnh ||SAVEf_SETMAGIC
282 S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
287 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
290 if (flags & SAVEf_KEEPOLDELEM)
293 sv = (*sptr = newSV_type(SVt_NULL));
294 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
295 mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
302 Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
312 Perl_save_scalar(pTHX_ GV *gv)
314 SV ** const sptr = &GvSVn(gv);
316 PERL_ARGS_ASSERT_SAVE_SCALAR;
318 if (UNLIKELY(SvGMAGICAL(*sptr))) {
323 save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
324 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
328 =for apidoc save_generic_svref
330 Implements C<SAVEGENERICSV>.
332 Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
333 restore a global SV to its prior contents, freeing new value.
339 Perl_save_generic_svref(pTHX_ SV **sptr)
341 PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
343 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
348 =for apidoc save_rcpv
350 Implements C<SAVERCPV>.
352 Saves and restores a refcounted string, similar to what
353 save_generic_svref would do for a SV*. Can be used to restore
354 a refcounted string to its previous state. Performs the
355 appropriate refcount counting so that nothing should leak
356 or be prematurely freed.
361 Perl_save_rcpv(pTHX_ char **prcpv) {
362 PERL_ARGS_ASSERT_SAVE_RCPV;
363 save_pushptrptr(prcpv, rcpv_copy(*prcpv), SAVEt_RCPV);
367 =for apidoc save_freercpv
369 Implements C<SAVEFREERCPV>.
371 Saves and frees a refcounted string. Calls rcpv_free()
372 on the argument when the current pseudo block is finished.
377 Perl_save_freercpv(pTHX_ char *rcpv) {
378 PERL_ARGS_ASSERT_SAVE_FREERCPV;
379 save_pushptr(rcpv, SAVEt_FREERCPV);
384 =for apidoc_section $callback
385 =for apidoc save_generic_pvref
387 Implements C<SAVEGENERICPV>.
389 Like save_pptr(), but also Safefree()s the new value if it is different
390 from the old one. Can be used to restore a global char* to its prior
391 contents, freeing new value.
397 Perl_save_generic_pvref(pTHX_ char **str)
399 PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
401 save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
405 =for apidoc_section $callback
406 =for apidoc save_shared_pvref
408 Implements C<SAVESHAREDPV>.
410 Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
411 Can be used to restore a shared global char* to its prior
412 contents, freeing new value.
418 Perl_save_shared_pvref(pTHX_ char **str)
420 PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
422 save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
427 =for apidoc_section $callback
428 =for apidoc save_set_svflags
430 Implements C<SAVESETSVFLAGS>.
432 Set the SvFLAGS specified by mask to the values in val
438 Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
442 PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
447 SS_ADD_UV(SAVEt_SET_SVFLAGS);
453 =for apidoc_section $GV
457 Saves the current GP of gv on the save stack to be restored on scope exit.
459 If C<empty> is true, replace the GP with a new GP.
461 If C<empty> is false, mark C<gv> with C<GVf_INTRO> so the next reference
462 assigned is localized, which is how S<C< local *foo = $someref; >> works.
468 Perl_save_gp(pTHX_ GV *gv, I32 empty)
470 PERL_ARGS_ASSERT_SAVE_GP;
472 /* XXX For now, we just upgrade any coderef in the stash to a full GV
473 during localisation. Maybe at some point we could make localis-
474 ation work without needing the upgrade. (In which case our
475 callers should probably call a different function, not save_gp.)
478 assert(isGV_or_RVCV(gv));
479 (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
483 save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
486 GP *gp = Perl_newGP(aTHX_ gv);
487 HV * const stash = GvSTASH(gv);
488 bool isa_changed = 0;
490 if (stash && HvHasENAME(stash)) {
491 if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
494 /* taking a method out of circulation ("local")*/
495 mro_method_changed_in(stash);
497 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
499 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
502 if (isa_changed) mro_isa_changed_in(stash);
511 Perl_save_ary(pTHX_ GV *gv)
513 AV * const oav = GvAVn(gv);
516 PERL_ARGS_ASSERT_SAVE_ARY;
518 if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
520 save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
524 if (UNLIKELY(SvMAGIC(oav)))
525 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
530 Perl_save_hash(pTHX_ GV *gv)
534 PERL_ARGS_ASSERT_SAVE_HASH;
537 SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
542 if (UNLIKELY(SvMAGIC(ohv)))
543 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
548 Perl_save_item(pTHX_ SV *item)
550 SV * const sv = newSVsv(item);
552 PERL_ARGS_ASSERT_SAVE_ITEM;
554 save_pushptrptr(item, /* remember the pointer */
555 sv, /* remember the value */
560 Perl_save_bool(pTHX_ bool *boolp)
564 PERL_ARGS_ASSERT_SAVE_BOOL;
567 SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
572 Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
583 Perl_save_int(pTHX_ int *intp)
586 UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
590 PERL_ARGS_ASSERT_SAVE_INT;
592 if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
603 Perl_save_I8(pTHX_ I8 *bytep)
607 PERL_ARGS_ASSERT_SAVE_I8;
610 SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
615 Perl_save_I16(pTHX_ I16 *intp)
619 PERL_ARGS_ASSERT_SAVE_I16;
622 SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
627 Perl_save_I32(pTHX_ I32 *intp)
630 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
634 PERL_ARGS_ASSERT_SAVE_I32;
636 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
647 Perl_save_strlen(pTHX_ STRLEN *ptr)
650 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL);
654 PERL_ARGS_ASSERT_SAVE_STRLEN;
656 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
668 Perl_save_iv(pTHX_ IV *ivp)
670 PERL_ARGS_ASSERT_SAVE_IV;
678 /* Cannot use save_sptr() to store a char* since the SV** cast will
679 * force word-alignment and we'll miss the pointer.
682 Perl_save_pptr(pTHX_ char **pptr)
684 PERL_ARGS_ASSERT_SAVE_PPTR;
686 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
690 =for apidoc_section $callback
691 =for apidoc save_vptr
693 Implements C<SAVEVPTR>.
699 Perl_save_vptr(pTHX_ void *ptr)
701 PERL_ARGS_ASSERT_SAVE_VPTR;
703 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
707 Perl_save_sptr(pTHX_ SV **sptr)
709 PERL_ARGS_ASSERT_SAVE_SPTR;
711 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
715 =for apidoc_section $callback
716 =for apidoc save_padsv_and_mortalize
718 Implements C<SAVEPADSVANDMORTALIZE>.
724 Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
728 ASSERT_CURPAD_ACTIVE("save_padsv");
729 SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
730 SS_ADD_PTR(PL_comppad);
732 SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
737 Perl_save_hptr(pTHX_ HV **hptr)
739 PERL_ARGS_ASSERT_SAVE_HPTR;
741 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
745 Perl_save_aptr(pTHX_ AV **aptr)
747 PERL_ARGS_ASSERT_SAVE_APTR;
749 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
753 =for apidoc_section $callback
754 =for apidoc save_pushptr
756 The refcnt of object C<ptr> will be decremented at the end of the current
757 I<pseudo-block>. C<type> gives the type of C<ptr>, expressed as one of the
758 constants in F<scope.h> whose name begins with C<SAVEt_>.
760 This is the underlying implementation of several macros, like
767 Perl_save_pushptr(pTHX_ void *const ptr, const int type)
776 Perl_save_clearsv(pTHX_ SV **svp)
778 const UV offset = svp - PL_curpad;
779 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
781 PERL_ARGS_ASSERT_SAVE_CLEARSV;
783 ASSERT_CURPAD_ACTIVE("save_clearsv");
785 SvPADSTALE_off(*svp); /* mark lexical as active */
786 if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
787 Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
788 offset, svp, PL_curpad);
793 SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
799 Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
801 PERL_ARGS_ASSERT_SAVE_DELETE;
803 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
807 =for apidoc_section $callback
808 =for apidoc save_hdelete
810 Implements C<SAVEHDELETE>.
816 Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
822 PERL_ARGS_ASSERT_SAVE_HDELETE;
824 key = SvPV_const(keysv, len);
825 klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
826 SvREFCNT_inc_simple_void_NN(hv);
827 save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
831 =for apidoc_section $callback
832 =for apidoc save_adelete
834 Implements C<SAVEADELETE>.
840 Perl_save_adelete(pTHX_ AV *av, SSize_t key)
844 PERL_ARGS_ASSERT_SAVE_ADELETE;
846 SvREFCNT_inc_void(av);
849 SS_ADD_IV(SAVEt_ADELETE);
854 Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
857 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
861 SS_ADD_UV(SAVEt_DESTRUCTOR);
866 Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
872 SS_ADD_UV(SAVEt_DESTRUCTOR_X);
877 =for apidoc_section $callback
878 =for apidoc save_hints
880 Implements C<SAVEHINTS>.
886 Perl_save_hints(pTHX)
888 COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
889 if (PL_hints & HINT_LOCALIZE_HH) {
890 HV *oldhh = GvHV(PL_hintgv);
893 SS_ADD_INT(PL_hints);
894 SS_ADD_PTR(save_cophh);
896 SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8));
899 GvHV(PL_hintgv) = NULL; /* in case copying dies */
900 GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
903 save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8));
908 S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
920 =for apidoc_section $callback
921 =for apidoc save_aelem
922 =for apidoc_item save_aelem_flags
924 These each arrange for the value of the array element C<av[idx]> to be restored
925 at the end of the enclosing I<pseudo-block>.
927 In C<save_aelem>, the SV at C**sptr> will be replaced by a new C<undef>
928 scalar. That scalar will inherit any magic from the original C<**sptr>,
929 and any 'set' magic will be processed.
931 In C<save_aelem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
932 the function to forgo all that: the scalar at C<**sptr> is untouched.
933 If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
934 new C<undef> scalar. That scalar will inherit any magic from the original
935 C<**sptr>. Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
936 is set in in C<flags>.
942 Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
948 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
951 SS_ADD_PTR(SvREFCNT_inc_simple(av));
953 SS_ADD_PTR(SvREFCNT_inc(*sptr));
954 SS_ADD_UV(SAVEt_AELEM);
956 /* The array needs to hold a reference count on its new element, so it
958 if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
960 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
961 if (flags & SAVEf_KEEPOLDELEM)
964 /* If we're localizing a tied array element, this new sv
965 * won't actually be stored in the array - so it won't get
966 * reaped when the localize ends. Ensure it gets reaped by
967 * mortifying it instead. DAPM */
968 if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
973 =for apidoc_section $callback
974 =for apidoc save_helem
975 =for apidoc_item save_helem_flags
977 These each arrange for the value of the hash element (in Perlish terms)
978 C<$hv{key}]> to be restored at the end of the enclosing I<pseudo-block>.
980 In C<save_helem>, the SV at C**sptr> will be replaced by a new C<undef>
981 scalar. That scalar will inherit any magic from the original C<**sptr>,
982 and any 'set' magic will be processed.
984 In C<save_helem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
985 the function to forgo all that: the scalar at C<**sptr> is untouched.
986 If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
987 new C<undef> scalar. That scalar will inherit any magic from the original
988 C<**sptr>. Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
989 is set in in C<flags>.
995 Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
999 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
1004 SS_ADD_PTR(SvREFCNT_inc_simple(hv));
1005 SS_ADD_PTR(newSVsv(key));
1006 SS_ADD_PTR(SvREFCNT_inc(*sptr));
1007 SS_ADD_UV(SAVEt_HELEM);
1010 save_scalar_at(sptr, flags);
1011 if (flags & SAVEf_KEEPOLDELEM)
1014 /* If we're localizing a tied hash element, this new sv
1015 * won't actually be stored in the hash - so it won't get
1016 * reaped when the localize ends. Ensure it gets reaped by
1017 * mortifying it instead. DAPM */
1018 if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
1023 Perl_save_svref(pTHX_ SV **sptr)
1025 PERL_ARGS_ASSERT_SAVE_SVREF;
1028 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
1029 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
1037 SS_ADD_IV(PL_tmps_floor);
1038 PL_tmps_floor = PL_tmps_ix;
1039 SS_ADD_UV(SAVEt_TMPSFLOOR);
1044 =for apidoc_section $stack
1045 =for apidoc save_alloc
1047 Implements L<perlapi/C<SSNEW>> and kin, which should be used instead of this
1054 Perl_save_alloc(pTHX_ SSize_t size, I32 pad)
1056 const SSize_t start = pad + ((char*)&PL_savestack[PL_savestack_ix]
1057 - (char*)PL_savestack);
1058 const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
1059 const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
1061 if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
1063 "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
1064 elems, (IV)size, (IV)pad);
1068 PL_savestack_ix += elems;
1069 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
1076 =for apidoc_section $callback
1077 =for apidoc leave_scope
1079 Implements C<LEAVE_SCOPE> which you should use instead.
1085 Perl_leave_scope(pTHX_ I32 base)
1087 /* Localise the effects of the TAINT_NOT inside the loop. */
1088 bool was = TAINT_get;
1090 if (UNLIKELY(base < -1))
1091 Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
1092 DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
1093 (long)PL_savestack_ix, (long)base));
1094 while (PL_savestack_ix > base) {
1097 ANY *ap; /* arg pointer */
1098 ANY a0, a1, a2; /* up to 3 args */
1104 I32 ix = PL_savestack_ix - 1;
1106 ap = &PL_savestack[ix];
1108 type = (U8)uv & SAVE_MASK;
1109 argcount = leave_scope_arg_counts[type];
1110 PL_savestack_ix = ix - argcount;
1115 case SAVEt_ITEM: /* normal string */
1116 a0 = ap[0]; a1 = ap[1];
1117 sv_replace(a0.any_sv, a1.any_sv);
1118 if (UNLIKELY(SvSMAGICAL(a0.any_sv))) {
1125 /* This would be a mathom, but Perl_save_svref() calls a static
1126 function, S_save_scalar_at(), so has to stay in this file. */
1127 case SAVEt_SVREF: /* scalar reference */
1128 a0 = ap[0]; a1 = ap[1];
1129 a2.any_svp = a0.any_svp;
1130 a0.any_sv = NULL; /* what to refcnt_dec */
1133 case SAVEt_SV: /* scalar reference */
1134 a0 = ap[0]; a1 = ap[1];
1135 a2.any_svp = &GvSV(a0.any_gv);
1138 /* do *a2.any_svp = a1 and free a0 */
1139 SV * const sv = *a2.any_svp;
1140 *a2.any_svp = a1.any_sv;
1142 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1143 /* mg_set could die, skipping the freeing of a0 and
1144 * a1; Ensure that they're always freed in that case */
1146 SS_ADD_PTR(a1.any_sv);
1147 SS_ADD_UV(SAVEt_FREESV);
1148 SS_ADD_PTR(a0.any_sv);
1149 SS_ADD_UV(SAVEt_FREESV);
1156 SvREFCNT_dec_NN(a1.any_sv);
1157 SvREFCNT_dec(a0.any_sv);
1161 case SAVEt_GENERIC_PVREF: /* generic pv */
1162 a0 = ap[0]; a1 = ap[1];
1163 if (*a1.any_pvp != a0.any_pv) {
1164 Safefree(*a1.any_pvp);
1165 *a1.any_pvp = a0.any_pv;
1169 case SAVEt_SHARED_PVREF: /* shared pv */
1170 a0 = ap[0]; a1 = ap[1];
1171 if (*a0.any_pvp != a1.any_pv) {
1172 PerlMemShared_free(*a0.any_pvp);
1173 *a0.any_pvp = a1.any_pv;
1177 case SAVEt_GVSV: /* scalar slot in GV */
1178 a0 = ap[0]; a1 = ap[1];
1179 a0.any_svp = &GvSV(a0.any_gv);
1183 case SAVEt_GENERIC_SVREF: /* generic sv */
1184 a0 = ap[0]; a1 = ap[1];
1187 /* do *a0.any_svp = a1 */
1188 SV * const sv = *a0.any_svp;
1189 *a0.any_svp = a1.any_sv;
1191 SvREFCNT_dec(a1.any_sv);
1195 case SAVEt_RCPV: /* like generic sv, but for struct rcpv */
1197 a0 = ap[0]; a1 = ap[1];
1198 char *old = *a0.any_pvp;
1199 *a0.any_pvp = a1.any_pv;
1200 (void)rcpv_free(old);
1201 (void)rcpv_free(a1.any_pv);
1205 case SAVEt_FREERCPV: /* like SAVEt_FREEPV but for a RCPV */
1208 char *rcpv = a0.any_pv;
1209 (void)rcpv_free(rcpv);
1213 case SAVEt_GVSLOT: /* any slot in GV */
1216 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1217 hv = GvSTASH(a0.any_gv);
1218 if (hv && HvHasENAME(hv) && (
1219 (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV)
1220 || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV)
1223 if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv)
1224 || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp)
1225 || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */
1226 PL_sub_generation++;
1227 else mro_method_changed_in(hv);
1229 a0.any_svp = a1.any_svp;
1230 a1.any_sv = a2.any_sv;
1234 case SAVEt_AV: /* array reference */
1235 a0 = ap[0]; a1 = ap[1];
1236 SvREFCNT_dec(GvAV(a0.any_gv));
1237 GvAV(a0.any_gv) = a1.any_av;
1239 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1240 /* mg_set might die, so make sure a0 isn't leaked */
1242 SS_ADD_PTR(a0.any_sv);
1243 SS_ADD_UV(SAVEt_FREESV);
1250 SvREFCNT_dec_NN(a0.any_sv);
1253 case SAVEt_HV: /* hash reference */
1254 a0 = ap[0]; a1 = ap[1];
1255 SvREFCNT_dec(GvHV(a0.any_gv));
1256 GvHV(a0.any_gv) = a1.any_hv;
1259 case SAVEt_INT_SMALL:
1261 *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
1264 case SAVEt_INT: /* int reference */
1265 a0 = ap[0]; a1 = ap[1];
1266 *(int*)a1.any_ptr = (int)a0.any_i32;
1269 case SAVEt_STRLEN_SMALL:
1271 *(STRLEN*)a0.any_ptr = (STRLEN)(uv >> SAVE_TIGHT_SHIFT);
1274 case SAVEt_STRLEN: /* STRLEN/size_t ref */
1275 a0 = ap[0]; a1 = ap[1];
1276 *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
1279 case SAVEt_TMPSFLOOR: /* restore PL_tmps_floor */
1281 PL_tmps_floor = (SSize_t)a0.any_iv;
1284 case SAVEt_BOOL: /* bool reference */
1286 *(bool*)a0.any_ptr = cBOOL(uv >> 8);
1287 #ifdef NO_TAINT_SUPPORT
1288 PERL_UNUSED_VAR(was);
1290 if (UNLIKELY(a0.any_ptr == &(PL_tainted))) {
1291 /* If we don't update <was>, to reflect what was saved on the
1292 * stack for PL_tainted, then we will overwrite this attempt to
1293 * restore it when we exit this routine. Note that this won't
1294 * work if this value was saved in a wider-than necessary type,
1296 was = *(bool*)a0.any_ptr;
1301 case SAVEt_I32_SMALL:
1303 *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
1306 case SAVEt_I32: /* I32 reference */
1307 a0 = ap[0]; a1 = ap[1];
1308 #ifdef PERL_DEBUG_READONLY_OPS
1309 if (*(I32*)a1.any_ptr != a0.any_i32)
1311 *(I32*)a1.any_ptr = a0.any_i32;
1314 case SAVEt_SPTR: /* SV* reference */
1315 case SAVEt_VPTR: /* random* reference */
1316 case SAVEt_PPTR: /* char* reference */
1317 case SAVEt_HPTR: /* HV* reference */
1318 case SAVEt_APTR: /* AV* reference */
1319 a0 = ap[0]; a1 = ap[1];
1320 *a1.any_svp= a0.any_sv;
1323 case SAVEt_GP: /* scalar reference */
1328 a0 = ap[0]; a1 = ap[1];
1329 /* possibly taking a method out of circulation */
1330 had_method = cBOOL(GvCVu(a0.any_gv));
1332 GvGP_set(a0.any_gv, (GP*)a1.any_ptr);
1333 if ((hv=GvSTASH(a0.any_gv)) && HvHasENAME(hv)) {
1334 if (memEQs(GvNAME(a0.any_gv), GvNAMELEN(a0.any_gv), "ISA"))
1335 mro_isa_changed_in(hv);
1336 else if (had_method || GvCVu(a0.any_gv))
1337 /* putting a method back into circulation ("local")*/
1338 gv_method_changed(a0.any_gv);
1340 SvREFCNT_dec_NN(a0.any_gv);
1346 SvREFCNT_dec(a0.any_sv);
1349 case SAVEt_FREEPADNAME:
1351 PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
1354 case SAVEt_FREECOPHH:
1356 cophh_free((COPHH *)a0.any_ptr);
1359 case SAVEt_MORTALIZESV:
1361 sv_2mortal(a0.any_sv);
1366 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
1372 Safefree(a0.any_ptr);
1375 case SAVEt_CLEARPADRANGE:
1379 i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
1380 svp = &PL_curpad[uv >>
1381 (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
1384 svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
1387 for (; i; i--, svp--) {
1390 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
1391 "Pad 0x%" UVxf "[0x%" UVxf "] clearsv: %ld sv=0x%" UVxf "<%" IVdf "> %s\n",
1392 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1393 (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
1394 (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
1397 /* Can clear pad variable in place? */
1398 if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) {
1400 /* these flags are the union of all the relevant flags
1401 * in the individual conditions within */
1402 if (UNLIKELY(SvFLAGS(sv) & (
1403 SVf_READONLY|SVf_PROTECT /*for SvREADONLY_off*/
1404 | (SVs_GMG|SVs_SMG|SVs_RMG) /* SvMAGICAL() */
1408 /* if a my variable that was made readonly is
1409 * going out of scope, we want to remove the
1410 * readonlyness so that it can go out of scope
1416 if (SvTYPE(sv) == SVt_PVHV && HvHasAUX(sv))
1417 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
1421 if (SvMAGICAL(sv)) {
1422 /* note that backrefs (either in HvAUX or magic)
1423 * must be removed before other magic */
1424 sv_unmagic(sv, PERL_MAGIC_backref);
1425 if (SvTYPE(sv) != SVt_PVCV)
1428 if (SvTHINKFIRST(sv))
1429 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
1433 switch (SvTYPE(sv)) {
1437 av_clear(MUTABLE_AV(sv));
1440 hv_clear(MUTABLE_HV(sv));
1444 HEK *hek = CvGvNAME_HEK(sv);
1446 (void)share_hek_hek(hek);
1448 CvNAME_HEK_set(sv, hek);
1453 /* This looks odd, but these two macros are for use in
1454 expressions and finish with a trailing comma, so
1455 adding a ; after them would be wrong. */
1458 SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8);
1462 SvPADSTALE_on(sv); /* mark as no longer live */
1464 else { /* Someone has a claim on this, so abandon it. */
1465 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
1466 case SVt_PVAV: *svp = MUTABLE_SV(newAV()); break;
1467 case SVt_PVHV: *svp = MUTABLE_SV(newHV()); break;
1470 HEK * const hek = CvGvNAME_HEK(sv);
1473 *svp = newSV_type(SVt_PVCV);
1476 CvNAME_HEK_set(*svp,
1477 share_hek_hek(hek));
1481 default: *svp = newSV_type(SVt_NULL); break;
1483 SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
1484 /* preserve pad nature, but also mark as not live
1485 * for any closure capturing */
1486 SvFLAGS(*svp) |= SVs_PADSTALE;
1493 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1494 /* hv_delete could die, so free the key and SvREFCNT_dec the
1495 * hv by pushing new save actions
1497 /* ap[0] is the key */
1498 ap[1].any_uv = SAVEt_FREEPV; /* was len */
1499 /* ap[2] is the hv */
1500 ap[3].any_uv = SAVEt_FREESV; /* was SAVEt_DELETE */
1501 PL_savestack_ix += 4;
1502 (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD);
1506 a0 = ap[0]; a1 = ap[1];
1507 /* av_delete could die, so SvREFCNT_dec the av by pushing a
1510 ap[0].any_av = a1.any_av;
1511 ap[1].any_uv = SAVEt_FREESV;
1512 PL_savestack_ix += 2;
1513 (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD);
1516 case SAVEt_DESTRUCTOR_X:
1517 a0 = ap[0]; a1 = ap[1];
1518 (*a0.any_dxptr)(aTHX_ a1.any_ptr);
1521 case SAVEt_REGCONTEXT:
1522 /* regexp must have croaked */
1524 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
1527 case SAVEt_STACK_POS: /* Position on Perl stack */
1529 PL_stack_sp = PL_stack_base + a0.any_i32;
1532 case SAVEt_AELEM: /* array element */
1535 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1536 svp = av_fetch(a0.any_av, a1.any_iv, 1);
1537 if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */
1538 SvREFCNT_dec(a2.any_sv);
1540 SV * const sv = *svp;
1541 if (LIKELY(sv && sv != &PL_sv_undef)) {
1542 if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied)))
1543 SvREFCNT_inc_void_NN(sv);
1544 a1.any_sv = a2.any_sv;
1549 SvREFCNT_dec(a0.any_av);
1550 SvREFCNT_dec(a2.any_sv);
1554 case SAVEt_HELEM: /* hash element */
1558 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1559 he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0);
1560 SvREFCNT_dec(a1.any_sv);
1562 const SV * const oval = HeVAL(he);
1563 if (LIKELY(oval && oval != &PL_sv_undef)) {
1564 SV **svp = &HeVAL(he);
1565 if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied)))
1566 SvREFCNT_inc_void(*svp);
1567 a1.any_sv = a2.any_sv;
1572 SvREFCNT_dec(a0.any_hv);
1573 SvREFCNT_dec(a2.any_sv);
1579 PL_op = (OP*)a0.any_ptr;
1582 case SAVEt_HINTS_HH:
1586 a0 = ap[0]; a1 = ap[1];
1587 if ((PL_hints & HINT_LOCALIZE_HH)) {
1588 while (GvHV(PL_hintgv)) {
1589 HV *hv = GvHV(PL_hintgv);
1590 GvHV(PL_hintgv) = NULL;
1591 SvREFCNT_dec(MUTABLE_SV(hv));
1594 cophh_free(CopHINTHASH_get(&PL_compiling));
1595 CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr);
1596 *(I32*)&PL_hints = a0.any_i32;
1597 PL_prevailing_version = (U16)(uv >> 8);
1598 if (type == SAVEt_HINTS_HH) {
1599 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
1600 GvHV(PL_hintgv) = MUTABLE_HV(a2.any_ptr);
1602 if (!GvHV(PL_hintgv)) {
1603 /* Need to add a new one manually, else rv2hv can
1604 add one via GvHVn and it won't have the magic set. */
1605 HV *const hv = newHV();
1606 hv_magic(hv, NULL, PERL_MAGIC_hints);
1607 GvHV(PL_hintgv) = hv;
1609 assert(GvHV(PL_hintgv));
1614 PL_comppad = (PAD*)a0.any_ptr;
1615 if (LIKELY(PL_comppad))
1616 PL_curpad = AvARRAY(PL_comppad);
1621 case SAVEt_PADSV_AND_MORTALIZE:
1625 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1626 assert (a1.any_ptr);
1627 svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv;
1628 /* This mortalizing used to be done by CX_POOPLOOP() via
1629 itersave. But as we have all the information here, we
1630 can do it here, save even having to have itersave in
1638 case SAVEt_SAVESWITCHSTACK:
1642 a0 = ap[0]; a1 = ap[1];
1643 SWITCHSTACK(a1.any_av, a0.any_av);
1644 PL_curstackinfo->si_stack = a0.any_av;
1648 case SAVEt_SET_SVFLAGS:
1649 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1650 SvFLAGS(a0.any_sv) &= ~(a1.any_u32);
1651 SvFLAGS(a0.any_sv) |= a2.any_u32;
1654 /* These are only saved in mathoms.c */
1657 (void)sv_clear(a0.any_sv);
1660 case SAVEt_LONG: /* long reference */
1661 a0 = ap[0]; a1 = ap[1];
1662 *(long*)a1.any_ptr = a0.any_long;
1665 case SAVEt_IV: /* IV reference */
1666 a0 = ap[0]; a1 = ap[1];
1667 *(IV*)a1.any_ptr = a0.any_iv;
1670 case SAVEt_I16: /* I16 reference */
1672 *(I16*)a0.any_ptr = (I16)(uv >> 8);
1675 case SAVEt_I8: /* I8 reference */
1677 *(I8*)a0.any_ptr = (I8)(uv >> 8);
1680 case SAVEt_DESTRUCTOR:
1681 a0 = ap[0]; a1 = ap[1];
1682 (*a0.any_dptr)(a1.any_ptr);
1685 case SAVEt_COMPILE_WARNINGS:
1686 /* NOTE: we can't put &PL_compiling or PL_curcop on the save
1687 * stack directly, as we currently cannot translate
1688 * them to the correct addresses after a thread start
1689 * or win32 fork start. - Yves
1692 free_and_set_cop_warnings(&PL_compiling, a0.any_pv);
1695 case SAVEt_CURCOP_WARNINGS:
1696 /* NOTE: see comment above about SAVEt_COMPILE_WARNINGS */
1698 free_and_set_cop_warnings(PL_curcop, a0.any_pv);
1703 parser_free((yy_parser *)a0.any_ptr);
1706 case SAVEt_READONLY_OFF:
1708 SvREADONLY_off(a0.any_sv);
1712 Perl_croak(aTHX_ "panic: leave_scope inconsistency %u",
1713 (U8)uv & SAVE_MASK);
1721 Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
1723 PERL_ARGS_ASSERT_CX_DUMP;
1726 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
1727 if (CxTYPE(cx) != CXt_SUBST) {
1728 const char *gimme_text;
1729 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
1730 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n",
1731 PTR2UV(cx->blk_oldcop));
1732 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1733 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
1734 PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
1735 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n",
1736 PTR2UV(cx->blk_oldpm));
1737 switch (cx->blk_gimme) {
1739 gimme_text = "VOID";
1742 gimme_text = "SCALAR";
1745 gimme_text = "LIST";
1748 gimme_text = "UNKNOWN";
1751 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
1753 switch (CxTYPE(cx)) {
1759 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n",
1760 PTR2UV(cx->blk_format.cv));
1761 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n",
1762 PTR2UV(cx->blk_format.gv));
1763 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n",
1764 PTR2UV(cx->blk_format.dfoutgv));
1765 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
1766 (int)CxHASARGS(cx));
1767 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n",
1768 PTR2UV(cx->blk_format.retop));
1771 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n",
1772 PTR2UV(cx->blk_sub.cv));
1773 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
1774 (long)cx->blk_sub.olddepth);
1775 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
1776 (int)CxHASARGS(cx));
1777 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
1778 PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n",
1779 PTR2UV(cx->blk_sub.retop));
1782 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
1783 (long)CxOLD_IN_EVAL(cx));
1784 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
1785 PL_op_name[CxOLD_OP_TYPE(cx)],
1786 PL_op_desc[CxOLD_OP_TYPE(cx)]);
1787 if (cx->blk_eval.old_namesv)
1788 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
1789 SvPVX_const(cx->blk_eval.old_namesv));
1790 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n",
1791 PTR2UV(cx->blk_eval.old_eval_root));
1792 PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n",
1793 PTR2UV(cx->blk_eval.retop));
1796 case CXt_LOOP_PLAIN:
1797 case CXt_LOOP_LAZYIV:
1798 case CXt_LOOP_LAZYSV:
1801 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
1802 PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n",
1803 PTR2UV(cx->blk_loop.my_op));
1804 if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
1805 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n",
1806 PTR2UV(CxITERVAR(cx)));
1807 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n",
1808 PTR2UV(cx->blk_loop.itersave));
1810 if (CxTYPE(cx) == CXt_LOOP_ARY) {
1811 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n",
1812 PTR2UV(cx->blk_loop.state_u.ary.ary));
1813 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1814 (long)cx->blk_loop.state_u.ary.ix);
1819 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
1820 (long)cx->sb_iters);
1821 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
1822 (long)cx->sb_maxiters);
1823 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1824 (long)cx->sb_rflags);
1825 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
1827 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
1829 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n",
1830 PTR2UV(cx->sb_dstr));
1831 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n",
1832 PTR2UV(cx->sb_targ));
1833 PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n",
1835 PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
1837 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n",
1838 PTR2UV(cx->sb_strend));
1839 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n",
1840 PTR2UV(cx->sb_rxres));
1844 PERL_UNUSED_CONTEXT;
1845 PERL_UNUSED_ARG(cx);
1846 #endif /* DEBUGGING */
1850 =for apidoc_section $callback
1851 =for apidoc mortal_destructor_sv
1853 This function arranges for either a Perl code reference, or a C function
1854 reference to be called at the B<end of the current statement>.
1856 The C<coderef> argument determines the type of function that will be
1857 called. If it is C<SvROK()> it is assumed to be a reference to a CV and
1858 will arrange for the coderef to be called. If it is not SvROK() then it
1859 is assumed to be a C<SvIV()> which is C<SvIOK()> whose value is a pointer
1860 to a C function of type C<DESTRUCTORFUNC_t> created using C<PTR2INT()>.
1861 Either way the C<args> parameter will be provided to the callback as a
1862 parameter, although the rules for doing so differ between the Perl and
1863 C mode. Normally this function is only used directly for the Perl case
1864 and the wrapper C<mortal_destructor_x()> is used for the C function case.
1866 When operating in Perl callback mode the C<args> parameter may be NULL
1867 in which case the code reference is called with no arguments, otherwise
1868 if it is an AV (SvTYPE(args) == SVt_PVAV) then the contents of the AV
1869 will be used as the arguments to the code reference, and if it is any
1870 other type then the C<args> SV will be provided as a single argument to
1873 When operating in a C callback mode the C<args> parameter will be passed
1874 directly to the C function as a C<void *> pointer. No additional
1875 processing of the argument will be peformed, and it is the callers
1876 responsibility to free the C<args> parameter if necessary.
1878 Be aware that there is a signficant difference in timing between the
1879 I<end of the current statement> and the I<end of the current pseudo
1880 block>. If you are looking for a mechanism to trigger a function at the
1881 end of the B<current pseudo block> you should look at
1882 C<SAVEDESTRUCTORX()> instead of this function.
1884 =for apidoc mortal_svfunc_x
1886 This function arranges for a C function reference to be called at the
1887 B<end of the current statement> with the arguments provided. It is a
1888 wrapper around C<mortal_destructor_sv()> which ensures that the latter
1889 function is called appropriately.
1891 Be aware that there is a signficant difference in timing between the
1892 I<end of the current statement> and the I<end of the current pseudo
1893 block>. If you are looking for a mechanism to trigger a function at the
1894 end of the B<current pseudo block> you should look at
1895 C<SAVEDESTRUCTORX()> instead of this function.
1897 =for apidoc magic_freedestruct
1899 This function is called via magic to implement the
1900 C<mortal_destructor_sv()> and C<mortal_destructor_x()> functions. It
1901 should not be called directly and has no user servicable parts.
1907 Perl_mortal_destructor_sv(pTHX_ SV *coderef, SV *args) {
1908 PERL_ARGS_ASSERT_MORTAL_DESTRUCTOR_SV;
1910 (SvROK(coderef) && SvTYPE(SvRV(coderef)) == SVt_PVCV) /* perl coderef */
1912 (SvIOK(coderef) && !SvROK(coderef)) /* C function ref */
1914 SV *variable = newSV_type_mortal(SVt_IV);
1915 (void)sv_magicext(variable, coderef, PERL_MAGIC_destruct,
1916 &PL_vtbl_destruct, (char *)args, args ? HEf_SVKEY : 0);
1921 Perl_mortal_svfunc_x(pTHX_ SVFUNC_t f, SV *sv) {
1922 PERL_ARGS_ASSERT_MORTAL_SVFUNC_X;
1923 SV *sviv = newSViv(PTR2IV(f));
1924 mortal_destructor_sv(sviv,sv);
1929 Perl_magic_freedestruct(pTHX_ SV* sv, MAGIC* mg) {
1930 PERL_ARGS_ASSERT_MAGIC_FREEDESTRUCT;
1940 if (PL_phase == PERL_PHASE_DESTRUCT) {
1941 Perl_warn(aTHX_ "Can't call destructor for 0x%p in global destruction\n", sv);
1945 args_any.pv = mg->mg_ptr;
1946 coderef = mg->mg_obj;
1948 /* Deal with C function destructor */
1949 if (SvTYPE(coderef) == SVt_IV && !SvROK(coderef)) {
1950 SVFUNC_t f = INT2PTR(SVFUNC_t, SvIV(coderef));
1951 (f)(aTHX_ args_any.sv);
1956 if (SvTYPE(args_any.sv) == SVt_PVAV) {
1957 nargs = av_len(args_any.av) + 1;
1962 PUSHSTACKi(PERLSI_MAGIC);
1963 ENTER_with_name("call_freedestruct");
1968 if (SvTYPE(args_any.sv) == SVt_PVAV) {
1970 for (n = 0 ; n < nargs ; n++ ) {
1971 SV **argp = av_fetch(args_any.av, n, 0);
1980 (void)call_sv(coderef, G_VOID | G_EVAL | G_KEEPERR);
1982 LEAVE_with_name("call_freedestruct");
1989 * ex: set ts=8 sts=4 sw=4 et: