This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
shrink pp_hot fractionally
[perl5.git] / scope.c
CommitLineData
a0d0e21e 1/* scope.c
79072805 2 *
cb50131a 3 * Copyright (c) 1991-2000, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "For the fashion of Minas Tirith was such that it was built on seven
12 * levels..."
79072805
LW
13 */
14
15#include "EXTERN.h"
864dbfa3 16#define PERL_IN_SCOPE_C
79072805
LW
17#include "perl.h"
18
14dd3ad8 19#if defined(PERL_FLEXIBLE_EXCEPTIONS)
312caa8e 20void *
146174a9
CB
21Perl_default_protect(pTHX_ volatile JMPENV *pcur_env, int *excpt,
22 protect_body_t body, ...)
312caa8e 23{
c5be433b
GS
24 void *ret;
25 va_list args;
26 va_start(args, body);
146174a9 27 ret = vdefault_protect(pcur_env, excpt, body, &args);
c5be433b
GS
28 va_end(args);
29 return ret;
30}
31
32void *
146174a9
CB
33Perl_vdefault_protect(pTHX_ volatile JMPENV *pcur_env, int *excpt,
34 protect_body_t body, va_list *args)
c5be433b 35{
312caa8e
CS
36 int ex;
37 void *ret;
38
312caa8e
CS
39 JMPENV_PUSH(ex);
40 if (ex)
41 ret = NULL;
c5be433b
GS
42 else
43 ret = CALL_FPTR(body)(aTHX_ *args);
a6c40364 44 *excpt = ex;
312caa8e
CS
45 JMPENV_POP;
46 return ret;
47}
14dd3ad8 48#endif
312caa8e 49
a0d0e21e 50SV**
864dbfa3 51Perl_stack_grow(pTHX_ SV **sp, SV **p, int n)
a0d0e21e 52{
a60c0954
NIS
53#if defined(DEBUGGING) && !defined(USE_THREADS)
54 static int growing = 0;
55 if (growing++)
56 abort();
57#endif
3280af22 58 PL_stack_sp = sp;
2ce36478 59#ifndef STRESS_REALLOC
3280af22 60 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
2ce36478 61#else
6b88bc9c 62 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
2ce36478 63#endif
a60c0954
NIS
64#if defined(DEBUGGING) && !defined(USE_THREADS)
65 growing--;
66#endif
3280af22 67 return PL_stack_sp;
a0d0e21e
LW
68}
69
2ce36478
SM
70#ifndef STRESS_REALLOC
71#define GROW(old) ((old) * 3 / 2)
72#else
73#define GROW(old) ((old) + 1)
74#endif
75
e336de0d 76PERL_SI *
864dbfa3 77Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
e336de0d
GS
78{
79 PERL_SI *si;
e336de0d
GS
80 New(56, si, 1, PERL_SI);
81 si->si_stack = newAV();
82 AvREAL_off(si->si_stack);
83 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
3280af22 84 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
e336de0d
GS
85 AvFILLp(si->si_stack) = 0;
86 si->si_prev = 0;
87 si->si_next = 0;
88 si->si_cxmax = cxitems - 1;
89 si->si_cxix = -1;
e788e7d3 90 si->si_type = PERLSI_UNDEF;
e336de0d
GS
91 New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
92 return si;
93}
94
79072805 95I32
864dbfa3 96Perl_cxinc(pTHX)
79072805 97{
2ce36478 98 cxstack_max = GROW(cxstack_max);
c09156bb 99 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
79072805
LW
100 return cxstack_ix + 1;
101}
102
103void
864dbfa3 104Perl_push_return(pTHX_ OP *retop)
79072805 105{
3280af22
NIS
106 if (PL_retstack_ix == PL_retstack_max) {
107 PL_retstack_max = GROW(PL_retstack_max);
108 Renew(PL_retstack, PL_retstack_max, OP*);
79072805 109 }
3280af22 110 PL_retstack[PL_retstack_ix++] = retop;
79072805
LW
111}
112
113OP *
864dbfa3 114Perl_pop_return(pTHX)
79072805 115{
3280af22
NIS
116 if (PL_retstack_ix > 0)
117 return PL_retstack[--PL_retstack_ix];
79072805
LW
118 else
119 return Nullop;
120}
121
122void
864dbfa3 123Perl_push_scope(pTHX)
79072805 124{
3280af22
NIS
125 if (PL_scopestack_ix == PL_scopestack_max) {
126 PL_scopestack_max = GROW(PL_scopestack_max);
127 Renew(PL_scopestack, PL_scopestack_max, I32);
79072805 128 }
3280af22 129 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
79072805
LW
130
131}
132
133void
864dbfa3 134Perl_pop_scope(pTHX)
79072805 135{
3280af22 136 I32 oldsave = PL_scopestack[--PL_scopestack_ix];
8990e307 137 LEAVE_SCOPE(oldsave);
79072805
LW
138}
139
140void
864dbfa3 141Perl_markstack_grow(pTHX)
a0d0e21e 142{
3280af22 143 I32 oldmax = PL_markstack_max - PL_markstack;
2ce36478 144 I32 newmax = GROW(oldmax);
a0d0e21e 145
3280af22
NIS
146 Renew(PL_markstack, newmax, I32);
147 PL_markstack_ptr = PL_markstack + oldmax;
148 PL_markstack_max = PL_markstack + newmax;
a0d0e21e
LW
149}
150
151void
864dbfa3 152Perl_savestack_grow(pTHX)
79072805 153{
3280af22
NIS
154 PL_savestack_max = GROW(PL_savestack_max) + 4;
155 Renew(PL_savestack, PL_savestack_max, ANY);
79072805
LW
156}
157
2ce36478
SM
158#undef GROW
159
79072805 160void
864dbfa3 161Perl_tmps_grow(pTHX_ I32 n)
677b06e3 162{
677b06e3
GS
163#ifndef STRESS_REALLOC
164 if (n < 128)
165 n = (PL_tmps_max < 512) ? 128 : 512;
166#endif
167 PL_tmps_max = PL_tmps_ix + n + 1;
168 Renew(PL_tmps_stack, PL_tmps_max, SV*);
169}
170
171
172void
864dbfa3 173Perl_free_tmps(pTHX)
79072805
LW
174{
175 /* XXX should tmps_floor live in cxstack? */
3280af22
NIS
176 I32 myfloor = PL_tmps_floor;
177 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
178 SV* sv = PL_tmps_stack[PL_tmps_ix];
179 PL_tmps_stack[PL_tmps_ix--] = Nullsv;
463ee0b2 180 if (sv) {
463ee0b2 181 SvTEMP_off(sv);
8990e307 182 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
463ee0b2 183 }
79072805
LW
184 }
185}
186
76e3520e 187STATIC SV *
cea2e8a9 188S_save_scalar_at(pTHX_ SV **sptr)
79072805
LW
189{
190 register SV *sv;
7a4c00b4 191 SV *osv = *sptr;
79072805 192
7a4c00b4 193 sv = *sptr = NEWSV(0,0);
a0d0e21e 194 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
79072805 195 sv_upgrade(sv, SvTYPE(osv));
a0d0e21e 196 if (SvGMAGICAL(osv)) {
748a9306 197 MAGIC* mg;
3280af22 198 bool oldtainted = PL_tainted;
a0d0e21e 199 mg_get(osv);
3280af22 200 if (PL_tainting && PL_tainted && (mg = mg_find(osv, 't'))) {
748a9306
LW
201 SAVESPTR(mg->mg_obj);
202 mg->mg_obj = osv;
203 }
a0d0e21e
LW
204 SvFLAGS(osv) |= (SvFLAGS(osv) &
205 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
3280af22 206 PL_tainted = oldtainted;
a0d0e21e 207 }
79072805 208 SvMAGIC(sv) = SvMAGIC(osv);
a0d0e21e 209 SvFLAGS(sv) |= SvMAGICAL(osv);
3280af22 210 PL_localizing = 1;
79072805 211 SvSETMAGIC(sv);
3280af22 212 PL_localizing = 0;
79072805
LW
213 }
214 return sv;
215}
216
7a4c00b4 217SV *
864dbfa3 218Perl_save_scalar(pTHX_ GV *gv)
7a4c00b4 219{
4e4c362e 220 SV **sptr = &GvSV(gv);
7a4c00b4 221 SSCHECK(3);
4e4c362e
GS
222 SSPUSHPTR(SvREFCNT_inc(gv));
223 SSPUSHPTR(SvREFCNT_inc(*sptr));
7a4c00b4 224 SSPUSHINT(SAVEt_SV);
4e4c362e 225 return save_scalar_at(sptr);
7a4c00b4
PP
226}
227
228SV*
864dbfa3 229Perl_save_svref(pTHX_ SV **sptr)
7a4c00b4
PP
230{
231 SSCHECK(3);
232 SSPUSHPTR(sptr);
4e4c362e 233 SSPUSHPTR(SvREFCNT_inc(*sptr));
7a4c00b4
PP
234 SSPUSHINT(SAVEt_SVREF);
235 return save_scalar_at(sptr);
236}
237
f4dd75d9 238/* Like save_sptr(), but also SvREFCNT_dec()s the new value. Can be used to
b9d12d37
GS
239 * restore a global SV to its prior contents, freeing new value. */
240void
864dbfa3 241Perl_save_generic_svref(pTHX_ SV **sptr)
b9d12d37 242{
b9d12d37
GS
243 SSCHECK(3);
244 SSPUSHPTR(sptr);
245 SSPUSHPTR(SvREFCNT_inc(*sptr));
246 SSPUSHINT(SAVEt_GENERIC_SVREF);
247}
248
f4dd75d9
GS
249/* Like save_pptr(), but also Safefree()s the new value if it is different
250 * from the old one. Can be used to restore a global char* to its prior
251 * contents, freeing new value. */
252void
253Perl_save_generic_pvref(pTHX_ char **str)
254{
f4dd75d9
GS
255 SSCHECK(3);
256 SSPUSHPTR(str);
257 SSPUSHPTR(*str);
258 SSPUSHINT(SAVEt_GENERIC_PVREF);
259}
260
79072805 261void
864dbfa3 262Perl_save_gp(pTHX_ GV *gv, I32 empty)
79072805 263{
fb73857a
PP
264 SSCHECK(6);
265 SSPUSHIV((IV)SvLEN(gv));
266 SvLEN(gv) = 0; /* forget that anything was allocated here */
267 SSPUSHIV((IV)SvCUR(gv));
268 SSPUSHPTR(SvPVX(gv));
269 SvPOK_off(gv);
4633a7c4 270 SSPUSHPTR(SvREFCNT_inc(gv));
5f05dabc 271 SSPUSHPTR(GvGP(gv));
79072805
LW
272 SSPUSHINT(SAVEt_GP);
273
5f05dabc
PP
274 if (empty) {
275 register GP *gp;
fae75791 276
146174a9
CB
277 Newz(602, gp, 1, GP);
278
fae75791 279 if (GvCVu(gv))
3280af22 280 PL_sub_generation++; /* taking a method out of circulation */
146174a9
CB
281 if (GvIOp(gv) && (IoFLAGS(GvIOp(gv)) & IOf_ARGV)) {
282 gp->gp_io = newIO();
283 IoFLAGS(gp->gp_io) |= IOf_ARGV|IOf_START;
284 }
44a8e56a 285 GvGP(gv) = gp_ref(gp);
5f05dabc 286 GvSV(gv) = NEWSV(72,0);
146174a9 287 GvLINE(gv) = CopLINE(PL_curcop);
94051fc1 288 GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : "";
5f05dabc
PP
289 GvEGV(gv) = gv;
290 }
291 else {
44a8e56a 292 gp_ref(GvGP(gv));
5f05dabc
PP
293 GvINTRO_on(gv);
294 }
79072805 295}
79072805 296
79072805 297AV *
864dbfa3 298Perl_save_ary(pTHX_ GV *gv)
79072805 299{
67a38de0
NIS
300 AV *oav = GvAVn(gv);
301 AV *av;
fb73857a 302
67a38de0
NIS
303 if (!AvREAL(oav) && AvREIFY(oav))
304 av_reify(oav);
79072805
LW
305 SSCHECK(3);
306 SSPUSHPTR(gv);
67a38de0 307 SSPUSHPTR(oav);
79072805
LW
308 SSPUSHINT(SAVEt_AV);
309
310 GvAV(gv) = Null(AV*);
fb73857a
PP
311 av = GvAVn(gv);
312 if (SvMAGIC(oav)) {
313 SvMAGIC(av) = SvMAGIC(oav);
32da55ab 314 SvFLAGS((SV*)av) |= SvMAGICAL(oav);
fb73857a
PP
315 SvMAGICAL_off(oav);
316 SvMAGIC(oav) = 0;
3280af22 317 PL_localizing = 1;
fb73857a 318 SvSETMAGIC((SV*)av);
3280af22 319 PL_localizing = 0;
fb73857a
PP
320 }
321 return av;
79072805
LW
322}
323
324HV *
864dbfa3 325Perl_save_hash(pTHX_ GV *gv)
79072805 326{
fb73857a
PP
327 HV *ohv, *hv;
328
79072805
LW
329 SSCHECK(3);
330 SSPUSHPTR(gv);
fb73857a 331 SSPUSHPTR(ohv = GvHVn(gv));
79072805
LW
332 SSPUSHINT(SAVEt_HV);
333
334 GvHV(gv) = Null(HV*);
fb73857a
PP
335 hv = GvHVn(gv);
336 if (SvMAGIC(ohv)) {
337 SvMAGIC(hv) = SvMAGIC(ohv);
32da55ab 338 SvFLAGS((SV*)hv) |= SvMAGICAL(ohv);
fb73857a
PP
339 SvMAGICAL_off(ohv);
340 SvMAGIC(ohv) = 0;
3280af22 341 PL_localizing = 1;
fb73857a 342 SvSETMAGIC((SV*)hv);
3280af22 343 PL_localizing = 0;
fb73857a
PP
344 }
345 return hv;
79072805
LW
346}
347
348void
864dbfa3 349Perl_save_item(pTHX_ register SV *item)
79072805 350{
f46d017c 351 register SV *sv = NEWSV(0,0);
79072805 352
f46d017c 353 sv_setsv(sv,item);
79072805
LW
354 SSCHECK(3);
355 SSPUSHPTR(item); /* remember the pointer */
79072805
LW
356 SSPUSHPTR(sv); /* remember the value */
357 SSPUSHINT(SAVEt_ITEM);
358}
359
360void
864dbfa3 361Perl_save_int(pTHX_ int *intp)
79072805
LW
362{
363 SSCHECK(3);
364 SSPUSHINT(*intp);
365 SSPUSHPTR(intp);
366 SSPUSHINT(SAVEt_INT);
367}
368
369void
864dbfa3 370Perl_save_long(pTHX_ long int *longp)
85e6fe83
LW
371{
372 SSCHECK(3);
373 SSPUSHLONG(*longp);
374 SSPUSHPTR(longp);
375 SSPUSHINT(SAVEt_LONG);
376}
377
378void
864dbfa3 379Perl_save_I32(pTHX_ I32 *intp)
79072805
LW
380{
381 SSCHECK(3);
382 SSPUSHINT(*intp);
383 SSPUSHPTR(intp);
384 SSPUSHINT(SAVEt_I32);
385}
386
a0d0e21e 387void
864dbfa3 388Perl_save_I16(pTHX_ I16 *intp)
55497cff
PP
389{
390 SSCHECK(3);
391 SSPUSHINT(*intp);
392 SSPUSHPTR(intp);
393 SSPUSHINT(SAVEt_I16);
394}
395
396void
146174a9
CB
397Perl_save_I8(pTHX_ I8 *bytep)
398{
146174a9
CB
399 SSCHECK(3);
400 SSPUSHINT(*bytep);
401 SSPUSHPTR(bytep);
402 SSPUSHINT(SAVEt_I8);
403}
404
405void
864dbfa3 406Perl_save_iv(pTHX_ IV *ivp)
a0d0e21e
LW
407{
408 SSCHECK(3);
4aa0a1f7 409 SSPUSHIV(*ivp);
a0d0e21e
LW
410 SSPUSHPTR(ivp);
411 SSPUSHINT(SAVEt_IV);
412}
413
85e6fe83
LW
414/* Cannot use save_sptr() to store a char* since the SV** cast will
415 * force word-alignment and we'll miss the pointer.
416 */
417void
864dbfa3 418Perl_save_pptr(pTHX_ char **pptr)
85e6fe83
LW
419{
420 SSCHECK(3);
421 SSPUSHPTR(*pptr);
422 SSPUSHPTR(pptr);
423 SSPUSHINT(SAVEt_PPTR);
424}
425
79072805 426void
146174a9
CB
427Perl_save_vptr(pTHX_ void *ptr)
428{
146174a9
CB
429 SSCHECK(3);
430 SSPUSHPTR(*(char**)ptr);
431 SSPUSHPTR(ptr);
432 SSPUSHINT(SAVEt_VPTR);
433}
434
435void
864dbfa3 436Perl_save_sptr(pTHX_ SV **sptr)
79072805
LW
437{
438 SSCHECK(3);
439 SSPUSHPTR(*sptr);
440 SSPUSHPTR(sptr);
441 SSPUSHINT(SAVEt_SPTR);
442}
443
c3564e5c
GS
444void
445Perl_save_padsv(pTHX_ PADOFFSET off)
446{
c3564e5c
GS
447 SSCHECK(4);
448 SSPUSHPTR(PL_curpad[off]);
449 SSPUSHPTR(PL_curpad);
450 SSPUSHLONG((long)off);
451 SSPUSHINT(SAVEt_PADSV);
452}
453
54b9620d 454SV **
864dbfa3 455Perl_save_threadsv(pTHX_ PADOFFSET i)
54b9620d
MB
456{
457#ifdef USE_THREADS
940cb80d 458 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
146174a9
CB
459 DEBUG_S(PerlIO_printf(Perl_debug_log, "save_threadsv %"UVuf": %p %p:%s\n",
460 (UV)i, svp, *svp, SvPEEK(*svp)));
54b9620d
MB
461 save_svref(svp);
462 return svp;
463#else
cea2e8a9 464 Perl_croak(aTHX_ "panic: save_threadsv called in non-threaded perl");
54b9620d
MB
465 return 0;
466#endif /* USE_THREADS */
467}
468
79072805 469void
864dbfa3 470Perl_save_nogv(pTHX_ GV *gv)
79072805
LW
471{
472 SSCHECK(2);
473 SSPUSHPTR(gv);
474 SSPUSHINT(SAVEt_NSTAB);
475}
476
477void
864dbfa3 478Perl_save_hptr(pTHX_ HV **hptr)
79072805
LW
479{
480 SSCHECK(3);
85e6fe83 481 SSPUSHPTR(*hptr);
79072805
LW
482 SSPUSHPTR(hptr);
483 SSPUSHINT(SAVEt_HPTR);
484}
485
486void
864dbfa3 487Perl_save_aptr(pTHX_ AV **aptr)
79072805
LW
488{
489 SSCHECK(3);
85e6fe83 490 SSPUSHPTR(*aptr);
79072805
LW
491 SSPUSHPTR(aptr);
492 SSPUSHINT(SAVEt_APTR);
493}
494
495void
864dbfa3 496Perl_save_freesv(pTHX_ SV *sv)
8990e307
LW
497{
498 SSCHECK(2);
499 SSPUSHPTR(sv);
500 SSPUSHINT(SAVEt_FREESV);
501}
502
503void
864dbfa3 504Perl_save_freeop(pTHX_ OP *o)
8990e307
LW
505{
506 SSCHECK(2);
11343788 507 SSPUSHPTR(o);
8990e307
LW
508 SSPUSHINT(SAVEt_FREEOP);
509}
510
511void
864dbfa3 512Perl_save_freepv(pTHX_ char *pv)
8990e307
LW
513{
514 SSCHECK(2);
515 SSPUSHPTR(pv);
516 SSPUSHINT(SAVEt_FREEPV);
517}
518
519void
864dbfa3 520Perl_save_clearsv(pTHX_ SV **svp)
8990e307
LW
521{
522 SSCHECK(2);
3280af22 523 SSPUSHLONG((long)(svp-PL_curpad));
8990e307
LW
524 SSPUSHINT(SAVEt_CLEARSV);
525}
526
527void
864dbfa3 528Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
8990e307
LW
529{
530 SSCHECK(4);
531 SSPUSHINT(klen);
532 SSPUSHPTR(key);
4e4c362e 533 SSPUSHPTR(SvREFCNT_inc(hv));
8990e307
LW
534 SSPUSHINT(SAVEt_DELETE);
535}
536
537void
864dbfa3 538Perl_save_list(pTHX_ register SV **sarg, I32 maxsarg)
79072805
LW
539{
540 register SV *sv;
541 register I32 i;
542
79072805 543 for (i = 1; i <= maxsarg; i++) {
79072805
LW
544 sv = NEWSV(0,0);
545 sv_setsv(sv,sarg[i]);
f46d017c
GS
546 SSCHECK(3);
547 SSPUSHPTR(sarg[i]); /* remember the pointer */
79072805
LW
548 SSPUSHPTR(sv); /* remember the value */
549 SSPUSHINT(SAVEt_ITEM);
550 }
551}
552
553void
146174a9 554Perl_save_destructor(pTHX_ DESTRUCTORFUNC_NOCONTEXT_t f, void* p)
a0d0e21e
LW
555{
556 SSCHECK(3);
557 SSPUSHDPTR(f);
558 SSPUSHPTR(p);
559 SSPUSHINT(SAVEt_DESTRUCTOR);
560}
561
562void
146174a9
CB
563Perl_save_destructor_x(pTHX_ DESTRUCTORFUNC_t f, void* p)
564{
146174a9
CB
565 SSCHECK(3);
566 SSPUSHDXPTR(f);
567 SSPUSHPTR(p);
568 SSPUSHINT(SAVEt_DESTRUCTOR_X);
569}
570
571void
864dbfa3 572Perl_save_aelem(pTHX_ AV *av, I32 idx, SV **sptr)
4e4c362e 573{
4e4c362e
GS
574 SSCHECK(4);
575 SSPUSHPTR(SvREFCNT_inc(av));
576 SSPUSHINT(idx);
577 SSPUSHPTR(SvREFCNT_inc(*sptr));
578 SSPUSHINT(SAVEt_AELEM);
579 save_scalar_at(sptr);
580}
581
582void
864dbfa3 583Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
4e4c362e 584{
4e4c362e
GS
585 SSCHECK(4);
586 SSPUSHPTR(SvREFCNT_inc(hv));
587 SSPUSHPTR(SvREFCNT_inc(key));
588 SSPUSHPTR(SvREFCNT_inc(*sptr));
589 SSPUSHINT(SAVEt_HELEM);
590 save_scalar_at(sptr);
591}
592
593void
864dbfa3 594Perl_save_op(pTHX)
462e5cf6 595{
462e5cf6 596 SSCHECK(2);
533c011a 597 SSPUSHPTR(PL_op);
462e5cf6
MB
598 SSPUSHINT(SAVEt_OP);
599}
600
455ece5e 601I32
864dbfa3 602Perl_save_alloc(pTHX_ I32 size, I32 pad)
455ece5e 603{
455ece5e
AD
604 register I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
605 - (char*)PL_savestack);
606 register I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
607
608 /* SSCHECK may not be good enough */
609 while (PL_savestack_ix + elems + 2 > PL_savestack_max)
610 savestack_grow();
611
612 PL_savestack_ix += elems;
613 SSPUSHINT(elems);
614 SSPUSHINT(SAVEt_ALLOC);
615 return start;
616}
617
462e5cf6 618void
864dbfa3 619Perl_leave_scope(pTHX_ I32 base)
79072805
LW
620{
621 register SV *sv;
622 register SV *value;
623 register GV *gv;
624 register AV *av;
625 register HV *hv;
626 register void* ptr;
f4dd75d9 627 register char* str;
161b7d16 628 I32 i;
79072805
LW
629
630 if (base < -1)
cea2e8a9 631 Perl_croak(aTHX_ "panic: corrupt saved stack index");
3280af22 632 while (PL_savestack_ix > base) {
79072805
LW
633 switch (SSPOPINT) {
634 case SAVEt_ITEM: /* normal string */
635 value = (SV*)SSPOPPTR;
636 sv = (SV*)SSPOPPTR;
637 sv_replace(sv,value);
3280af22 638 PL_localizing = 2;
79072805 639 SvSETMAGIC(sv);
3280af22 640 PL_localizing = 0;
79072805
LW
641 break;
642 case SAVEt_SV: /* scalar reference */
643 value = (SV*)SSPOPPTR;
644 gv = (GV*)SSPOPPTR;
7a4c00b4 645 ptr = &GvSV(gv);
4e4c362e 646 SvREFCNT_dec(gv);
7a4c00b4 647 goto restore_sv;
f4dd75d9
GS
648 case SAVEt_GENERIC_PVREF: /* generic pv */
649 str = (char*)SSPOPPTR;
650 ptr = SSPOPPTR;
651 if (*(char**)ptr != str) {
652 Safefree(*(char**)ptr);
653 *(char**)ptr = str;
654 }
655 break;
b9d12d37
GS
656 case SAVEt_GENERIC_SVREF: /* generic sv */
657 value = (SV*)SSPOPPTR;
658 ptr = SSPOPPTR;
f4dd75d9
GS
659 sv = *(SV**)ptr;
660 *(SV**)ptr = value;
661 SvREFCNT_dec(sv);
b9d12d37
GS
662 SvREFCNT_dec(value);
663 break;
79072805 664 case SAVEt_SVREF: /* scalar reference */
7a4c00b4 665 value = (SV*)SSPOPPTR;
79072805 666 ptr = SSPOPPTR;
7a4c00b4 667 restore_sv:
79072805 668 sv = *(SV**)ptr;
146174a9 669 DEBUG_S(PerlIO_printf(Perl_debug_log,
54b9620d
MB
670 "restore svref: %p %p:%s -> %p:%s\n",
671 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
748a9306
LW
672 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
673 SvTYPE(sv) != SVt_PVGV)
674 {
a0d0e21e
LW
675 (void)SvUPGRADE(value, SvTYPE(sv));
676 SvMAGIC(value) = SvMAGIC(sv);
677 SvFLAGS(value) |= SvMAGICAL(sv);
678 SvMAGICAL_off(sv);
79072805 679 SvMAGIC(sv) = 0;
a0d0e21e 680 }
7a4c00b4
PP
681 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
682 SvTYPE(value) != SVt_PVGV)
683 {
684 SvFLAGS(value) |= (SvFLAGS(value) &
685 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
686 SvMAGICAL_off(value);
687 SvMAGIC(value) = 0;
688 }
8990e307 689 SvREFCNT_dec(sv);
a0d0e21e 690 *(SV**)ptr = value;
3280af22 691 PL_localizing = 2;
a0d0e21e 692 SvSETMAGIC(value);
3280af22 693 PL_localizing = 0;
4e4c362e 694 SvREFCNT_dec(value);
79072805
LW
695 break;
696 case SAVEt_AV: /* array reference */
697 av = (AV*)SSPOPPTR;
698 gv = (GV*)SSPOPPTR;
fb73857a
PP
699 if (GvAV(gv)) {
700 AV *goner = GvAV(gv);
701 SvMAGIC(av) = SvMAGIC(goner);
32da55ab 702 SvFLAGS((SV*)av) |= SvMAGICAL(goner);
fb73857a
PP
703 SvMAGICAL_off(goner);
704 SvMAGIC(goner) = 0;
705 SvREFCNT_dec(goner);
706 }
79072805 707 GvAV(gv) = av;
fb73857a 708 if (SvMAGICAL(av)) {
3280af22 709 PL_localizing = 2;
fb73857a 710 SvSETMAGIC((SV*)av);
3280af22 711 PL_localizing = 0;
fb73857a 712 }
79072805
LW
713 break;
714 case SAVEt_HV: /* hash reference */
715 hv = (HV*)SSPOPPTR;
716 gv = (GV*)SSPOPPTR;
fb73857a
PP
717 if (GvHV(gv)) {
718 HV *goner = GvHV(gv);
719 SvMAGIC(hv) = SvMAGIC(goner);
720 SvFLAGS(hv) |= SvMAGICAL(goner);
721 SvMAGICAL_off(goner);
722 SvMAGIC(goner) = 0;
723 SvREFCNT_dec(goner);
724 }
79072805 725 GvHV(gv) = hv;
fb73857a 726 if (SvMAGICAL(hv)) {
3280af22 727 PL_localizing = 2;
fb73857a 728 SvSETMAGIC((SV*)hv);
3280af22 729 PL_localizing = 0;
fb73857a 730 }
79072805
LW
731 break;
732 case SAVEt_INT: /* int reference */
733 ptr = SSPOPPTR;
734 *(int*)ptr = (int)SSPOPINT;
735 break;
85e6fe83
LW
736 case SAVEt_LONG: /* long reference */
737 ptr = SSPOPPTR;
738 *(long*)ptr = (long)SSPOPLONG;
739 break;
79072805
LW
740 case SAVEt_I32: /* I32 reference */
741 ptr = SSPOPPTR;
742 *(I32*)ptr = (I32)SSPOPINT;
743 break;
55497cff
PP
744 case SAVEt_I16: /* I16 reference */
745 ptr = SSPOPPTR;
746 *(I16*)ptr = (I16)SSPOPINT;
747 break;
146174a9
CB
748 case SAVEt_I8: /* I8 reference */
749 ptr = SSPOPPTR;
750 *(I8*)ptr = (I8)SSPOPINT;
751 break;
a0d0e21e
LW
752 case SAVEt_IV: /* IV reference */
753 ptr = SSPOPPTR;
754 *(IV*)ptr = (IV)SSPOPIV;
755 break;
79072805
LW
756 case SAVEt_SPTR: /* SV* reference */
757 ptr = SSPOPPTR;
758 *(SV**)ptr = (SV*)SSPOPPTR;
759 break;
146174a9 760 case SAVEt_VPTR: /* random* reference */
85e6fe83
LW
761 case SAVEt_PPTR: /* char* reference */
762 ptr = SSPOPPTR;
763 *(char**)ptr = (char*)SSPOPPTR;
764 break;
79072805
LW
765 case SAVEt_HPTR: /* HV* reference */
766 ptr = SSPOPPTR;
767 *(HV**)ptr = (HV*)SSPOPPTR;
768 break;
769 case SAVEt_APTR: /* AV* reference */
770 ptr = SSPOPPTR;
771 *(AV**)ptr = (AV*)SSPOPPTR;
772 break;
773 case SAVEt_NSTAB:
774 gv = (GV*)SSPOPPTR;
1f96a9ed 775 (void)sv_clear((SV*)gv);
79072805 776 break;
fb73857a 777 case SAVEt_GP: /* scalar reference */
79072805
LW
778 ptr = SSPOPPTR;
779 gv = (GV*)SSPOPPTR;
ed6c1d6b 780 if (SvPVX(gv) && SvLEN(gv) > 0) {
fb73857a
PP
781 Safefree(SvPVX(gv));
782 }
783 SvPVX(gv) = (char *)SSPOPPTR;
784 SvCUR(gv) = (STRLEN)SSPOPIV;
785 SvLEN(gv) = (STRLEN)SSPOPIV;
f46d017c
GS
786 gp_free(gv);
787 GvGP(gv) = (GP*)ptr;
fae75791 788 if (GvCVu(gv))
3280af22 789 PL_sub_generation++; /* putting a method back into circulation */
4633a7c4 790 SvREFCNT_dec(gv);
79072805 791 break;
8990e307
LW
792 case SAVEt_FREESV:
793 ptr = SSPOPPTR;
794 SvREFCNT_dec((SV*)ptr);
795 break;
796 case SAVEt_FREEOP:
797 ptr = SSPOPPTR;
3280af22
NIS
798 if (PL_comppad)
799 PL_curpad = AvARRAY(PL_comppad);
8990e307
LW
800 op_free((OP*)ptr);
801 break;
802 case SAVEt_FREEPV:
803 ptr = SSPOPPTR;
804 Safefree((char*)ptr);
805 break;
806 case SAVEt_CLEARSV:
3280af22 807 ptr = (void*)&PL_curpad[SSPOPLONG];
8990e307 808 sv = *(SV**)ptr;
bc44cdaf
GS
809 /* Can clear pad variable in place? */
810 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
6fc92669
GS
811 if (SvTHINKFIRST(sv))
812 sv_force_normal(sv);
a0d0e21e
LW
813 if (SvMAGICAL(sv))
814 mg_free(sv);
8990e307
LW
815
816 switch (SvTYPE(sv)) {
817 case SVt_NULL:
818 break;
819 case SVt_PVAV:
44a8e56a 820 av_clear((AV*)sv);
8990e307
LW
821 break;
822 case SVt_PVHV:
44a8e56a 823 hv_clear((HV*)sv);
8990e307
LW
824 break;
825 case SVt_PVCV:
cea2e8a9 826 Perl_croak(aTHX_ "panic: leave_scope pad code");
5377b701
CS
827 case SVt_RV:
828 case SVt_IV:
829 case SVt_NV:
830 (void)SvOK_off(sv);
8990e307
LW
831 break;
832 default:
a0d0e21e 833 (void)SvOK_off(sv);
5377b701 834 (void)SvOOK_off(sv);
8990e307
LW
835 break;
836 }
837 }
838 else { /* Someone has a claim on this, so abandon it. */
4aa0a1f7 839 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
8990e307
LW
840 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
841 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
842 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
843 default: *(SV**)ptr = NEWSV(0,0); break;
844 }
53868620 845 SvREFCNT_dec(sv); /* Cast current value to the winds. */
4aa0a1f7 846 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
8990e307
LW
847 }
848 break;
849 case SAVEt_DELETE:
850 ptr = SSPOPPTR;
851 hv = (HV*)ptr;
852 ptr = SSPOPPTR;
748a9306 853 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
4e4c362e 854 SvREFCNT_dec(hv);
adbc6bb1 855 Safefree(ptr);
8990e307 856 break;
a0d0e21e
LW
857 case SAVEt_DESTRUCTOR:
858 ptr = SSPOPPTR;
146174a9
CB
859 (*SSPOPDPTR)(ptr);
860 break;
861 case SAVEt_DESTRUCTOR_X:
862 ptr = SSPOPPTR;
863 (*SSPOPDXPTR)(aTHXo_ ptr);
a0d0e21e
LW
864 break;
865 case SAVEt_REGCONTEXT:
455ece5e 866 case SAVEt_ALLOC:
161b7d16 867 i = SSPOPINT;
3280af22 868 PL_savestack_ix -= i; /* regexp must have croaked */
a0d0e21e 869 break;
55497cff 870 case SAVEt_STACK_POS: /* Position on Perl stack */
161b7d16 871 i = SSPOPINT;
3280af22 872 PL_stack_sp = PL_stack_base + i;
55497cff 873 break;
161b7d16
SM
874 case SAVEt_AELEM: /* array element */
875 value = (SV*)SSPOPPTR;
876 i = SSPOPINT;
877 av = (AV*)SSPOPPTR;
878 ptr = av_fetch(av,i,1);
4e4c362e
GS
879 if (ptr) {
880 sv = *(SV**)ptr;
3280af22 881 if (sv && sv != &PL_sv_undef) {
33c27489 882 if (SvTIED_mg((SV*)av, 'P'))
4e4c362e
GS
883 (void)SvREFCNT_inc(sv);
884 SvREFCNT_dec(av);
885 goto restore_sv;
886 }
887 }
888 SvREFCNT_dec(av);
889 SvREFCNT_dec(value);
890 break;
161b7d16
SM
891 case SAVEt_HELEM: /* hash element */
892 value = (SV*)SSPOPPTR;
9002cb76 893 sv = (SV*)SSPOPPTR;
161b7d16
SM
894 hv = (HV*)SSPOPPTR;
895 ptr = hv_fetch_ent(hv, sv, 1, 0);
4e4c362e
GS
896 if (ptr) {
897 SV *oval = HeVAL((HE*)ptr);
3280af22 898 if (oval && oval != &PL_sv_undef) {
4e4c362e 899 ptr = &HeVAL((HE*)ptr);
33c27489 900 if (SvTIED_mg((SV*)hv, 'P'))
4e4c362e
GS
901 (void)SvREFCNT_inc(*(SV**)ptr);
902 SvREFCNT_dec(hv);
903 SvREFCNT_dec(sv);
904 goto restore_sv;
905 }
906 }
907 SvREFCNT_dec(hv);
908 SvREFCNT_dec(sv);
909 SvREFCNT_dec(value);
910 break;
462e5cf6 911 case SAVEt_OP:
533c011a 912 PL_op = (OP*)SSPOPPTR;
462e5cf6 913 break;
25eaa213 914 case SAVEt_HINTS:
3280af22
NIS
915 if (GvHV(PL_hintgv)) {
916 SvREFCNT_dec((SV*)GvHV(PL_hintgv));
917 GvHV(PL_hintgv) = NULL;
b3ac6de7 918 }
3280af22 919 *(I32*)&PL_hints = (I32)SSPOPINT;
b3ac6de7 920 break;
cb50131a
CB
921 case SAVEt_COMPPAD:
922 PL_comppad = (AV*)SSPOPPTR;
923 if (PL_comppad)
924 PL_curpad = AvARRAY(PL_comppad);
925 else
926 PL_curpad = Null(SV**);
927 break;
c3564e5c
GS
928 case SAVEt_PADSV:
929 {
930 PADOFFSET off = (PADOFFSET)SSPOPLONG;
931 ptr = SSPOPPTR;
932 if (ptr)
933 ((SV**)ptr)[off] = (SV*)SSPOPPTR;
934 }
935 break;
79072805 936 default:
cea2e8a9 937 Perl_croak(aTHX_ "panic: leave_scope inconsistency");
79072805
LW
938 }
939 }
940}
8990e307 941
8990e307 942void
864dbfa3 943Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 944{
35ff7856 945#ifdef DEBUGGING
22c35a8c 946 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 947 if (CxTYPE(cx) != CXt_SUBST) {
760ac839 948 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
146174a9
CB
949 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
950 PTR2UV(cx->blk_oldcop));
760ac839
LW
951 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
952 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
953 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
146174a9
CB
954 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
955 PTR2UV(cx->blk_oldpm));
760ac839 956 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
8990e307 957 }
6b35e009 958 switch (CxTYPE(cx)) {
8990e307
LW
959 case CXt_NULL:
960 case CXt_BLOCK:
961 break;
146174a9
CB
962 case CXt_FORMAT:
963 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
964 PTR2UV(cx->blk_sub.cv));
965 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%"UVxf"\n",
966 PTR2UV(cx->blk_sub.gv));
967 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%"UVxf"\n",
968 PTR2UV(cx->blk_sub.dfoutgv));
969 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
970 (int)cx->blk_sub.hasargs);
971 break;
8990e307 972 case CXt_SUB:
146174a9
CB
973 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
974 PTR2UV(cx->blk_sub.cv));
760ac839 975 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 976 (long)cx->blk_sub.olddepth);
760ac839 977 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
8990e307 978 (int)cx->blk_sub.hasargs);
146174a9
CB
979 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n",
980 (int)cx->blk_sub.lval);
8990e307
LW
981 break;
982 case CXt_EVAL:
760ac839 983 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
8990e307 984 (long)cx->blk_eval.old_in_eval);
760ac839 985 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
22c35a8c
GS
986 PL_op_name[cx->blk_eval.old_op_type],
987 PL_op_desc[cx->blk_eval.old_op_type]);
0f79a09d
GS
988 if (cx->blk_eval.old_namesv)
989 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
990 SvPVX(cx->blk_eval.old_namesv));
146174a9
CB
991 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
992 PTR2UV(cx->blk_eval.old_eval_root));
8990e307
LW
993 break;
994
995 case CXt_LOOP:
760ac839 996 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
8990e307 997 cx->blk_loop.label);
760ac839 998 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
8990e307 999 (long)cx->blk_loop.resetsp);
146174a9
CB
1000 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%"UVxf"\n",
1001 PTR2UV(cx->blk_loop.redo_op));
1002 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%"UVxf"\n",
1003 PTR2UV(cx->blk_loop.next_op));
1004 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%"UVxf"\n",
1005 PTR2UV(cx->blk_loop.last_op));
760ac839 1006 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
8990e307 1007 (long)cx->blk_loop.iterix);
146174a9
CB
1008 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
1009 PTR2UV(cx->blk_loop.iterary));
1010 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
1011 PTR2UV(CxITERVAR(cx)));
1012 if (CxITERVAR(cx))
1013 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%"UVxf"\n",
1014 PTR2UV(cx->blk_loop.itersave));
1015 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%"UVxf"\n",
1016 PTR2UV(cx->blk_loop.iterlval));
8990e307
LW
1017 break;
1018
1019 case CXt_SUBST:
760ac839 1020 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 1021 (long)cx->sb_iters);
760ac839 1022 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 1023 (long)cx->sb_maxiters);
35ef4773
GS
1024 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1025 (long)cx->sb_rflags);
760ac839 1026 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
8990e307 1027 (long)cx->sb_once);
760ac839 1028 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 1029 cx->sb_orig);
146174a9
CB
1030 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
1031 PTR2UV(cx->sb_dstr));
1032 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
1033 PTR2UV(cx->sb_targ));
1034 PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
1035 PTR2UV(cx->sb_s));
1036 PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
1037 PTR2UV(cx->sb_m));
1038 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
1039 PTR2UV(cx->sb_strend));
1040 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
1041 PTR2UV(cx->sb_rxres));
8990e307
LW
1042 break;
1043 }
17c3b450 1044#endif /* DEBUGGING */
35ff7856 1045}