This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
If we're going to introduce an @@ array, we'll want to be able to parse $#@ too
[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**
864dbfa3 30Perl_stack_grow(pTHX_ SV **sp, SV **p, int n)
a0d0e21e 31{
97aff369 32 dVAR;
7918f24d
NC
33
34 PERL_ARGS_ASSERT_STACK_GROW;
35
3280af22 36 PL_stack_sp = sp;
2ce36478 37#ifndef STRESS_REALLOC
3280af22 38 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
2ce36478 39#else
6b88bc9c 40 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
2ce36478 41#endif
3280af22 42 return PL_stack_sp;
a0d0e21e
LW
43}
44
2ce36478
SM
45#ifndef STRESS_REALLOC
46#define GROW(old) ((old) * 3 / 2)
47#else
48#define GROW(old) ((old) + 1)
49#endif
50
e336de0d 51PERL_SI *
864dbfa3 52Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
e336de0d 53{
97aff369 54 dVAR;
e336de0d 55 PERL_SI *si;
a02a5408 56 Newx(si, 1, PERL_SI);
e336de0d
GS
57 si->si_stack = newAV();
58 AvREAL_off(si->si_stack);
59 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
3280af22 60 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
e336de0d
GS
61 AvFILLp(si->si_stack) = 0;
62 si->si_prev = 0;
63 si->si_next = 0;
64 si->si_cxmax = cxitems - 1;
65 si->si_cxix = -1;
e788e7d3 66 si->si_type = PERLSI_UNDEF;
a02a5408 67 Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
9965345d
JH
68 /* Without any kind of initialising PUSHSUBST()
69 * in pp_subst() will read uninitialised heap. */
7e337ee0 70 PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT);
e336de0d
GS
71 return si;
72}
73
79072805 74I32
864dbfa3 75Perl_cxinc(pTHX)
79072805 76{
97aff369 77 dVAR;
a3b680e6 78 const IV old_max = cxstack_max;
2ce36478 79 cxstack_max = GROW(cxstack_max);
c09156bb 80 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
9965345d
JH
81 /* Without any kind of initialising deep enough recursion
82 * will end up reading uninitialised PERL_CONTEXTs. */
7e337ee0 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{
97aff369 90 dVAR;
3280af22
NIS
91 if (PL_scopestack_ix == PL_scopestack_max) {
92 PL_scopestack_max = GROW(PL_scopestack_max);
93 Renew(PL_scopestack, PL_scopestack_max, I32);
d343c3ef
GG
94#ifdef DEBUGGING
95 Renew(PL_scopestack_name, PL_scopestack_max, const char*);
520bb150 96#endif
79072805 97 }
d343c3ef
GG
98#ifdef DEBUGGING
99 PL_scopestack_name[PL_scopestack_ix] = "unknown";
100#endif
3280af22 101 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
79072805
LW
102
103}
104
105void
864dbfa3 106Perl_pop_scope(pTHX)
79072805 107{
97aff369 108 dVAR;
35a4481c 109 const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
8990e307 110 LEAVE_SCOPE(oldsave);
79072805
LW
111}
112
113void
864dbfa3 114Perl_markstack_grow(pTHX)
a0d0e21e 115{
97aff369 116 dVAR;
35a4481c
AL
117 const I32 oldmax = PL_markstack_max - PL_markstack;
118 const I32 newmax = GROW(oldmax);
a0d0e21e 119
3280af22
NIS
120 Renew(PL_markstack, newmax, I32);
121 PL_markstack_ptr = PL_markstack + oldmax;
122 PL_markstack_max = PL_markstack + newmax;
a0d0e21e
LW
123}
124
125void
864dbfa3 126Perl_savestack_grow(pTHX)
79072805 127{
97aff369 128 dVAR;
8aacddc1 129 PL_savestack_max = GROW(PL_savestack_max) + 4;
3280af22 130 Renew(PL_savestack, PL_savestack_max, ANY);
79072805
LW
131}
132
4b3c1a47
AE
133void
134Perl_savestack_grow_cnt(pTHX_ I32 need)
135{
97aff369 136 dVAR;
4b3c1a47
AE
137 PL_savestack_max = PL_savestack_ix + need;
138 Renew(PL_savestack, PL_savestack_max, ANY);
139}
140
2ce36478
SM
141#undef GROW
142
79072805 143void
864dbfa3 144Perl_tmps_grow(pTHX_ I32 n)
677b06e3 145{
97aff369 146 dVAR;
677b06e3
GS
147#ifndef STRESS_REALLOC
148 if (n < 128)
149 n = (PL_tmps_max < 512) ? 128 : 512;
150#endif
151 PL_tmps_max = PL_tmps_ix + n + 1;
152 Renew(PL_tmps_stack, PL_tmps_max, SV*);
153}
154
155
156void
864dbfa3 157Perl_free_tmps(pTHX)
79072805 158{
97aff369 159 dVAR;
79072805 160 /* XXX should tmps_floor live in cxstack? */
35a4481c 161 const I32 myfloor = PL_tmps_floor;
3280af22 162 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
901017d6 163 SV* const sv = PL_tmps_stack[PL_tmps_ix];
a0714e2c 164 PL_tmps_stack[PL_tmps_ix--] = NULL;
8aacddc1 165 if (sv && sv != &PL_sv_undef) {
463ee0b2 166 SvTEMP_off(sv);
8990e307 167 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
463ee0b2 168 }
79072805
LW
169 }
170}
171
76e3520e 172STATIC SV *
af7df257 173S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
79072805 174{
97aff369 175 dVAR;
75d34a09
VP
176 SV * osv;
177 register SV *sv;
79072805 178
7918f24d
NC
179 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
180
75d34a09
VP
181 osv = *sptr;
182 sv = (flags & SAVEf_KEEPOLDELEM) ? osv : (*sptr = newSV(0));
183
a0d0e21e 184 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
a0d0e21e 185 if (SvGMAGICAL(osv)) {
35a4481c 186 const bool oldtainted = PL_tainted;
a0d0e21e 187 SvFLAGS(osv) |= (SvFLAGS(osv) &
c268c2a6 188 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
3280af22 189 PL_tainted = oldtainted;
a0d0e21e 190 }
75d34a09
VP
191 if (!(flags & SAVEf_KEEPOLDELEM))
192 mg_localize(osv, sv, (flags & SAVEf_SETMAGIC) != 0);
79072805 193 }
75d34a09 194
79072805
LW
195 return sv;
196}
197
dfcd3de5
NC
198void
199Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
e22024d3
NC
200{
201 dVAR;
202 SSCHECK(3);
203 SSPUSHPTR(ptr1);
204 SSPUSHPTR(ptr2);
c6bf6a65 205 SSPUSHUV(type);
e22024d3
NC
206}
207
7a4c00b4 208SV *
864dbfa3 209Perl_save_scalar(pTHX_ GV *gv)
7a4c00b4 210{
97aff369 211 dVAR;
fb4fc1fa 212 SV ** const sptr = &GvSVn(gv);
7918f24d
NC
213
214 PERL_ARGS_ASSERT_SAVE_SCALAR;
215
27cc343c 216 PL_localizing = 1;
0cbee0a4 217 SvGETMAGIC(*sptr);
27cc343c 218 PL_localizing = 0;
e22024d3 219 save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
af7df257 220 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
7a4c00b4
PP
221}
222
f4dd75d9 223/* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
b9d12d37
GS
224 * restore a global SV to its prior contents, freeing new value. */
225void
864dbfa3 226Perl_save_generic_svref(pTHX_ SV **sptr)
b9d12d37 227{
97aff369 228 dVAR;
7918f24d
NC
229
230 PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
231
e22024d3 232 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
b9d12d37
GS
233}
234
f4dd75d9
GS
235/* Like save_pptr(), but also Safefree()s the new value if it is different
236 * from the old one. Can be used to restore a global char* to its prior
237 * contents, freeing new value. */
238void
239Perl_save_generic_pvref(pTHX_ char **str)
240{
97aff369 241 dVAR;
7918f24d
NC
242
243 PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
244
e22024d3 245 save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
f4dd75d9
GS
246}
247
05ec9bb3
NIS
248/* Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
249 * Can be used to restore a shared global char* to its prior
250 * contents, freeing new value. */
251void
252Perl_save_shared_pvref(pTHX_ char **str)
253{
97aff369 254 dVAR;
7918f24d
NC
255
256 PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
257
e22024d3 258 save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
05ec9bb3
NIS
259}
260
14f338dc
DM
261/* set the SvFLAGS specified by mask to the values in val */
262
263void
264Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
265{
97aff369 266 dVAR;
7918f24d
NC
267
268 PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
269
14f338dc
DM
270 SSCHECK(4);
271 SSPUSHPTR(sv);
272 SSPUSHINT(mask);
273 SSPUSHINT(val);
c6bf6a65 274 SSPUSHUV(SAVEt_SET_SVFLAGS);
14f338dc
DM
275}
276
79072805 277void
864dbfa3 278Perl_save_gp(pTHX_ GV *gv, I32 empty)
79072805 279{
97aff369 280 dVAR;
7918f24d
NC
281
282 PERL_ARGS_ASSERT_SAVE_GP;
283
b9e00b79
LRG
284 SSCHECK(4);
285 SSPUSHINT(SvFAKE(gv));
286 SSPUSHPTR(GvGP(gv));
287 SSPUSHPTR(SvREFCNT_inc(gv));
86e10ae1 288 SSPUSHUV(SAVEt_GP);
b9e00b79
LRG
289
290 /* Don't let the localized GV coerce into non-glob, otherwise we would
291 * not be able to restore GP upon leave from context if that happened */
292 SvFAKE_off(gv);
79072805 293
5f05dabc 294 if (empty) {
12816592 295 GP *gp = Perl_newGP(aTHX_ gv);
146174a9 296
fae75791 297 if (GvCVu(gv))
e1a479c5 298 mro_method_changed_in(GvSTASH(gv)); /* taking a method out of circulation ("local")*/
146174a9
CB
299 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
300 gp->gp_io = newIO();
301 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
302 }
72651472
NC
303#ifdef PERL_DONT_CREATE_GVSV
304 if (gv == PL_errgv) {
305 /* We could scatter this logic everywhere by changing the
306 definition of ERRSV from GvSV() to GvSVn(), but it seems more
307 efficient to do this check once here. */
308 gp->gp_sv = newSV(0);
309 }
310#endif
12816592 311 GvGP(gv) = gp;
5f05dabc
PP
312 }
313 else {
44a8e56a 314 gp_ref(GvGP(gv));
5f05dabc
PP
315 GvINTRO_on(gv);
316 }
79072805 317}
79072805 318
79072805 319AV *
864dbfa3 320Perl_save_ary(pTHX_ GV *gv)
79072805 321{
97aff369 322 dVAR;
901017d6 323 AV * const oav = GvAVn(gv);
67a38de0 324 AV *av;
fb73857a 325
7918f24d
NC
326 PERL_ARGS_ASSERT_SAVE_ARY;
327
67a38de0
NIS
328 if (!AvREAL(oav) && AvREIFY(oav))
329 av_reify(oav);
e22024d3 330 save_pushptrptr(gv, oav, SAVEt_AV);
79072805 331
4608196e 332 GvAV(gv) = NULL;
fb73857a 333 av = GvAVn(gv);
0cbee0a4 334 if (SvMAGIC(oav))
9711599e 335 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
fb73857a 336 return av;
79072805
LW
337}
338
339HV *
864dbfa3 340Perl_save_hash(pTHX_ GV *gv)
79072805 341{
97aff369 342 dVAR;
fb73857a
PP
343 HV *ohv, *hv;
344
7918f24d
NC
345 PERL_ARGS_ASSERT_SAVE_HASH;
346
e22024d3 347 save_pushptrptr(gv, (ohv = GvHVn(gv)), SAVEt_HV);
79072805 348
4608196e 349 GvHV(gv) = NULL;
fb73857a 350 hv = GvHVn(gv);
0cbee0a4 351 if (SvMAGIC(ohv))
9711599e 352 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
fb73857a 353 return hv;
79072805
LW
354}
355
356void
864dbfa3 357Perl_save_item(pTHX_ register SV *item)
79072805 358{
97aff369 359 dVAR;
901017d6 360 register SV * const sv = newSVsv(item);
79072805 361
7918f24d
NC
362 PERL_ARGS_ASSERT_SAVE_ITEM;
363
e22024d3
NC
364 save_pushptrptr(item, /* remember the pointer */
365 sv, /* remember the value */
366 SAVEt_ITEM);
79072805
LW
367}
368
369void
1409bc06 370Perl_save_bool(pTHX_ bool *boolp)
79072805 371{
97aff369 372 dVAR;
7918f24d 373
1409bc06 374 PERL_ARGS_ASSERT_SAVE_BOOL;
7918f24d 375
65504245 376 SSCHECK(2);
1409bc06 377 SSPUSHPTR(boolp);
65504245 378 SSPUSHUV(SAVEt_BOOL | (*boolp << 8));
1409bc06
NC
379}
380
7623d426
NC
381void
382Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
1409bc06
NC
383{
384 dVAR;
385 SSCHECK(3);
386 SSPUSHINT(i);
387 SSPUSHPTR(ptr);
c6bf6a65 388 SSPUSHUV(type);
79072805
LW
389}
390
391void
1409bc06 392Perl_save_int(pTHX_ int *intp)
9febdf04 393{
97aff369 394 dVAR;
7918f24d 395
1409bc06 396 PERL_ARGS_ASSERT_SAVE_INT;
7918f24d 397
1409bc06 398 save_pushi32ptr(*intp, intp, SAVEt_INT);
9febdf04
RH
399}
400
401void
58188858
RGS
402Perl_save_I8(pTHX_ I8 *bytep)
403{
404 dVAR;
7918f24d
NC
405
406 PERL_ARGS_ASSERT_SAVE_I8;
407
6c61c2d4
NC
408 SSCHECK(2);
409 SSPUSHPTR(bytep);
410 SSPUSHUV(SAVEt_I8 | ((UV)*bytep << 8));
58188858
RGS
411}
412
413void
87a84751
JH
414Perl_save_I16(pTHX_ I16 *intp)
415{
416 dVAR;
7918f24d
NC
417
418 PERL_ARGS_ASSERT_SAVE_I16;
419
c9441fce
NC
420 SSCHECK(2);
421 SSPUSHPTR(intp);
422 SSPUSHUV(SAVEt_I16 | ((UV)*intp << 8));
87a84751
JH
423}
424
425void
864dbfa3 426Perl_save_I32(pTHX_ I32 *intp)
79072805 427{
97aff369 428 dVAR;
7918f24d
NC
429
430 PERL_ARGS_ASSERT_SAVE_I32;
431
1409bc06 432 save_pushi32ptr(*intp, intp, SAVEt_I32);
79072805
LW
433}
434
85e6fe83
LW
435/* Cannot use save_sptr() to store a char* since the SV** cast will
436 * force word-alignment and we'll miss the pointer.
437 */
438void
864dbfa3 439Perl_save_pptr(pTHX_ char **pptr)
85e6fe83 440{
97aff369 441 dVAR;
7918f24d
NC
442
443 PERL_ARGS_ASSERT_SAVE_PPTR;
444
e22024d3 445 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
85e6fe83
LW
446}
447
79072805 448void
146174a9
CB
449Perl_save_vptr(pTHX_ void *ptr)
450{
97aff369 451 dVAR;
7918f24d
NC
452
453 PERL_ARGS_ASSERT_SAVE_VPTR;
454
e22024d3 455 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
146174a9
CB
456}
457
458void
864dbfa3 459Perl_save_sptr(pTHX_ SV **sptr)
79072805 460{
97aff369 461 dVAR;
7918f24d
NC
462
463 PERL_ARGS_ASSERT_SAVE_SPTR;
464
e22024d3 465 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
79072805
LW
466}
467
c3564e5c 468void
09edbca0 469Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
c3564e5c 470{
97aff369 471 dVAR;
c3564e5c 472 SSCHECK(4);
f3548bdc 473 ASSERT_CURPAD_ACTIVE("save_padsv");
09edbca0 474 SSPUSHPTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
f3548bdc 475 SSPUSHPTR(PL_comppad);
c3564e5c 476 SSPUSHLONG((long)off);
c6bf6a65 477 SSPUSHUV(SAVEt_PADSV_AND_MORTALIZE);
c3564e5c
GS
478}
479
79072805 480void
864dbfa3 481Perl_save_hptr(pTHX_ HV **hptr)
79072805 482{
97aff369 483 dVAR;
7918f24d
NC
484
485 PERL_ARGS_ASSERT_SAVE_HPTR;
486
e22024d3 487 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
79072805
LW
488}
489
490void
864dbfa3 491Perl_save_aptr(pTHX_ AV **aptr)
79072805 492{
97aff369 493 dVAR;
7918f24d
NC
494
495 PERL_ARGS_ASSERT_SAVE_APTR;
496
e22024d3 497 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
79072805
LW
498}
499
500void
2fd8beea 501Perl_save_pushptr(pTHX_ void *const ptr, const int type)
8990e307 502{
97aff369 503 dVAR;
8990e307 504 SSCHECK(2);
2fd8beea 505 SSPUSHPTR(ptr);
c6bf6a65 506 SSPUSHUV(type);
8990e307
LW
507}
508
509void
864dbfa3 510Perl_save_clearsv(pTHX_ SV **svp)
8990e307 511{
97aff369 512 dVAR;
cdcdfc56
NC
513 const UV offset = svp - PL_curpad;
514 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
7918f24d
NC
515
516 PERL_ARGS_ASSERT_SAVE_CLEARSV;
517
f3548bdc 518 ASSERT_CURPAD_ACTIVE("save_clearsv");
cdcdfc56
NC
519 if ((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)
520 Perl_croak(aTHX_ "panic: pad offset %"UVuf" out of range (%p-%p)",
521 offset, svp, PL_curpad);
522
523 SSCHECK(1);
524 SSPUSHUV(offset_shifted | SAVEt_CLEARSV);
d9d18af6 525 SvPADSTALE_off(*svp); /* mark lexical as active */
8990e307
LW
526}
527
528void
864dbfa3 529Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
8990e307 530{
97aff369 531 dVAR;
7918f24d
NC
532
533 PERL_ARGS_ASSERT_SAVE_DELETE;
534
85a721ca 535 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
8990e307
LW
536}
537
538void
af097752
VP
539Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
540{
541 STRLEN len;
542 I32 klen;
543 const char *key;
544
545 PERL_ARGS_ASSERT_SAVE_HDELETE;
546
547 key = SvPV_const(keysv, len);
548 klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
549 SvREFCNT_inc_simple_void_NN(hv);
550 save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
551}
552
553void
c68ec7a9
VP
554Perl_save_adelete(pTHX_ AV *av, I32 key)
555{
556 dVAR;
557
558 PERL_ARGS_ASSERT_SAVE_ADELETE;
559
560 SvREFCNT_inc_void(av);
561 save_pushi32ptr(key, av, SAVEt_ADELETE);
562}
563
564void
12ab1f58
JH
565Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
566{
567 dVAR;
7918f24d
NC
568
569 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
570
12ab1f58
JH
571 SSCHECK(3);
572 SSPUSHDPTR(f);
573 SSPUSHPTR(p);
c6bf6a65 574 SSPUSHUV(SAVEt_DESTRUCTOR);
12ab1f58
JH
575}
576
577void
146174a9
CB
578Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
579{
97aff369 580 dVAR;
146174a9
CB
581 SSCHECK(3);
582 SSPUSHDXPTR(f);
583 SSPUSHPTR(p);
c6bf6a65 584 SSPUSHUV(SAVEt_DESTRUCTOR_X);
146174a9
CB
585}
586
587void
da8315f8
NC
588Perl_save_hints(pTHX)
589{
590 dVAR;
591 if (PL_compiling.cop_hints_hash) {
592 HINTS_REFCNT_LOCK;
593 PL_compiling.cop_hints_hash->refcounted_he_refcnt++;
594 HINTS_REFCNT_UNLOCK;
595 }
da8315f8 596 if (PL_hints & HINT_LOCALIZE_HH) {
be84297e
NC
597 save_pushptri32ptr(GvHV(PL_hintgv), PL_hints,
598 PL_compiling.cop_hints_hash, SAVEt_HINTS);
da8315f8 599 GvHV(PL_hintgv) = Perl_hv_copy_hints_hv(aTHX_ GvHV(PL_hintgv));
be84297e
NC
600 } else {
601 save_pushi32ptr(PL_hints, PL_compiling.cop_hints_hash, SAVEt_HINTS);
da8315f8 602 }
be84297e
NC
603}
604
605static void
606S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
607 const int type)
608{
609 SSCHECK(4);
610 SSPUSHPTR(ptr1);
611 SSPUSHINT(i);
612 SSPUSHPTR(ptr2);
c6bf6a65 613 SSPUSHUV(type);
da8315f8
NC
614}
615
616void
91d1c79f 617Perl_save_aelem_flags(pTHX_ AV *av, I32 idx, SV **sptr, const U32 flags)
4e4c362e 618{
97aff369 619 dVAR;
bfc4de9f 620 SV *sv;
7918f24d 621
91d1c79f 622 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
7918f24d 623
0cbee0a4 624 SvGETMAGIC(*sptr);
be84297e
NC
625 save_pushptri32ptr(SvREFCNT_inc_simple(av), idx, SvREFCNT_inc(*sptr),
626 SAVEt_AELEM);
5dd42e15
DM
627 /* if it gets reified later, the restore will have the wrong refcnt */
628 if (!AvREAL(av) && AvREIFY(av))
b37c2d43 629 SvREFCNT_inc_void(*sptr);
91d1c79f 630 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
75d34a09
VP
631 if (flags & SAVEf_KEEPOLDELEM)
632 return;
bfc4de9f
DM
633 sv = *sptr;
634 /* If we're localizing a tied array element, this new sv
635 * won't actually be stored in the array - so it won't get
636 * reaped when the localize ends. Ensure it gets reaped by
637 * mortifying it instead. DAPM */
daeb922a 638 if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied))
bfc4de9f 639 sv_2mortal(sv);
4e4c362e
GS
640}
641
642void
af7df257 643Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
4e4c362e 644{
97aff369 645 dVAR;
bfc4de9f 646 SV *sv;
7918f24d 647
af7df257 648 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
7918f24d 649
0cbee0a4 650 SvGETMAGIC(*sptr);
4e4c362e 651 SSCHECK(4);
b37c2d43 652 SSPUSHPTR(SvREFCNT_inc_simple(hv));
b2096149 653 SSPUSHPTR(newSVsv(key));
4e4c362e 654 SSPUSHPTR(SvREFCNT_inc(*sptr));
c6bf6a65 655 SSPUSHUV(SAVEt_HELEM);
af7df257 656 save_scalar_at(sptr, flags);
75d34a09
VP
657 if (flags & SAVEf_KEEPOLDELEM)
658 return;
bfc4de9f
DM
659 sv = *sptr;
660 /* If we're localizing a tied hash element, this new sv
661 * won't actually be stored in the hash - so it won't get
662 * reaped when the localize ends. Ensure it gets reaped by
663 * mortifying it instead. DAPM */
daeb922a 664 if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
bfc4de9f 665 sv_2mortal(sv);
4e4c362e
GS
666}
667
2053acbf
NC
668SV*
669Perl_save_svref(pTHX_ SV **sptr)
670{
671 dVAR;
7918f24d
NC
672
673 PERL_ARGS_ASSERT_SAVE_SVREF;
674
2053acbf 675 SvGETMAGIC(*sptr);
e22024d3 676 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
af7df257 677 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
2053acbf
NC
678}
679
455ece5e 680I32
864dbfa3 681Perl_save_alloc(pTHX_ I32 size, I32 pad)
455ece5e 682{
97aff369 683 dVAR;
35a4481c 684 register const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
8aacddc1 685 - (char*)PL_savestack);
1be36ce0
NC
686 const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
687 const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
455ece5e 688
1be36ce0
NC
689 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != elems)
690 Perl_croak(aTHX_ "panic: save_alloc elems %"UVuf" out of range (%ld-%ld)",
691 elems, size, pad);
692
693 SSGROW(elems + 1);
455ece5e
AD
694
695 PL_savestack_ix += elems;
1be36ce0 696 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
455ece5e
AD
697 return start;
698}
699
462e5cf6 700void
864dbfa3 701Perl_leave_scope(pTHX_ I32 base)
79072805 702{
97aff369 703 dVAR;
79072805
LW
704 register SV *sv;
705 register SV *value;
706 register GV *gv;
707 register AV *av;
708 register HV *hv;
20454177 709 void* ptr;
f4dd75d9 710 register char* str;
161b7d16 711 I32 i;
302c0c93
NC
712 /* Localise the effects of the TAINT_NOT inside the loop. */
713 const bool was = PL_tainted;
79072805
LW
714
715 if (base < -1)
cea2e8a9 716 Perl_croak(aTHX_ "panic: corrupt saved stack index");
1c98cc53
DM
717 DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
718 (long)PL_savestack_ix, (long)base));
3280af22 719 while (PL_savestack_ix > base) {
c6bf6a65
NC
720 UV uv = SSPOPUV;
721 const U8 type = (U8)uv & SAVE_MASK;
c6ae7647
NC
722 TAINT_NOT;
723
c6bf6a65 724 switch (type) {
79072805 725 case SAVEt_ITEM: /* normal string */
ad64d0ec
NC
726 value = MUTABLE_SV(SSPOPPTR);
727 sv = MUTABLE_SV(SSPOPPTR);
79072805 728 sv_replace(sv,value);
3280af22 729 PL_localizing = 2;
79072805 730 SvSETMAGIC(sv);
3280af22 731 PL_localizing = 0;
79072805 732 break;
8aacddc1 733 case SAVEt_SV: /* scalar reference */
ad64d0ec 734 value = MUTABLE_SV(SSPOPPTR);
159b6efe 735 gv = MUTABLE_GV(SSPOPPTR);
7a4c00b4 736 ptr = &GvSV(gv);
502c6561 737 av = MUTABLE_AV(gv); /* what to refcnt_dec */
2053acbf
NC
738 restore_sv:
739 sv = *(SV**)ptr;
2053acbf
NC
740 *(SV**)ptr = value;
741 SvREFCNT_dec(sv);
742 PL_localizing = 2;
743 SvSETMAGIC(value);
744 PL_localizing = 0;
745 SvREFCNT_dec(value);
746 if (av) /* actually an av, hv or gv */
747 SvREFCNT_dec(av);
748 break;
8aacddc1 749 case SAVEt_GENERIC_PVREF: /* generic pv */
f4dd75d9 750 ptr = SSPOPPTR;
b03d03b0 751 str = (char*)SSPOPPTR;
f4dd75d9
GS
752 if (*(char**)ptr != str) {
753 Safefree(*(char**)ptr);
754 *(char**)ptr = str;
755 }
756 break;
05ec9bb3
NIS
757 case SAVEt_SHARED_PVREF: /* shared pv */
758 str = (char*)SSPOPPTR;
759 ptr = SSPOPPTR;
760 if (*(char**)ptr != str) {
5e54c26f 761#ifdef NETWARE
9ecbcc42 762 PerlMem_free(*(char**)ptr);
5e54c26f 763#else
05ec9bb3 764 PerlMemShared_free(*(char**)ptr);
5e54c26f 765#endif
05ec9bb3
NIS
766 *(char**)ptr = str;
767 }
768 break;
8aacddc1 769 case SAVEt_GENERIC_SVREF: /* generic sv */
ad64d0ec 770 value = MUTABLE_SV(SSPOPPTR);
b9d12d37 771 ptr = SSPOPPTR;
f4dd75d9
GS
772 sv = *(SV**)ptr;
773 *(SV**)ptr = value;
774 SvREFCNT_dec(sv);
b9d12d37
GS
775 SvREFCNT_dec(value);
776 break;
8aacddc1 777 case SAVEt_AV: /* array reference */
502c6561 778 av = MUTABLE_AV(SSPOPPTR);
159b6efe 779 gv = MUTABLE_GV(SSPOPPTR);
ef8d46e8 780 SvREFCNT_dec(GvAV(gv));
8aacddc1 781 GvAV(gv) = av;
fb73857a 782 if (SvMAGICAL(av)) {
3280af22 783 PL_localizing = 2;
ad64d0ec 784 SvSETMAGIC(MUTABLE_SV(av));
3280af22 785 PL_localizing = 0;
fb73857a 786 }
8aacddc1
NIS
787 break;
788 case SAVEt_HV: /* hash reference */
85fbaab2 789 hv = MUTABLE_HV(SSPOPPTR);
159b6efe 790 gv = MUTABLE_GV(SSPOPPTR);
ef8d46e8 791 SvREFCNT_dec(GvHV(gv));
8aacddc1 792 GvHV(gv) = hv;
fb73857a 793 if (SvMAGICAL(hv)) {
3280af22 794 PL_localizing = 2;
ad64d0ec 795 SvSETMAGIC(MUTABLE_SV(hv));
3280af22 796 PL_localizing = 0;
fb73857a 797 }
8aacddc1 798 break;
79072805
LW
799 case SAVEt_INT: /* int reference */
800 ptr = SSPOPPTR;
801 *(int*)ptr = (int)SSPOPINT;
802 break;
9febdf04
RH
803 case SAVEt_BOOL: /* bool reference */
804 ptr = SSPOPPTR;
65504245 805 *(bool*)ptr = cBOOL(uv >> 8);
9febdf04 806 break;
79072805
LW
807 case SAVEt_I32: /* I32 reference */
808 ptr = SSPOPPTR;
3235b7a3
NC
809#ifdef PERL_DEBUG_READONLY_OPS
810 {
811 const I32 val = SSPOPINT;
812 if (*(I32*)ptr != val)
813 *(I32*)ptr = val;
814 }
815#else
79072805 816 *(I32*)ptr = (I32)SSPOPINT;
3235b7a3 817#endif
79072805
LW
818 break;
819 case SAVEt_SPTR: /* SV* reference */
820 ptr = SSPOPPTR;
ad64d0ec 821 *(SV**)ptr = MUTABLE_SV(SSPOPPTR);
79072805 822 break;
146174a9 823 case SAVEt_VPTR: /* random* reference */
85e6fe83
LW
824 case SAVEt_PPTR: /* char* reference */
825 ptr = SSPOPPTR;
826 *(char**)ptr = (char*)SSPOPPTR;
827 break;
79072805
LW
828 case SAVEt_HPTR: /* HV* reference */
829 ptr = SSPOPPTR;
85fbaab2 830 *(HV**)ptr = MUTABLE_HV(SSPOPPTR);
79072805
LW
831 break;
832 case SAVEt_APTR: /* AV* reference */
833 ptr = SSPOPPTR;
502c6561 834 *(AV**)ptr = MUTABLE_AV(SSPOPPTR);
79072805 835 break;
fb73857a 836 case SAVEt_GP: /* scalar reference */
159b6efe 837 gv = MUTABLE_GV(SSPOPPTR);
8aacddc1 838 gp_free(gv);
b9e00b79
LRG
839 GvGP(gv) = (GP*)SSPOPPTR;
840 if (SSPOPINT)
841 SvFAKE_on(gv);
dd69841b
BB
842 /* putting a method back into circulation ("local")*/
843 if (GvCVu(gv) && (hv=GvSTASH(gv)) && HvNAME_get(hv))
844 mro_method_changed_in(hv);
4633a7c4 845 SvREFCNT_dec(gv);
8aacddc1 846 break;
8990e307
LW
847 case SAVEt_FREESV:
848 ptr = SSPOPPTR;
ad64d0ec 849 SvREFCNT_dec(MUTABLE_SV(ptr));
8990e307 850 break;
26d9b02f
JH
851 case SAVEt_MORTALIZESV:
852 ptr = SSPOPPTR;
ad64d0ec 853 sv_2mortal(MUTABLE_SV(ptr));
26d9b02f 854 break;
8990e307
LW
855 case SAVEt_FREEOP:
856 ptr = SSPOPPTR;
f3548bdc 857 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP"); /* XXX DAPM tmp */
8990e307
LW
858 op_free((OP*)ptr);
859 break;
860 case SAVEt_FREEPV:
861 ptr = SSPOPPTR;
1df70142 862 Safefree(ptr);
8990e307
LW
863 break;
864 case SAVEt_CLEARSV:
cdcdfc56 865 ptr = (void*)&PL_curpad[uv >> SAVE_TIGHT_SHIFT];
8990e307 866 sv = *(SV**)ptr;
dd2155a4
DM
867
868 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
f3548bdc
DM
869 "Pad 0x%"UVxf"[0x%"UVxf"] clearsv: %ld sv=0x%"UVxf"<%"IVdf"> %s\n",
870 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
871 (long)((SV **)ptr-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
dd2155a4
DM
872 (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
873 ));
874
bc44cdaf
GS
875 /* Can clear pad variable in place? */
876 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
8aacddc1
NIS
877 /*
878 * if a my variable that was made readonly is going out of
879 * scope, we want to remove the readonlyness so that it can
880 * go out of scope quietly
8aacddc1 881 */
a26e96df 882 if (SvPADMY(sv) && !SvFAKE(sv))
8aacddc1
NIS
883 SvREADONLY_off(sv);
884
6fc92669 885 if (SvTHINKFIRST(sv))
840a7b70 886 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF);
a0d0e21e
LW
887 if (SvMAGICAL(sv))
888 mg_free(sv);
8990e307
LW
889
890 switch (SvTYPE(sv)) {
891 case SVt_NULL:
892 break;
893 case SVt_PVAV:
502c6561 894 av_clear(MUTABLE_AV(sv));
8990e307
LW
895 break;
896 case SVt_PVHV:
85fbaab2 897 hv_clear(MUTABLE_HV(sv));
8990e307
LW
898 break;
899 case SVt_PVCV:
cea2e8a9 900 Perl_croak(aTHX_ "panic: leave_scope pad code");
8990e307 901 default:
0c34ef67 902 SvOK_off(sv);
8990e307
LW
903 break;
904 }
d9d18af6 905 SvPADSTALE_on(sv); /* mark as no longer live */
8990e307
LW
906 }
907 else { /* Someone has a claim on this, so abandon it. */
35a4481c 908 const U32 padflags = SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP);
8990e307 909 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
ad64d0ec
NC
910 case SVt_PVAV: *(SV**)ptr = MUTABLE_SV(newAV()); break;
911 case SVt_PVHV: *(SV**)ptr = MUTABLE_SV(newHV()); break;
561b68a9 912 default: *(SV**)ptr = newSV(0); break;
8990e307 913 }
53868620 914 SvREFCNT_dec(sv); /* Cast current value to the winds. */
d9d18af6
DM
915 /* preserve pad nature, but also mark as not live
916 * for any closure capturing */
2740392c 917 SvFLAGS(*(SV**)ptr) |= padflags | SVs_PADSTALE;
8990e307
LW
918 }
919 break;
920 case SAVEt_DELETE:
921 ptr = SSPOPPTR;
85fbaab2 922 hv = MUTABLE_HV(ptr);
35d4f826 923 i = SSPOPINT;
8990e307 924 ptr = SSPOPPTR;
35d4f826 925 (void)hv_delete(hv, (char*)ptr, i, G_DISCARD);
4e4c362e 926 SvREFCNT_dec(hv);
8aacddc1 927 Safefree(ptr);
8990e307 928 break;
c68ec7a9
VP
929 case SAVEt_ADELETE:
930 ptr = SSPOPPTR;
931 av = MUTABLE_AV(ptr);
932 i = SSPOPINT;
933 (void)av_delete(av, i, G_DISCARD);
934 SvREFCNT_dec(av);
935 break;
146174a9
CB
936 case SAVEt_DESTRUCTOR_X:
937 ptr = SSPOPPTR;
acfe0abc 938 (*SSPOPDXPTR)(aTHX_ ptr);
a0d0e21e
LW
939 break;
940 case SAVEt_REGCONTEXT:
e0fa7e2b 941 /* regexp must have croaked */
455ece5e 942 case SAVEt_ALLOC:
1be36ce0 943 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
a0d0e21e 944 break;
55497cff 945 case SAVEt_STACK_POS: /* Position on Perl stack */
161b7d16 946 i = SSPOPINT;
3280af22 947 PL_stack_sp = PL_stack_base + i;
55497cff 948 break;
ea8d6ae1
DB
949 case SAVEt_STACK_CXPOS: /* blk_oldsp on context stack */
950 i = SSPOPINT;
951 cxstack[i].blk_oldsp = SSPOPINT;
952 break;
161b7d16 953 case SAVEt_AELEM: /* array element */
ad64d0ec 954 value = MUTABLE_SV(SSPOPPTR);
161b7d16 955 i = SSPOPINT;
502c6561 956 av = MUTABLE_AV(SSPOPPTR);
658aef79 957 ptr = av_fetch(av,i,1);
5dd42e15
DM
958 if (!AvREAL(av) && AvREIFY(av)) /* undo reify guard */
959 SvREFCNT_dec(value);
4e4c362e
GS
960 if (ptr) {
961 sv = *(SV**)ptr;
3280af22 962 if (sv && sv != &PL_sv_undef) {
ad64d0ec 963 if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied))
b37c2d43 964 SvREFCNT_inc_void_NN(sv);
4e4c362e
GS
965 goto restore_sv;
966 }
967 }
968 SvREFCNT_dec(av);
969 SvREFCNT_dec(value);
970 break;
161b7d16 971 case SAVEt_HELEM: /* hash element */
ad64d0ec
NC
972 value = MUTABLE_SV(SSPOPPTR);
973 sv = MUTABLE_SV(SSPOPPTR);
85fbaab2 974 hv = MUTABLE_HV(SSPOPPTR);
161b7d16 975 ptr = hv_fetch_ent(hv, sv, 1, 0);
fd1b367e 976 SvREFCNT_dec(sv);
4e4c362e 977 if (ptr) {
35a4481c 978 const SV * const oval = HeVAL((HE*)ptr);
3280af22 979 if (oval && oval != &PL_sv_undef) {
4e4c362e 980 ptr = &HeVAL((HE*)ptr);
ad64d0ec 981 if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
b37c2d43 982 SvREFCNT_inc_void(*(SV**)ptr);
502c6561 983 av = MUTABLE_AV(hv); /* what to refcnt_dec */
4e4c362e
GS
984 goto restore_sv;
985 }
986 }
987 SvREFCNT_dec(hv);
4e4c362e
GS
988 SvREFCNT_dec(value);
989 break;
462e5cf6 990 case SAVEt_OP:
533c011a 991 PL_op = (OP*)SSPOPPTR;
462e5cf6 992 break;
25eaa213 993 case SAVEt_HINTS:
045ac317 994 if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) {
ad64d0ec 995 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
045ac317
RGS
996 GvHV(PL_hintgv) = NULL;
997 }
c28fe1ec
NC
998 Perl_refcounted_he_free(aTHX_ PL_compiling.cop_hints_hash);
999 PL_compiling.cop_hints_hash = (struct refcounted_he *) SSPOPPTR;
601cee3b 1000 *(I32*)&PL_hints = (I32)SSPOPINT;
dfa41748 1001 if (PL_hints & HINT_LOCALIZE_HH) {
ad64d0ec 1002 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
85fbaab2 1003 GvHV(PL_hintgv) = MUTABLE_HV(SSPOPPTR);
5b9c0671
NC
1004 assert(GvHV(PL_hintgv));
1005 } else if (!GvHV(PL_hintgv)) {
1006 /* Need to add a new one manually, else gv_fetchpv() can
1007 add one in this code:
1008
1009 if (SvTYPE(gv) == SVt_PVGV) {
1010 if (add) {
1011 GvMULTI_on(gv);
1012 gv_init_sv(gv, sv_type);
1013 if (*name=='!' && sv_type == SVt_PVHV && len==1)
1014 require_errno(gv);
1015 }
1016 return gv;
1017 }
1018
1019 and it won't have the magic set. */
1020
1021 HV *const hv = newHV();
1022 hv_magic(hv, NULL, PERL_MAGIC_hints);
1023 GvHV(PL_hintgv) = hv;
dfa41748 1024 }
5b9c0671 1025 assert(GvHV(PL_hintgv));
b3ac6de7 1026 break;
cb50131a 1027 case SAVEt_COMPPAD:
f3548bdc 1028 PL_comppad = (PAD*)SSPOPPTR;
58ed4fbe 1029 if (PL_comppad)
cb50131a
CB
1030 PL_curpad = AvARRAY(PL_comppad);
1031 else
4608196e 1032 PL_curpad = NULL;
cb50131a 1033 break;
09edbca0 1034 case SAVEt_PADSV_AND_MORTALIZE:
c3564e5c 1035 {
35a4481c 1036 const PADOFFSET off = (PADOFFSET)SSPOPLONG;
09edbca0 1037 SV **svp;
c3564e5c 1038 ptr = SSPOPPTR;
09edbca0
NC
1039 assert (ptr);
1040 svp = AvARRAY((PAD*)ptr) + off;
1041 /* This mortalizing used to be done by POPLOOP() via itersave.
1042 But as we have all the information here, we can do it here,
1043 save even having to have itersave in the struct. */
1044 sv_2mortal(*svp);
ad64d0ec 1045 *svp = MUTABLE_SV(SSPOPPTR);
c3564e5c
GS
1046 }
1047 break;
8b7059b1
DM
1048 case SAVEt_SAVESWITCHSTACK:
1049 {
1050 dSP;
502c6561
NC
1051 AV *const t = MUTABLE_AV(SSPOPPTR);
1052 AV *const f = MUTABLE_AV(SSPOPPTR);
8b7059b1
DM
1053 SWITCHSTACK(t,f);
1054 PL_curstackinfo->si_stack = f;
1055 }
1056 break;
14f338dc
DM
1057 case SAVEt_SET_SVFLAGS:
1058 {
35a4481c
AL
1059 const U32 val = (U32)SSPOPINT;
1060 const U32 mask = (U32)SSPOPINT;
ad64d0ec 1061 sv = MUTABLE_SV(SSPOPPTR);
14f338dc
DM
1062 SvFLAGS(sv) &= ~mask;
1063 SvFLAGS(sv) |= val;
1064 }
1065 break;
95e06916
NC
1066
1067 /* This would be a mathom, but Perl_save_svref() calls a static
1068 function, S_save_scalar_at(), so has to stay in this file. */
2053acbf 1069 case SAVEt_SVREF: /* scalar reference */
ad64d0ec 1070 value = MUTABLE_SV(SSPOPPTR);
2053acbf
NC
1071 ptr = SSPOPPTR;
1072 av = NULL; /* what to refcnt_dec */
1073 goto restore_sv;
95e06916
NC
1074
1075 /* These are only saved in mathoms.c */
1076 case SAVEt_NSTAB:
159b6efe 1077 gv = MUTABLE_GV(SSPOPPTR);
ad64d0ec 1078 (void)sv_clear(MUTABLE_SV(gv));
95e06916 1079 break;
2053acbf
NC
1080 case SAVEt_LONG: /* long reference */
1081 ptr = SSPOPPTR;
1082 *(long*)ptr = (long)SSPOPLONG;
1083 break;
95e06916
NC
1084 case SAVEt_IV: /* IV reference */
1085 ptr = SSPOPPTR;
1086 *(IV*)ptr = (IV)SSPOPIV;
1087 break;
1088
2053acbf
NC
1089 case SAVEt_I16: /* I16 reference */
1090 ptr = SSPOPPTR;
c9441fce 1091 *(I16*)ptr = (I16)(uv >> 8);
2053acbf
NC
1092 break;
1093 case SAVEt_I8: /* I8 reference */
1094 ptr = SSPOPPTR;
6c61c2d4 1095 *(I8*)ptr = (I8)(uv >> 8);
2053acbf 1096 break;
2053acbf
NC
1097 case SAVEt_DESTRUCTOR:
1098 ptr = SSPOPPTR;
1099 (*SSPOPDPTR)(ptr);
1100 break;
fc15ae8f
NC
1101 case SAVEt_COP_ARYBASE:
1102 ptr = SSPOPPTR;
1103 i = SSPOPINT;
1104 CopARYBASE_set((COP *)ptr, i);
1105 break;
68da3b2f
NC
1106 case SAVEt_COMPILE_WARNINGS:
1107 ptr = SSPOPPTR;
72dc9ed5 1108
68da3b2f
NC
1109 if (!specialWARN(PL_compiling.cop_warnings))
1110 PerlMemShared_free(PL_compiling.cop_warnings);
72dc9ed5 1111
10edeb5d 1112 PL_compiling.cop_warnings = (STRLEN*)ptr;
72dc9ed5 1113 break;
1ade1aa1
NC
1114 case SAVEt_RE_STATE:
1115 {
1116 const struct re_save_state *const state
1117 = (struct re_save_state *)
1118 (PL_savestack + PL_savestack_ix
1119 - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE);
1120 PL_savestack_ix -= SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
1121
1ade1aa1
NC
1122 if (PL_reg_start_tmp != state->re_state_reg_start_tmp) {
1123 Safefree(PL_reg_start_tmp);
1ade1aa1 1124 }
1ade1aa1
NC
1125 if (PL_reg_poscache != state->re_state_reg_poscache) {
1126 Safefree(PL_reg_poscache);
1ade1aa1 1127 }
46ab3289 1128 Copy(state, &PL_reg_state, 1, struct re_save_state);
1ade1aa1
NC
1129 }
1130 break;
7c197c94
DM
1131 case SAVEt_PARSER:
1132 ptr = SSPOPPTR;
1133 parser_free((yy_parser *) ptr);
1134 break;
79072805 1135 default:
cea2e8a9 1136 Perl_croak(aTHX_ "panic: leave_scope inconsistency");
79072805
LW
1137 }
1138 }
302c0c93
NC
1139
1140 PL_tainted = was;
f410a211
NC
1141
1142 PERL_ASYNC_CHECK();
79072805 1143}
8990e307 1144
8990e307 1145void
864dbfa3 1146Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 1147{
97aff369 1148 dVAR;
7918f24d
NC
1149
1150 PERL_ARGS_ASSERT_CX_DUMP;
1151
35ff7856 1152#ifdef DEBUGGING
22c35a8c 1153 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 1154 if (CxTYPE(cx) != CXt_SUBST) {
760ac839 1155 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
146174a9
CB
1156 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
1157 PTR2UV(cx->blk_oldcop));
760ac839
LW
1158 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1159 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
146174a9
CB
1160 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
1161 PTR2UV(cx->blk_oldpm));
760ac839 1162 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
8990e307 1163 }
6b35e009 1164 switch (CxTYPE(cx)) {
8990e307
LW
1165 case CXt_NULL:
1166 case CXt_BLOCK:
1167 break;
146174a9 1168 case CXt_FORMAT:
f9c764c5
NC
1169 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%"UVxf"\n",
1170 PTR2UV(cx->blk_format.cv));
1171 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%"UVxf"\n",
1172 PTR2UV(cx->blk_format.gv));
1173 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%"UVxf"\n",
1174 PTR2UV(cx->blk_format.dfoutgv));
1175 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
bafb2adc 1176 (int)CxHASARGS(cx));
f9c764c5
NC
1177 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%"UVxf"\n",
1178 PTR2UV(cx->blk_format.retop));
146174a9 1179 break;
8990e307 1180 case CXt_SUB:
146174a9
CB
1181 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
1182 PTR2UV(cx->blk_sub.cv));
760ac839 1183 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 1184 (long)cx->blk_sub.olddepth);
760ac839 1185 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
bafb2adc
NC
1186 (int)CxHASARGS(cx));
1187 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
f39bc417
DM
1188 PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n",
1189 PTR2UV(cx->blk_sub.retop));
8990e307
LW
1190 break;
1191 case CXt_EVAL:
760ac839 1192 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
85a64632 1193 (long)CxOLD_IN_EVAL(cx));
760ac839 1194 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
85a64632
NC
1195 PL_op_name[CxOLD_OP_TYPE(cx)],
1196 PL_op_desc[CxOLD_OP_TYPE(cx)]);
0f79a09d
GS
1197 if (cx->blk_eval.old_namesv)
1198 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
aa07b2f6 1199 SvPVX_const(cx->blk_eval.old_namesv));
146174a9
CB
1200 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
1201 PTR2UV(cx->blk_eval.old_eval_root));
f39bc417
DM
1202 PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%"UVxf"\n",
1203 PTR2UV(cx->blk_eval.retop));
8990e307
LW
1204 break;
1205
c6fdafd0 1206 case CXt_LOOP_LAZYIV:
d01136d6 1207 case CXt_LOOP_LAZYSV:
3b719c58
NC
1208 case CXt_LOOP_FOR:
1209 case CXt_LOOP_PLAIN:
0cbdab38 1210 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
760ac839 1211 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
8990e307 1212 (long)cx->blk_loop.resetsp);
022eaa24
NC
1213 PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%"UVxf"\n",
1214 PTR2UV(cx->blk_loop.my_op));
146174a9 1215 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%"UVxf"\n",
022eaa24 1216 PTR2UV(CX_LOOP_NEXTOP_GET(cx)));
d01136d6 1217 /* XXX: not accurate for LAZYSV/IV */
146174a9 1218 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
d01136d6
BS
1219 PTR2UV(cx->blk_loop.state_u.ary.ary));
1220 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1221 (long)cx->blk_loop.state_u.ary.ix);
146174a9
CB
1222 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
1223 PTR2UV(CxITERVAR(cx)));
8990e307
LW
1224 break;
1225
1226 case CXt_SUBST:
760ac839 1227 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 1228 (long)cx->sb_iters);
760ac839 1229 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 1230 (long)cx->sb_maxiters);
35ef4773
GS
1231 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1232 (long)cx->sb_rflags);
760ac839 1233 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
c5bed6a7 1234 (long)CxONCE(cx));
760ac839 1235 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 1236 cx->sb_orig);
146174a9
CB
1237 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
1238 PTR2UV(cx->sb_dstr));
1239 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
1240 PTR2UV(cx->sb_targ));
1241 PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
1242 PTR2UV(cx->sb_s));
1243 PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
1244 PTR2UV(cx->sb_m));
1245 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
1246 PTR2UV(cx->sb_strend));
1247 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
1248 PTR2UV(cx->sb_rxres));
8990e307
LW
1249 break;
1250 }
65e66c80 1251#else
96a5add6 1252 PERL_UNUSED_CONTEXT;
65e66c80 1253 PERL_UNUSED_ARG(cx);
17c3b450 1254#endif /* DEBUGGING */
35ff7856 1255}
241d1a3b
NC
1256
1257/*
1258 * Local variables:
1259 * c-indentation-style: bsd
1260 * c-basic-offset: 4
1261 * indent-tabs-mode: t
1262 * End:
1263 *
37442d52
RGS
1264 * ex: set ts=8 sts=4 sw=4 noet:
1265 */