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