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