This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[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 27#include "perl.h"
9f601cf3 28#include "feature.h"
79072805 29
a0d0e21e 30SV**
fc16c392 31Perl_stack_grow(pTHX_ SV **sp, SV **p, SSize_t n)
a0d0e21e 32{
402d079e
DM
33 SSize_t extra;
34 SSize_t current = (p - PL_stack_base);
35
7918f24d
NC
36 PERL_ARGS_ASSERT_STACK_GROW;
37
402d079e 38 if (UNLIKELY(n < 0))
6768377c 39 Perl_croak(aTHX_
147e3846 40 "panic: stack_grow() negative count (%" IVdf ")", (IV)n);
6768377c 41
3280af22 42 PL_stack_sp = sp;
402d079e
DM
43 extra =
44#ifdef STRESS_REALLOC
45 1;
2ce36478 46#else
402d079e 47 128;
2ce36478 48#endif
402d079e
DM
49 /* If the total might wrap, panic instead. This is really testing
50 * that (current + n + extra < SSize_t_MAX), but done in a way that
51 * can't wrap */
52 if (UNLIKELY( current > SSize_t_MAX - extra
53 || current + extra > SSize_t_MAX - n
54 ))
55 /* diag_listed_as: Out of memory during %s extend */
56 Perl_croak(aTHX_ "Out of memory during stack extend");
57
58 av_extend(PL_curstack, current + n + extra);
87058c31
DM
59#ifdef DEBUGGING
60 PL_curstackinfo->si_stack_hwm = current + n + extra;
61#endif
62
3280af22 63 return PL_stack_sp;
a0d0e21e
LW
64}
65
2ce36478
SM
66#ifndef STRESS_REALLOC
67#define GROW(old) ((old) * 3 / 2)
68#else
69#define GROW(old) ((old) + 1)
70#endif
71
e336de0d 72PERL_SI *
864dbfa3 73Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
e336de0d
GS
74{
75 PERL_SI *si;
a02a5408 76 Newx(si, 1, PERL_SI);
e336de0d
GS
77 si->si_stack = newAV();
78 AvREAL_off(si->si_stack);
79 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
3280af22 80 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
e336de0d
GS
81 AvFILLp(si->si_stack) = 0;
82 si->si_prev = 0;
83 si->si_next = 0;
84 si->si_cxmax = cxitems - 1;
85 si->si_cxix = -1;
5b6f7443 86 si->si_cxsubix = -1;
e788e7d3 87 si->si_type = PERLSI_UNDEF;
a02a5408 88 Newx(si->si_cxstack, cxitems, PERL_CONTEXT);
490576d1 89 /* Without any kind of initialising CX_PUSHSUBST()
9965345d 90 * in pp_subst() will read uninitialised heap. */
7e337ee0 91 PoisonNew(si->si_cxstack, cxitems, PERL_CONTEXT);
e336de0d
GS
92 return si;
93}
94
79072805 95I32
864dbfa3 96Perl_cxinc(pTHX)
79072805 97{
a3b680e6 98 const IV old_max = cxstack_max;
00195859
HS
99 const IV new_max = GROW(cxstack_max);
100 Renew(cxstack, new_max + 1, PERL_CONTEXT);
101 cxstack_max = new_max;
9965345d
JH
102 /* Without any kind of initialising deep enough recursion
103 * will end up reading uninitialised PERL_CONTEXTs. */
00195859 104 PoisonNew(cxstack + old_max + 1, new_max - old_max, PERL_CONTEXT);
79072805
LW
105 return cxstack_ix + 1;
106}
107
f58e1f6c 108/*
e446bf93 109=for apidoc_section $callback
f58e1f6c
KW
110=for apidoc push_scope
111
112Implements L<perlapi/C<ENTER>>
113
114=cut
115*/
116
79072805 117void
864dbfa3 118Perl_push_scope(pTHX)
79072805 119{
5d9574c1 120 if (UNLIKELY(PL_scopestack_ix == PL_scopestack_max)) {
00195859 121 const IV new_max = GROW(PL_scopestack_max);
1604cfb0 122 Renew(PL_scopestack, new_max, I32);
d343c3ef 123#ifdef DEBUGGING
1604cfb0 124 Renew(PL_scopestack_name, new_max, const char*);
520bb150 125#endif
1604cfb0 126 PL_scopestack_max = new_max;
79072805 127 }
d343c3ef
GG
128#ifdef DEBUGGING
129 PL_scopestack_name[PL_scopestack_ix] = "unknown";
130#endif
3280af22 131 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
79072805
LW
132
133}
134
f58e1f6c 135/*
e446bf93 136=for apidoc_section $callback
f58e1f6c
KW
137=for apidoc pop_scope
138
139Implements L<perlapi/C<LEAVE>>
140
141=cut
142*/
143
79072805 144void
864dbfa3 145Perl_pop_scope(pTHX)
79072805 146{
35a4481c 147 const I32 oldsave = PL_scopestack[--PL_scopestack_ix];
8990e307 148 LEAVE_SCOPE(oldsave);
79072805
LW
149}
150
ba5248fc 151I32 *
864dbfa3 152Perl_markstack_grow(pTHX)
a0d0e21e 153{
35a4481c
AL
154 const I32 oldmax = PL_markstack_max - PL_markstack;
155 const I32 newmax = GROW(oldmax);
a0d0e21e 156
3280af22 157 Renew(PL_markstack, newmax, I32);
3280af22 158 PL_markstack_max = PL_markstack + newmax;
ba5248fc 159 PL_markstack_ptr = PL_markstack + oldmax;
30773234 160 DEBUG_s(DEBUG_v(PerlIO_printf(Perl_debug_log,
147e3846 161 "MARK grow %p %" IVdf " by %" IVdf "\n",
30773234 162 PL_markstack_ptr, (IV)*PL_markstack_ptr, (IV)oldmax)));
ba5248fc 163 return PL_markstack_ptr;
a0d0e21e
LW
164}
165
166void
864dbfa3 167Perl_savestack_grow(pTHX)
79072805 168{
00195859 169 IV new_max;
49706441 170#ifdef STRESS_REALLOC
00195859 171 new_max = PL_savestack_max + SS_MAXPUSH;
49706441 172#else
00195859 173 new_max = GROW(PL_savestack_max);
49706441 174#endif
3caf0269
DM
175 /* Note that we allocate SS_MAXPUSH slots higher than ss_max
176 * so that SS_ADD_END(), SSGROW() etc can do a simper check */
00195859
HS
177 Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
178 PL_savestack_max = new_max;
79072805
LW
179}
180
4b3c1a47
AE
181void
182Perl_savestack_grow_cnt(pTHX_ I32 need)
183{
00195859 184 const IV new_max = PL_savestack_ix + need;
3caf0269
DM
185 /* Note that we allocate SS_MAXPUSH slots higher than ss_max
186 * so that SS_ADD_END(), SSGROW() etc can do a simper check */
00195859
HS
187 Renew(PL_savestack, new_max + SS_MAXPUSH, ANY);
188 PL_savestack_max = new_max;
4b3c1a47
AE
189}
190
2ce36478
SM
191#undef GROW
192
a953aca5
DD
193/* The original function was called Perl_tmps_grow and was removed from public
194 API, Perl_tmps_grow_p is the replacement and it used in public macros but
195 isn't public itself.
196
197 Perl_tmps_grow_p takes a proposed ix. A proposed ix is PL_tmps_ix + extend_by,
198 where the result of (PL_tmps_ix + extend_by) is >= PL_tmps_max
199 Upon return, PL_tmps_stack[ix] will be a valid address. For machine code
200 optimization and register usage reasons, the proposed ix passed into
201 tmps_grow is returned to the caller which the caller can then use to write
202 an SV * to PL_tmps_stack[ix]. If the caller was using tmps_grow in
203 pre-extend mode (EXTEND_MORTAL macro), then it ignores the return value of
204 tmps_grow. Note, tmps_grow DOES NOT write ix to PL_tmps_ix, the caller
205 must assign ix or ret val of tmps_grow to PL_temps_ix themselves if that is
206 appropriate. The assignment to PL_temps_ix can happen before or after
207 tmps_grow call since tmps_grow doesn't look at PL_tmps_ix.
208 */
209
210SSize_t
211Perl_tmps_grow_p(pTHX_ SSize_t ix)
677b06e3 212{
a953aca5 213 SSize_t extend_to = ix;
677b06e3 214#ifndef STRESS_REALLOC
a953aca5 215 if (ix - PL_tmps_max < 128)
1604cfb0 216 extend_to += (PL_tmps_max < 512) ? 128 : 512;
677b06e3 217#endif
00195859 218 Renew(PL_tmps_stack, extend_to + 1, SV*);
a953aca5 219 PL_tmps_max = extend_to + 1;
a953aca5 220 return ix;
677b06e3
GS
221}
222
223
224void
864dbfa3 225Perl_free_tmps(pTHX)
79072805
LW
226{
227 /* XXX should tmps_floor live in cxstack? */
e8eb279c 228 const SSize_t myfloor = PL_tmps_floor;
3280af22 229 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
1604cfb0 230 SV* const sv = PL_tmps_stack[PL_tmps_ix--];
72581b5b 231#ifdef PERL_POISON
1604cfb0 232 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
22ade07d 233#endif
1604cfb0
MS
234 if (LIKELY(sv)) {
235 SvTEMP_off(sv);
236 SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
237 }
79072805
LW
238 }
239}
240
077e4a84
KW
241/*
242=for apidoc save_scalar_at
243
244A helper function for localizing the SV referenced by C<*sptr>.
245
246If C<SAVEf_KEEPOLDELEM> is set in in C<flags>, the function returns the input
247scalar untouched.
248
249Otherwise it replaces C<*sptr> with a new C<undef> scalar, and returns that.
250The new scalar will have the old one's magic (if any) copied to it.
251If there is such magic, and C<SAVEf_SETMAGIC> is set in in C<flags>, 'set'
252magic will be processed on the new scalar. If unset, 'set' magic will be
253skipped. The latter typically means that assignment will soon follow (I<e.g.>,
254S<C<'local $x = $y'>>), and that will handle the magic.
255
256=for apidoc Amnh ||SAVEf_KEEPOLDELEM
257=for apidoc Amnh ||SAVEf_SETMAGIC
258
259=cut
260*/
261
76e3520e 262STATIC SV *
af7df257 263S_save_scalar_at(pTHX_ SV **sptr, const U32 flags)
79072805 264{
75d34a09 265 SV * osv;
eb578fdb 266 SV *sv;
79072805 267
7918f24d
NC
268 PERL_ARGS_ASSERT_SAVE_SCALAR_AT;
269
75d34a09 270 osv = *sptr;
3c84a230
DM
271 if (flags & SAVEf_KEEPOLDELEM)
272 sv = osv;
273 else {
8fcb2425 274 sv = (*sptr = newSV_type(SVt_NULL));
3c84a230
DM
275 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv))
276 mg_localize(osv, sv, cBOOL(flags & SAVEf_SETMAGIC));
79072805 277 }
75d34a09 278
79072805
LW
279 return sv;
280}
281
dfcd3de5
NC
282void
283Perl_save_pushptrptr(pTHX_ void *const ptr1, void *const ptr2, const int type)
e22024d3 284{
a3444cc5
DM
285 dSS_ADD;
286 SS_ADD_PTR(ptr1);
287 SS_ADD_PTR(ptr2);
288 SS_ADD_UV(type);
289 SS_ADD_END(3);
e22024d3
NC
290}
291
7a4c00b4 292SV *
864dbfa3 293Perl_save_scalar(pTHX_ GV *gv)
7a4c00b4 294{
13c59d41 295 SV ** const sptr = &GvSVn(gv);
7918f24d
NC
296
297 PERL_ARGS_ASSERT_SAVE_SCALAR;
298
13c59d41 299 if (UNLIKELY(SvGMAGICAL(*sptr))) {
b492a59e 300 PL_localizing = 1;
13c59d41 301 (void)mg_get(*sptr);
b492a59e
DM
302 PL_localizing = 0;
303 }
13c59d41
MH
304 save_pushptrptr(SvREFCNT_inc_simple(gv), SvREFCNT_inc(*sptr), SAVEt_SV);
305 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
7a4c00b4 306}
307
e446bf93
KW
308/*
309=for apidoc save_generic_svref
310
311Implements C<SAVEGENERICSV>.
312
313Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
314restore a global SV to its prior contents, freeing new value.
315
316=cut
317 */
318
b9d12d37 319void
864dbfa3 320Perl_save_generic_svref(pTHX_ SV **sptr)
b9d12d37 321{
7918f24d
NC
322 PERL_ARGS_ASSERT_SAVE_GENERIC_SVREF;
323
e22024d3 324 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_GENERIC_SVREF);
b9d12d37
GS
325}
326
e446bf93
KW
327/*
328=for apidoc_section $callback
329=for apidoc save_generic_pvref
330
331Implements C<SAVEGENERICPV>.
332
333Like save_pptr(), but also Safefree()s the new value if it is different
334from the old one. Can be used to restore a global char* to its prior
335contents, freeing new value.
336
337=cut
338 */
339
f4dd75d9
GS
340void
341Perl_save_generic_pvref(pTHX_ char **str)
342{
7918f24d
NC
343 PERL_ARGS_ASSERT_SAVE_GENERIC_PVREF;
344
e22024d3 345 save_pushptrptr(*str, str, SAVEt_GENERIC_PVREF);
f4dd75d9
GS
346}
347
e446bf93
KW
348/*
349=for apidoc_section $callback
350=for apidoc save_shared_pvref
351
352Implements C<SAVESHAREDPV>.
353
354Like save_generic_pvref(), but uses PerlMemShared_free() rather than Safefree().
355Can be used to restore a shared global char* to its prior
356contents, freeing new value.
357
358=cut
359 */
360
05ec9bb3
NIS
361void
362Perl_save_shared_pvref(pTHX_ char **str)
363{
7918f24d
NC
364 PERL_ARGS_ASSERT_SAVE_SHARED_PVREF;
365
e22024d3 366 save_pushptrptr(str, *str, SAVEt_SHARED_PVREF);
05ec9bb3
NIS
367}
368
e446bf93
KW
369
370/*
371=for apidoc_section $callback
372=for apidoc save_set_svflags
373
374Implements C<SAVESETSVFLAGS>.
375
376Set the SvFLAGS specified by mask to the values in val
377
378=cut
379 */
14f338dc
DM
380
381void
382Perl_save_set_svflags(pTHX_ SV* sv, U32 mask, U32 val)
383{
a3444cc5 384 dSS_ADD;
7918f24d
NC
385
386 PERL_ARGS_ASSERT_SAVE_SET_SVFLAGS;
387
a3444cc5
DM
388 SS_ADD_PTR(sv);
389 SS_ADD_INT(mask);
390 SS_ADD_INT(val);
391 SS_ADD_UV(SAVEt_SET_SVFLAGS);
392 SS_ADD_END(4);
14f338dc
DM
393}
394
364bbfa4 395/*
d4f04357 396
3f620621 397=for apidoc_section $GV
d4f04357 398
364bbfa4
TC
399=for apidoc save_gp
400
401Saves the current GP of gv on the save stack to be restored on scope exit.
402
06ecdb43 403If C<empty> is true, replace the GP with a new GP.
364bbfa4 404
780fa0f3 405If C<empty> is false, mark C<gv> with C<GVf_INTRO> so the next reference
06ecdb43 406assigned is localized, which is how S<C< local *foo = $someref; >> works.
364bbfa4
TC
407
408=cut
409*/
410
79072805 411void
864dbfa3 412Perl_save_gp(pTHX_ GV *gv, I32 empty)
79072805 413{
7918f24d
NC
414 PERL_ARGS_ASSERT_SAVE_GP;
415
6881372e
FC
416 /* XXX For now, we just upgrade any coderef in the stash to a full GV
417 during localisation. Maybe at some point we could make localis-
418 ation work without needing the upgrade. (In which case our
419 callers should probably call a different function, not save_gp.)
420 */
421 if (!isGV(gv)) {
422 assert(isGV_or_RVCV(gv));
423 (void)CvGV(SvRV((SV *)gv)); /* CvGV does the upgrade */
424 assert(isGV(gv));
425 }
426
10507e11 427 save_pushptrptr(SvREFCNT_inc(gv), GvGP(gv), SAVEt_GP);
79072805 428
5f05dabc 429 if (empty) {
1604cfb0
MS
430 GP *gp = Perl_newGP(aTHX_ gv);
431 HV * const stash = GvSTASH(gv);
432 bool isa_changed = 0;
433
434 if (stash && HvENAME(stash)) {
435 if (memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
436 isa_changed = TRUE;
437 else if (GvCVu(gv))
438 /* taking a method out of circulation ("local")*/
3d460042 439 mro_method_changed_in(stash);
1604cfb0
MS
440 }
441 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
442 gp->gp_io = newIO();
443 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
444 }
445 GvGP_set(gv,gp);
446 if (isa_changed) mro_isa_changed_in(stash);
5f05dabc 447 }
448 else {
1604cfb0
MS
449 gp_ref(GvGP(gv));
450 GvINTRO_on(gv);
5f05dabc 451 }
79072805 452}
79072805 453
79072805 454AV *
864dbfa3 455Perl_save_ary(pTHX_ GV *gv)
79072805 456{
901017d6 457 AV * const oav = GvAVn(gv);
67a38de0 458 AV *av;
fb73857a 459
7918f24d
NC
460 PERL_ARGS_ASSERT_SAVE_ARY;
461
5d9574c1 462 if (UNLIKELY(!AvREAL(oav) && AvREIFY(oav)))
1604cfb0 463 av_reify(oav);
01433346 464 save_pushptrptr(SvREFCNT_inc_simple_NN(gv), oav, SAVEt_AV);
79072805 465
4608196e 466 GvAV(gv) = NULL;
fb73857a 467 av = GvAVn(gv);
5d9574c1 468 if (UNLIKELY(SvMAGIC(oav)))
1604cfb0 469 mg_localize(MUTABLE_SV(oav), MUTABLE_SV(av), TRUE);
fb73857a 470 return av;
79072805
LW
471}
472
473HV *
864dbfa3 474Perl_save_hash(pTHX_ GV *gv)
79072805 475{
fb73857a 476 HV *ohv, *hv;
477
7918f24d
NC
478 PERL_ARGS_ASSERT_SAVE_HASH;
479
01433346 480 save_pushptrptr(
1604cfb0 481 SvREFCNT_inc_simple_NN(gv), (ohv = GvHVn(gv)), SAVEt_HV
01433346 482 );
79072805 483
4608196e 484 GvHV(gv) = NULL;
fb73857a 485 hv = GvHVn(gv);
5d9574c1 486 if (UNLIKELY(SvMAGIC(ohv)))
1604cfb0 487 mg_localize(MUTABLE_SV(ohv), MUTABLE_SV(hv), TRUE);
fb73857a 488 return hv;
79072805
LW
489}
490
491void
5aaab254 492Perl_save_item(pTHX_ SV *item)
79072805 493{
eb578fdb 494 SV * const sv = newSVsv(item);
79072805 495
7918f24d
NC
496 PERL_ARGS_ASSERT_SAVE_ITEM;
497
e22024d3 498 save_pushptrptr(item, /* remember the pointer */
1604cfb0
MS
499 sv, /* remember the value */
500 SAVEt_ITEM);
79072805
LW
501}
502
503void
1409bc06 504Perl_save_bool(pTHX_ bool *boolp)
79072805 505{
a3444cc5 506 dSS_ADD;
7918f24d 507
1409bc06 508 PERL_ARGS_ASSERT_SAVE_BOOL;
7918f24d 509
a3444cc5
DM
510 SS_ADD_PTR(boolp);
511 SS_ADD_UV(SAVEt_BOOL | (*boolp << 8));
512 SS_ADD_END(2);
1409bc06
NC
513}
514
7623d426
NC
515void
516Perl_save_pushi32ptr(pTHX_ const I32 i, void *const ptr, const int type)
1409bc06 517{
a3444cc5
DM
518 dSS_ADD;
519
520 SS_ADD_INT(i);
521 SS_ADD_PTR(ptr);
522 SS_ADD_UV(type);
523 SS_ADD_END(3);
79072805
LW
524}
525
526void
1409bc06 527Perl_save_int(pTHX_ int *intp)
9febdf04 528{
d172696c 529 const int i = *intp;
9b139d09 530 UV type = ((UV)((UV)i << SAVE_TIGHT_SHIFT) | SAVEt_INT_SMALL);
d172696c
DM
531 int size = 2;
532 dSS_ADD;
7918f24d 533
1409bc06 534 PERL_ARGS_ASSERT_SAVE_INT;
7918f24d 535
5d9574c1 536 if (UNLIKELY((int)(type >> SAVE_TIGHT_SHIFT) != i)) {
d172696c
DM
537 SS_ADD_INT(i);
538 type = SAVEt_INT;
539 size++;
540 }
541 SS_ADD_PTR(intp);
542 SS_ADD_UV(type);
543 SS_ADD_END(size);
9febdf04
RH
544}
545
546void
58188858
RGS
547Perl_save_I8(pTHX_ I8 *bytep)
548{
a3444cc5 549 dSS_ADD;
7918f24d
NC
550
551 PERL_ARGS_ASSERT_SAVE_I8;
552
a3444cc5
DM
553 SS_ADD_PTR(bytep);
554 SS_ADD_UV(SAVEt_I8 | ((UV)*bytep << 8));
555 SS_ADD_END(2);
58188858
RGS
556}
557
558void
87a84751
JH
559Perl_save_I16(pTHX_ I16 *intp)
560{
a3444cc5 561 dSS_ADD;
7918f24d
NC
562
563 PERL_ARGS_ASSERT_SAVE_I16;
564
a3444cc5
DM
565 SS_ADD_PTR(intp);
566 SS_ADD_UV(SAVEt_I16 | ((UV)*intp << 8));
567 SS_ADD_END(2);
87a84751
JH
568}
569
570void
864dbfa3 571Perl_save_I32(pTHX_ I32 *intp)
79072805 572{
d172696c 573 const I32 i = *intp;
9b139d09 574 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_I32_SMALL);
d172696c
DM
575 int size = 2;
576 dSS_ADD;
7918f24d
NC
577
578 PERL_ARGS_ASSERT_SAVE_I32;
579
5d9574c1 580 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
d172696c
DM
581 SS_ADD_INT(i);
582 type = SAVEt_I32;
583 size++;
584 }
585 SS_ADD_PTR(intp);
586 SS_ADD_UV(type);
587 SS_ADD_END(size);
79072805
LW
588}
589
e8eb279c
FC
590void
591Perl_save_strlen(pTHX_ STRLEN *ptr)
592{
8462890b
PE
593 const IV i = *ptr;
594 UV type = ((I32)((U32)i << SAVE_TIGHT_SHIFT) | SAVEt_STRLEN_SMALL);
595 int size = 2;
e8eb279c
FC
596 dSS_ADD;
597
598 PERL_ARGS_ASSERT_SAVE_STRLEN;
599
8462890b
PE
600 if (UNLIKELY((I32)(type >> SAVE_TIGHT_SHIFT) != i)) {
601 SS_ADD_IV(*ptr);
602 type = SAVEt_STRLEN;
603 size++;
604 }
605
e8eb279c 606 SS_ADD_PTR(ptr);
8462890b
PE
607 SS_ADD_UV(type);
608 SS_ADD_END(size);
e8eb279c
FC
609}
610
f8c11501
FC
611void
612Perl_save_iv(pTHX_ IV *ivp)
613{
614 PERL_ARGS_ASSERT_SAVE_IV;
615
616 SSCHECK(3);
617 SSPUSHIV(*ivp);
618 SSPUSHPTR(ivp);
619 SSPUSHUV(SAVEt_IV);
620}
621
85e6fe83
LW
622/* Cannot use save_sptr() to store a char* since the SV** cast will
623 * force word-alignment and we'll miss the pointer.
624 */
625void
864dbfa3 626Perl_save_pptr(pTHX_ char **pptr)
85e6fe83 627{
7918f24d
NC
628 PERL_ARGS_ASSERT_SAVE_PPTR;
629
e22024d3 630 save_pushptrptr(*pptr, pptr, SAVEt_PPTR);
85e6fe83
LW
631}
632
e446bf93
KW
633/*
634=for apidoc_section $callback
635=for apidoc save_vptr
636
637Implements C<SAVEVPTR>.
638
639=cut
640 */
641
79072805 642void
146174a9
CB
643Perl_save_vptr(pTHX_ void *ptr)
644{
7918f24d
NC
645 PERL_ARGS_ASSERT_SAVE_VPTR;
646
e22024d3 647 save_pushptrptr(*(char**)ptr, ptr, SAVEt_VPTR);
146174a9
CB
648}
649
650void
864dbfa3 651Perl_save_sptr(pTHX_ SV **sptr)
79072805 652{
7918f24d
NC
653 PERL_ARGS_ASSERT_SAVE_SPTR;
654
e22024d3 655 save_pushptrptr(*sptr, sptr, SAVEt_SPTR);
79072805
LW
656}
657
e446bf93
KW
658/*
659=for apidoc_section $callback
660=for apidoc save_padsv_and_mortalize
661
662Implements C<SAVEPADSVANDMORTALIZE>.
663
664=cut
665 */
666
c3564e5c 667void
09edbca0 668Perl_save_padsv_and_mortalize(pTHX_ PADOFFSET off)
c3564e5c 669{
a3444cc5
DM
670 dSS_ADD;
671
f3548bdc 672 ASSERT_CURPAD_ACTIVE("save_padsv");
a3444cc5
DM
673 SS_ADD_PTR(SvREFCNT_inc_simple_NN(PL_curpad[off]));
674 SS_ADD_PTR(PL_comppad);
03dba561 675 SS_ADD_UV((UV)off);
a3444cc5
DM
676 SS_ADD_UV(SAVEt_PADSV_AND_MORTALIZE);
677 SS_ADD_END(4);
c3564e5c
GS
678}
679
79072805 680void
864dbfa3 681Perl_save_hptr(pTHX_ HV **hptr)
79072805 682{
7918f24d
NC
683 PERL_ARGS_ASSERT_SAVE_HPTR;
684
e22024d3 685 save_pushptrptr(*hptr, hptr, SAVEt_HPTR);
79072805
LW
686}
687
688void
864dbfa3 689Perl_save_aptr(pTHX_ AV **aptr)
79072805 690{
7918f24d
NC
691 PERL_ARGS_ASSERT_SAVE_APTR;
692
e22024d3 693 save_pushptrptr(*aptr, aptr, SAVEt_APTR);
79072805
LW
694}
695
4601e425
KW
696/*
697=for apidoc_section $callback
698=for apidoc save_pushptr
699
700The refcnt of object C<ptr> will be decremented at the end of the current
701I<pseudo-block>. C<type> gives the type of C<ptr>, expressed as one of the
702constants in F<scope.h> whose name begins with C<SAVEt_>.
703
704This is the underlying implementation of several macros, like
705C<SAVEFREESV>.
706
707=cut
708*/
709
79072805 710void
2fd8beea 711Perl_save_pushptr(pTHX_ void *const ptr, const int type)
8990e307 712{
a3444cc5
DM
713 dSS_ADD;
714 SS_ADD_PTR(ptr);
715 SS_ADD_UV(type);
716 SS_ADD_END(2);
8990e307
LW
717}
718
719void
864dbfa3 720Perl_save_clearsv(pTHX_ SV **svp)
8990e307 721{
cdcdfc56
NC
722 const UV offset = svp - PL_curpad;
723 const UV offset_shifted = offset << SAVE_TIGHT_SHIFT;
7918f24d
NC
724
725 PERL_ARGS_ASSERT_SAVE_CLEARSV;
726
f3548bdc 727 ASSERT_CURPAD_ACTIVE("save_clearsv");
623e28c6 728 SvPADSTALE_off(*svp); /* mark lexical as active */
5d9574c1 729 if (UNLIKELY((offset_shifted >> SAVE_TIGHT_SHIFT) != offset)) {
1604cfb0
MS
730 Perl_croak(aTHX_ "panic: pad offset %" UVuf " out of range (%p-%p)",
731 offset, svp, PL_curpad);
a3444cc5 732 }
cdcdfc56 733
a3444cc5
DM
734 {
735 dSS_ADD;
736 SS_ADD_UV(offset_shifted | SAVEt_CLEARSV);
737 SS_ADD_END(1);
738 }
8990e307
LW
739}
740
741void
864dbfa3 742Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
8990e307 743{
7918f24d
NC
744 PERL_ARGS_ASSERT_SAVE_DELETE;
745
85a721ca 746 save_pushptri32ptr(key, klen, SvREFCNT_inc_simple(hv), SAVEt_DELETE);
8990e307
LW
747}
748
aaa067ea
KW
749/*
750=for apidoc_section $callback
751=for apidoc save_hdelete
752
753Implements C<SAVEHDELETE>.
754
755=cut
756*/
757
8990e307 758void
af097752
VP
759Perl_save_hdelete(pTHX_ HV *hv, SV *keysv)
760{
761 STRLEN len;
762 I32 klen;
763 const char *key;
764
765 PERL_ARGS_ASSERT_SAVE_HDELETE;
766
767 key = SvPV_const(keysv, len);
768 klen = SvUTF8(keysv) ? -(I32)len : (I32)len;
769 SvREFCNT_inc_simple_void_NN(hv);
770 save_pushptri32ptr(savepvn(key, len), klen, hv, SAVEt_DELETE);
771}
772
aaa067ea
KW
773/*
774=for apidoc_section $callback
775=for apidoc save_adelete
776
777Implements C<SAVEADELETE>.
778
779=cut
780*/
781
af097752 782void
c70927a6 783Perl_save_adelete(pTHX_ AV *av, SSize_t key)
c68ec7a9 784{
c70927a6 785 dSS_ADD;
c68ec7a9
VP
786
787 PERL_ARGS_ASSERT_SAVE_ADELETE;
788
789 SvREFCNT_inc_void(av);
c70927a6
FC
790 SS_ADD_UV(key);
791 SS_ADD_PTR(av);
792 SS_ADD_IV(SAVEt_ADELETE);
793 SS_ADD_END(3);
c68ec7a9
VP
794}
795
796void
12ab1f58
JH
797Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
798{
a3444cc5 799 dSS_ADD;
7918f24d
NC
800 PERL_ARGS_ASSERT_SAVE_DESTRUCTOR;
801
a3444cc5
DM
802 SS_ADD_DPTR(f);
803 SS_ADD_PTR(p);
804 SS_ADD_UV(SAVEt_DESTRUCTOR);
805 SS_ADD_END(3);
12ab1f58
JH
806}
807
808void
146174a9
CB
809Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
810{
a3444cc5
DM
811 dSS_ADD;
812
813 SS_ADD_DXPTR(f);
814 SS_ADD_PTR(p);
815 SS_ADD_UV(SAVEt_DESTRUCTOR_X);
816 SS_ADD_END(3);
146174a9
CB
817}
818
e446bf93
KW
819/*
820=for apidoc_section $callback
821=for apidoc save_hints
822
823Implements C<SAVEHINTS>.
824
825=cut
826 */
827
146174a9 828void
da8315f8
NC
829Perl_save_hints(pTHX)
830{
20439bc7 831 COPHH *save_cophh = cophh_copy(CopHINTHASH_get(&PL_compiling));
da8315f8 832 if (PL_hints & HINT_LOCALIZE_HH) {
1604cfb0 833 HV *oldhh = GvHV(PL_hintgv);
6c148c86
PE
834 {
835 dSS_ADD;
836 SS_ADD_INT(PL_hints);
837 SS_ADD_PTR(save_cophh);
838 SS_ADD_PTR(oldhh);
78efaf03 839 SS_ADD_UV(SAVEt_HINTS_HH | (PL_prevailing_version << 8));
6c148c86
PE
840 SS_ADD_END(4);
841 }
1604cfb0
MS
842 GvHV(PL_hintgv) = NULL; /* in case copying dies */
843 GvHV(PL_hintgv) = hv_copy_hints_hv(oldhh);
9f601cf3 844 SAVEFEATUREBITS();
be84297e 845 } else {
78efaf03 846 save_pushi32ptr(PL_hints, save_cophh, SAVEt_HINTS | (PL_prevailing_version << 8));
da8315f8 847 }
be84297e
NC
848}
849
850static void
851S_save_pushptri32ptr(pTHX_ void *const ptr1, const I32 i, void *const ptr2,
1604cfb0 852 const int type)
be84297e 853{
a3444cc5
DM
854 dSS_ADD;
855 SS_ADD_PTR(ptr1);
856 SS_ADD_INT(i);
857 SS_ADD_PTR(ptr2);
858 SS_ADD_UV(type);
859 SS_ADD_END(4);
da8315f8
NC
860}
861
d4192ed9
KW
862/*
863=for apidoc_section $callback
864=for apidoc save_aelem
865=for apidoc_item save_aelem_flags
866
867These each arrange for the value of the array element C<av[idx]> to be restored
868at the end of the enclosing I<pseudo-block>.
869
870In C<save_aelem>, the SV at C**sptr> will be replaced by a new C<undef>
871scalar. That scalar will inherit any magic from the original C<**sptr>,
872and any 'set' magic will be processed.
873
874In C<save_aelem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
875the function to forgo all that: the scalar at C<**sptr> is untouched.
876If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
877new C<undef> scalar. That scalar will inherit any magic from the original
878C<**sptr>. Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
879is set in in C<flags>.
880
881=cut
882*/
883
da8315f8 884void
c70927a6 885Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
1604cfb0 886 const U32 flags)
4e4c362e 887{
20b7effb 888 dSS_ADD;
bfc4de9f 889 SV *sv;
7918f24d 890
91d1c79f 891 PERL_ARGS_ASSERT_SAVE_AELEM_FLAGS;
7918f24d 892
0cbee0a4 893 SvGETMAGIC(*sptr);
c70927a6
FC
894 SS_ADD_PTR(SvREFCNT_inc_simple(av));
895 SS_ADD_IV(idx);
896 SS_ADD_PTR(SvREFCNT_inc(*sptr));
897 SS_ADD_UV(SAVEt_AELEM);
898 SS_ADD_END(4);
1cdc9186
FC
899 /* The array needs to hold a reference count on its new element, so it
900 must be AvREAL. */
5d9574c1 901 if (UNLIKELY(!AvREAL(av) && AvREIFY(av)))
1604cfb0 902 av_reify(av);
91d1c79f 903 save_scalar_at(sptr, flags); /* XXX - FIXME - see #60360 */
75d34a09 904 if (flags & SAVEf_KEEPOLDELEM)
1604cfb0 905 return;
bfc4de9f
DM
906 sv = *sptr;
907 /* If we're localizing a tied array element, this new sv
908 * won't actually be stored in the array - so it won't get
909 * reaped when the localize ends. Ensure it gets reaped by
910 * mortifying it instead. DAPM */
5d9574c1 911 if (UNLIKELY(SvTIED_mg((const SV *)av, PERL_MAGIC_tied)))
1604cfb0 912 sv_2mortal(sv);
4e4c362e
GS
913}
914
d4192ed9
KW
915/*
916=for apidoc_section $callback
917=for apidoc save_helem
918=for apidoc_item save_helem_flags
919
920These each arrange for the value of the hash element (in Perlish terms)
921C<$hv{key}]> to be restored at the end of the enclosing I<pseudo-block>.
922
923In C<save_helem>, the SV at C**sptr> will be replaced by a new C<undef>
924scalar. That scalar will inherit any magic from the original C<**sptr>,
925and any 'set' magic will be processed.
926
927In C<save_helem_flags>, C<SAVEf_KEEPOLDELEM> being set in C<flags> causes
928the function to forgo all that: the scalar at C<**sptr> is untouched.
929If C<SAVEf_KEEPOLDELEM> is not set, the SV at C**sptr> will be replaced by a
930new C<undef> scalar. That scalar will inherit any magic from the original
931C<**sptr>. Any 'set' magic will be processed if and only if C<SAVEf_SETMAGIC>
932is set in in C<flags>.
933
934=cut
935*/
936
4e4c362e 937void
af7df257 938Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr, const U32 flags)
4e4c362e 939{
bfc4de9f 940 SV *sv;
7918f24d 941
af7df257 942 PERL_ARGS_ASSERT_SAVE_HELEM_FLAGS;
7918f24d 943
0cbee0a4 944 SvGETMAGIC(*sptr);
a3444cc5
DM
945 {
946 dSS_ADD;
947 SS_ADD_PTR(SvREFCNT_inc_simple(hv));
948 SS_ADD_PTR(newSVsv(key));
949 SS_ADD_PTR(SvREFCNT_inc(*sptr));
950 SS_ADD_UV(SAVEt_HELEM);
951 SS_ADD_END(4);
952 }
af7df257 953 save_scalar_at(sptr, flags);
75d34a09 954 if (flags & SAVEf_KEEPOLDELEM)
1604cfb0 955 return;
bfc4de9f
DM
956 sv = *sptr;
957 /* If we're localizing a tied hash element, this new sv
958 * won't actually be stored in the hash - so it won't get
959 * reaped when the localize ends. Ensure it gets reaped by
960 * mortifying it instead. DAPM */
5d9574c1 961 if (UNLIKELY(SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)))
1604cfb0 962 sv_2mortal(sv);
4e4c362e
GS
963}
964
2053acbf
NC
965SV*
966Perl_save_svref(pTHX_ SV **sptr)
967{
7918f24d
NC
968 PERL_ARGS_ASSERT_SAVE_SVREF;
969
2053acbf 970 SvGETMAGIC(*sptr);
e22024d3 971 save_pushptrptr(sptr, SvREFCNT_inc(*sptr), SAVEt_SVREF);
af7df257 972 return save_scalar_at(sptr, SAVEf_SETMAGIC); /* XXX - FIXME - see #60360 */
2053acbf
NC
973}
974
2ef9a108
DM
975
976void
977Perl_savetmps(pTHX)
978{
979 dSS_ADD;
980 SS_ADD_IV(PL_tmps_floor);
981 PL_tmps_floor = PL_tmps_ix;
982 SS_ADD_UV(SAVEt_TMPSFLOOR);
983 SS_ADD_END(2);
984}
985
1f3d7af7
KW
986/*
987=for apidoc_section $stack
988=for apidoc save_alloc
989
990Implements L<perlapi/C<SSNEW>> and kin, which should be used instead of this
991function.
992
993=cut
994*/
2ef9a108 995
455ece5e 996I32
864dbfa3 997Perl_save_alloc(pTHX_ I32 size, I32 pad)
455ece5e 998{
eb578fdb
KW
999 const I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
1000 - (char*)PL_savestack);
1be36ce0
NC
1001 const UV elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
1002 const UV elems_shifted = elems << SAVE_TIGHT_SHIFT;
455ece5e 1003
5d9574c1 1004 if (UNLIKELY((elems_shifted >> SAVE_TIGHT_SHIFT) != elems))
1604cfb0 1005 Perl_croak(aTHX_
147e3846 1006 "panic: save_alloc elems %" UVuf " out of range (%" IVdf "-%" IVdf ")",
1604cfb0 1007 elems, (IV)size, (IV)pad);
1be36ce0
NC
1008
1009 SSGROW(elems + 1);
455ece5e
AD
1010
1011 PL_savestack_ix += elems;
1be36ce0 1012 SSPUSHUV(SAVEt_ALLOC | elems_shifted);
455ece5e
AD
1013 return start;
1014}
1015
03dba561 1016
8c036433 1017static const U8 arg_counts[] = {
9a2fefd6
DM
1018 0, /* SAVEt_ALLOC */
1019 0, /* SAVEt_CLEARPADRANGE */
1020 0, /* SAVEt_CLEARSV */
1021 0, /* SAVEt_REGCONTEXT */
1022 1, /* SAVEt_TMPSFLOOR */
1023 1, /* SAVEt_BOOL */
1024 1, /* SAVEt_COMPILE_WARNINGS */
1025 1, /* SAVEt_COMPPAD */
1026 1, /* SAVEt_FREECOPHH */
1027 1, /* SAVEt_FREEOP */
1028 1, /* SAVEt_FREEPV */
1029 1, /* SAVEt_FREESV */
1030 1, /* SAVEt_I16 */
1031 1, /* SAVEt_I32_SMALL */
1032 1, /* SAVEt_I8 */
1033 1, /* SAVEt_INT_SMALL */
1034 1, /* SAVEt_MORTALIZESV */
1035 1, /* SAVEt_NSTAB */
1036 1, /* SAVEt_OP */
1037 1, /* SAVEt_PARSER */
1038 1, /* SAVEt_STACK_POS */
1039 1, /* SAVEt_READONLY_OFF */
1040 1, /* SAVEt_FREEPADNAME */
8462890b 1041 1, /* SAVEt_STRLEN_SMALL */
9a2fefd6
DM
1042 2, /* SAVEt_AV */
1043 2, /* SAVEt_DESTRUCTOR */
1044 2, /* SAVEt_DESTRUCTOR_X */
1045 2, /* SAVEt_GENERIC_PVREF */
1046 2, /* SAVEt_GENERIC_SVREF */
1047 2, /* SAVEt_GP */
1048 2, /* SAVEt_GVSV */
1049 2, /* SAVEt_HINTS */
1050 2, /* SAVEt_HPTR */
1051 2, /* SAVEt_HV */
1052 2, /* SAVEt_I32 */
1053 2, /* SAVEt_INT */
1054 2, /* SAVEt_ITEM */
1055 2, /* SAVEt_IV */
1056 2, /* SAVEt_LONG */
1057 2, /* SAVEt_PPTR */
1058 2, /* SAVEt_SAVESWITCHSTACK */
1059 2, /* SAVEt_SHARED_PVREF */
1060 2, /* SAVEt_SPTR */
1061 2, /* SAVEt_STRLEN */
1062 2, /* SAVEt_SV */
1063 2, /* SAVEt_SVREF */
1064 2, /* SAVEt_VPTR */
1065 2, /* SAVEt_ADELETE */
1066 2, /* SAVEt_APTR */
1067 3, /* SAVEt_HELEM */
1068 3, /* SAVEt_PADSV_AND_MORTALIZE*/
1069 3, /* SAVEt_SET_SVFLAGS */
1070 3, /* SAVEt_GVSLOT */
1071 3, /* SAVEt_AELEM */
6c148c86
PE
1072 3, /* SAVEt_DELETE */
1073 3 /* SAVEt_HINTS_HH */
9a2fefd6
DM
1074};
1075
03dba561 1076
a8ed036f
KW
1077/*
1078=for apidoc_section $callback
1079=for apidoc leave_scope
1080
1081Implements C<LEAVE_SCOPE> which you should use instead.
1082
1083=cut
1084 */
1085
462e5cf6 1086void
864dbfa3 1087Perl_leave_scope(pTHX_ I32 base)
79072805 1088{
302c0c93 1089 /* Localise the effects of the TAINT_NOT inside the loop. */
284167a5 1090 bool was = TAINT_get;
79072805 1091
5d9574c1 1092 if (UNLIKELY(base < -1))
1604cfb0 1093 Perl_croak(aTHX_ "panic: corrupt saved stack index %ld", (long) base);
1c98cc53 1094 DEBUG_l(Perl_deb(aTHX_ "savestack: releasing items %ld -> %ld\n",
1604cfb0 1095 (long)PL_savestack_ix, (long)base));
3280af22 1096 while (PL_savestack_ix > base) {
1604cfb0
MS
1097 UV uv;
1098 U8 type;
9a2fefd6 1099 ANY *ap; /* arg pointer */
c9728782 1100 ANY a0, a1, a2; /* up to 3 args */
03dba561 1101
1604cfb0 1102 TAINT_NOT;
c6ae7647 1103
03dba561 1104 {
9a2fefd6 1105 U8 argcount;
03dba561 1106 I32 ix = PL_savestack_ix - 1;
9a2fefd6
DM
1107
1108 ap = &PL_savestack[ix];
1109 uv = ap->any_uv;
03dba561 1110 type = (U8)uv & SAVE_MASK;
9a2fefd6
DM
1111 argcount = arg_counts[type];
1112 PL_savestack_ix = ix - argcount;
1113 ap -= argcount;
03dba561
DM
1114 }
1115
1604cfb0
MS
1116 switch (type) {
1117 case SAVEt_ITEM: /* normal string */
c9728782 1118 a0 = ap[0]; a1 = ap[1];
1604cfb0 1119 sv_replace(a0.any_sv, a1.any_sv);
c9728782 1120 if (UNLIKELY(SvSMAGICAL(a0.any_sv))) {
b492a59e 1121 PL_localizing = 2;
c9728782 1122 mg_set(a0.any_sv);
b492a59e
DM
1123 PL_localizing = 0;
1124 }
1604cfb0 1125 break;
03dba561 1126
1604cfb0
MS
1127 /* This would be a mathom, but Perl_save_svref() calls a static
1128 function, S_save_scalar_at(), so has to stay in this file. */
1129 case SAVEt_SVREF: /* scalar reference */
c9728782 1130 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1131 a2.any_svp = a0.any_svp;
1132 a0.any_sv = NULL; /* what to refcnt_dec */
1133 goto restore_sv;
03dba561 1134
1604cfb0 1135 case SAVEt_SV: /* scalar reference */
c9728782 1136 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1137 a2.any_svp = &GvSV(a0.any_gv);
1138 restore_sv:
03dba561 1139 {
75ebea3c 1140 /* do *a2.any_svp = a1 and free a0 */
1604cfb0
MS
1141 SV * const sv = *a2.any_svp;
1142 *a2.any_svp = a1.any_sv;
1143 SvREFCNT_dec(sv);
c9728782
DM
1144 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1145 /* mg_set could die, skipping the freeing of a0 and
1146 * a1; Ensure that they're always freed in that case */
2baebb98 1147 dSS_ADD;
c9728782 1148 SS_ADD_PTR(a1.any_sv);
2baebb98 1149 SS_ADD_UV(SAVEt_FREESV);
c9728782 1150 SS_ADD_PTR(a0.any_sv);
2baebb98
DM
1151 SS_ADD_UV(SAVEt_FREESV);
1152 SS_ADD_END(4);
b492a59e 1153 PL_localizing = 2;
c9728782 1154 mg_set(a1.any_sv);
b492a59e 1155 PL_localizing = 0;
2baebb98 1156 break;
b492a59e 1157 }
1604cfb0
MS
1158 SvREFCNT_dec_NN(a1.any_sv);
1159 SvREFCNT_dec(a0.any_sv);
1160 break;
03dba561 1161 }
c9728782 1162
1604cfb0 1163 case SAVEt_GENERIC_PVREF: /* generic pv */
c9728782 1164 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1165 if (*a1.any_pvp != a0.any_pv) {
1166 Safefree(*a1.any_pvp);
1167 *a1.any_pvp = a0.any_pv;
1168 }
1169 break;
c9728782 1170
1604cfb0 1171 case SAVEt_SHARED_PVREF: /* shared pv */
c9728782 1172 a0 = ap[0]; a1 = ap[1];
1604cfb0 1173 if (*a0.any_pvp != a1.any_pv) {
1604cfb0 1174 PerlMemShared_free(*a0.any_pvp);
1604cfb0
MS
1175 *a0.any_pvp = a1.any_pv;
1176 }
1177 break;
c9728782 1178
1604cfb0 1179 case SAVEt_GVSV: /* scalar slot in GV */
c9728782 1180 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1181 a0.any_svp = &GvSV(a0.any_gv);
1182 goto restore_svp;
c9728782 1183
1604cfb0 1184 case SAVEt_GENERIC_SVREF: /* generic sv */
c9728782 1185 a0 = ap[0]; a1 = ap[1];
1604cfb0 1186 restore_svp:
03dba561 1187 {
75ebea3c 1188 /* do *a0.any_svp = a1 */
1604cfb0
MS
1189 SV * const sv = *a0.any_svp;
1190 *a0.any_svp = a1.any_sv;
1191 SvREFCNT_dec(sv);
1192 SvREFCNT_dec(a1.any_sv);
1193 break;
03dba561 1194 }
c9728782 1195
1604cfb0 1196 case SAVEt_GVSLOT: /* any slot in GV */
03dba561 1197 {
9a2fefd6 1198 HV * hv;
c9728782
DM
1199 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1200 hv = GvSTASH(a0.any_gv);
1604cfb0
MS
1201 if (hv && HvENAME(hv) && (
1202 (a2.any_sv && SvTYPE(a2.any_sv) == SVt_PVCV)
1203 || (*a1.any_svp && SvTYPE(*a1.any_svp) == SVt_PVCV)
1204 ))
1205 {
1206 if ((char *)a1.any_svp < (char *)GvGP(a0.any_gv)
1207 || (char *)a1.any_svp > (char *)GvGP(a0.any_gv) + sizeof(struct gp)
1208 || GvREFCNT(a0.any_gv) > 2) /* "> 2" to ignore savestack's ref */
1209 PL_sub_generation++;
1210 else mro_method_changed_in(hv);
1211 }
75ebea3c
DM
1212 a0.any_svp = a1.any_svp;
1213 a1.any_sv = a2.any_sv;
1604cfb0 1214 goto restore_svp;
03dba561 1215 }
c9728782 1216
1604cfb0 1217 case SAVEt_AV: /* array reference */
c9728782 1218 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1219 SvREFCNT_dec(GvAV(a0.any_gv));
1220 GvAV(a0.any_gv) = a1.any_av;
2baebb98 1221 avhv_common:
c9728782
DM
1222 if (UNLIKELY(SvSMAGICAL(a1.any_sv))) {
1223 /* mg_set might die, so make sure a0 isn't leaked */
2baebb98 1224 dSS_ADD;
c9728782 1225 SS_ADD_PTR(a0.any_sv);
2baebb98
DM
1226 SS_ADD_UV(SAVEt_FREESV);
1227 SS_ADD_END(2);
b492a59e 1228 PL_localizing = 2;
c9728782 1229 mg_set(a1.any_sv);
b492a59e 1230 PL_localizing = 0;
2baebb98 1231 break;
b492a59e 1232 }
1604cfb0
MS
1233 SvREFCNT_dec_NN(a0.any_sv);
1234 break;
c9728782 1235
1604cfb0 1236 case SAVEt_HV: /* hash reference */
c9728782 1237 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1238 SvREFCNT_dec(GvHV(a0.any_gv));
1239 GvHV(a0.any_gv) = a1.any_hv;
2baebb98
DM
1240 goto avhv_common;
1241
1604cfb0 1242 case SAVEt_INT_SMALL:
c9728782 1243 a0 = ap[0];
1604cfb0
MS
1244 *(int*)a0.any_ptr = (int)(uv >> SAVE_TIGHT_SHIFT);
1245 break;
c9728782 1246
1604cfb0 1247 case SAVEt_INT: /* int reference */
c9728782 1248 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1249 *(int*)a1.any_ptr = (int)a0.any_i32;
1250 break;
c9728782 1251
8462890b 1252 case SAVEt_STRLEN_SMALL:
1604cfb0
MS
1253 a0 = ap[0];
1254 *(STRLEN*)a0.any_ptr = (STRLEN)(uv >> SAVE_TIGHT_SHIFT);
8462890b
PE
1255 break;
1256
1604cfb0 1257 case SAVEt_STRLEN: /* STRLEN/size_t ref */
c9728782 1258 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1259 *(STRLEN*)a1.any_ptr = (STRLEN)a0.any_iv;
1260 break;
c9728782 1261
1604cfb0 1262 case SAVEt_TMPSFLOOR: /* restore PL_tmps_floor */
c9728782 1263 a0 = ap[0];
1604cfb0
MS
1264 PL_tmps_floor = (SSize_t)a0.any_iv;
1265 break;
c9728782 1266
1604cfb0 1267 case SAVEt_BOOL: /* bool reference */
c9728782 1268 a0 = ap[0];
1604cfb0 1269 *(bool*)a0.any_ptr = cBOOL(uv >> 8);
9a9b5ec9
DM
1270#ifdef NO_TAINT_SUPPORT
1271 PERL_UNUSED_VAR(was);
1272#else
1604cfb0
MS
1273 if (UNLIKELY(a0.any_ptr == &(PL_tainted))) {
1274 /* If we don't update <was>, to reflect what was saved on the
1275 * stack for PL_tainted, then we will overwrite this attempt to
1276 * restore it when we exit this routine. Note that this won't
1277 * work if this value was saved in a wider-than necessary type,
1278 * such as I32 */
1279 was = *(bool*)a0.any_ptr;
1280 }
284167a5 1281#endif
1604cfb0 1282 break;
c9728782 1283
1604cfb0 1284 case SAVEt_I32_SMALL:
c9728782 1285 a0 = ap[0];
1604cfb0
MS
1286 *(I32*)a0.any_ptr = (I32)(uv >> SAVE_TIGHT_SHIFT);
1287 break;
c9728782 1288
1604cfb0 1289 case SAVEt_I32: /* I32 reference */
c9728782 1290 a0 = ap[0]; a1 = ap[1];
3235b7a3 1291#ifdef PERL_DEBUG_READONLY_OPS
c9728782 1292 if (*(I32*)a1.any_ptr != a0.any_i32)
3235b7a3 1293#endif
c9728782 1294 *(I32*)a1.any_ptr = a0.any_i32;
1604cfb0 1295 break;
c9728782 1296
1604cfb0
MS
1297 case SAVEt_SPTR: /* SV* reference */
1298 case SAVEt_VPTR: /* random* reference */
1299 case SAVEt_PPTR: /* char* reference */
1300 case SAVEt_HPTR: /* HV* reference */
1301 case SAVEt_APTR: /* AV* reference */
c9728782 1302 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1303 *a1.any_svp= a0.any_sv;
1304 break;
c9728782 1305
1604cfb0 1306 case SAVEt_GP: /* scalar reference */
03dba561
DM
1307 {
1308 HV *hv;
1604cfb0 1309 bool had_method;
9a2fefd6 1310
c9728782 1311 a0 = ap[0]; a1 = ap[1];
03dba561 1312 /* possibly taking a method out of circulation */
82943faa 1313 had_method = cBOOL(GvCVu(a0.any_gv));
1604cfb0
MS
1314 gp_free(a0.any_gv);
1315 GvGP_set(a0.any_gv, (GP*)a1.any_ptr);
1316 if ((hv=GvSTASH(a0.any_gv)) && HvENAME_get(hv)) {
1317 if (memEQs(GvNAME(a0.any_gv), GvNAMELEN(a0.any_gv), "ISA"))
1318 mro_isa_changed_in(hv);
c9728782 1319 else if (had_method || GvCVu(a0.any_gv))
03dba561 1320 /* putting a method back into circulation ("local")*/
c9728782 1321 gv_method_changed(a0.any_gv);
1604cfb0
MS
1322 }
1323 SvREFCNT_dec_NN(a0.any_gv);
1324 break;
03dba561 1325 }
c9728782 1326
1604cfb0 1327 case SAVEt_FREESV:
c9728782 1328 a0 = ap[0];
1604cfb0
MS
1329 SvREFCNT_dec(a0.any_sv);
1330 break;
c9728782 1331
1604cfb0 1332 case SAVEt_FREEPADNAME:
c9728782 1333 a0 = ap[0];
1604cfb0
MS
1334 PadnameREFCNT_dec((PADNAME *)a0.any_ptr);
1335 break;
c9728782 1336
1604cfb0 1337 case SAVEt_FREECOPHH:
c9728782 1338 a0 = ap[0];
1604cfb0
MS
1339 cophh_free((COPHH *)a0.any_ptr);
1340 break;
c9728782 1341
1604cfb0 1342 case SAVEt_MORTALIZESV:
c9728782 1343 a0 = ap[0];
1604cfb0
MS
1344 sv_2mortal(a0.any_sv);
1345 break;
c9728782 1346
1604cfb0 1347 case SAVEt_FREEOP:
c9728782 1348 a0 = ap[0];
1604cfb0
MS
1349 ASSERT_CURPAD_LEGAL("SAVEt_FREEOP");
1350 op_free(a0.any_op);
1351 break;
c9728782 1352
1604cfb0 1353 case SAVEt_FREEPV:
c9728782 1354 a0 = ap[0];
1604cfb0
MS
1355 Safefree(a0.any_ptr);
1356 break;
4e09461c 1357
4e09461c 1358 case SAVEt_CLEARPADRANGE:
75ebea3c 1359 {
bdb0ae96 1360 I32 i;
1604cfb0 1361 SV **svp;
bdb0ae96
FC
1362 i = (I32)((uv >> SAVE_TIGHT_SHIFT) & OPpPADRANGE_COUNTMASK);
1363 svp = &PL_curpad[uv >>
4e09461c
DM
1364 (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT)] + i - 1;
1365 goto clearsv;
1604cfb0
MS
1366 case SAVEt_CLEARSV:
1367 svp = &PL_curpad[uv >> SAVE_TIGHT_SHIFT];
4e09461c
DM
1368 i = 1;
1369 clearsv:
1370 for (; i; i--, svp--) {
9a2fefd6 1371 SV *sv = *svp;
528ad060
DM
1372
1373 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
147e3846 1374 "Pad 0x%" UVxf "[0x%" UVxf "] clearsv: %ld sv=0x%" UVxf "<%" IVdf "> %s\n",
528ad060
DM
1375 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1376 (long)(svp-PL_curpad), PTR2UV(sv), (IV)SvREFCNT(sv),
1377 (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) ? "clear" : "abandon"
1378 ));
1379
1380 /* Can clear pad variable in place? */
9af15990 1381 if (SvREFCNT(sv) == 1 && !SvOBJECT(sv)) {
a07f0bef 1382
a07f0bef
DM
1383 /* these flags are the union of all the relevant flags
1384 * in the individual conditions within */
1385 if (UNLIKELY(SvFLAGS(sv) & (
a623f893 1386 SVf_READONLY|SVf_PROTECT /*for SvREADONLY_off*/
a07f0bef 1387 | (SVs_GMG|SVs_SMG|SVs_RMG) /* SvMAGICAL() */
7532eaae 1388 | SVf_OOK
a07f0bef
DM
1389 | SVf_THINKFIRST)))
1390 {
7500fc82
DM
1391 /* if a my variable that was made readonly is
1392 * going out of scope, we want to remove the
1393 * readonlyness so that it can go out of scope
1394 * quietly
1395 */
57c404c9 1396 if (SvREADONLY(sv))
7500fc82
DM
1397 SvREADONLY_off(sv);
1398
53083cad
PE
1399 if (SvTYPE(sv) == SVt_PVHV && HvHasAUX(sv))
1400 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
1401 else if(SvOOK(sv))
1402 sv_backoff(sv);
7532eaae 1403
7500fc82 1404 if (SvMAGICAL(sv)) {
7532eaae
DM
1405 /* note that backrefs (either in HvAUX or magic)
1406 * must be removed before other magic */
7500fc82
DM
1407 sv_unmagic(sv, PERL_MAGIC_backref);
1408 if (SvTYPE(sv) != SVt_PVCV)
1409 mg_free(sv);
1410 }
1411 if (SvTHINKFIRST(sv))
1412 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF
1413 |SV_COW_DROP_PV);
528ad060 1414
a07f0bef 1415 }
528ad060
DM
1416 switch (SvTYPE(sv)) {
1417 case SVt_NULL:
1418 break;
1419 case SVt_PVAV:
1420 av_clear(MUTABLE_AV(sv));
1421 break;
1422 case SVt_PVHV:
1423 hv_clear(MUTABLE_HV(sv));
1424 break;
1425 case SVt_PVCV:
1426 {
79656330 1427 HEK *hek = CvGvNAME_HEK(sv);
528ad060 1428 assert(hek);
6110d17a 1429 (void)share_hek_hek(hek);
528ad060
DM
1430 cv_undef((CV *)sv);
1431 CvNAME_HEK_set(sv, hek);
f3feca7a 1432 CvLEXICAL_on(sv);
528ad060
DM
1433 break;
1434 }
1435 default:
c79d0076
NC
1436 /* This looks odd, but these two macros are for use in
1437 expressions and finish with a trailing comma, so
1438 adding a ; after them would be wrong. */
1439 assert_not_ROK(sv)
1440 assert_not_glob(sv)
5c85b638 1441 SvFLAGS(sv) &=~ (SVf_OK|SVf_IVisUV|SVf_UTF8);
528ad060
DM
1442 break;
1443 }
82e85a9c 1444 SvPADTMP_off(sv);
528ad060
DM
1445 SvPADSTALE_on(sv); /* mark as no longer live */
1446 }
1447 else { /* Someone has a claim on this, so abandon it. */
528ad060
DM
1448 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
1449 case SVt_PVAV: *svp = MUTABLE_SV(newAV()); break;
1450 case SVt_PVHV: *svp = MUTABLE_SV(newHV()); break;
1451 case SVt_PVCV:
1452 {
79656330 1453 HEK * const hek = CvGvNAME_HEK(sv);
9c98a81f 1454
528ad060
DM
1455 /* Create a stub */
1456 *svp = newSV_type(SVt_PVCV);
1457
1458 /* Share name */
528ad060 1459 CvNAME_HEK_set(*svp,
9c98a81f 1460 share_hek_hek(hek));
f3feca7a 1461 CvLEXICAL_on(*svp);
528ad060
DM
1462 break;
1463 }
8fcb2425 1464 default: *svp = newSV_type(SVt_NULL); break;
528ad060 1465 }
4a9a56a7 1466 SvREFCNT_dec_NN(sv); /* Cast current value to the winds. */
528ad060
DM
1467 /* preserve pad nature, but also mark as not live
1468 * for any closure capturing */
145bf8ee 1469 SvFLAGS(*svp) |= SVs_PADSTALE;
528ad060 1470 }
4e09461c 1471 }
1604cfb0 1472 break;
75ebea3c 1473 }
c9728782 1474
1604cfb0 1475 case SAVEt_DELETE:
c9728782 1476 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
85df897f
DM
1477 /* hv_delete could die, so free the key and SvREFCNT_dec the
1478 * hv by pushing new save actions
1479 */
1480 /* ap[0] is the key */
1481 ap[1].any_uv = SAVEt_FREEPV; /* was len */
1482 /* ap[2] is the hv */
1483 ap[3].any_uv = SAVEt_FREESV; /* was SAVEt_DELETE */
1484 PL_savestack_ix += 4;
1604cfb0
MS
1485 (void)hv_delete(a2.any_hv, a0.any_pv, a1.any_i32, G_DISCARD);
1486 break;
c9728782 1487
1604cfb0 1488 case SAVEt_ADELETE:
c9728782 1489 a0 = ap[0]; a1 = ap[1];
85df897f
DM
1490 /* av_delete could die, so SvREFCNT_dec the av by pushing a
1491 * new save action
1492 */
1493 ap[0].any_av = a1.any_av;
1494 ap[1].any_uv = SAVEt_FREESV;
1495 PL_savestack_ix += 2;
1604cfb0
MS
1496 (void)av_delete(a1.any_av, a0.any_iv, G_DISCARD);
1497 break;
c9728782 1498
1604cfb0 1499 case SAVEt_DESTRUCTOR_X:
c9728782 1500 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1501 (*a0.any_dxptr)(aTHX_ a1.any_ptr);
1502 break;
c9728782 1503
1604cfb0
MS
1504 case SAVEt_REGCONTEXT:
1505 /* regexp must have croaked */
1506 case SAVEt_ALLOC:
1507 PL_savestack_ix -= uv >> SAVE_TIGHT_SHIFT;
1508 break;
c9728782 1509
1604cfb0 1510 case SAVEt_STACK_POS: /* Position on Perl stack */
c9728782 1511 a0 = ap[0];
1604cfb0
MS
1512 PL_stack_sp = PL_stack_base + a0.any_i32;
1513 break;
c9728782 1514
1604cfb0 1515 case SAVEt_AELEM: /* array element */
75ebea3c
DM
1516 {
1517 SV **svp;
c9728782 1518 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1604cfb0
MS
1519 svp = av_fetch(a0.any_av, a1.any_iv, 1);
1520 if (UNLIKELY(!AvREAL(a0.any_av) && AvREIFY(a0.any_av))) /* undo reify guard */
1521 SvREFCNT_dec(a2.any_sv);
1522 if (LIKELY(svp)) {
1523 SV * const sv = *svp;
1524 if (LIKELY(sv && sv != &PL_sv_undef)) {
1525 if (UNLIKELY(SvTIED_mg((const SV *)a0.any_av, PERL_MAGIC_tied)))
1526 SvREFCNT_inc_void_NN(sv);
75ebea3c
DM
1527 a1.any_sv = a2.any_sv;
1528 a2.any_svp = svp;
1604cfb0
MS
1529 goto restore_sv;
1530 }
1531 }
1532 SvREFCNT_dec(a0.any_av);
1533 SvREFCNT_dec(a2.any_sv);
1534 break;
75ebea3c 1535 }
c9728782 1536
1604cfb0 1537 case SAVEt_HELEM: /* hash element */
03dba561 1538 {
1604cfb0 1539 HE *he;
9a2fefd6 1540
c9728782 1541 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1604cfb0
MS
1542 he = hv_fetch_ent(a0.any_hv, a1.any_sv, 1, 0);
1543 SvREFCNT_dec(a1.any_sv);
1544 if (LIKELY(he)) {
1545 const SV * const oval = HeVAL(he);
1546 if (LIKELY(oval && oval != &PL_sv_undef)) {
75ebea3c 1547 SV **svp = &HeVAL(he);
1604cfb0
MS
1548 if (UNLIKELY(SvTIED_mg((const SV *)a0.any_hv, PERL_MAGIC_tied)))
1549 SvREFCNT_inc_void(*svp);
75ebea3c
DM
1550 a1.any_sv = a2.any_sv;
1551 a2.any_svp = svp;
1604cfb0
MS
1552 goto restore_sv;
1553 }
1554 }
1555 SvREFCNT_dec(a0.any_hv);
1556 SvREFCNT_dec(a2.any_sv);
1557 break;
03dba561 1558 }
75ebea3c 1559
1604cfb0 1560 case SAVEt_OP:
c9728782 1561 a0 = ap[0];
1604cfb0
MS
1562 PL_op = (OP*)a0.any_ptr;
1563 break;
c9728782 1564
6c148c86
PE
1565 case SAVEt_HINTS_HH:
1566 a2 = ap[2];
1567 /* FALLTHROUGH */
1568 case SAVEt_HINTS:
c9728782 1569 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1570 if ((PL_hints & HINT_LOCALIZE_HH)) {
1571 while (GvHV(PL_hintgv)) {
1572 HV *hv = GvHV(PL_hintgv);
1573 GvHV(PL_hintgv) = NULL;
1574 SvREFCNT_dec(MUTABLE_SV(hv));
1575 }
1576 }
1577 cophh_free(CopHINTHASH_get(&PL_compiling));
1578 CopHINTHASH_set(&PL_compiling, (COPHH*)a1.any_ptr);
1579 *(I32*)&PL_hints = a0.any_i32;
78efaf03 1580 PL_prevailing_version = (U16)(uv >> 8);
1604cfb0
MS
1581 if (type == SAVEt_HINTS_HH) {
1582 SvREFCNT_dec(MUTABLE_SV(GvHV(PL_hintgv)));
6c148c86 1583 GvHV(PL_hintgv) = MUTABLE_HV(a2.any_ptr);
1604cfb0
MS
1584 }
1585 if (!GvHV(PL_hintgv)) {
1586 /* Need to add a new one manually, else rv2hv can
1587 add one via GvHVn and it won't have the magic set. */
1588 HV *const hv = newHV();
1589 hv_magic(hv, NULL, PERL_MAGIC_hints);
1590 GvHV(PL_hintgv) = hv;
1591 }
1592 assert(GvHV(PL_hintgv));
1593 break;
1594
1595 case SAVEt_COMPPAD:
c9728782 1596 a0 = ap[0];
1604cfb0
MS
1597 PL_comppad = (PAD*)a0.any_ptr;
1598 if (LIKELY(PL_comppad))
1599 PL_curpad = AvARRAY(PL_comppad);
1600 else
1601 PL_curpad = NULL;
1602 break;
c9728782 1603
1604cfb0
MS
1604 case SAVEt_PADSV_AND_MORTALIZE:
1605 {
1606 SV **svp;
9a2fefd6 1607
c9728782 1608 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1604cfb0
MS
1609 assert (a1.any_ptr);
1610 svp = AvARRAY((PAD*)a1.any_ptr) + (PADOFFSET)a2.any_uv;
b405d38b
DM
1611 /* This mortalizing used to be done by CX_POOPLOOP() via
1612 itersave. But as we have all the information here, we
1613 can do it here, save even having to have itersave in
1614 the struct.
1615 */
1604cfb0
MS
1616 sv_2mortal(*svp);
1617 *svp = a0.any_sv;
1618 }
1619 break;
c9728782 1620
1604cfb0
MS
1621 case SAVEt_SAVESWITCHSTACK:
1622 {
1623 dSP;
9a2fefd6 1624
c9728782 1625 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1626 SWITCHSTACK(a1.any_av, a0.any_av);
1627 PL_curstackinfo->si_stack = a0.any_av;
1628 }
1629 break;
c9728782 1630
1604cfb0 1631 case SAVEt_SET_SVFLAGS:
c9728782
DM
1632 a0 = ap[0]; a1 = ap[1]; a2 = ap[2];
1633 SvFLAGS(a0.any_sv) &= ~(a1.any_u32);
1634 SvFLAGS(a0.any_sv) |= a2.any_u32;
1604cfb0 1635 break;
95e06916 1636
1604cfb0
MS
1637 /* These are only saved in mathoms.c */
1638 case SAVEt_NSTAB:
c9728782 1639 a0 = ap[0];
1604cfb0
MS
1640 (void)sv_clear(a0.any_sv);
1641 break;
c9728782 1642
1604cfb0 1643 case SAVEt_LONG: /* long reference */
c9728782 1644 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1645 *(long*)a1.any_ptr = a0.any_long;
1646 break;
c9728782 1647
1604cfb0 1648 case SAVEt_IV: /* IV reference */
c9728782 1649 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1650 *(IV*)a1.any_ptr = a0.any_iv;
1651 break;
95e06916 1652
1604cfb0 1653 case SAVEt_I16: /* I16 reference */
c9728782 1654 a0 = ap[0];
1604cfb0
MS
1655 *(I16*)a0.any_ptr = (I16)(uv >> 8);
1656 break;
c9728782 1657
1604cfb0 1658 case SAVEt_I8: /* I8 reference */
c9728782 1659 a0 = ap[0];
1604cfb0
MS
1660 *(I8*)a0.any_ptr = (I8)(uv >> 8);
1661 break;
c9728782 1662
1604cfb0 1663 case SAVEt_DESTRUCTOR:
c9728782 1664 a0 = ap[0]; a1 = ap[1];
1604cfb0
MS
1665 (*a0.any_dptr)(a1.any_ptr);
1666 break;
c9728782 1667
1604cfb0 1668 case SAVEt_COMPILE_WARNINGS:
c9728782 1669 a0 = ap[0];
1943af61 1670 free_and_set_cop_warnings(&PL_compiling, (STRLEN*) a0.any_ptr);
1604cfb0 1671 break;
c9728782 1672
1604cfb0 1673 case SAVEt_PARSER:
c9728782 1674 a0 = ap[0];
1604cfb0
MS
1675 parser_free((yy_parser *)a0.any_ptr);
1676 break;
c9728782 1677
1604cfb0 1678 case SAVEt_READONLY_OFF:
c9728782 1679 a0 = ap[0];
1604cfb0
MS
1680 SvREADONLY_off(a0.any_sv);
1681 break;
c9728782 1682
1604cfb0
MS
1683 default:
1684 Perl_croak(aTHX_ "panic: leave_scope inconsistency %u",
75ebea3c 1685 (U8)uv & SAVE_MASK);
1604cfb0 1686 }
79072805 1687 }
302c0c93 1688
284167a5 1689 TAINT_set(was);
79072805 1690}
8990e307 1691
8990e307 1692void
864dbfa3 1693Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 1694{
7918f24d
NC
1695 PERL_ARGS_ASSERT_CX_DUMP;
1696
35ff7856 1697#ifdef DEBUGGING
22c35a8c 1698 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 1699 if (CxTYPE(cx) != CXt_SUBST) {
1604cfb0
MS
1700 const char *gimme_text;
1701 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
1702 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%" UVxf "\n",
1703 PTR2UV(cx->blk_oldcop));
1704 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
1705 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
1706 PerlIO_printf(Perl_debug_log, "BLK_OLDSAVEIX = %ld\n", (long)cx->blk_oldsaveix);
1707 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%" UVxf "\n",
1708 PTR2UV(cx->blk_oldpm));
1709 switch (cx->blk_gimme) {
1710 case G_VOID:
1711 gimme_text = "VOID";
1712 break;
1713 case G_SCALAR:
1714 gimme_text = "SCALAR";
1715 break;
eb7e169e 1716 case G_LIST:
1604cfb0
MS
1717 gimme_text = "LIST";
1718 break;
1719 default:
1720 gimme_text = "UNKNOWN";
1721 break;
1722 }
1723 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", gimme_text);
8990e307 1724 }
6b35e009 1725 switch (CxTYPE(cx)) {
8990e307
LW
1726 case CXt_NULL:
1727 case CXt_BLOCK:
f79e2ff9 1728 case CXt_DEFER:
1604cfb0 1729 break;
146174a9 1730 case CXt_FORMAT:
1604cfb0
MS
1731 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.CV = 0x%" UVxf "\n",
1732 PTR2UV(cx->blk_format.cv));
1733 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.GV = 0x%" UVxf "\n",
1734 PTR2UV(cx->blk_format.gv));
1735 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.DFOUTGV = 0x%" UVxf "\n",
1736 PTR2UV(cx->blk_format.dfoutgv));
1737 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.HASARGS = %d\n",
1738 (int)CxHASARGS(cx));
1739 PerlIO_printf(Perl_debug_log, "BLK_FORMAT.RETOP = 0x%" UVxf "\n",
1740 PTR2UV(cx->blk_format.retop));
1741 break;
8990e307 1742 case CXt_SUB:
1604cfb0
MS
1743 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%" UVxf "\n",
1744 PTR2UV(cx->blk_sub.cv));
1745 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
1746 (long)cx->blk_sub.olddepth);
1747 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
1748 (int)CxHASARGS(cx));
1749 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n", (int)CxLVAL(cx));
1750 PerlIO_printf(Perl_debug_log, "BLK_SUB.RETOP = 0x%" UVxf "\n",
1751 PTR2UV(cx->blk_sub.retop));
1752 break;
8990e307 1753 case CXt_EVAL:
1604cfb0
MS
1754 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
1755 (long)CxOLD_IN_EVAL(cx));
1756 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
1757 PL_op_name[CxOLD_OP_TYPE(cx)],
1758 PL_op_desc[CxOLD_OP_TYPE(cx)]);
1759 if (cx->blk_eval.old_namesv)
1760 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
1761 SvPVX_const(cx->blk_eval.old_namesv));
1762 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%" UVxf "\n",
1763 PTR2UV(cx->blk_eval.old_eval_root));
1764 PerlIO_printf(Perl_debug_log, "BLK_EVAL.RETOP = 0x%" UVxf "\n",
1765 PTR2UV(cx->blk_eval.retop));
1766 break;
8990e307 1767
93661e56 1768 case CXt_LOOP_PLAIN:
c6fdafd0 1769 case CXt_LOOP_LAZYIV:
d01136d6 1770 case CXt_LOOP_LAZYSV:
93661e56
DM
1771 case CXt_LOOP_LIST:
1772 case CXt_LOOP_ARY:
1604cfb0
MS
1773 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n", CxLABEL(cx));
1774 PerlIO_printf(Perl_debug_log, "BLK_LOOP.MY_OP = 0x%" UVxf "\n",
1775 PTR2UV(cx->blk_loop.my_op));
0925dc7f 1776 if (CxTYPE(cx) != CXt_LOOP_PLAIN) {
147e3846 1777 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%" UVxf "\n",
0925dc7f 1778 PTR2UV(CxITERVAR(cx)));
147e3846 1779 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%" UVxf "\n",
0925dc7f 1780 PTR2UV(cx->blk_loop.itersave));
1604cfb0
MS
1781 }
1782 if (CxTYPE(cx) == CXt_LOOP_ARY) {
147e3846 1783 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%" UVxf "\n",
0925dc7f
DM
1784 PTR2UV(cx->blk_loop.state_u.ary.ary));
1785 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
1786 (long)cx->blk_loop.state_u.ary.ix);
1787 }
1604cfb0 1788 break;
8990e307
LW
1789
1790 case CXt_SUBST:
1604cfb0
MS
1791 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
1792 (long)cx->sb_iters);
1793 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
1794 (long)cx->sb_maxiters);
1795 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1796 (long)cx->sb_rflags);
1797 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
1798 (long)CxONCE(cx));
1799 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
1800 cx->sb_orig);
1801 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%" UVxf "\n",
1802 PTR2UV(cx->sb_dstr));
1803 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%" UVxf "\n",
1804 PTR2UV(cx->sb_targ));
1805 PerlIO_printf(Perl_debug_log, "SB_S = 0x%" UVxf "\n",
1806 PTR2UV(cx->sb_s));
1807 PerlIO_printf(Perl_debug_log, "SB_M = 0x%" UVxf "\n",
1808 PTR2UV(cx->sb_m));
1809 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%" UVxf "\n",
1810 PTR2UV(cx->sb_strend));
1811 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%" UVxf "\n",
1812 PTR2UV(cx->sb_rxres));
1813 break;
8990e307 1814 }
65e66c80 1815#else
96a5add6 1816 PERL_UNUSED_CONTEXT;
65e66c80 1817 PERL_UNUSED_ARG(cx);
17c3b450 1818#endif /* DEBUGGING */
35ff7856 1819}
241d1a3b
NC
1820
1821/*
14d04a33 1822 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1823 */