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;
66 #ifndef STRESS_REALLOC
67 #define GROW(old) ((old) * 3 / 2)
69 #define GROW(old) ((old) + 1)
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 Perl_push_scope(pTHX)
111 if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
112 const IV new_max = GROW(PL_scopestack_max);
113 Renew(PL_scopestack, new_max, I32);
115 Renew(PL_scopestack_name, new_max, const char*);
117 PL_scopestack_max = new_max;
120 PL_scopestack_name[PL_scopestack_ix] = "unknown";
122 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
129 const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
130 LEAVE_SCOPE(oldsave);
134 Perl_markstack_grow(pTHX)
136 const I32 oldmax = PL_markstack_max - PL_markstack;
137 const I32 newmax = GROW(oldmax);
139 Renew(PL_markstack, newmax, I32);
140 PL_markstack_max = PL_markstack + newmax;
141 PL_markstack_ptr = PL_markstack + oldmax;
142 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
143 "MARK grow %p %" IVdf " by %" IVdf "\n",
144 PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax)));
145 return PL_markstack_ptr;
149 Perl_savestack_grow(pTHX)
152 #ifdef STRESS_REALLOC
153 new_max = PL_savestack_max + SS_MAXPUSH;
155 new_max = GROW(PL_savestack_max);
157 /* Note that we allocate SS_MAXPUSH slots higher than ss_max
158 * so that SS_ADD_END(), SSGROW() etc can do a simper check */
159 Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
160 PL_savestack_max = new_max;
164 Perl_savestack_grow_cnt(pTHX_ I32 need)
166 const IV new_max = PL_savestack_ix + need;
167 /* Note that we allocate SS_MAXPUSH slots higher than ss_max
168 * so that SS_ADD_END(), SSGROW() etc can do a simper check */
169 Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
170 PL_savestack_max = new_max;
175 /* The original function was called Perl_tmps_grow and was removed from public
176 API, Perl_tmps_grow_p is the replacement and it used in public macros but
179 Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by,
180 where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max
181 Upon return, PL_tmps_stack[ix] will be a valid address. For machine code
182 optimization and register usage reasons, the proposed ix passed into
183 tmps_grow is returned to the caller which the caller can then use to write
184 an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in
185 pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of
186 tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller
187 must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is
188 appropriate. The assignment to PL_temps_ix can happen before or after
189 tmps_grow call since tmps_grow doesn't look at PL_tmps_ix.
193 Perl_tmps_grow_p(pTHX_ SSize_t ix)
195 SSize_t extend_to = ix;
196 #ifndef STRESS_REALLOC
197 if (ix - PL_tmps_max < 128)
198 extend_to += (PL_tmps_max < 512) ? 128 : 512;
200 Renew(PL_tmps_stack, extend_to + 1, SV*);
201 PL_tmps_max = extend_to + 1;
209 /* XXX should tmps_floor live in cxstack? */
210 const SSize_t myfloor = PL_tmps_floor;
211 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
212 SV* const sv = PL_tmps_stack[PL_tmps_ix--];
214 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
218 SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
224 S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
229 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
232 if (flags & SAVEf_KEEPOLDELEM)
235 sv = (*sptr = newSV(0));
236 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
237 mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
244 Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
254 Perl_save_scalar(pTHX_ GV *gv)
256 SV ** const sptr = &GvSVn(gv);
258 PERL_ARGS_ASSERT_SAVE_SCALAR;
260 if (UNLIKELY(SvGMAGICAL(*sptr))) {
265 save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
266 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
269 /* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
270 * restore a global SV to its prior contents, freeing new value. */
272 Perl_save_generic_svref(pTHX_ SV **sptr)
274 PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
276 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
279 /* Like save_pptr(), but also Safefree()s the new value if it is different
280 * from the old one. Can be used to restore a global char* to its prior
281 * contents, freeing new value. */
283 Perl_save_generic_pvref(pTHX_ char **str)
285 PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
287 save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
290 /* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
291 * Can be used to restore a shared global char* to its prior
292 * contents, freeing new value. */
294 Perl_save_shared_pvref(pTHX_ char **str)
296 PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
298 save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
301 /* set the SvFLAGS specified by mask to the values in val */
304 Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
308 PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
313 SS_ADD_UV(SAVEt_SET_SVFLAGS);
319 =for apidoc_section GV Handling
323 Saves the current GP of gv on the save stack to be restored on scope exit.
325 If empty is true, replace the GP with a new GP.
327 If empty is false, mark gv with GVf_INTRO so the next reference
328 assigned is localized, which is how C< local *foo = $someref; > works.
334 Perl_save_gp(pTHX_ GV *gv, I32 empty)
336 PERL_ARGS_ASSERT_SAVE_GP;
338 /* XXX For now, we just upgrade any coderef in the stash to a full GV
339 during localisation. Maybe at some point we could make localis-
340 ation work without needing the upgrade. (In which case our
341 callers should probably call a different function, not save_gp.)
344 assert(isGV_or_RVCV(gv));
345 (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
349 save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
352 GP *gp = Perl_newGP(aTHX_ gv);
353 HV * const stash = GvSTASH(gv);
354 bool isa_changed = 0;
356 if (stash && HvENAME(stash)) {
357 if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
360 /* taking a method out of circulation ("local")*/
361 mro_method_changed_in(stash);
363 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
365 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
368 if (isa_changed) mro_isa_changed_in(stash);
377 Perl_save_ary(pTHX_ GV *gv)
379 AV * const oav = GvAVn(gv);
382 PERL_ARGS_ASSERT_SAVE_ARY;
384 if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
386 save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
390 if (UNLIKELY(SvMAGIC(oav)))
391 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
396 Perl_save_hash(pTHX_ GV *gv)
400 PERL_ARGS_ASSERT_SAVE_HASH;
403 SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
408 if (UNLIKELY(SvMAGIC(ohv)))
409 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
414 Perl_save_item(pTHX_ SV *item)
416 SV * const sv = newSVsv(item);
418 PERL_ARGS_ASSERT_SAVE_ITEM;
420 save_pushptrptr(item, /* remember the pointer */
421 sv, /* remember the value */
426 Perl_save_bool(pTHX_ bool *boolp)
430 PERL_ARGS_ASSERT_SAVE_BOOL;
433 SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
438 Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
449 Perl_save_int(pTHX_ int *intp)
452 UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
456 PERL_ARGS_ASSERT_SAVE_INT;
458 if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
469 Perl_save_I8(pTHX_ I8 *bytep)
473 PERL_ARGS_ASSERT_SAVE_I8;
476 SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
481 Perl_save_I16(pTHX_ I16 *intp)
485 PERL_ARGS_ASSERT_SAVE_I16;
488 SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
493 Perl_save_I32(pTHX_ I32 *intp)
496 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
500 PERL_ARGS_ASSERT_SAVE_I32;
502 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
513 Perl_save_strlen(pTHX_ STRLEN *ptr)
517 PERL_ARGS_ASSERT_SAVE_STRLEN;
521 SS_ADD_UV(SAVEt_STRLEN);
526 Perl_save_iv(pTHX_ IV *ivp)
528 PERL_ARGS_ASSERT_SAVE_IV;
536 /* Cannot use save_sptr() to store a char* since the SV** cast will
537 * force word-alignment and we'll miss the pointer.
540 Perl_save_pptr(pTHX_ char **pptr)
542 PERL_ARGS_ASSERT_SAVE_PPTR;
544 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
548 Perl_save_vptr(pTHX_ void *ptr)
550 PERL_ARGS_ASSERT_SAVE_VPTR;
552 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
556 Perl_save_sptr(pTHX_ SV **sptr)
558 PERL_ARGS_ASSERT_SAVE_SPTR;
560 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
564 Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
568 ASSERT_CURPAD_ACTIVE("save_padsv");
569 SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
570 SS_ADD_PTR(PL_comppad);
572 SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
577 Perl_save_hptr(pTHX_ HV **hptr)
579 PERL_ARGS_ASSERT_SAVE_HPTR;
581 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
585 Perl_save_aptr(pTHX_ AV **aptr)
587 PERL_ARGS_ASSERT_SAVE_APTR;
589 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
593 Perl_save_pushptr(pTHX_ void *const ptr, const int type)
602 Perl_save_clearsv(pTHX_ SV **svp)
604 const UV offset = svp - PL_curpad;
605 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
607 PERL_ARGS_ASSERT_SAVE_CLEARSV;
609 ASSERT_CURPAD_ACTIVE("save_clearsv");
610 SvPADSTALE_off(*svp); /* mark lexical as active */
611 if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
612 Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
613 offset, svp, PL_curpad);
618 SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
624 Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
626 PERL_ARGS_ASSERT_SAVE_DELETE;
628 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
632 Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
638 PERL_ARGS_ASSERT_SAVE_HDELETE;
640 key = SvPV_const(keysv, len);
641 klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
642 SvREFCNT_inc_simple_void_NN(hv);
643 save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
647 Perl_save_adelete(pTHX_ AV *av, SSize_t key)
651 PERL_ARGS_ASSERT_SAVE_ADELETE;
653 SvREFCNT_inc_void(av);
656 SS_ADD_IV(SAVEt_ADELETE);
661 Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
664 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
668 SS_ADD_UV(SAVEt_DESTRUCTOR);
673 Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
679 SS_ADD_UV(SAVEt_DESTRUCTOR_X);
684 Perl_save_hints(pTHX)
686 COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
687 if (PL_hints & HINT_LOCALIZE_HH) {
688 HV *oldhh = GvHV(PL_hintgv);
691 SS_ADD_INT(PL_hints);
692 SS_ADD_PTR(save_cophh);
694 SS_ADD_UV(SAVEt_HINTS_HH);
697 GvHV(PL_hintgv) = NULL; /* in case copying dies */
698 GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
701 save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS);
706 S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
718 Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
724 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
727 SS_ADD_PTR(SvREFCNT_inc_simple(av));
729 SS_ADD_PTR(SvREFCNT_inc(*sptr));
730 SS_ADD_UV(SAVEt_AELEM);
732 /* The array needs to hold a reference count on its new element, so it
734 if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
736 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
737 if (flags & SAVEf_KEEPOLDELEM)
740 /* If we're localizing a tied array element, this new sv
741 * won't actually be stored in the array - so it won't get
742 * reaped when the localize ends. Ensure it gets reaped by
743 * mortifying it instead. DAPM */
744 if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
749 Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
753 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
758 SS_ADD_PTR(SvREFCNT_inc_simple(hv));
759 SS_ADD_PTR(newSVsv(key));
760 SS_ADD_PTR(SvREFCNT_inc(*sptr));
761 SS_ADD_UV(SAVEt_HELEM);
764 save_scalar_at(sptr, flags);
765 if (flags & SAVEf_KEEPOLDELEM)
768 /* If we're localizing a tied hash element, this new sv
769 * won't actually be stored in the hash - so it won't get
770 * reaped when the localize ends. Ensure it gets reaped by
771 * mortifying it instead. DAPM */
772 if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
777 Perl_save_svref(pTHX_ SV **sptr)
779 PERL_ARGS_ASSERT_SAVE_SVREF;
782 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
783 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
791 SS_ADD_IV(PL_tmps_floor);
792 PL_tmps_floor = PL_tmps_ix;
793 SS_ADD_UV(SAVEt_TMPSFLOOR);
799 Perl_save_alloc(pTHX_ I32 size, I32 pad)
801 const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
802 - (char*)PL_savestack);
803 const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
804 const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
806 if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
808 "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
809 elems, (IV)size, (IV)pad);
813 PL_savestack_ix += elems;
814 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
819 static const U8 arg_counts[] = {
821 0, /* SAVEt_CLEARPADRANGE */
822 0, /* SAVEt_CLEARSV */
823 0, /* SAVEt_REGCONTEXT */
824 1, /* SAVEt_TMPSFLOOR */
826 1, /* SAVEt_COMPILE_WARNINGS */
827 1, /* SAVEt_COMPPAD */
828 1, /* SAVEt_FREECOPHH */
829 1, /* SAVEt_FREEOP */
830 1, /* SAVEt_FREEPV */
831 1, /* SAVEt_FREESV */
833 1, /* SAVEt_I32_SMALL */
835 1, /* SAVEt_INT_SMALL */
836 1, /* SAVEt_MORTALIZESV */
839 1, /* SAVEt_PARSER */
840 1, /* SAVEt_STACK_POS */
841 1, /* SAVEt_READONLY_OFF */
842 1, /* SAVEt_FREEPADNAME */
844 2, /* SAVEt_DESTRUCTOR */
845 2, /* SAVEt_DESTRUCTOR_X */
846 2, /* SAVEt_GENERIC_PVREF */
847 2, /* SAVEt_GENERIC_SVREF */
859 2, /* SAVEt_SAVESWITCHSTACK */
860 2, /* SAVEt_SHARED_PVREF */
862 2, /* SAVEt_STRLEN */
866 2, /* SAVEt_ADELETE */
869 3, /* SAVEt_PADSV_AND_MORTALIZE*/
870 3, /* SAVEt_SET_SVFLAGS */
871 3, /* SAVEt_GVSLOT */
873 3, /* SAVEt_DELETE */
874 3 /* SAVEt_HINTS_HH */
879 Perl_leave_scope(pTHX_ I32 base)
881 /* Localise the effects of the TAINT_NOT inside the loop. */
882 bool was = TAINT_get;
884 if (UNLIKELY(base < -1))
885 Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
886 DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
887 (long)PL_savestack_ix, (long)base));
888 while (PL_savestack_ix > base) {
891 ANY *ap; /* arg pointer */
892 ANY a0, a1, a2; /* up to 3 args */
898 I32 ix = PL_savestack_ix - 1;
900 ap = &PL_savestack[ix];
902 type = (U8)uv & SAVE_MASK;
903 argcount = arg_counts[type];
904 PL_savestack_ix = ix - argcount;
909 case SAVEt_ITEM: /* normal string */
910 a0 = ap[0]; a1 = ap[1];
911 sv_replace(a0.any_sv, a1.any_sv);
912 if (UNLIKELY(SvSMAGICAL(a0.any_sv))) {
919 /* This would be a mathom, but Perl_save_svref() calls a static
920 function, S_save_scalar_at(), so has to stay in this file. */
921 case SAVEt_SVREF: /* scalar reference */
922 a0 = ap[0]; a1 = ap[1];
923 a2.any_svp = a0.any_svp;
924 a0.any_sv = NULL; /* what to refcnt_dec */
927 case SAVEt_SV: /* scalar reference */
928 a0 = ap[0]; a1 = ap[1];
929 a2.any_svp = &GvSV(a0.any_gv);
932 /* do *a2.any_svp = a1 and free a0 */
933 SV * const sv = *a2.any_svp;
934 *a2.any_svp = a1.any_sv;
936 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
937 /* mg_set could die, skipping the freeing of a0 and
938 * a1; Ensure that they're always freed in that case */
940 SS_ADD_PTR(a1.any_sv);
941 SS_ADD_UV(SAVEt_FREESV);
942 SS_ADD_PTR(a0.any_sv);
943 SS_ADD_UV(SAVEt_FREESV);
950 SvREFCNT_dec_NN(a1.any_sv);
951 SvREFCNT_dec(a0.any_sv);
955 case SAVEt_GENERIC_PVREF: /* generic pv */
956 a0 = ap[0]; a1 = ap[1];
957 if (*a1.any_pvp != a0.any_pv) {
958 Safefree(*a1.any_pvp);
959 *a1.any_pvp = a0.any_pv;
963 case SAVEt_SHARED_PVREF: /* shared pv */
964 a0 = ap[0]; a1 = ap[1];
965 if (*a0.any_pvp != a1.any_pv) {
967 PerlMem_free(*a0.any_pvp);
969 PerlMemShared_free(*a0.any_pvp);
971 *a0.any_pvp = a1.any_pv;
975 case SAVEt_GVSV: /* scalar slot in GV */
976 a0 = ap[0]; a1 = ap[1];
977 a0.any_svp = &GvSV(a0.any_gv);
980 case SAVEt_GENERIC_SVREF: /* generic sv */
981 a0 = ap[0]; a1 = ap[1];
984 /* do *a0.any_svp = a1 */
985 SV * const sv = *a0.any_svp;
986 *a0.any_svp = a1.any_sv;
988 SvREFCNT_dec(a1.any_sv);
992 case SAVEt_GVSLOT: /* any slot in GV */
995 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
996 hv = GvSTASH(a0.any_gv);
997 if (hv && HvENAME(hv) && (
998 (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV)
999 || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV)
1002 if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv)
1003 || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp)
1004 || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */
1005 PL_sub_generation++;
1006 else mro_method_changed_in(hv);
1008 a0.any_svp = a1.any_svp;
1009 a1.any_sv = a2.any_sv;
1013 case SAVEt_AV: /* array reference */
1014 a0 = ap[0]; a1 = ap[1];
1015 SvREFCNT_dec(GvAV(a0.any_gv));
1016 GvAV(a0.any_gv) = a1.any_av;
1018 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1019 /* mg_set might die, so make sure a0 isn't leaked */
1021 SS_ADD_PTR(a0.any_sv);
1022 SS_ADD_UV(SAVEt_FREESV);
1029 SvREFCNT_dec_NN(a0.any_sv);
1032 case SAVEt_HV: /* hash reference */
1033 a0 = ap[0]; a1 = ap[1];
1034 SvREFCNT_dec(GvHV(a0.any_gv));
1035 GvHV(a0.any_gv) = a1.any_hv;
1038 case SAVEt_INT_SMALL:
1040 *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
1043 case SAVEt_INT: /* int reference */
1044 a0 = ap[0]; a1 = ap[1];
1045 *(int*)a1.any_ptr = (int)a0.any_i32;
1048 case SAVEt_STRLEN: /* STRLEN/size_t ref */
1049 a0 = ap[0]; a1 = ap[1];
1050 *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
1053 case SAVEt_TMPSFLOOR: /* restore PL_tmps_floor */
1055 PL_tmps_floor = (SSize_t)a0.any_iv;
1058 case SAVEt_BOOL: /* bool reference */
1060 *(bool*)a0.any_ptr = cBOOL(uv >> 8);
1061 #ifdef NO_TAINT_SUPPORT
1062 PERL_UNUSED_VAR(was);
1064 if (UNLIKELY(a0.any_ptr == &(PL_tainted))) {
1065 /* If we don't update <was>, to reflect what was saved on the
1066 * stack for PL_tainted, then we will overwrite this attempt to
1067 * restore it when we exit this routine. Note that this won't
1068 * work if this value was saved in a wider-than necessary type,
1070 was = *(bool*)a0.any_ptr;
1075 case SAVEt_I32_SMALL:
1077 *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
1080 case SAVEt_I32: /* I32 reference */
1081 a0 = ap[0]; a1 = ap[1];
1082 #ifdef PERL_DEBUG_READONLY_OPS
1083 if (*(I32*)a1.any_ptr != a0.any_i32)
1085 *(I32*)a1.any_ptr = a0.any_i32;
1088 case SAVEt_SPTR: /* SV* reference */
1089 case SAVEt_VPTR: /* random* reference */
1090 case SAVEt_PPTR: /* char* reference */
1091 case SAVEt_HPTR: /* HV* reference */
1092 case SAVEt_APTR: /* AV* reference */
1093 a0 = ap[0]; a1 = ap[1];
1094 *a1.any_svp= a0.any_sv;
1097 case SAVEt_GP: /* scalar reference */
1102 a0 = ap[0]; a1 = ap[1];
1103 /* possibly taking a method out of circulation */
1104 had_method = !!GvCVu(a0.any_gv);
1106 GvGP_set(a0.any_gv, (GP*)a1.any_ptr);
1107 if ((hv=GvSTASH(a0.any_gv)) && HvENAME_get(hv)) {
1108 if (memEQs(GvNAME(a0.any_gv), GvNAMELEN(a0.any_gv), "ISA"))
1109 mro_isa_changed_in(hv);
1110 else if (had_method || GvCVu(a0.any_gv))
1111 /* putting a method back into circulation ("local")*/
1112 gv_method_changed(a0.any_gv);
1114 SvREFCNT_dec_NN(a0.any_gv);
1120 SvREFCNT_dec(a0.any_sv);
1123 case SAVEt_FREEPADNAME:
1125 PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
1128 case SAVEt_FREECOPHH:
1130 cophh_free((COPHH *)a0.any_ptr);
1133 case SAVEt_MORTALIZESV:
1135 sv_2mortal(a0.any_sv);
1140 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
1146 Safefree(a0.any_ptr);
1149 case SAVEt_CLEARPADRANGE:
1153 i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
1154 svp = &PL_curpad[uv >>
1155 (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
1158 svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
1161 for (; i; i--, svp--) {
1164 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
1165 "Pad 0x%" UVxf "[0x%" UVxf "] clearsv: %ld sv=0x%" UVxf "<%" IVdf "> %s\n",
1166 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1167 (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
1168 (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
1171 /* Can clear pad variable in place? */
1172 if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) {
1174 /* these flags are the union of all the relevant flags
1175 * in the individual conditions within */
1176 if (UNLIKELY(SvFLAGS(sv) & (
1177 SVf_READONLY|SVf_PROTECT /*for SvREADONLY_off*/
1178 | (SVs_GMG|SVs_SMG|SVs_RMG) /* SvMAGICAL() */
1182 /* if a my variable that was made readonly is
1183 * going out of scope, we want to remove the
1184 * readonlyness so that it can go out of scope
1190 if (SvOOK(sv)) { /* OOK or HvAUX */
1191 if (SvTYPE(sv) == SVt_PVHV)
1192 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
1197 if (SvMAGICAL(sv)) {
1198 /* note that backrefs (either in HvAUX or magic)
1199 * must be removed before other magic */
1200 sv_unmagic(sv, PERL_MAGIC_backref);
1201 if (SvTYPE(sv) != SVt_PVCV)
1204 if (SvTHINKFIRST(sv))
1205 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
1209 switch (SvTYPE(sv)) {
1213 av_clear(MUTABLE_AV(sv));
1216 hv_clear(MUTABLE_HV(sv));
1220 HEK *hek = CvGvNAME_HEK(sv);
1222 (void)share_hek_hek(hek);
1224 CvNAME_HEK_set(sv, hek);
1229 /* This looks odd, but these two macros are for use in
1230 expressions and finish with a trailing comma, so
1231 adding a ; after them would be wrong. */
1234 SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8);
1238 SvPADSTALE_on(sv); /* mark as no longer live */
1240 else { /* Someone has a claim on this, so abandon it. */
1241 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
1242 case SVt_PVAV: *svp = MUTABLE_SV(newAV()); break;
1243 case SVt_PVHV: *svp = MUTABLE_SV(newHV()); break;
1246 HEK * const hek = CvGvNAME_HEK(sv);
1249 *svp = newSV_type(SVt_PVCV);
1252 CvNAME_HEK_set(*svp,
1253 share_hek_hek(hek));
1257 default: *svp = newSV(0); break;
1259 SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
1260 /* preserve pad nature, but also mark as not live
1261 * for any closure capturing */
1262 SvFLAGS(*svp) |= SVs_PADSTALE;
1269 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1270 /* hv_delete could die, so free the key and SvREFCNT_dec the
1271 * hv by pushing new save actions
1273 /* ap[0] is the key */
1274 ap[1].any_uv = SAVEt_FREEPV; /* was len */
1275 /* ap[2] is the hv */
1276 ap[3].any_uv = SAVEt_FREESV; /* was SAVEt_DELETE */
1277 PL_savestack_ix += 4;
1278 (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD);
1282 a0 = ap[0]; a1 = ap[1];
1283 /* av_delete could die, so SvREFCNT_dec the av by pushing a
1286 ap[0].any_av = a1.any_av;
1287 ap[1].any_uv = SAVEt_FREESV;
1288 PL_savestack_ix += 2;
1289 (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD);
1292 case SAVEt_DESTRUCTOR_X:
1293 a0 = ap[0]; a1 = ap[1];
1294 (*a0.any_dxptr)(aTHX_ a1.any_ptr);
1297 case SAVEt_REGCONTEXT:
1298 /* regexp must have croaked */
1300 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
1303 case SAVEt_STACK_POS: /* Position on Perl stack */
1305 PL_stack_sp = PL_stack_base + a0.any_i32;
1308 case SAVEt_AELEM: /* array element */
1311 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1312 svp = av_fetch(a0.any_av, a1.any_iv, 1);
1313 if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */
1314 SvREFCNT_dec(a2.any_sv);
1316 SV * const sv = *svp;
1317 if (LIKELY(sv && sv != &PL_sv_undef)) {
1318 if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied)))
1319 SvREFCNT_inc_void_NN(sv);
1320 a1.any_sv = a2.any_sv;
1325 SvREFCNT_dec(a0.any_av);
1326 SvREFCNT_dec(a2.any_sv);
1330 case SAVEt_HELEM: /* hash element */
1334 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1335 he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0);
1336 SvREFCNT_dec(a1.any_sv);
1338 const SV * const oval = HeVAL(he);
1339 if (LIKELY(oval && oval != &PL_sv_undef)) {
1340 SV **svp = &HeVAL(he);
1341 if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied)))
1342 SvREFCNT_inc_void(*svp);
1343 a1.any_sv = a2.any_sv;
1348 SvREFCNT_dec(a0.any_hv);
1349 SvREFCNT_dec(a2.any_sv);
1355 PL_op = (OP*)a0.any_ptr;
1358 case SAVEt_HINTS_HH:
1362 a0 = ap[0]; a1 = ap[1];
1363 if ((PL_hints & HINT_LOCALIZE_HH)) {
1364 while (GvHV(PL_hintgv)) {
1365 HV *hv = GvHV(PL_hintgv);
1366 GvHV(PL_hintgv) = NULL;
1367 SvREFCNT_dec(MUTABLE_SV(hv));
1370 cophh_free(CopHINTHASH_get(&PL_compiling));
1371 CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr);
1372 *(I32*)&PL_hints = a0.any_i32;
1373 if (type == SAVEt_HINTS_HH) {
1374 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
1375 GvHV(PL_hintgv) = MUTABLE_HV(a2.any_ptr);
1377 if (!GvHV(PL_hintgv)) {
1378 /* Need to add a new one manually, else rv2hv can
1379 add one via GvHVn and it won't have the magic set. */
1380 HV *const hv = newHV();
1381 hv_magic(hv, NULL, PERL_MAGIC_hints);
1382 GvHV(PL_hintgv) = hv;
1384 assert(GvHV(PL_hintgv));
1389 PL_comppad = (PAD*)a0.any_ptr;
1390 if (LIKELY(PL_comppad))
1391 PL_curpad = AvARRAY(PL_comppad);
1396 case SAVEt_PADSV_AND_MORTALIZE:
1400 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1401 assert (a1.any_ptr);
1402 svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv;
1403 /* This mortalizing used to be done by CX_POOPLOOP() via
1404 itersave. But as we have all the information here, we
1405 can do it here, save even having to have itersave in
1413 case SAVEt_SAVESWITCHSTACK:
1417 a0 = ap[0]; a1 = ap[1];
1418 SWITCHSTACK(a1.any_av, a0.any_av);
1419 PL_curstackinfo->si_stack = a0.any_av;
1423 case SAVEt_SET_SVFLAGS:
1424 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1425 SvFLAGS(a0.any_sv) &= ~(a1.any_u32);
1426 SvFLAGS(a0.any_sv) |= a2.any_u32;
1429 /* These are only saved in mathoms.c */
1432 (void)sv_clear(a0.any_sv);
1435 case SAVEt_LONG: /* long reference */
1436 a0 = ap[0]; a1 = ap[1];
1437 *(long*)a1.any_ptr = a0.any_long;
1440 case SAVEt_IV: /* IV reference */
1441 a0 = ap[0]; a1 = ap[1];
1442 *(IV*)a1.any_ptr = a0.any_iv;
1445 case SAVEt_I16: /* I16 reference */
1447 *(I16*)a0.any_ptr = (I16)(uv >> 8);
1450 case SAVEt_I8: /* I8 reference */
1452 *(I8*)a0.any_ptr = (I8)(uv >> 8);
1455 case SAVEt_DESTRUCTOR:
1456 a0 = ap[0]; a1 = ap[1];
1457 (*a0.any_dptr)(a1.any_ptr);
1460 case SAVEt_COMPILE_WARNINGS:
1462 free_and_set_cop_warnings(&PL_compiling, (STRLEN*) a0.any_ptr);
1467 parser_free((yy_parser *)a0.any_ptr);
1470 case SAVEt_READONLY_OFF:
1472 SvREADONLY_off(a0.any_sv);
1476 Perl_croak(aTHX_ "panic: leave_scope inconsistency %u",
1477 (U8)uv & SAVE_MASK);
1485 Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
1487 PERL_ARGS_ASSERT_CX_DUMP;
1490 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
1491 if (CxTYPE(cx) != CXt_SUBST) {
1492 const char *gimme_text;
1493 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
1494 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n",
1495 PTR2UV(cx->blk_oldcop));
1496 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1497 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
1498 PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
1499 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n",
1500 PTR2UV(cx->blk_oldpm));
1501 switch (cx->blk_gimme) {
1503 gimme_text = "VOID";
1506 gimme_text = "SCALAR";
1509 gimme_text = "LIST";
1512 gimme_text = "UNKNOWN";
1515 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
1517 switch (CxTYPE(cx)) {
1522 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n",
1523 PTR2UV(cx->blk_format.cv));
1524 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n",
1525 PTR2UV(cx->blk_format.gv));
1526 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n",
1527 PTR2UV(cx->blk_format.dfoutgv));
1528 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
1529 (int)CxHASARGS(cx));
1530 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n",
1531 PTR2UV(cx->blk_format.retop));
1534 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n",
1535 PTR2UV(cx->blk_sub.cv));
1536 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
1537 (long)cx->blk_sub.olddepth);
1538 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
1539 (int)CxHASARGS(cx));
1540 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
1541 PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n",
1542 PTR2UV(cx->blk_sub.retop));
1545 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
1546 (long)CxOLD_IN_EVAL(cx));
1547 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
1548 PL_op_name[CxOLD_OP_TYPE(cx)],
1549 PL_op_desc[CxOLD_OP_TYPE(cx)]);
1550 if (cx->blk_eval.old_namesv)
1551 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
1552 SvPVX_const(cx->blk_eval.old_namesv));
1553 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n",
1554 PTR2UV(cx->blk_eval.old_eval_root));
1555 PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n",
1556 PTR2UV(cx->blk_eval.retop));
1559 case CXt_LOOP_PLAIN:
1560 case CXt_LOOP_LAZYIV:
1561 case CXt_LOOP_LAZYSV:
1564 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
1565 PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n",
1566 PTR2UV(cx->blk_loop.my_op));
1567 if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
1568 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n",
1569 PTR2UV(CxITERVAR(cx)));
1570 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n",
1571 PTR2UV(cx->blk_loop.itersave));
1573 if (CxTYPE(cx) == CXt_LOOP_ARY) {
1574 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n",
1575 PTR2UV(cx->blk_loop.state_u.ary.ary));
1576 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1577 (long)cx->blk_loop.state_u.ary.ix);
1582 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
1583 (long)cx->sb_iters);
1584 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
1585 (long)cx->sb_maxiters);
1586 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1587 (long)cx->sb_rflags);
1588 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
1590 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
1592 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n",
1593 PTR2UV(cx->sb_dstr));
1594 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n",
1595 PTR2UV(cx->sb_targ));
1596 PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n",
1598 PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
1600 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n",
1601 PTR2UV(cx->sb_strend));
1602 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n",
1603 PTR2UV(cx->sb_rxres));
1607 PERL_UNUSED_CONTEXT;
1608 PERL_UNUSED_ARG(cx);
1609 #endif /* DEBUGGING */
1613 * ex: set ts=8 sts=4 sw=4 et: