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