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