This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Lower version.pm version
[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);
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{
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 */
22ade07d 163 SV* const sv = PL_tmps_stack[PL_tmps_ix--];
72581b5b
VP
164#ifdef PERL_POISON
165 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
22ade07d 166#endif
8aacddc1 167 if (sv && sv != &PL_sv_undef) {
463ee0b2 168 SvTEMP_off(sv);
8990e307 169 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
463ee0b2 170 }
79072805
LW
171 }
172}
173
76e3520e 174STATIC SV *
af7df257 175S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
79072805 176{
97aff369 177 dVAR;
75d34a09 178 SV * osv;
eb578fdb 179 SV *sv;
79072805 180
7918f24d
NC
181 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
182
75d34a09
VP
183 osv = *sptr;
184 sv = (flags & SAVEf_KEEPOLDELEM) ? osv : (*sptr = newSV(0));
185
f1f99dc1 186 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv)) {
a0d0e21e 187 if (SvGMAGICAL(osv)) {
a0d0e21e 188 SvFLAGS(osv) |= (SvFLAGS(osv) &
c268c2a6 189 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
a0d0e21e 190 }
75d34a09 191 if (!(flags & SAVEf_KEEPOLDELEM))
2dcac756 192 mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
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
10507e11 284 save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
79072805 285
5f05dabc 286 if (empty) {
12816592 287 GP *gp = Perl_newGP(aTHX_ gv);
959f7ad7 288 HV * const stash = GvSTASH(gv);
3d460042
FC
289 bool isa_changed = 0;
290
291 if (stash && HvENAME(stash)) {
292 if (GvNAMELEN(gv) == 3 && strnEQ(GvNAME(gv), "ISA", 3))
293 isa_changed = TRUE;
294 else if (GvCVu(gv))
295 /* taking a method out of circulation ("local")*/
296 mro_method_changed_in(stash);
297 }
146174a9
CB
298 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
299 gp->gp_io = newIO();
300 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
301 }
c43ae56f 302 GvGP_set(gv,gp);
3d460042 303 if (isa_changed) mro_isa_changed_in(stash);
5f05dabc
PP
304 }
305 else {
44a8e56a 306 gp_ref(GvGP(gv));
5f05dabc
PP
307 GvINTRO_on(gv);
308 }
79072805 309}
79072805 310
79072805 311AV *
864dbfa3 312Perl_save_ary(pTHX_ GV *gv)
79072805 313{
97aff369 314 dVAR;
901017d6 315 AV * const oav = GvAVn(gv);
67a38de0 316 AV *av;
fb73857a 317
7918f24d
NC
318 PERL_ARGS_ASSERT_SAVE_ARY;
319
67a38de0
NIS
320 if (!AvREAL(oav) && AvREIFY(oav))
321 av_reify(oav);
01433346 322 save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
79072805 323
4608196e 324 GvAV(gv) = NULL;
fb73857a 325 av = GvAVn(gv);
0cbee0a4 326 if (SvMAGIC(oav))
9711599e 327 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
fb73857a 328 return av;
79072805
LW
329}
330
331HV *
864dbfa3 332Perl_save_hash(pTHX_ GV *gv)
79072805 333{
97aff369 334 dVAR;
fb73857a
PP
335 HV *ohv, *hv;
336
7918f24d
NC
337 PERL_ARGS_ASSERT_SAVE_HASH;
338
01433346
FC
339 save_pushptrptr(
340 SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
341 );
79072805 342
4608196e 343 GvHV(gv) = NULL;
fb73857a 344 hv = GvHVn(gv);
0cbee0a4 345 if (SvMAGIC(ohv))
9711599e 346 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
fb73857a 347 return hv;
79072805
LW
348}
349
350void
5aaab254 351Perl_save_item(pTHX_ SV *item)
79072805 352{
97aff369 353 dVAR;
eb578fdb 354 SV * const sv = newSVsv(item);
79072805 355
7918f24d
NC
356 PERL_ARGS_ASSERT_SAVE_ITEM;
357
e22024d3
NC
358 save_pushptrptr(item, /* remember the pointer */
359 sv, /* remember the value */
360 SAVEt_ITEM);
79072805
LW
361}
362
363void
1409bc06 364Perl_save_bool(pTHX_ bool *boolp)
79072805 365{
97aff369 366 dVAR;
7918f24d 367
1409bc06 368 PERL_ARGS_ASSERT_SAVE_BOOL;
7918f24d 369
65504245 370 SSCHECK(2);
1409bc06 371 SSPUSHPTR(boolp);
65504245 372 SSPUSHUV(SAVEt_BOOL | (*boolp << 8));
1409bc06
NC
373}
374
7623d426
NC
375void
376Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
1409bc06
NC
377{
378 dVAR;
379 SSCHECK(3);
380 SSPUSHINT(i);
381 SSPUSHPTR(ptr);
c6bf6a65 382 SSPUSHUV(type);
79072805
LW
383}
384
385void
1409bc06 386Perl_save_int(pTHX_ int *intp)
9febdf04 387{
97aff369 388 dVAR;
994d373a 389 const UV shifted = (UV)*intp << SAVE_TIGHT_SHIFT;
7918f24d 390
1409bc06 391 PERL_ARGS_ASSERT_SAVE_INT;
7918f24d 392
994d373a
NC
393 if ((int)(shifted >> SAVE_TIGHT_SHIFT) == *intp) {
394 SSCHECK(2);
395 SSPUSHPTR(intp);
396 SSPUSHUV(SAVEt_INT_SMALL | shifted);
397 } else
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;
89abef21 429 const UV shifted = (UV)*intp << SAVE_TIGHT_SHIFT;
7918f24d
NC
430
431 PERL_ARGS_ASSERT_SAVE_I32;
432
89abef21
NC
433 if ((I32)(shifted >> SAVE_TIGHT_SHIFT) == *intp) {
434 SSCHECK(2);
435 SSPUSHPTR(intp);
436 SSPUSHUV(SAVEt_I32_SMALL | shifted);
437 } else
438 save_pushi32ptr(*intp, intp, SAVEt_I32);
79072805
LW
439}
440
85e6fe83
LW
441/* Cannot use save_sptr() to store a char* since the SV** cast will
442 * force word-alignment and we'll miss the pointer.
443 */
444void
864dbfa3 445Perl_save_pptr(pTHX_ char **pptr)
85e6fe83 446{
97aff369 447 dVAR;
7918f24d
NC
448
449 PERL_ARGS_ASSERT_SAVE_PPTR;
450
e22024d3 451 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
85e6fe83
LW
452}
453
79072805 454void
146174a9
CB
455Perl_save_vptr(pTHX_ void *ptr)
456{
97aff369 457 dVAR;
7918f24d
NC
458
459 PERL_ARGS_ASSERT_SAVE_VPTR;
460
e22024d3 461 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
146174a9
CB
462}
463
464void
864dbfa3 465Perl_save_sptr(pTHX_ SV **sptr)
79072805 466{
97aff369 467 dVAR;
7918f24d
NC
468
469 PERL_ARGS_ASSERT_SAVE_SPTR;
470
e22024d3 471 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
79072805
LW
472}
473
c3564e5c 474void
09edbca0 475Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
c3564e5c 476{
97aff369 477 dVAR;
c3564e5c 478 SSCHECK(4);
f3548bdc 479 ASSERT_CURPAD_ACTIVE("save_padsv");
09edbca0 480 SSPUSHPTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
f3548bdc 481 SSPUSHPTR(PL_comppad);
c3564e5c 482 SSPUSHLONG((long)off);
c6bf6a65 483 SSPUSHUV(SAVEt_PADSV_AND_MORTALIZE);
c3564e5c
GS
484}
485
79072805 486void
864dbfa3 487Perl_save_hptr(pTHX_ HV **hptr)
79072805 488{
97aff369 489 dVAR;
7918f24d
NC
490
491 PERL_ARGS_ASSERT_SAVE_HPTR;
492
e22024d3 493 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
79072805
LW
494}
495
496void
864dbfa3 497Perl_save_aptr(pTHX_ AV **aptr)
79072805 498{
97aff369 499 dVAR;
7918f24d
NC
500
501 PERL_ARGS_ASSERT_SAVE_APTR;
502
e22024d3 503 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
79072805
LW
504}
505
506void
2fd8beea 507Perl_save_pushptr(pTHX_ void *const ptr, const int type)
8990e307 508{
97aff369 509 dVAR;
8990e307 510 SSCHECK(2);
2fd8beea 511 SSPUSHPTR(ptr);
c6bf6a65 512 SSPUSHUV(type);
8990e307
LW
513}
514
515void
864dbfa3 516Perl_save_clearsv(pTHX_ SV **svp)
8990e307 517{
97aff369 518 dVAR;
cdcdfc56
NC
519 const UV offset = svp - PL_curpad;
520 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
7918f24d
NC
521
522 PERL_ARGS_ASSERT_SAVE_CLEARSV;
523
f3548bdc 524 ASSERT_CURPAD_ACTIVE("save_clearsv");
623e28c6 525 SvPADSTALE_off(*svp); /* mark lexical as active */
cdcdfc56
NC
526 if ((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)
527 Perl_croak(aTHX_ "panic: pad offset %"UVuf" out of range (%p-%p)",
528 offset, svp, PL_curpad);
529
530 SSCHECK(1);
531 SSPUSHUV(offset_shifted | SAVEt_CLEARSV);
8990e307
LW
532}
533
534void
864dbfa3 535Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
8990e307 536{
97aff369 537 dVAR;
7918f24d
NC
538
539 PERL_ARGS_ASSERT_SAVE_DELETE;
540
85a721ca 541 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
8990e307
LW
542}
543
544void
af097752
VP
545Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
546{
547 STRLEN len;
548 I32 klen;
549 const char *key;
550
551 PERL_ARGS_ASSERT_SAVE_HDELETE;
552
553 key = SvPV_const(keysv, len);
554 klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
555 SvREFCNT_inc_simple_void_NN(hv);
556 save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
557}
558
559void
c68ec7a9
VP
560Perl_save_adelete(pTHX_ AV *av, I32 key)
561{
562 dVAR;
563
564 PERL_ARGS_ASSERT_SAVE_ADELETE;
565
566 SvREFCNT_inc_void(av);
567 save_pushi32ptr(key, av, SAVEt_ADELETE);
568}
569
570void
12ab1f58
JH
571Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
572{
573 dVAR;
7918f24d
NC
574
575 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
576
12ab1f58
JH
577 SSCHECK(3);
578 SSPUSHDPTR(f);
579 SSPUSHPTR(p);
c6bf6a65 580 SSPUSHUV(SAVEt_DESTRUCTOR);
12ab1f58
JH
581}
582
583void
146174a9
CB
584Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
585{
97aff369 586 dVAR;
146174a9
CB
587 SSCHECK(3);
588 SSPUSHDXPTR(f);
589 SSPUSHPTR(p);
c6bf6a65 590 SSPUSHUV(SAVEt_DESTRUCTOR_X);
146174a9
CB
591}
592
593void
da8315f8
NC
594Perl_save_hints(pTHX)
595{
596 dVAR;
20439bc7 597 COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
da8315f8 598 if (PL_hints & HINT_LOCALIZE_HH) {
52c7aca6
FC
599 HV *oldhh = GvHV(PL_hintgv);
600 save_pushptri32ptr(oldhh, PL_hints, save_cophh, SAVEt_HINTS);
601 GvHV(PL_hintgv) = NULL; /* in case copying dies */
602 GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
be84297e 603 } else {
20439bc7 604 save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS);
da8315f8 605 }
be84297e
NC
606}
607
608static void
609S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
610 const int type)
611{
612 SSCHECK(4);
613 SSPUSHPTR(ptr1);
614 SSPUSHINT(i);
615 SSPUSHPTR(ptr2);
c6bf6a65 616 SSPUSHUV(type);
da8315f8
NC
617}
618
619void
91d1c79f 620Perl_save_aelem_flags(pTHX_ AV *av, I32 idx, SV **sptr, const U32 flags)
4e4c362e 621{
97aff369 622 dVAR;
bfc4de9f 623 SV *sv;
7918f24d 624
91d1c79f 625 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
7918f24d 626
0cbee0a4 627 SvGETMAGIC(*sptr);
be84297e
NC
628 save_pushptri32ptr(SvREFCNT_inc_simple(av), idx, SvREFCNT_inc(*sptr),
629 SAVEt_AELEM);
1cdc9186
FC
630 /* The array needs to hold a reference count on its new element, so it
631 must be AvREAL. */
5dd42e15 632 if (!AvREAL(av) && AvREIFY(av))
1cdc9186 633 av_reify(av);
91d1c79f 634 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
75d34a09
VP
635 if (flags & SAVEf_KEEPOLDELEM)
636 return;
bfc4de9f
DM
637 sv = *sptr;
638 /* If we're localizing a tied array element, this new sv
639 * won't actually be stored in the array - so it won't get
640 * reaped when the localize ends. Ensure it gets reaped by
641 * mortifying it instead. DAPM */
daeb922a 642 if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied))
bfc4de9f 643 sv_2mortal(sv);
4e4c362e
GS
644}
645
646void
af7df257 647Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
4e4c362e 648{
97aff369 649 dVAR;
bfc4de9f 650 SV *sv;
7918f24d 651
af7df257 652 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
7918f24d 653
0cbee0a4 654 SvGETMAGIC(*sptr);
4e4c362e 655 SSCHECK(4);
b37c2d43 656 SSPUSHPTR(SvREFCNT_inc_simple(hv));
b2096149 657 SSPUSHPTR(newSVsv(key));
4e4c362e 658 SSPUSHPTR(SvREFCNT_inc(*sptr));
c6bf6a65 659 SSPUSHUV(SAVEt_HELEM);
af7df257 660 save_scalar_at(sptr, flags);
75d34a09
VP
661 if (flags & SAVEf_KEEPOLDELEM)
662 return;
bfc4de9f
DM
663 sv = *sptr;
664 /* If we're localizing a tied hash element, this new sv
665 * won't actually be stored in the hash - so it won't get
666 * reaped when the localize ends. Ensure it gets reaped by
667 * mortifying it instead. DAPM */
daeb922a 668 if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
bfc4de9f 669 sv_2mortal(sv);
4e4c362e
GS
670}
671
2053acbf
NC
672SV*
673Perl_save_svref(pTHX_ SV **sptr)
674{
675 dVAR;
7918f24d
NC
676
677 PERL_ARGS_ASSERT_SAVE_SVREF;
678
2053acbf 679 SvGETMAGIC(*sptr);
e22024d3 680 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
af7df257 681 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
2053acbf
NC
682}
683
455ece5e 684I32
864dbfa3 685Perl_save_alloc(pTHX_ I32 size, I32 pad)
455ece5e 686{
97aff369 687 dVAR;
eb578fdb
KW
688 const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
689 - (char*)PL_savestack);
1be36ce0
NC
690 const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
691 const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
455ece5e 692
1be36ce0
NC
693 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != elems)
694 Perl_croak(aTHX_ "panic: save_alloc elems %"UVuf" out of range (%ld-%ld)",
695 elems, size, pad);
696
697 SSGROW(elems + 1);
455ece5e
AD
698
699 PL_savestack_ix += elems;
1be36ce0 700 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
455ece5e
AD
701 return start;
702}
703
462e5cf6 704void
864dbfa3 705Perl_leave_scope(pTHX_ I32 base)
79072805 706{
97aff369 707 dVAR;
eb578fdb
KW
708 SV *sv;
709 SV *value;
710 GV *gv;
711 AV *av;
712 HV *hv;
20454177 713 void* ptr;
eb578fdb 714 char* str;
161b7d16 715 I32 i;
302c0c93 716 /* Localise the effects of the TAINT_NOT inside the loop. */
284167a5 717 bool was = TAINT_get;
79072805
LW
718
719 if (base < -1)
5637ef5b 720 Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
1c98cc53
DM
721 DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
722 (long)PL_savestack_ix, (long)base));
3280af22 723 while (PL_savestack_ix > base) {
c6bf6a65
NC
724 UV uv = SSPOPUV;
725 const U8 type = (U8)uv & SAVE_MASK;
c6ae7647
NC
726 TAINT_NOT;
727
c6bf6a65 728 switch (type) {
79072805 729 case SAVEt_ITEM: /* normal string */
ad64d0ec
NC
730 value = MUTABLE_SV(SSPOPPTR);
731 sv = MUTABLE_SV(SSPOPPTR);
79072805 732 sv_replace(sv,value);
3280af22 733 PL_localizing = 2;
79072805 734 SvSETMAGIC(sv);
3280af22 735 PL_localizing = 0;
79072805 736 break;
8aacddc1 737 case SAVEt_SV: /* scalar reference */
ad64d0ec 738 value = MUTABLE_SV(SSPOPPTR);
159b6efe 739 gv = MUTABLE_GV(SSPOPPTR);
7a4c00b4 740 ptr = &GvSV(gv);
502c6561 741 av = MUTABLE_AV(gv); /* what to refcnt_dec */
2053acbf
NC
742 restore_sv:
743 sv = *(SV**)ptr;
2053acbf
NC
744 *(SV**)ptr = value;
745 SvREFCNT_dec(sv);
746 PL_localizing = 2;
747 SvSETMAGIC(value);
748 PL_localizing = 0;
749 SvREFCNT_dec(value);
9bfbb681 750 SvREFCNT_dec(av); /* av may actually be an AV, HV or GV */
2053acbf 751 break;
8aacddc1 752 case SAVEt_GENERIC_PVREF: /* generic pv */
f4dd75d9 753 ptr = SSPOPPTR;
b03d03b0 754 str = (char*)SSPOPPTR;
f4dd75d9
GS
755 if (*(char**)ptr != str) {
756 Safefree(*(char**)ptr);
757 *(char**)ptr = str;
758 }
759 break;
05ec9bb3
NIS
760 case SAVEt_SHARED_PVREF: /* shared pv */
761 str = (char*)SSPOPPTR;
762 ptr = SSPOPPTR;
763 if (*(char**)ptr != str) {
5e54c26f 764#ifdef NETWARE
9ecbcc42 765 PerlMem_free(*(char**)ptr);
5e54c26f 766#else
05ec9bb3 767 PerlMemShared_free(*(char**)ptr);
5e54c26f 768#endif
05ec9bb3
NIS
769 *(char**)ptr = str;
770 }
771 break;
f83b46a0
DM
772 case SAVEt_GVSV: /* scalar slot in GV */
773 value = MUTABLE_SV(SSPOPPTR);
774 gv = MUTABLE_GV(SSPOPPTR);
775 ptr = &GvSV(gv);
776 goto restore_svp;
8aacddc1 777 case SAVEt_GENERIC_SVREF: /* generic sv */
ad64d0ec 778 value = MUTABLE_SV(SSPOPPTR);
b9d12d37 779 ptr = SSPOPPTR;
f83b46a0 780 restore_svp:
f4dd75d9
GS
781 sv = *(SV**)ptr;
782 *(SV**)ptr = value;
783 SvREFCNT_dec(sv);
b9d12d37
GS
784 SvREFCNT_dec(value);
785 break;
db9306af
FC
786 case SAVEt_GVSLOT: /* any slot in GV */
787 value = MUTABLE_SV(SSPOPPTR);
788 ptr = SSPOPPTR;
789 gv = MUTABLE_GV(SSPOPPTR);
790 hv = GvSTASH(gv);
791 if (hv && HvENAME(hv) && (
792 (value && SvTYPE(value) == SVt_PVCV)
793 || (*(SV **)ptr && SvTYPE(*(SV**)ptr) == SVt_PVCV)
794 ))
795 {
796 if ((char *)ptr < (char *)GvGP(gv)
797 || (char *)ptr > (char *)GvGP(gv) + sizeof(struct gp)
798 || GvREFCNT(gv) > 1)
799 PL_sub_generation++;
800 else mro_method_changed_in(hv);
801 }
802 goto restore_svp;
8aacddc1 803 case SAVEt_AV: /* array reference */
502c6561 804 av = MUTABLE_AV(SSPOPPTR);
159b6efe 805 gv = MUTABLE_GV(SSPOPPTR);
ef8d46e8 806 SvREFCNT_dec(GvAV(gv));
8aacddc1 807 GvAV(gv) = av;
fb73857a 808 if (SvMAGICAL(av)) {
3280af22 809 PL_localizing = 2;
ad64d0ec 810 SvSETMAGIC(MUTABLE_SV(av));
3280af22 811 PL_localizing = 0;
fb73857a 812 }
01433346 813 SvREFCNT_dec(gv);
8aacddc1
NIS
814 break;
815 case SAVEt_HV: /* hash reference */
85fbaab2 816 hv = MUTABLE_HV(SSPOPPTR);
159b6efe 817 gv = MUTABLE_GV(SSPOPPTR);
ef8d46e8 818 SvREFCNT_dec(GvHV(gv));
8aacddc1 819 GvHV(gv) = hv;
fb73857a 820 if (SvMAGICAL(hv)) {
3280af22 821 PL_localizing = 2;
ad64d0ec 822 SvSETMAGIC(MUTABLE_SV(hv));
3280af22 823 PL_localizing = 0;
fb73857a 824 }
01433346 825 SvREFCNT_dec(gv);
8aacddc1 826 break;
994d373a
NC
827 case SAVEt_INT_SMALL:
828 ptr = SSPOPPTR;
829 *(int*)ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
830 break;
79072805
LW
831 case SAVEt_INT: /* int reference */
832 ptr = SSPOPPTR;
833 *(int*)ptr = (int)SSPOPINT;
834 break;
9febdf04
RH
835 case SAVEt_BOOL: /* bool reference */
836 ptr = SSPOPPTR;
65504245 837 *(bool*)ptr = cBOOL(uv >> 8);
284167a5 838#if !NO_TAINT_SUPPORT
173c3f27 839 if (ptr == &(TAINT_get)) {
b6f93e7a
KW
840 /* If we don't update <was>, to reflect what was saved on the
841 * stack for PL_tainted, then we will overwrite this attempt to
842 * restore it when we exit this routine. Note that this won't
843 * work if this value was saved in a wider-than necessary type,
844 * such as I32 */
845 was = *(bool*)ptr;
846 }
284167a5 847#endif
9febdf04 848 break;
89abef21
NC
849 case SAVEt_I32_SMALL:
850 ptr = SSPOPPTR;
851 *(I32*)ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
852 break;
79072805
LW
853 case SAVEt_I32: /* I32 reference */
854 ptr = SSPOPPTR;
3235b7a3
NC
855#ifdef PERL_DEBUG_READONLY_OPS
856 {
857 const I32 val = SSPOPINT;
858 if (*(I32*)ptr != val)
859 *(I32*)ptr = val;
860 }
861#else
79072805 862 *(I32*)ptr = (I32)SSPOPINT;
3235b7a3 863#endif
79072805
LW
864 break;
865 case SAVEt_SPTR: /* SV* reference */
866 ptr = SSPOPPTR;
ad64d0ec 867 *(SV**)ptr = MUTABLE_SV(SSPOPPTR);
79072805 868 break;
146174a9 869 case SAVEt_VPTR: /* random* reference */
85e6fe83
LW
870 case SAVEt_PPTR: /* char* reference */
871 ptr = SSPOPPTR;
872 *(char**)ptr = (char*)SSPOPPTR;
873 break;
79072805
LW
874 case SAVEt_HPTR: /* HV* reference */
875 ptr = SSPOPPTR;
85fbaab2 876 *(HV**)ptr = MUTABLE_HV(SSPOPPTR);
79072805
LW
877 break;
878 case SAVEt_APTR: /* AV* reference */
879 ptr = SSPOPPTR;
502c6561 880 *(AV**)ptr = MUTABLE_AV(SSPOPPTR);
79072805 881 break;
fb73857a 882 case SAVEt_GP: /* scalar reference */
10507e11 883 ptr = SSPOPPTR;
159b6efe 884 gv = MUTABLE_GV(SSPOPPTR);
4e52a9b6
FC
885 {
886 /* possibly taking a method out of circulation */
887 const bool had_method = !!GvCVu(gv);
888 gp_free(gv);
889 GvGP_set(gv, (GP*)ptr);
890 if ((hv=GvSTASH(gv)) && HvENAME_get(hv)) {
3d460042
FC
891 if (GvNAMELEN(gv) == 3 && strnEQ(GvNAME(gv), "ISA", 3))
892 mro_isa_changed_in(hv);
4e52a9b6 893 else if (had_method || GvCVu(gv))
3d460042 894 /* putting a method back into circulation ("local")*/
978a498e 895 gv_method_changed(gv);
4e52a9b6 896 }
3d460042 897 }
4633a7c4 898 SvREFCNT_dec(gv);
8aacddc1 899 break;
8990e307
LW
900 case SAVEt_FREESV:
901 ptr = SSPOPPTR;
ad64d0ec 902 SvREFCNT_dec(MUTABLE_SV(ptr));
8990e307 903 break;
3987a177
Z
904 case SAVEt_FREECOPHH:
905 ptr = SSPOPPTR;
906 cophh_free((COPHH *)ptr);
907 break;
26d9b02f
JH
908 case SAVEt_MORTALIZESV:
909 ptr = SSPOPPTR;
ad64d0ec 910 sv_2mortal(MUTABLE_SV(ptr));
26d9b02f 911 break;
8990e307
LW
912 case SAVEt_FREEOP:
913 ptr = SSPOPPTR;
5e5ba94b 914 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
8990e307
LW
915 op_free((OP*)ptr);
916 break;
917 case SAVEt_FREEPV:
918 ptr = SSPOPPTR;
1df70142 919 Safefree(ptr);
8990e307 920 break;
4e09461c
DM
921
922 {
923 SV **svp;
924 case SAVEt_CLEARPADRANGE:
925 i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
926 svp = &PL_curpad[uv >>
927 (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
928 goto clearsv;
8990e307 929 case SAVEt_CLEARSV:
4e09461c
DM
930 svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
931 i = 1;
932 clearsv:
933 for (; i; i--, svp--) {
528ad060
DM
934 sv = *svp;
935
936 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
937 "Pad 0x%"UVxf"[0x%"UVxf"] clearsv: %ld sv=0x%"UVxf"<%"IVdf"> %s\n",
938 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
939 (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
940 (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
941 ));
942
943 /* Can clear pad variable in place? */
944 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
945 /*
946 * if a my variable that was made readonly is going out of
947 * scope, we want to remove the readonlyness so that it can
948 * go out of scope quietly
949 */
950 if (SvPADMY(sv) && !SvFAKE(sv))
951 SvREADONLY_off(sv);
952
953 if (SvTHINKFIRST(sv))
954 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
955 |SV_COW_DROP_PV);
956 if (SvTYPE(sv) == SVt_PVHV)
957 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
958 if (SvMAGICAL(sv))
959 {
960 sv_unmagic(sv, PERL_MAGIC_backref);
961 if (SvTYPE(sv) != SVt_PVCV)
962 mg_free(sv);
963 }
964
965 switch (SvTYPE(sv)) {
966 case SVt_NULL:
967 break;
968 case SVt_PVAV:
969 av_clear(MUTABLE_AV(sv));
970 break;
971 case SVt_PVHV:
972 hv_clear(MUTABLE_HV(sv));
973 break;
974 case SVt_PVCV:
975 {
976 HEK * const hek = CvNAME_HEK((CV *)sv);
977 assert(hek);
978 share_hek_hek(hek);
979 cv_undef((CV *)sv);
980 CvNAME_HEK_set(sv, hek);
981 break;
982 }
983 default:
984 SvOK_off(sv);
985 break;
986 }
987 SvPADSTALE_on(sv); /* mark as no longer live */
988 }
989 else { /* Someone has a claim on this, so abandon it. */
990 assert( SvFLAGS(sv) & SVs_PADMY);
991 assert(!(SvFLAGS(sv) & SVs_PADTMP));
992 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
993 case SVt_PVAV: *svp = MUTABLE_SV(newAV()); break;
994 case SVt_PVHV: *svp = MUTABLE_SV(newHV()); break;
995 case SVt_PVCV:
996 {
997 /* Create a stub */
998 *svp = newSV_type(SVt_PVCV);
999
1000 /* Share name */
1001 assert(CvNAMED(sv));
1002 CvNAME_HEK_set(*svp,
1003 share_hek_hek(CvNAME_HEK((CV *)sv)));
1004 break;
1005 }
1006 default: *svp = newSV(0); break;
1007 }
1008 SvREFCNT_dec(sv); /* Cast current value to the winds. */
1009 /* preserve pad nature, but also mark as not live
1010 * for any closure capturing */
1011 SvFLAGS(*svp) |= (SVs_PADMY|SVs_PADSTALE);
1012 }
4e09461c 1013 }
8990e307 1014 break;
4e09461c 1015 }
8990e307
LW
1016 case SAVEt_DELETE:
1017 ptr = SSPOPPTR;
85fbaab2 1018 hv = MUTABLE_HV(ptr);
35d4f826 1019 i = SSPOPINT;
8990e307 1020 ptr = SSPOPPTR;
35d4f826 1021 (void)hv_delete(hv, (char*)ptr, i, G_DISCARD);
4e4c362e 1022 SvREFCNT_dec(hv);
8aacddc1 1023 Safefree(ptr);
8990e307 1024 break;
c68ec7a9
VP
1025 case SAVEt_ADELETE:
1026 ptr = SSPOPPTR;
1027 av = MUTABLE_AV(ptr);
1028 i = SSPOPINT;
1029 (void)av_delete(av, i, G_DISCARD);
1030 SvREFCNT_dec(av);
1031 break;
146174a9
CB
1032 case SAVEt_DESTRUCTOR_X:
1033 ptr = SSPOPPTR;
acfe0abc 1034 (*SSPOPDXPTR)(aTHX_ ptr);
a0d0e21e
LW
1035 break;
1036 case SAVEt_REGCONTEXT:
e0fa7e2b 1037 /* regexp must have croaked */
455ece5e 1038 case SAVEt_ALLOC:
1be36ce0 1039 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
a0d0e21e 1040 break;
55497cff 1041 case SAVEt_STACK_POS: /* Position on Perl stack */
161b7d16 1042 i = SSPOPINT;
3280af22 1043 PL_stack_sp = PL_stack_base + i;
55497cff 1044 break;
ea8d6ae1
DB
1045 case SAVEt_STACK_CXPOS: /* blk_oldsp on context stack */
1046 i = SSPOPINT;
1047 cxstack[i].blk_oldsp = SSPOPINT;
1048 break;
161b7d16 1049 case SAVEt_AELEM: /* array element */
ad64d0ec 1050 value = MUTABLE_SV(SSPOPPTR);
161b7d16 1051 i = SSPOPINT;
502c6561 1052 av = MUTABLE_AV(SSPOPPTR);
658aef79 1053 ptr = av_fetch(av,i,1);
5dd42e15
DM
1054 if (!AvREAL(av) && AvREIFY(av)) /* undo reify guard */
1055 SvREFCNT_dec(value);
4e4c362e
GS
1056 if (ptr) {
1057 sv = *(SV**)ptr;
3280af22 1058 if (sv && sv != &PL_sv_undef) {
ad64d0ec 1059 if (SvTIED_mg((const SV *)av, PERL_MAGIC_tied))
b37c2d43 1060 SvREFCNT_inc_void_NN(sv);
4e4c362e
GS
1061 goto restore_sv;
1062 }
1063 }
1064 SvREFCNT_dec(av);
1065 SvREFCNT_dec(value);
1066 break;
161b7d16 1067 case SAVEt_HELEM: /* hash element */
ad64d0ec
NC
1068 value = MUTABLE_SV(SSPOPPTR);
1069 sv = MUTABLE_SV(SSPOPPTR);
85fbaab2 1070 hv = MUTABLE_HV(SSPOPPTR);
161b7d16 1071 ptr = hv_fetch_ent(hv, sv, 1, 0);
fd1b367e 1072 SvREFCNT_dec(sv);
4e4c362e 1073 if (ptr) {
35a4481c 1074 const SV * const oval = HeVAL((HE*)ptr);
3280af22 1075 if (oval && oval != &PL_sv_undef) {
4e4c362e 1076 ptr = &HeVAL((HE*)ptr);
ad64d0ec 1077 if (SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
b37c2d43 1078 SvREFCNT_inc_void(*(SV**)ptr);
502c6561 1079 av = MUTABLE_AV(hv); /* what to refcnt_dec */
4e4c362e
GS
1080 goto restore_sv;
1081 }
1082 }
1083 SvREFCNT_dec(hv);
4e4c362e
GS
1084 SvREFCNT_dec(value);
1085 break;
462e5cf6 1086 case SAVEt_OP:
533c011a 1087 PL_op = (OP*)SSPOPPTR;
462e5cf6 1088 break;
25eaa213 1089 case SAVEt_HINTS:
3607ca02
FC
1090 if ((PL_hints & HINT_LOCALIZE_HH)) {
1091 while (GvHV(PL_hintgv)) {
2653c1e3 1092 HV *hv = GvHV(PL_hintgv);
045ac317 1093 GvHV(PL_hintgv) = NULL;
2653c1e3 1094 SvREFCNT_dec(MUTABLE_SV(hv));
3607ca02 1095 }
045ac317 1096 }
20439bc7
Z
1097 cophh_free(CopHINTHASH_get(&PL_compiling));
1098 CopHINTHASH_set(&PL_compiling, (COPHH*)SSPOPPTR);
601cee3b 1099 *(I32*)&PL_hints = (I32)SSPOPINT;
dfa41748 1100 if (PL_hints & HINT_LOCALIZE_HH) {
ad64d0ec 1101 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
85fbaab2 1102 GvHV(PL_hintgv) = MUTABLE_HV(SSPOPPTR);
2653c1e3
DM
1103 }
1104 if (!GvHV(PL_hintgv)) {
a3fb8386
FC
1105 /* Need to add a new one manually, else rv2hv can
1106 add one via GvHVn and it won't have the magic set. */
5b9c0671
NC
1107 HV *const hv = newHV();
1108 hv_magic(hv, NULL, PERL_MAGIC_hints);
1109 GvHV(PL_hintgv) = hv;
dfa41748 1110 }
5b9c0671 1111 assert(GvHV(PL_hintgv));
b3ac6de7 1112 break;
cb50131a 1113 case SAVEt_COMPPAD:
f3548bdc 1114 PL_comppad = (PAD*)SSPOPPTR;
58ed4fbe 1115 if (PL_comppad)
cb50131a
CB
1116 PL_curpad = AvARRAY(PL_comppad);
1117 else
4608196e 1118 PL_curpad = NULL;
cb50131a 1119 break;
09edbca0 1120 case SAVEt_PADSV_AND_MORTALIZE:
c3564e5c 1121 {
35a4481c 1122 const PADOFFSET off = (PADOFFSET)SSPOPLONG;
09edbca0 1123 SV **svp;
c3564e5c 1124 ptr = SSPOPPTR;
09edbca0
NC
1125 assert (ptr);
1126 svp = AvARRAY((PAD*)ptr) + off;
1127 /* This mortalizing used to be done by POPLOOP() via itersave.
1128 But as we have all the information here, we can do it here,
1129 save even having to have itersave in the struct. */
1130 sv_2mortal(*svp);
ad64d0ec 1131 *svp = MUTABLE_SV(SSPOPPTR);
c3564e5c
GS
1132 }
1133 break;
8b7059b1
DM
1134 case SAVEt_SAVESWITCHSTACK:
1135 {
1136 dSP;
502c6561
NC
1137 AV *const t = MUTABLE_AV(SSPOPPTR);
1138 AV *const f = MUTABLE_AV(SSPOPPTR);
8b7059b1
DM
1139 SWITCHSTACK(t,f);
1140 PL_curstackinfo->si_stack = f;
1141 }
1142 break;
14f338dc
DM
1143 case SAVEt_SET_SVFLAGS:
1144 {
35a4481c
AL
1145 const U32 val = (U32)SSPOPINT;
1146 const U32 mask = (U32)SSPOPINT;
ad64d0ec 1147 sv = MUTABLE_SV(SSPOPPTR);
14f338dc
DM
1148 SvFLAGS(sv) &= ~mask;
1149 SvFLAGS(sv) |= val;
1150 }
1151 break;
95e06916
NC
1152
1153 /* This would be a mathom, but Perl_save_svref() calls a static
1154 function, S_save_scalar_at(), so has to stay in this file. */
2053acbf 1155 case SAVEt_SVREF: /* scalar reference */
ad64d0ec 1156 value = MUTABLE_SV(SSPOPPTR);
2053acbf
NC
1157 ptr = SSPOPPTR;
1158 av = NULL; /* what to refcnt_dec */
1159 goto restore_sv;
95e06916
NC
1160
1161 /* These are only saved in mathoms.c */
1162 case SAVEt_NSTAB:
159b6efe 1163 gv = MUTABLE_GV(SSPOPPTR);
ad64d0ec 1164 (void)sv_clear(MUTABLE_SV(gv));
95e06916 1165 break;
2053acbf
NC
1166 case SAVEt_LONG: /* long reference */
1167 ptr = SSPOPPTR;
1168 *(long*)ptr = (long)SSPOPLONG;
1169 break;
95e06916
NC
1170 case SAVEt_IV: /* IV reference */
1171 ptr = SSPOPPTR;
1172 *(IV*)ptr = (IV)SSPOPIV;
1173 break;
1174
2053acbf
NC
1175 case SAVEt_I16: /* I16 reference */
1176 ptr = SSPOPPTR;
c9441fce 1177 *(I16*)ptr = (I16)(uv >> 8);
2053acbf
NC
1178 break;
1179 case SAVEt_I8: /* I8 reference */
1180 ptr = SSPOPPTR;
6c61c2d4 1181 *(I8*)ptr = (I8)(uv >> 8);
2053acbf 1182 break;
2053acbf
NC
1183 case SAVEt_DESTRUCTOR:
1184 ptr = SSPOPPTR;
1185 (*SSPOPDPTR)(ptr);
1186 break;
68da3b2f
NC
1187 case SAVEt_COMPILE_WARNINGS:
1188 ptr = SSPOPPTR;
72dc9ed5 1189
68da3b2f
NC
1190 if (!specialWARN(PL_compiling.cop_warnings))
1191 PerlMemShared_free(PL_compiling.cop_warnings);
72dc9ed5 1192
10edeb5d 1193 PL_compiling.cop_warnings = (STRLEN*)ptr;
72dc9ed5 1194 break;
1ade1aa1
NC
1195 case SAVEt_RE_STATE:
1196 {
1197 const struct re_save_state *const state
1198 = (struct re_save_state *)
1199 (PL_savestack + PL_savestack_ix
1200 - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE);
1201 PL_savestack_ix -= SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
1202
1ade1aa1
NC
1203 if (PL_reg_poscache != state->re_state_reg_poscache) {
1204 Safefree(PL_reg_poscache);
1ade1aa1 1205 }
46ab3289 1206 Copy(state, &PL_reg_state, 1, struct re_save_state);
1ade1aa1
NC
1207 }
1208 break;
7c197c94
DM
1209 case SAVEt_PARSER:
1210 ptr = SSPOPPTR;
1211 parser_free((yy_parser *) ptr);
1212 break;
79072805 1213 default:
5637ef5b 1214 Perl_croak(aTHX_ "panic: leave_scope inconsistency %u", type);
79072805
LW
1215 }
1216 }
302c0c93 1217
284167a5 1218 TAINT_set(was);
f410a211
NC
1219
1220 PERL_ASYNC_CHECK();
79072805 1221}
8990e307 1222
8990e307 1223void
864dbfa3 1224Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 1225{
97aff369 1226 dVAR;
7918f24d
NC
1227
1228 PERL_ARGS_ASSERT_CX_DUMP;
1229
35ff7856 1230#ifdef DEBUGGING
22c35a8c 1231 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 1232 if (CxTYPE(cx) != CXt_SUBST) {
e8d9e9d0 1233 const char *gimme_text;
760ac839 1234 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
146174a9
CB
1235 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
1236 PTR2UV(cx->blk_oldcop));
760ac839
LW
1237 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1238 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
146174a9
CB
1239 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
1240 PTR2UV(cx->blk_oldpm));
e8d9e9d0
VP
1241 switch (cx->blk_gimme) {
1242 case G_VOID:
1243 gimme_text = "VOID";
1244 break;
1245 case G_SCALAR:
1246 gimme_text = "SCALAR";
1247 break;
1248 case G_ARRAY:
1249 gimme_text = "LIST";
1250 break;
1251 default:
1252 gimme_text = "UNKNOWN";
1253 break;
1254 }
1255 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
8990e307 1256 }
6b35e009 1257 switch (CxTYPE(cx)) {
8990e307
LW
1258 case CXt_NULL:
1259 case CXt_BLOCK:
1260 break;
146174a9 1261 case CXt_FORMAT:
f9c764c5
NC
1262 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%"UVxf"\n",
1263 PTR2UV(cx->blk_format.cv));
1264 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%"UVxf"\n",
1265 PTR2UV(cx->blk_format.gv));
1266 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%"UVxf"\n",
1267 PTR2UV(cx->blk_format.dfoutgv));
1268 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
bafb2adc 1269 (int)CxHASARGS(cx));
f9c764c5
NC
1270 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%"UVxf"\n",
1271 PTR2UV(cx->blk_format.retop));
146174a9 1272 break;
8990e307 1273 case CXt_SUB:
146174a9
CB
1274 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
1275 PTR2UV(cx->blk_sub.cv));
760ac839 1276 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 1277 (long)cx->blk_sub.olddepth);
760ac839 1278 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
bafb2adc
NC
1279 (int)CxHASARGS(cx));
1280 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
f39bc417
DM
1281 PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%"UVxf"\n",
1282 PTR2UV(cx->blk_sub.retop));
8990e307
LW
1283 break;
1284 case CXt_EVAL:
760ac839 1285 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
85a64632 1286 (long)CxOLD_IN_EVAL(cx));
760ac839 1287 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
85a64632
NC
1288 PL_op_name[CxOLD_OP_TYPE(cx)],
1289 PL_op_desc[CxOLD_OP_TYPE(cx)]);
0f79a09d
GS
1290 if (cx->blk_eval.old_namesv)
1291 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
aa07b2f6 1292 SvPVX_const(cx->blk_eval.old_namesv));
146174a9
CB
1293 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
1294 PTR2UV(cx->blk_eval.old_eval_root));
f39bc417
DM
1295 PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%"UVxf"\n",
1296 PTR2UV(cx->blk_eval.retop));
8990e307
LW
1297 break;
1298
c6fdafd0 1299 case CXt_LOOP_LAZYIV:
d01136d6 1300 case CXt_LOOP_LAZYSV:
3b719c58
NC
1301 case CXt_LOOP_FOR:
1302 case CXt_LOOP_PLAIN:
0cbdab38 1303 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
760ac839 1304 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
8990e307 1305 (long)cx->blk_loop.resetsp);
022eaa24
NC
1306 PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%"UVxf"\n",
1307 PTR2UV(cx->blk_loop.my_op));
d01136d6 1308 /* XXX: not accurate for LAZYSV/IV */
146174a9 1309 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
d01136d6
BS
1310 PTR2UV(cx->blk_loop.state_u.ary.ary));
1311 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1312 (long)cx->blk_loop.state_u.ary.ix);
146174a9
CB
1313 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
1314 PTR2UV(CxITERVAR(cx)));
8990e307
LW
1315 break;
1316
1317 case CXt_SUBST:
760ac839 1318 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 1319 (long)cx->sb_iters);
760ac839 1320 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 1321 (long)cx->sb_maxiters);
35ef4773
GS
1322 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1323 (long)cx->sb_rflags);
760ac839 1324 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
c5bed6a7 1325 (long)CxONCE(cx));
760ac839 1326 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 1327 cx->sb_orig);
146174a9
CB
1328 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
1329 PTR2UV(cx->sb_dstr));
1330 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
1331 PTR2UV(cx->sb_targ));
1332 PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
1333 PTR2UV(cx->sb_s));
1334 PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
1335 PTR2UV(cx->sb_m));
1336 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
1337 PTR2UV(cx->sb_strend));
1338 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
1339 PTR2UV(cx->sb_rxres));
8990e307
LW
1340 break;
1341 }
65e66c80 1342#else
96a5add6 1343 PERL_UNUSED_CONTEXT;
65e66c80 1344 PERL_UNUSED_ARG(cx);
17c3b450 1345#endif /* DEBUGGING */
35ff7856 1346}
241d1a3b
NC
1347
1348/*
1349 * Local variables:
1350 * c-indentation-style: bsd
1351 * c-basic-offset: 4
14d04a33 1352 * indent-tabs-mode: nil
241d1a3b
NC
1353 * End:
1354 *
14d04a33 1355 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1356 */