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