This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
offset PL_savestack_max by SS_MAXPUSH
[perl5.git] / scope.c
CommitLineData
a0d0e21e 1/* scope.c
79072805 2 *
1129b882
NC
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
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
12 * For the fashion of Minas Tirith was such that it was built on seven
13 * levels...
14 *
15 * [p.751 of _The Lord of the Rings_, V/i: "Minas Tirith"]
79072805
LW
16 */
17
ddfa107c 18/* This file contains functions to manipulate several of Perl's stacks;
166f8a29
DM
19 * in particular it contains code to push various types of things onto
20 * the savestack, then to pop them off and perform the correct restorative
21 * action for each one. This corresponds to the cleanup Perl does at
22 * each scope exit.
23 */
24
79072805 25#include "EXTERN.h"
864dbfa3 26#define PERL_IN_SCOPE_C
79072805
LW
27#include "perl.h"
28
a0d0e21e 29SV**
fc16c392 30Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
a0d0e21e 31{
402d079e
DM
32 SSize_t extra;
33 SSize_t current = (p - PL_stack_base);
34
7918f24d
NC
35 PERL_ARGS_ASSERT_STACK_GROW;
36
402d079e 37 if (UNLIKELY(n < 0))
6768377c
DM
38 Perl_croak(aTHX_
39 "panic: stack_grow() negative count (%"IVdf")", (IV)n);
40
3280af22 41 PL_stack_sp = sp;
402d079e
DM
42 extra =
43#ifdef STRESS_REALLOC
44 1;
2ce36478 45#else
402d079e 46 128;
2ce36478 47#endif
402d079e
DM
48 /* If the total might wrap, panic instead. This is really testing
49 * that (current + n + extra < SSize_t_MAX), but done in a way that
50 * can't wrap */
51 if (UNLIKELY( current > SSize_t_MAX - extra
52 || current + extra > SSize_t_MAX - n
53 ))
54 /* diag_listed_as: Out of memory during %s extend */
55 Perl_croak(aTHX_ "Out of memory during stack extend");
56
57 av_extend(PL_curstack, current + n + extra);
3280af22 58 return PL_stack_sp;
a0d0e21e
LW
59}
60
2ce36478
SM
61#ifndef STRESS_REALLOC
62#define GROW(old) ((old) * 3 / 2)
63#else
64#define GROW(old) ((old) + 1)
65#endif
66
e336de0d 67PERL_SI *
864dbfa3 68Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
e336de0d
GS
69{
70 PERL_SI *si;
a02a5408 71 Newx(si, 1, PERL_SI);
e336de0d
GS
72 si->si_stack = newAV();
73 AvREAL_off(si->si_stack);
74 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
3280af22 75 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
e336de0d
GS
76 AvFILLp(si->si_stack) = 0;
77 si->si_prev = 0;
78 si->si_next = 0;
79 si->si_cxmax = cxitems - 1;
80 si->si_cxix = -1;
e788e7d3 81 si->si_type = PERLSI_UNDEF;
a02a5408 82 Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
490576d1 83 /* Without any kind of initialising CX_PUSHSUBST()
9965345d 84 * in pp_subst() will read uninitialised heap. */
7e337ee0 85 PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT);
e336de0d
GS
86 return si;
87}
88
79072805 89I32
864dbfa3 90Perl_cxinc(pTHX)
79072805 91{
a3b680e6 92 const IV old_max = cxstack_max;
2ce36478 93 cxstack_max = GROW(cxstack_max);
401a9b14 94 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT);
9965345d
JH
95 /* Without any kind of initialising deep enough recursion
96 * will end up reading uninitialised PERL_CONTEXTs. */
109bf713 97 PoisonNew(cxstack + old_max + 1, cxstack_max - old_max, PERL_CONTEXT);
79072805
LW
98 return cxstack_ix + 1;
99}
100
79072805 101void
864dbfa3 102Perl_push_scope(pTHX)
79072805 103{
5d9574c1 104 if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
3280af22
NIS
105 PL_scopestack_max = GROW(PL_scopestack_max);
106 Renew(PL_scopestack, PL_scopestack_max, I32);
d343c3ef
GG
107#ifdef DEBUGGING
108 Renew(PL_scopestack_name, PL_scopestack_max, const char*);
520bb150 109#endif
79072805 110 }
d343c3ef
GG
111#ifdef DEBUGGING
112 PL_scopestack_name[PL_scopestack_ix] = "unknown";
113#endif
3280af22 114 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
79072805
LW
115
116}
117
118void
864dbfa3 119Perl_pop_scope(pTHX)
79072805 120{
35a4481c 121 const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
8990e307 122 LEAVE_SCOPE(oldsave);
79072805
LW
123}
124
ba5248fc 125I32 *
864dbfa3 126Perl_markstack_grow(pTHX)
a0d0e21e 127{
35a4481c
AL
128 const I32 oldmax = PL_markstack_max - PL_markstack;
129 const I32 newmax = GROW(oldmax);
a0d0e21e 130
3280af22 131 Renew(PL_markstack, newmax, I32);
3280af22 132 PL_markstack_max = PL_markstack + newmax;
ba5248fc 133 PL_markstack_ptr = PL_markstack + oldmax;
30773234
DM
134 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
135 "MARK grow %p %"IVdf" by %"IVdf"\n",
136 PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax)));
ba5248fc 137 return PL_markstack_ptr;
a0d0e21e
LW
138}
139
140void
864dbfa3 141Perl_savestack_grow(pTHX)
79072805 142{
3caf0269
DM
143 PL_savestack_max = GROW(PL_savestack_max);
144 /* Note that we allocate SS_MAXPUSH slots higher than ss_max
145 * so that SS_ADD_END(), SSGROW() etc can do a simper check */
146 Renew(PL_savestack, PL_savestack_max + SS_MAXPUSH, ANY);
79072805
LW
147}
148
4b3c1a47
AE
149void
150Perl_savestack_grow_cnt(pTHX_ I32 need)
151{
152 PL_savestack_max = PL_savestack_ix + need;
3caf0269
DM
153 /* Note that we allocate SS_MAXPUSH slots higher than ss_max
154 * so that SS_ADD_END(), SSGROW() etc can do a simper check */
155 Renew(PL_savestack, PL_savestack_max + SS_MAXPUSH, ANY);
4b3c1a47
AE
156}
157
2ce36478
SM
158#undef GROW
159
a953aca5
DD
160/* The original function was called Perl_tmps_grow and was removed from public
161 API, Perl_tmps_grow_p is the replacement and it used in public macros but
162 isn't public itself.
163
164 Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by,
165 where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max
166 Upon return, PL_tmps_stack[ix] will be a valid address. For machine code
167 optimization and register usage reasons, the proposed ix passed into
168 tmps_grow is returned to the caller which the caller can then use to write
169 an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in
170 pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of
171 tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller
172 must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is
173 appropriate. The assignment to PL_temps_ix can happen before or after
174 tmps_grow call since tmps_grow doesn't look at PL_tmps_ix.
175 */
176
177SSize_t
178Perl_tmps_grow_p(pTHX_ SSize_t ix)
677b06e3 179{
a953aca5 180 SSize_t extend_to = ix;
677b06e3 181#ifndef STRESS_REALLOC
a953aca5
DD
182 if (ix - PL_tmps_max < 128)
183 extend_to += (PL_tmps_max < 512) ? 128 : 512;
677b06e3 184#endif
a953aca5 185 PL_tmps_max = extend_to + 1;
677b06e3 186 Renew(PL_tmps_stack, PL_tmps_max, SV*);
a953aca5 187 return ix;
677b06e3
GS
188}
189
190
191void
864dbfa3 192Perl_free_tmps(pTHX)
79072805
LW
193{
194 /* XXX should tmps_floor live in cxstack? */
e8eb279c 195 const SSize_t myfloor = PL_tmps_floor;
3280af22 196 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
22ade07d 197 SV* const sv = PL_tmps_stack[PL_tmps_ix--];
72581b5b
VP
198#ifdef PERL_POISON
199 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
22ade07d 200#endif
520275ba 201 if (LIKELY(sv)) {
463ee0b2 202 SvTEMP_off(sv);
4a9a56a7 203 SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
463ee0b2 204 }
79072805
LW
205 }
206}
207
76e3520e 208STATIC SV *
af7df257 209S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
79072805 210{
75d34a09 211 SV * osv;
eb578fdb 212 SV *sv;
79072805 213
7918f24d
NC
214 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
215
75d34a09 216 osv = *sptr;
3c84a230
DM
217 if (flags & SAVEf_KEEPOLDELEM)
218 sv = osv;
219 else {
220 sv = (*sptr = newSV(0));
221 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
222 mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
79072805 223 }
75d34a09 224
79072805
LW
225 return sv;
226}
227
dfcd3de5
NC
228void
229Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
e22024d3 230{
a3444cc5
DM
231 dSS_ADD;
232 SS_ADD_PTR(ptr1);
233 SS_ADD_PTR(ptr2);
234 SS_ADD_UV(type);
235 SS_ADD_END(3);
e22024d3
NC
236}
237
7a4c00b4 238SV *
864dbfa3 239Perl_save_scalar(pTHX_ GV *gv)
7a4c00b4 240{
13c59d41 241 SV ** const sptr = &GvSVn(gv);
7918f24d
NC
242
243 PERL_ARGS_ASSERT_SAVE_SCALAR;
244
13c59d41 245 if (UNLIKELY(SvGMAGICAL(*sptr))) {
b492a59e 246 PL_localizing = 1;
13c59d41 247 (void)mg_get(*sptr);
b492a59e
DM
248 PL_localizing = 0;
249 }
13c59d41
MH
250 save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
251 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
7a4c00b4
PP
252}
253
f4dd75d9 254/* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
b9d12d37
GS
255 * restore a global SV to its prior contents, freeing new value. */
256void
864dbfa3 257Perl_save_generic_svref(pTHX_ SV **sptr)
b9d12d37 258{
7918f24d
NC
259 PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
260
e22024d3 261 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
b9d12d37
GS
262}
263
f4dd75d9
GS
264/* Like save_pptr(), but also Safefree()s the new value if it is different
265 * from the old one. Can be used to restore a global char* to its prior
266 * contents, freeing new value. */
267void
268Perl_save_generic_pvref(pTHX_ char **str)
269{
7918f24d
NC
270 PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
271
e22024d3 272 save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
f4dd75d9
GS
273}
274
05ec9bb3
NIS
275/* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
276 * Can be used to restore a shared global char* to its prior
277 * contents, freeing new value. */
278void
279Perl_save_shared_pvref(pTHX_ char **str)
280{
7918f24d
NC
281 PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
282
e22024d3 283 save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
05ec9bb3
NIS
284}
285
14f338dc
DM
286/* set the SvFLAGS specified by mask to the values in val */
287
288void
289Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
290{
a3444cc5 291 dSS_ADD;
7918f24d
NC
292
293 PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
294
a3444cc5
DM
295 SS_ADD_PTR(sv);
296 SS_ADD_INT(mask);
297 SS_ADD_INT(val);
298 SS_ADD_UV(SAVEt_SET_SVFLAGS);
299 SS_ADD_END(4);
14f338dc
DM
300}
301
364bbfa4
TC
302/*
303=for apidoc save_gp
304
305Saves the current GP of gv on the save stack to be restored on scope exit.
306
307If empty is true, replace the GP with a new GP.
308
309If empty is false, mark gv with GVf_INTRO so the next reference
310assigned is localized, which is how C< local *foo = $someref; > works.
311
312=cut
313*/
314
79072805 315void
864dbfa3 316Perl_save_gp(pTHX_ GV *gv, I32 empty)
79072805 317{
7918f24d
NC
318 PERL_ARGS_ASSERT_SAVE_GP;
319
10507e11 320 save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
79072805 321
5f05dabc 322 if (empty) {
12816592 323 GP *gp = Perl_newGP(aTHX_ gv);
959f7ad7 324 HV * const stash = GvSTASH(gv);
3d460042
FC
325 bool isa_changed = 0;
326
327 if (stash && HvENAME(stash)) {
328 if (GvNAMELEN(gv) == 3 && strnEQ(GvNAME(gv), "ISA", 3))
329 isa_changed = TRUE;
330 else if (GvCVu(gv))
331 /* taking a method out of circulation ("local")*/
332 mro_method_changed_in(stash);
333 }
146174a9
CB
334 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
335 gp->gp_io = newIO();
336 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
337 }
c43ae56f 338 GvGP_set(gv,gp);
3d460042 339 if (isa_changed) mro_isa_changed_in(stash);
5f05dabc
PP
340 }
341 else {
44a8e56a 342 gp_ref(GvGP(gv));
5f05dabc
PP
343 GvINTRO_on(gv);
344 }
79072805 345}
79072805 346
79072805 347AV *
864dbfa3 348Perl_save_ary(pTHX_ GV *gv)
79072805 349{
901017d6 350 AV * const oav = GvAVn(gv);
67a38de0 351 AV *av;
fb73857a 352
7918f24d
NC
353 PERL_ARGS_ASSERT_SAVE_ARY;
354
5d9574c1 355 if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
67a38de0 356 av_reify(oav);
01433346 357 save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
79072805 358
4608196e 359 GvAV(gv) = NULL;
fb73857a 360 av = GvAVn(gv);
5d9574c1 361 if (UNLIKELY(SvMAGIC(oav)))
9711599e 362 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
fb73857a 363 return av;
79072805
LW
364}
365
366HV *
864dbfa3 367Perl_save_hash(pTHX_ GV *gv)
79072805 368{
fb73857a
PP
369 HV *ohv, *hv;
370
7918f24d
NC
371 PERL_ARGS_ASSERT_SAVE_HASH;
372
01433346
FC
373 save_pushptrptr(
374 SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
375 );
79072805 376
4608196e 377 GvHV(gv) = NULL;
fb73857a 378 hv = GvHVn(gv);
5d9574c1 379 if (UNLIKELY(SvMAGIC(ohv)))
9711599e 380 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
fb73857a 381 return hv;
79072805
LW
382}
383
384void
5aaab254 385Perl_save_item(pTHX_ SV *item)
79072805 386{
eb578fdb 387 SV * const sv = newSVsv(item);
79072805 388
7918f24d
NC
389 PERL_ARGS_ASSERT_SAVE_ITEM;
390
e22024d3
NC
391 save_pushptrptr(item, /* remember the pointer */
392 sv, /* remember the value */
393 SAVEt_ITEM);
79072805
LW
394}
395
396void
1409bc06 397Perl_save_bool(pTHX_ bool *boolp)
79072805 398{
a3444cc5 399 dSS_ADD;
7918f24d 400
1409bc06 401 PERL_ARGS_ASSERT_SAVE_BOOL;
7918f24d 402
a3444cc5
DM
403 SS_ADD_PTR(boolp);
404 SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
405 SS_ADD_END(2);
1409bc06
NC
406}
407
7623d426
NC
408void
409Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
1409bc06 410{
a3444cc5
DM
411 dSS_ADD;
412
413 SS_ADD_INT(i);
414 SS_ADD_PTR(ptr);
415 SS_ADD_UV(type);
416 SS_ADD_END(3);
79072805
LW
417}
418
419void
1409bc06 420Perl_save_int(pTHX_ int *intp)
9febdf04 421{
d172696c 422 const int i = *intp;
9b139d09 423 UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
d172696c
DM
424 int size = 2;
425 dSS_ADD;
7918f24d 426
1409bc06 427 PERL_ARGS_ASSERT_SAVE_INT;
7918f24d 428
5d9574c1 429 if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
d172696c
DM
430 SS_ADD_INT(i);
431 type = SAVEt_INT;
432 size++;
433 }
434 SS_ADD_PTR(intp);
435 SS_ADD_UV(type);
436 SS_ADD_END(size);
9febdf04
RH
437}
438
439void
58188858
RGS
440Perl_save_I8(pTHX_ I8 *bytep)
441{
a3444cc5 442 dSS_ADD;
7918f24d
NC
443
444 PERL_ARGS_ASSERT_SAVE_I8;
445
a3444cc5
DM
446 SS_ADD_PTR(bytep);
447 SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
448 SS_ADD_END(2);
58188858
RGS
449}
450
451void
87a84751
JH
452Perl_save_I16(pTHX_ I16 *intp)
453{
a3444cc5 454 dSS_ADD;
7918f24d
NC
455
456 PERL_ARGS_ASSERT_SAVE_I16;
457
a3444cc5
DM
458 SS_ADD_PTR(intp);
459 SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
460 SS_ADD_END(2);
87a84751
JH
461}
462
463void
864dbfa3 464Perl_save_I32(pTHX_ I32 *intp)
79072805 465{
d172696c 466 const I32 i = *intp;
9b139d09 467 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
d172696c
DM
468 int size = 2;
469 dSS_ADD;
7918f24d
NC
470
471 PERL_ARGS_ASSERT_SAVE_I32;
472
5d9574c1 473 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
d172696c
DM
474 SS_ADD_INT(i);
475 type = SAVEt_I32;
476 size++;
477 }
478 SS_ADD_PTR(intp);
479 SS_ADD_UV(type);
480 SS_ADD_END(size);
79072805
LW
481}
482
e8eb279c
FC
483void
484Perl_save_strlen(pTHX_ STRLEN *ptr)
485{
e8eb279c
FC
486 dSS_ADD;
487
488 PERL_ARGS_ASSERT_SAVE_STRLEN;
489
490 SS_ADD_IV(*ptr);
491 SS_ADD_PTR(ptr);
492 SS_ADD_UV(SAVEt_STRLEN);
493 SS_ADD_END(3);
494}
495
85e6fe83
LW
496/* Cannot use save_sptr() to store a char* since the SV** cast will
497 * force word-alignment and we'll miss the pointer.
498 */
499void
864dbfa3 500Perl_save_pptr(pTHX_ char **pptr)
85e6fe83 501{
7918f24d
NC
502 PERL_ARGS_ASSERT_SAVE_PPTR;
503
e22024d3 504 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
85e6fe83
LW
505}
506
79072805 507void
146174a9
CB
508Perl_save_vptr(pTHX_ void *ptr)
509{
7918f24d
NC
510 PERL_ARGS_ASSERT_SAVE_VPTR;
511
e22024d3 512 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
146174a9
CB
513}
514
515void
864dbfa3 516Perl_save_sptr(pTHX_ SV **sptr)
79072805 517{
7918f24d
NC
518 PERL_ARGS_ASSERT_SAVE_SPTR;
519
e22024d3 520 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
79072805
LW
521}
522
c3564e5c 523void
09edbca0 524Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
c3564e5c 525{
a3444cc5
DM
526 dSS_ADD;
527
f3548bdc 528 ASSERT_CURPAD_ACTIVE("save_padsv");
a3444cc5
DM
529 SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
530 SS_ADD_PTR(PL_comppad);
03dba561 531 SS_ADD_UV((UV)off);
a3444cc5
DM
532 SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
533 SS_ADD_END(4);
c3564e5c
GS
534}
535
79072805 536void
864dbfa3 537Perl_save_hptr(pTHX_ HV **hptr)
79072805 538{
7918f24d
NC
539 PERL_ARGS_ASSERT_SAVE_HPTR;
540
e22024d3 541 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
79072805
LW
542}
543
544void
864dbfa3 545Perl_save_aptr(pTHX_ AV **aptr)
79072805 546{
7918f24d
NC
547 PERL_ARGS_ASSERT_SAVE_APTR;
548
e22024d3 549 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
79072805
LW
550}
551
552void
2fd8beea 553Perl_save_pushptr(pTHX_ void *const ptr, const int type)
8990e307 554{
a3444cc5
DM
555 dSS_ADD;
556 SS_ADD_PTR(ptr);
557 SS_ADD_UV(type);
558 SS_ADD_END(2);
8990e307
LW
559}
560
561void
864dbfa3 562Perl_save_clearsv(pTHX_ SV **svp)
8990e307 563{
cdcdfc56
NC
564 const UV offset = svp - PL_curpad;
565 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
7918f24d
NC
566
567 PERL_ARGS_ASSERT_SAVE_CLEARSV;
568
f3548bdc 569 ASSERT_CURPAD_ACTIVE("save_clearsv");
623e28c6 570 SvPADSTALE_off(*svp); /* mark lexical as active */
5d9574c1 571 if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
cdcdfc56
NC
572 Perl_croak(aTHX_ "panic: pad offset %"UVuf" out of range (%p-%p)",
573 offset, svp, PL_curpad);
a3444cc5 574 }
cdcdfc56 575
a3444cc5
DM
576 {
577 dSS_ADD;
578 SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
579 SS_ADD_END(1);
580 }
8990e307
LW
581}
582
583void
864dbfa3 584Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
8990e307 585{
7918f24d
NC
586 PERL_ARGS_ASSERT_SAVE_DELETE;
587
85a721ca 588 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
8990e307
LW
589}
590
591void
af097752
VP
592Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
593{
594 STRLEN len;
595 I32 klen;
596 const char *key;
597
598 PERL_ARGS_ASSERT_SAVE_HDELETE;
599
600 key = SvPV_const(keysv, len);
601 klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
602 SvREFCNT_inc_simple_void_NN(hv);
603 save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
604}
605
606void
c70927a6 607Perl_save_adelete(pTHX_ AV *av, SSize_t key)
c68ec7a9 608{
c70927a6 609 dSS_ADD;
c68ec7a9
VP
610
611 PERL_ARGS_ASSERT_SAVE_ADELETE;
612
613 SvREFCNT_inc_void(av);
c70927a6
FC
614 SS_ADD_UV(key);
615 SS_ADD_PTR(av);
616 SS_ADD_IV(SAVEt_ADELETE);
617 SS_ADD_END(3);
c68ec7a9
VP
618}
619
620void
12ab1f58
JH
621Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
622{
a3444cc5 623 dSS_ADD;
7918f24d
NC
624 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
625
a3444cc5
DM
626 SS_ADD_DPTR(f);
627 SS_ADD_PTR(p);
628 SS_ADD_UV(SAVEt_DESTRUCTOR);
629 SS_ADD_END(3);
12ab1f58
JH
630}
631
632void
146174a9
CB
633Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
634{
a3444cc5
DM
635 dSS_ADD;
636
637 SS_ADD_DXPTR(f);
638 SS_ADD_PTR(p);
639 SS_ADD_UV(SAVEt_DESTRUCTOR_X);
640 SS_ADD_END(3);
146174a9
CB
641}
642
643void
da8315f8
NC
644Perl_save_hints(pTHX)
645{
20439bc7 646 COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
da8315f8 647 if (PL_hints & HINT_LOCALIZE_HH) {
52c7aca6
FC
648 HV *oldhh = GvHV(PL_hintgv);
649 save_pushptri32ptr(oldhh, PL_hints, save_cophh, SAVEt_HINTS);
650 GvHV(PL_hintgv) = NULL; /* in case copying dies */
651 GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
be84297e 652 } else {
20439bc7 653 save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS);
da8315f8 654 }
be84297e
NC
655}
656
657static void
658S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
659 const int type)
660{
a3444cc5
DM
661 dSS_ADD;
662 SS_ADD_PTR(ptr1);
663 SS_ADD_INT(i);
664 SS_ADD_PTR(ptr2);
665 SS_ADD_UV(type);
666 SS_ADD_END(4);
da8315f8
NC
667}
668
669void
c70927a6
FC
670Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
671 const U32 flags)
4e4c362e 672{
20b7effb 673 dSS_ADD;
bfc4de9f 674 SV *sv;
7918f24d 675
91d1c79f 676 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
7918f24d 677
0cbee0a4 678 SvGETMAGIC(*sptr);
c70927a6
FC
679 SS_ADD_PTR(SvREFCNT_inc_simple(av));
680 SS_ADD_IV(idx);
681 SS_ADD_PTR(SvREFCNT_inc(*sptr));
682 SS_ADD_UV(SAVEt_AELEM);
683 SS_ADD_END(4);
1cdc9186
FC
684 /* The array needs to hold a reference count on its new element, so it
685 must be AvREAL. */
5d9574c1 686 if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
1cdc9186 687 av_reify(av);
91d1c79f 688 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
75d34a09
VP
689 if (flags & SAVEf_KEEPOLDELEM)
690 return;
bfc4de9f
DM
691 sv = *sptr;
692 /* If we're localizing a tied array element, this new sv
693 * won't actually be stored in the array - so it won't get
694 * reaped when the localize ends. Ensure it gets reaped by
695 * mortifying it instead. DAPM */
5d9574c1 696 if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
bfc4de9f 697 sv_2mortal(sv);
4e4c362e
GS
698}
699
700void
af7df257 701Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
4e4c362e 702{
bfc4de9f 703 SV *sv;
7918f24d 704
af7df257 705 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
7918f24d 706
0cbee0a4 707 SvGETMAGIC(*sptr);
a3444cc5
DM
708 {
709 dSS_ADD;
710 SS_ADD_PTR(SvREFCNT_inc_simple(hv));
711 SS_ADD_PTR(newSVsv(key));
712 SS_ADD_PTR(SvREFCNT_inc(*sptr));
713 SS_ADD_UV(SAVEt_HELEM);
714 SS_ADD_END(4);
715 }
af7df257 716 save_scalar_at(sptr, flags);
75d34a09
VP
717 if (flags & SAVEf_KEEPOLDELEM)
718 return;
bfc4de9f
DM
719 sv = *sptr;
720 /* If we're localizing a tied hash element, this new sv
721 * won't actually be stored in the hash - so it won't get
722 * reaped when the localize ends. Ensure it gets reaped by
723 * mortifying it instead. DAPM */
5d9574c1 724 if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
bfc4de9f 725 sv_2mortal(sv);
4e4c362e
GS
726}
727
2053acbf
NC
728SV*
729Perl_save_svref(pTHX_ SV **sptr)
730{
7918f24d
NC
731 PERL_ARGS_ASSERT_SAVE_SVREF;
732
2053acbf 733 SvGETMAGIC(*sptr);
e22024d3 734 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
af7df257 735 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
2053acbf
NC
736}
737
2ef9a108
DM
738
739void
740Perl_savetmps(pTHX)
741{
742 dSS_ADD;
743 SS_ADD_IV(PL_tmps_floor);
744 PL_tmps_floor = PL_tmps_ix;
745 SS_ADD_UV(SAVEt_TMPSFLOOR);
746 SS_ADD_END(2);
747}
748
749
455ece5e 750I32
864dbfa3 751Perl_save_alloc(pTHX_ I32 size, I32 pad)
455ece5e 752{
eb578fdb
KW
753 const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
754 - (char*)PL_savestack);
1be36ce0
NC
755 const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
756 const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
455ece5e 757
5d9574c1 758 if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
93a641ae
DM
759 Perl_croak(aTHX_
760 "panic: save_alloc elems %"UVuf" out of range (%"IVdf"-%"IVdf")",
761 elems, (IV)size, (IV)pad);
1be36ce0
NC
762
763 SSGROW(elems + 1);
455ece5e
AD
764
765 PL_savestack_ix += elems;
1be36ce0 766 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
455ece5e
AD
767 return start;
768}
769
03dba561
DM
770
771
772#define ARG0_SV MUTABLE_SV(arg0.any_ptr)
773#define ARG0_AV MUTABLE_AV(arg0.any_ptr)
774#define ARG0_HV MUTABLE_HV(arg0.any_ptr)
775#define ARG0_PTR arg0.any_ptr
776#define ARG0_PV (char*)(arg0.any_ptr)
777#define ARG0_PVP (char**)(arg0.any_ptr)
778#define ARG0_I32 (arg0.any_i32)
779
780#define ARG1_SV MUTABLE_SV(arg1.any_ptr)
781#define ARG1_AV MUTABLE_AV(arg1.any_ptr)
782#define ARG1_GV MUTABLE_GV(arg1.any_ptr)
783#define ARG1_SVP (SV**)(arg1.any_ptr)
784#define ARG1_PVP (char**)(arg1.any_ptr)
785#define ARG1_PTR arg1.any_ptr
786#define ARG1_PV (char*)(arg1.any_ptr)
787#define ARG1_I32 (arg1.any_i32)
788
789#define ARG2_SV MUTABLE_SV(arg2.any_ptr)
790#define ARG2_AV MUTABLE_AV(arg2.any_ptr)
791#define ARG2_HV MUTABLE_HV(arg2.any_ptr)
792#define ARG2_GV MUTABLE_GV(arg2.any_ptr)
793#define ARG2_PV (char*)(arg2.any_ptr)
794
462e5cf6 795void
864dbfa3 796Perl_leave_scope(pTHX_ I32 base)
79072805 797{
302c0c93 798 /* Localise the effects of the TAINT_NOT inside the loop. */
284167a5 799 bool was = TAINT_get;
79072805 800
51418c6a
JH
801 I32 i;
802 SV *sv;
803
03dba561
DM
804 ANY arg0, arg1, arg2;
805
806 /* these initialisations are logically unnecessary, but they shut up
807 * spurious 'may be used uninitialized' compiler warnings */
808 arg0.any_ptr = NULL;
809 arg1.any_ptr = NULL;
810 arg2.any_ptr = NULL;
811
5d9574c1 812 if (UNLIKELY(base < -1))
5637ef5b 813 Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
1c98cc53
DM
814 DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
815 (long)PL_savestack_ix, (long)base));
3280af22 816 while (PL_savestack_ix > base) {
03dba561
DM
817 UV uv;
818 U8 type;
819
820 SV *refsv;
821 SV **svp;
822
c6ae7647
NC
823 TAINT_NOT;
824
03dba561
DM
825 {
826 I32 ix = PL_savestack_ix - 1;
827 ANY *p = &PL_savestack[ix];
828 uv = p->any_uv;
829 type = (U8)uv & SAVE_MASK;
830 if (type > SAVEt_ARG0_MAX) {
831 ANY *p0 = p;
832 arg0 = *--p;
833 if (type > SAVEt_ARG1_MAX) {
834 arg1 = *--p;
835 if (type > SAVEt_ARG2_MAX) {
836 arg2 = *--p;
837 }
838 }
839 ix -= (p0 - p);
840 }
841 PL_savestack_ix = ix;
842 }
843
c6bf6a65 844 switch (type) {
79072805 845 case SAVEt_ITEM: /* normal string */
03dba561 846 sv_replace(ARG1_SV, ARG0_SV);
5d9574c1 847 if (UNLIKELY(SvSMAGICAL(ARG1_SV))) {
b492a59e
DM
848 PL_localizing = 2;
849 mg_set(ARG1_SV);
850 PL_localizing = 0;
851 }
79072805 852 break;
03dba561
DM
853
854 /* This would be a mathom, but Perl_save_svref() calls a static
855 function, S_save_scalar_at(), so has to stay in this file. */
856 case SAVEt_SVREF: /* scalar reference */
857 svp = ARG1_SVP;
858 refsv = NULL; /* what to refcnt_dec */
859 goto restore_sv;
860
8aacddc1 861 case SAVEt_SV: /* scalar reference */
03dba561
DM
862 svp = &GvSV(ARG1_GV);
863 refsv = ARG1_SV; /* what to refcnt_dec */
2053acbf 864 restore_sv:
03dba561
DM
865 {
866 SV * const sv = *svp;
867 *svp = ARG0_SV;
2053acbf 868 SvREFCNT_dec(sv);
5d9574c1 869 if (UNLIKELY(SvSMAGICAL(ARG0_SV))) {
2baebb98
DM
870 /* mg_set could die, skipping the freeing of ARG0_SV and
871 * refsv; Ensure that they're always freed in that case */
872 dSS_ADD;
873 SS_ADD_PTR(ARG0_SV);
874 SS_ADD_UV(SAVEt_FREESV);
875 SS_ADD_PTR(refsv);
876 SS_ADD_UV(SAVEt_FREESV);
877 SS_ADD_END(4);
b492a59e
DM
878 PL_localizing = 2;
879 mg_set(ARG0_SV);
880 PL_localizing = 0;
2baebb98 881 break;
b492a59e 882 }
4a9a56a7 883 SvREFCNT_dec_NN(ARG0_SV);
03dba561 884 SvREFCNT_dec(refsv);
2053acbf 885 break;
03dba561 886 }
8aacddc1 887 case SAVEt_GENERIC_PVREF: /* generic pv */
03dba561
DM
888 if (*ARG0_PVP != ARG1_PV) {
889 Safefree(*ARG0_PVP);
890 *ARG0_PVP = ARG1_PV;
f4dd75d9
GS
891 }
892 break;
05ec9bb3 893 case SAVEt_SHARED_PVREF: /* shared pv */
03dba561 894 if (*ARG1_PVP != ARG0_PV) {
5e54c26f 895#ifdef NETWARE
03dba561 896 PerlMem_free(*ARG1_PVP);
5e54c26f 897#else
03dba561 898 PerlMemShared_free(*ARG1_PVP);
5e54c26f 899#endif
03dba561 900 *ARG1_PVP = ARG0_PV;
05ec9bb3
NIS
901 }
902 break;
f83b46a0 903 case SAVEt_GVSV: /* scalar slot in GV */
03dba561 904 svp = &GvSV(ARG1_GV);
f83b46a0 905 goto restore_svp;
8aacddc1 906 case SAVEt_GENERIC_SVREF: /* generic sv */
03dba561 907 svp = ARG1_SVP;
f83b46a0 908 restore_svp:
03dba561
DM
909 {
910 SV * const sv = *svp;
911 *svp = ARG0_SV;
f4dd75d9 912 SvREFCNT_dec(sv);
03dba561 913 SvREFCNT_dec(ARG0_SV);
b9d12d37 914 break;
03dba561 915 }
db9306af 916 case SAVEt_GVSLOT: /* any slot in GV */
03dba561
DM
917 {
918 HV *const hv = GvSTASH(ARG2_GV);
919 svp = ARG1_SVP;
db9306af 920 if (hv && HvENAME(hv) && (
03dba561
DM
921 (ARG0_SV && SvTYPE(ARG0_SV) == SVt_PVCV)
922 || (*svp && SvTYPE(*svp) == SVt_PVCV)
db9306af
FC
923 ))
924 {
03dba561
DM
925 if ((char *)svp < (char *)GvGP(ARG2_GV)
926 || (char *)svp > (char *)GvGP(ARG2_GV) + sizeof(struct gp)
a3d47e0d 927 || GvREFCNT(ARG2_GV) > 2) /* "> 2" to ignore savestack's ref */
db9306af
FC
928 PL_sub_generation++;
929 else mro_method_changed_in(hv);
930 }
931 goto restore_svp;
03dba561 932 }
8aacddc1 933 case SAVEt_AV: /* array reference */
03dba561
DM
934 SvREFCNT_dec(GvAV(ARG1_GV));
935 GvAV(ARG1_GV) = ARG0_AV;
2baebb98 936 avhv_common:
5d9574c1 937 if (UNLIKELY(SvSMAGICAL(ARG0_SV))) {
2baebb98
DM
938 /* mg_set might die, so make sure ARG1 isn't leaked */
939 dSS_ADD;
940 SS_ADD_PTR(ARG1_SV);
941 SS_ADD_UV(SAVEt_FREESV);
942 SS_ADD_END(2);
b492a59e
DM
943 PL_localizing = 2;
944 mg_set(ARG0_SV);
945 PL_localizing = 0;
2baebb98 946 break;
b492a59e 947 }
4a9a56a7 948 SvREFCNT_dec_NN(ARG1_GV);
8aacddc1
NIS
949 break;
950 case SAVEt_HV: /* hash reference */
03dba561
DM
951 SvREFCNT_dec(GvHV(ARG1_GV));
952 GvHV(ARG1_GV) = ARG0_HV;
2baebb98
DM
953 goto avhv_common;
954
994d373a 955 case SAVEt_INT_SMALL:
03dba561 956 *(int*)ARG0_PTR = (int)(uv >> SAVE_TIGHT_SHIFT);
994d373a 957 break;
79072805 958 case SAVEt_INT: /* int reference */
03dba561 959 *(int*)ARG0_PTR = (int)ARG1_I32;
79072805 960 break;
e8eb279c
FC
961 case SAVEt_STRLEN: /* STRLEN/size_t ref */
962 *(STRLEN*)ARG0_PTR = (STRLEN)arg1.any_iv;
963 break;
2ef9a108
DM
964 case SAVEt_TMPSFLOOR: /* restore PL_tmps_floor */
965 PL_tmps_floor = (SSize_t)arg0.any_iv;
966 break;
9febdf04 967 case SAVEt_BOOL: /* bool reference */
03dba561 968 *(bool*)ARG0_PTR = cBOOL(uv >> 8);
9a9b5ec9
DM
969#ifdef NO_TAINT_SUPPORT
970 PERL_UNUSED_VAR(was);
971#else
5d9574c1 972 if (UNLIKELY(ARG0_PTR == &(TAINT_get))) {
b6f93e7a
KW
973 /* If we don't update <was>, to reflect what was saved on the
974 * stack for PL_tainted, then we will overwrite this attempt to
975 * restore it when we exit this routine. Note that this won't
976 * work if this value was saved in a wider-than necessary type,
977 * such as I32 */
03dba561 978 was = *(bool*)ARG0_PTR;
b6f93e7a 979 }
284167a5 980#endif
9febdf04 981 break;
89abef21 982 case SAVEt_I32_SMALL:
03dba561 983 *(I32*)ARG0_PTR = (I32)(uv >> SAVE_TIGHT_SHIFT);
89abef21 984 break;
79072805 985 case SAVEt_I32: /* I32 reference */
3235b7a3 986#ifdef PERL_DEBUG_READONLY_OPS
03dba561 987 if (*(I32*)ARG0_PTR != ARG1_I32)
3235b7a3 988#endif
03dba561 989 *(I32*)ARG0_PTR = ARG1_I32;
79072805
LW
990 break;
991 case SAVEt_SPTR: /* SV* reference */
03dba561 992 *(SV**)(ARG0_PTR)= ARG1_SV;
79072805 993 break;
146174a9 994 case SAVEt_VPTR: /* random* reference */
85e6fe83 995 case SAVEt_PPTR: /* char* reference */
03dba561 996 *ARG0_PVP = ARG1_PV;
85e6fe83 997 break;
79072805 998 case SAVEt_HPTR: /* HV* reference */
03dba561 999 *(HV**)ARG0_PTR = MUTABLE_HV(ARG1_PTR);
79072805
LW
1000 break;
1001 case SAVEt_APTR: /* AV* reference */
03dba561 1002 *(AV**)ARG0_PTR = ARG1_AV;
79072805 1003 break;
fb73857a 1004 case SAVEt_GP: /* scalar reference */
03dba561
DM
1005 {
1006 HV *hv;
1007 /* possibly taking a method out of circulation */
1008 const bool had_method = !!GvCVu(ARG1_GV);
1009 gp_free(ARG1_GV);
1010 GvGP_set(ARG1_GV, (GP*)ARG0_PTR);
1011 if ((hv=GvSTASH(ARG1_GV)) && HvENAME_get(hv)) {
1012 if ( GvNAMELEN(ARG1_GV) == 3
1013 && strnEQ(GvNAME(ARG1_GV), "ISA", 3)
1014 )
1015 mro_isa_changed_in(hv);
1016 else if (had_method || GvCVu(ARG1_GV))
1017 /* putting a method back into circulation ("local")*/
1018 gv_method_changed(ARG1_GV);
3d460042 1019 }
4a9a56a7 1020 SvREFCNT_dec_NN(ARG1_GV);
8aacddc1 1021 break;
03dba561 1022 }
8990e307 1023 case SAVEt_FREESV:
03dba561 1024 SvREFCNT_dec(ARG0_SV);
8990e307 1025 break;
0f94cb1f
FC
1026 case SAVEt_FREEPADNAME:
1027 PadnameREFCNT_dec((PADNAME *)ARG0_PTR);
1028 break;
3987a177 1029 case SAVEt_FREECOPHH:
03dba561 1030 cophh_free((COPHH *)ARG0_PTR);
3987a177 1031 break;
26d9b02f 1032 case SAVEt_MORTALIZESV:
03dba561 1033 sv_2mortal(ARG0_SV);
26d9b02f 1034 break;
8990e307 1035 case SAVEt_FREEOP:
5e5ba94b 1036 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
03dba561 1037 op_free((OP*)ARG0_PTR);
8990e307
LW
1038 break;
1039 case SAVEt_FREEPV:
03dba561 1040 Safefree(ARG0_PTR);
8990e307 1041 break;
4e09461c 1042
4e09461c
DM
1043 case SAVEt_CLEARPADRANGE:
1044 i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
1045 svp = &PL_curpad[uv >>
1046 (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
1047 goto clearsv;
8990e307 1048 case SAVEt_CLEARSV:
4e09461c
DM
1049 svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
1050 i = 1;
1051 clearsv:
1052 for (; i; i--, svp--) {
528ad060
DM
1053 sv = *svp;
1054
1055 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
1056 "Pad 0x%"UVxf"[0x%"UVxf"] clearsv: %ld sv=0x%"UVxf"<%"IVdf"> %s\n",
1057 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1058 (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
1059 (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
1060 ));
1061
1062 /* Can clear pad variable in place? */
9af15990 1063 if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) {
a07f0bef 1064
a07f0bef
DM
1065 /* these flags are the union of all the relevant flags
1066 * in the individual conditions within */
1067 if (UNLIKELY(SvFLAGS(sv) & (
a623f893 1068 SVf_READONLY|SVf_PROTECT /*for SvREADONLY_off*/
a07f0bef 1069 | (SVs_GMG|SVs_SMG|SVs_RMG) /* SvMAGICAL() */
7532eaae 1070 | SVf_OOK
a07f0bef
DM
1071 | SVf_THINKFIRST)))
1072 {
7500fc82
DM
1073 /* if a my variable that was made readonly is
1074 * going out of scope, we want to remove the
1075 * readonlyness so that it can go out of scope
1076 * quietly
1077 */
57c404c9 1078 if (SvREADONLY(sv))
7500fc82
DM
1079 SvREADONLY_off(sv);
1080
7532eaae
DM
1081 if (SvOOK(sv)) { /* OOK or HvAUX */
1082 if (SvTYPE(sv) == SVt_PVHV)
1083 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
1084 else
1085 sv_backoff(sv);
1086 }
1087
7500fc82 1088 if (SvMAGICAL(sv)) {
7532eaae
DM
1089 /* note that backrefs (either in HvAUX or magic)
1090 * must be removed before other magic */
7500fc82
DM
1091 sv_unmagic(sv, PERL_MAGIC_backref);
1092 if (SvTYPE(sv) != SVt_PVCV)
1093 mg_free(sv);
1094 }
1095 if (SvTHINKFIRST(sv))
1096 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
1097 |SV_COW_DROP_PV);
528ad060 1098
a07f0bef 1099 }
528ad060
DM
1100 switch (SvTYPE(sv)) {
1101 case SVt_NULL:
1102 break;
1103 case SVt_PVAV:
1104 av_clear(MUTABLE_AV(sv));
1105 break;
1106 case SVt_PVHV:
1107 hv_clear(MUTABLE_HV(sv));
1108 break;
1109 case SVt_PVCV:
1110 {
db5cc3ee 1111 HEK *hek =
db5cc3ee 1112 CvNAMED(sv)
9c98a81f
FC
1113 ? CvNAME_HEK((CV *)sv)
1114 : GvNAME_HEK(CvGV(sv));
528ad060 1115 assert(hek);
6110d17a 1116 (void)share_hek_hek(hek);
528ad060
DM
1117 cv_undef((CV *)sv);
1118 CvNAME_HEK_set(sv, hek);
f3feca7a 1119 CvLEXICAL_on(sv);
528ad060
DM
1120 break;
1121 }
1122 default:
c79d0076
NC
1123 /* This looks odd, but these two macros are for use in
1124 expressions and finish with a trailing comma, so
1125 adding a ; after them would be wrong. */
1126 assert_not_ROK(sv)
1127 assert_not_glob(sv)
5c85b638 1128 SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8);
528ad060
DM
1129 break;
1130 }
82e85a9c 1131 SvPADTMP_off(sv);
528ad060
DM
1132 SvPADSTALE_on(sv); /* mark as no longer live */
1133 }
1134 else { /* Someone has a claim on this, so abandon it. */
528ad060
DM
1135 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
1136 case SVt_PVAV: *svp = MUTABLE_SV(newAV()); break;
1137 case SVt_PVHV: *svp = MUTABLE_SV(newHV()); break;
1138 case SVt_PVCV:
1139 {
9c98a81f
FC
1140 HEK * const hek = CvNAMED(sv)
1141 ? CvNAME_HEK((CV *)sv)
1142 : GvNAME_HEK(CvGV(sv));
1143
528ad060
DM
1144 /* Create a stub */
1145 *svp = newSV_type(SVt_PVCV);
1146
1147 /* Share name */
528ad060 1148 CvNAME_HEK_set(*svp,
9c98a81f 1149 share_hek_hek(hek));
f3feca7a 1150 CvLEXICAL_on(*svp);
528ad060
DM
1151 break;
1152 }
1153 default: *svp = newSV(0); break;
1154 }
4a9a56a7 1155 SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
528ad060
DM
1156 /* preserve pad nature, but also mark as not live
1157 * for any closure capturing */
145bf8ee 1158 SvFLAGS(*svp) |= SVs_PADSTALE;
528ad060 1159 }
4e09461c 1160 }
8990e307
LW
1161 break;
1162 case SAVEt_DELETE:
03dba561
DM
1163 (void)hv_delete(ARG0_HV, ARG2_PV, ARG1_I32, G_DISCARD);
1164 SvREFCNT_dec(ARG0_HV);
1165 Safefree(arg2.any_ptr);
8990e307 1166 break;
c68ec7a9 1167 case SAVEt_ADELETE:
c70927a6 1168 (void)av_delete(ARG0_AV, arg1.any_iv, G_DISCARD);
03dba561 1169 SvREFCNT_dec(ARG0_AV);
c68ec7a9 1170 break;
146174a9 1171 case SAVEt_DESTRUCTOR_X:
03dba561 1172 (*arg1.any_dxptr)(aTHX_ ARG0_PTR);
a0d0e21e
LW
1173 break;
1174 case SAVEt_REGCONTEXT:
e0fa7e2b 1175 /* regexp must have croaked */
455ece5e 1176 case SAVEt_ALLOC:
1be36ce0 1177 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
a0d0e21e 1178 break;
55497cff 1179 case SAVEt_STACK_POS: /* Position on Perl stack */
03dba561 1180 PL_stack_sp = PL_stack_base + arg0.any_i32;
55497cff 1181 break;
161b7d16 1182 case SAVEt_AELEM: /* array element */
c70927a6 1183 svp = av_fetch(ARG2_AV, arg1.any_iv, 1);
5d9574c1 1184 if (UNLIKELY(!AvREAL(ARG2_AV) && AvREIFY(ARG2_AV))) /* undo reify guard */
03dba561 1185 SvREFCNT_dec(ARG0_SV);
5d9574c1 1186 if (LIKELY(svp)) {
03dba561 1187 SV * const sv = *svp;
5d9574c1
DM
1188 if (LIKELY(sv && sv != &PL_sv_undef)) {
1189 if (UNLIKELY(SvTIED_mg((const SV *)ARG2_AV, PERL_MAGIC_tied)))
b37c2d43 1190 SvREFCNT_inc_void_NN(sv);
03dba561 1191 refsv = ARG2_SV;
4e4c362e
GS
1192 goto restore_sv;
1193 }
1194 }
03dba561
DM
1195 SvREFCNT_dec(ARG2_AV);
1196 SvREFCNT_dec(ARG0_SV);
4e4c362e 1197 break;
161b7d16 1198 case SAVEt_HELEM: /* hash element */
03dba561
DM
1199 {
1200 HE * const he = hv_fetch_ent(ARG2_HV, ARG1_SV, 1, 0);
1201 SvREFCNT_dec(ARG1_SV);
5d9574c1 1202 if (LIKELY(he)) {
03dba561 1203 const SV * const oval = HeVAL(he);
5d9574c1 1204 if (LIKELY(oval && oval != &PL_sv_undef)) {
03dba561 1205 svp = &HeVAL(he);
5d9574c1 1206 if (UNLIKELY(SvTIED_mg((const SV *)ARG2_HV, PERL_MAGIC_tied)))
03dba561
DM
1207 SvREFCNT_inc_void(*svp);
1208 refsv = ARG2_SV; /* what to refcnt_dec */
4e4c362e
GS
1209 goto restore_sv;
1210 }
1211 }
03dba561
DM
1212 SvREFCNT_dec(ARG2_HV);
1213 SvREFCNT_dec(ARG0_SV);
4e4c362e 1214 break;
03dba561 1215 }
462e5cf6 1216 case SAVEt_OP:
03dba561 1217 PL_op = (OP*)ARG0_PTR;
462e5cf6 1218 break;
25eaa213 1219 case SAVEt_HINTS:
3607ca02
FC
1220 if ((PL_hints & HINT_LOCALIZE_HH)) {
1221 while (GvHV(PL_hintgv)) {
2653c1e3 1222 HV *hv = GvHV(PL_hintgv);
045ac317 1223 GvHV(PL_hintgv) = NULL;
2653c1e3 1224 SvREFCNT_dec(MUTABLE_SV(hv));
3607ca02 1225 }
045ac317 1226 }
20439bc7 1227 cophh_free(CopHINTHASH_get(&PL_compiling));
03dba561
DM
1228 CopHINTHASH_set(&PL_compiling, (COPHH*)ARG0_PTR);
1229 *(I32*)&PL_hints = ARG1_I32;
dfa41748 1230 if (PL_hints & HINT_LOCALIZE_HH) {
ad64d0ec 1231 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
85fbaab2 1232 GvHV(PL_hintgv) = MUTABLE_HV(SSPOPPTR);
2653c1e3
DM
1233 }
1234 if (!GvHV(PL_hintgv)) {
a3fb8386
FC
1235 /* Need to add a new one manually, else rv2hv can
1236 add one via GvHVn and it won't have the magic set. */
5b9c0671
NC
1237 HV *const hv = newHV();
1238 hv_magic(hv, NULL, PERL_MAGIC_hints);
1239 GvHV(PL_hintgv) = hv;
dfa41748 1240 }
5b9c0671 1241 assert(GvHV(PL_hintgv));
b3ac6de7 1242 break;
cb50131a 1243 case SAVEt_COMPPAD:
03dba561 1244 PL_comppad = (PAD*)ARG0_PTR;
5d9574c1 1245 if (LIKELY(PL_comppad))
cb50131a
CB
1246 PL_curpad = AvARRAY(PL_comppad);
1247 else
4608196e 1248 PL_curpad = NULL;
cb50131a 1249 break;
09edbca0 1250 case SAVEt_PADSV_AND_MORTALIZE:
c3564e5c 1251 {
09edbca0 1252 SV **svp;
03dba561
DM
1253 assert (ARG1_PTR);
1254 svp = AvARRAY((PAD*)ARG1_PTR) + (PADOFFSET)arg0.any_uv;
b405d38b
DM
1255 /* This mortalizing used to be done by CX_POOPLOOP() via
1256 itersave. But as we have all the information here, we
1257 can do it here, save even having to have itersave in
1258 the struct.
1259 */
09edbca0 1260 sv_2mortal(*svp);
03dba561 1261 *svp = ARG2_SV;
c3564e5c
GS
1262 }
1263 break;
8b7059b1
DM
1264 case SAVEt_SAVESWITCHSTACK:
1265 {
1266 dSP;
03dba561
DM
1267 SWITCHSTACK(ARG0_AV, ARG1_AV);
1268 PL_curstackinfo->si_stack = ARG1_AV;
8b7059b1
DM
1269 }
1270 break;
14f338dc 1271 case SAVEt_SET_SVFLAGS:
03dba561
DM
1272 SvFLAGS(ARG2_SV) &= ~((U32)ARG1_I32);
1273 SvFLAGS(ARG2_SV) |= (U32)ARG0_I32;
14f338dc 1274 break;
95e06916 1275
95e06916
NC
1276 /* These are only saved in mathoms.c */
1277 case SAVEt_NSTAB:
03dba561 1278 (void)sv_clear(ARG0_SV);
95e06916 1279 break;
2053acbf 1280 case SAVEt_LONG: /* long reference */
03dba561 1281 *(long*)ARG0_PTR = arg1.any_long;
2053acbf 1282 break;
95e06916 1283 case SAVEt_IV: /* IV reference */
03dba561 1284 *(IV*)ARG0_PTR = arg1.any_iv;
95e06916
NC
1285 break;
1286
2053acbf 1287 case SAVEt_I16: /* I16 reference */
03dba561 1288 *(I16*)ARG0_PTR = (I16)(uv >> 8);
2053acbf
NC
1289 break;
1290 case SAVEt_I8: /* I8 reference */
03dba561 1291 *(I8*)ARG0_PTR = (I8)(uv >> 8);
2053acbf 1292 break;
2053acbf 1293 case SAVEt_DESTRUCTOR:
03dba561 1294 (*arg1.any_dptr)(ARG0_PTR);
2053acbf 1295 break;
68da3b2f 1296 case SAVEt_COMPILE_WARNINGS:
68da3b2f
NC
1297 if (!specialWARN(PL_compiling.cop_warnings))
1298 PerlMemShared_free(PL_compiling.cop_warnings);
72dc9ed5 1299
03dba561 1300 PL_compiling.cop_warnings = (STRLEN*)ARG0_PTR;
72dc9ed5 1301 break;
7c197c94 1302 case SAVEt_PARSER:
03dba561 1303 parser_free((yy_parser *) ARG0_PTR);
7c197c94 1304 break;
20d5dc23
FC
1305 case SAVEt_READONLY_OFF:
1306 SvREADONLY_off(ARG0_SV);
1307 break;
79072805 1308 default:
5637ef5b 1309 Perl_croak(aTHX_ "panic: leave_scope inconsistency %u", type);
79072805
LW
1310 }
1311 }
302c0c93 1312
284167a5 1313 TAINT_set(was);
79072805 1314}
8990e307 1315
8990e307 1316void
864dbfa3 1317Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 1318{
7918f24d
NC
1319 PERL_ARGS_ASSERT_CX_DUMP;
1320
35ff7856 1321#ifdef DEBUGGING
22c35a8c 1322 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 1323 if (CxTYPE(cx) != CXt_SUBST) {
e8d9e9d0 1324 const char *gimme_text;
760ac839 1325 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
146174a9
CB
1326 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
1327 PTR2UV(cx->blk_oldcop));
760ac839
LW
1328 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1329 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
e992140c 1330 PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
146174a9
CB
1331 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
1332 PTR2UV(cx->blk_oldpm));
e8d9e9d0
VP
1333 switch (cx->blk_gimme) {
1334 case G_VOID:
1335 gimme_text = "VOID";
1336 break;
1337 case G_SCALAR:
1338 gimme_text = "SCALAR";
1339 break;
1340 case G_ARRAY:
1341 gimme_text = "LIST";
1342 break;
1343 default:
1344 gimme_text = "UNKNOWN";
1345 break;
1346 }
1347 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
8990e307 1348 }
6b35e009 1349 switch (CxTYPE(cx)) {
8990e307
LW
1350 case CXt_NULL:
1351 case CXt_BLOCK:
1352 break;
146174a9 1353 case CXt_FORMAT:
f9c764c5
NC
1354 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%"UVxf"\n",
1355 PTR2UV(cx->blk_format.cv));
1356 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%"UVxf"\n",
1357 PTR2UV(cx->blk_format.gv));
1358 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%"UVxf"\n",
1359 PTR2UV(cx->blk_format.dfoutgv));
1360 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
bafb2adc 1361 (int)CxHASARGS(cx));
f9c764c5
NC
1362 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%"UVxf"\n",
1363 PTR2UV(cx->blk_format.retop));
146174a9 1364 break;
8990e307 1365 case CXt_SUB:
146174a9
CB
1366 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
1367 PTR2UV(cx->blk_sub.cv));
760ac839 1368 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 1369 (long)cx->blk_sub.olddepth);
760ac839 1370 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
bafb2adc
NC
1371 (int)CxHASARGS(cx));
1372 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
f39bc417
DM
1373 PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n",
1374 PTR2UV(cx->blk_sub.retop));
8990e307
LW
1375 break;
1376 case CXt_EVAL:
760ac839 1377 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
85a64632 1378 (long)CxOLD_IN_EVAL(cx));
760ac839 1379 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
85a64632
NC
1380 PL_op_name[CxOLD_OP_TYPE(cx)],
1381 PL_op_desc[CxOLD_OP_TYPE(cx)]);
0f79a09d
GS
1382 if (cx->blk_eval.old_namesv)
1383 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
aa07b2f6 1384 SvPVX_const(cx->blk_eval.old_namesv));
146174a9
CB
1385 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
1386 PTR2UV(cx->blk_eval.old_eval_root));
f39bc417
DM
1387 PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%"UVxf"\n",
1388 PTR2UV(cx->blk_eval.retop));
8990e307
LW
1389 break;
1390
93661e56 1391 case CXt_LOOP_PLAIN:
c6fdafd0 1392 case CXt_LOOP_LAZYIV:
d01136d6 1393 case CXt_LOOP_LAZYSV:
93661e56
DM
1394 case CXt_LOOP_LIST:
1395 case CXt_LOOP_ARY:
0cbdab38 1396 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
022eaa24
NC
1397 PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%"UVxf"\n",
1398 PTR2UV(cx->blk_loop.my_op));
0925dc7f
DM
1399 if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
1400 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
1401 PTR2UV(CxITERVAR(cx)));
1402 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%"UVxf"\n",
1403 PTR2UV(cx->blk_loop.itersave));
1404 /* XXX: not accurate for LAZYSV/IV/LIST */
1405 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
1406 PTR2UV(cx->blk_loop.state_u.ary.ary));
1407 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1408 (long)cx->blk_loop.state_u.ary.ix);
1409 }
8990e307
LW
1410 break;
1411
1412 case CXt_SUBST:
760ac839 1413 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 1414 (long)cx->sb_iters);
760ac839 1415 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 1416 (long)cx->sb_maxiters);
35ef4773
GS
1417 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1418 (long)cx->sb_rflags);
760ac839 1419 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
c5bed6a7 1420 (long)CxONCE(cx));
760ac839 1421 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 1422 cx->sb_orig);
146174a9
CB
1423 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
1424 PTR2UV(cx->sb_dstr));
1425 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
1426 PTR2UV(cx->sb_targ));
1427 PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
1428 PTR2UV(cx->sb_s));
1429 PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
1430 PTR2UV(cx->sb_m));
1431 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
1432 PTR2UV(cx->sb_strend));
1433 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
1434 PTR2UV(cx->sb_rxres));
8990e307
LW
1435 break;
1436 }
65e66c80 1437#else
96a5add6 1438 PERL_UNUSED_CONTEXT;
65e66c80 1439 PERL_UNUSED_ARG(cx);
17c3b450 1440#endif /* DEBUGGING */
35ff7856 1441}
241d1a3b
NC
1442
1443/*
14d04a33 1444 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1445 */