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