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