This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
[perl5.git] / scope.c
CommitLineData
a0d0e21e 1/* scope.c
79072805 2 *
bc89e66f 3 * Copyright (c) 1991-2001, 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;
5cfc7842 199 mg_get(osv); /* note, can croak! */
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 }
5cfc7842
JH
681 /* XXX this branch is pretty bogus--note that we seem to
682 * only get here if the mg_get() in save_scalar_at() ends
683 * up croaking. This code irretrievably clears(!) the magic
684 * on the SV to avoid further croaking that might ensue
685 * when the SvSETMAGIC() below is called. This needs a
686 * total rethink. --GSAR */
7a4c00b4
PP
687 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
688 SvTYPE(value) != SVt_PVGV)
689 {
690 SvFLAGS(value) |= (SvFLAGS(value) &
691 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
692 SvMAGICAL_off(value);
5cfc7842 693 mg_free(value);
7a4c00b4 694 }
8990e307 695 SvREFCNT_dec(sv);
a0d0e21e 696 *(SV**)ptr = value;
3280af22 697 PL_localizing = 2;
a0d0e21e 698 SvSETMAGIC(value);
3280af22 699 PL_localizing = 0;
4e4c362e 700 SvREFCNT_dec(value);
79072805
LW
701 break;
702 case SAVEt_AV: /* array reference */
703 av = (AV*)SSPOPPTR;
704 gv = (GV*)SSPOPPTR;
fb73857a
PP
705 if (GvAV(gv)) {
706 AV *goner = GvAV(gv);
707 SvMAGIC(av) = SvMAGIC(goner);
32da55ab 708 SvFLAGS((SV*)av) |= SvMAGICAL(goner);
fb73857a
PP
709 SvMAGICAL_off(goner);
710 SvMAGIC(goner) = 0;
711 SvREFCNT_dec(goner);
712 }
79072805 713 GvAV(gv) = av;
fb73857a 714 if (SvMAGICAL(av)) {
3280af22 715 PL_localizing = 2;
fb73857a 716 SvSETMAGIC((SV*)av);
3280af22 717 PL_localizing = 0;
fb73857a 718 }
79072805
LW
719 break;
720 case SAVEt_HV: /* hash reference */
721 hv = (HV*)SSPOPPTR;
722 gv = (GV*)SSPOPPTR;
fb73857a
PP
723 if (GvHV(gv)) {
724 HV *goner = GvHV(gv);
725 SvMAGIC(hv) = SvMAGIC(goner);
726 SvFLAGS(hv) |= SvMAGICAL(goner);
727 SvMAGICAL_off(goner);
728 SvMAGIC(goner) = 0;
729 SvREFCNT_dec(goner);
730 }
79072805 731 GvHV(gv) = hv;
fb73857a 732 if (SvMAGICAL(hv)) {
3280af22 733 PL_localizing = 2;
fb73857a 734 SvSETMAGIC((SV*)hv);
3280af22 735 PL_localizing = 0;
fb73857a 736 }
79072805
LW
737 break;
738 case SAVEt_INT: /* int reference */
739 ptr = SSPOPPTR;
740 *(int*)ptr = (int)SSPOPINT;
741 break;
85e6fe83
LW
742 case SAVEt_LONG: /* long reference */
743 ptr = SSPOPPTR;
744 *(long*)ptr = (long)SSPOPLONG;
745 break;
79072805
LW
746 case SAVEt_I32: /* I32 reference */
747 ptr = SSPOPPTR;
748 *(I32*)ptr = (I32)SSPOPINT;
749 break;
55497cff
PP
750 case SAVEt_I16: /* I16 reference */
751 ptr = SSPOPPTR;
752 *(I16*)ptr = (I16)SSPOPINT;
753 break;
146174a9
CB
754 case SAVEt_I8: /* I8 reference */
755 ptr = SSPOPPTR;
756 *(I8*)ptr = (I8)SSPOPINT;
757 break;
a0d0e21e
LW
758 case SAVEt_IV: /* IV reference */
759 ptr = SSPOPPTR;
760 *(IV*)ptr = (IV)SSPOPIV;
761 break;
79072805
LW
762 case SAVEt_SPTR: /* SV* reference */
763 ptr = SSPOPPTR;
764 *(SV**)ptr = (SV*)SSPOPPTR;
765 break;
146174a9 766 case SAVEt_VPTR: /* random* reference */
85e6fe83
LW
767 case SAVEt_PPTR: /* char* reference */
768 ptr = SSPOPPTR;
769 *(char**)ptr = (char*)SSPOPPTR;
770 break;
79072805
LW
771 case SAVEt_HPTR: /* HV* reference */
772 ptr = SSPOPPTR;
773 *(HV**)ptr = (HV*)SSPOPPTR;
774 break;
775 case SAVEt_APTR: /* AV* reference */
776 ptr = SSPOPPTR;
777 *(AV**)ptr = (AV*)SSPOPPTR;
778 break;
779 case SAVEt_NSTAB:
780 gv = (GV*)SSPOPPTR;
1f96a9ed 781 (void)sv_clear((SV*)gv);
79072805 782 break;
fb73857a 783 case SAVEt_GP: /* scalar reference */
79072805
LW
784 ptr = SSPOPPTR;
785 gv = (GV*)SSPOPPTR;
ed6c1d6b 786 if (SvPVX(gv) && SvLEN(gv) > 0) {
fb73857a
PP
787 Safefree(SvPVX(gv));
788 }
789 SvPVX(gv) = (char *)SSPOPPTR;
790 SvCUR(gv) = (STRLEN)SSPOPIV;
791 SvLEN(gv) = (STRLEN)SSPOPIV;
f46d017c
GS
792 gp_free(gv);
793 GvGP(gv) = (GP*)ptr;
fae75791 794 if (GvCVu(gv))
3280af22 795 PL_sub_generation++; /* putting a method back into circulation */
4633a7c4 796 SvREFCNT_dec(gv);
79072805 797 break;
8990e307
LW
798 case SAVEt_FREESV:
799 ptr = SSPOPPTR;
800 SvREFCNT_dec((SV*)ptr);
801 break;
802 case SAVEt_FREEOP:
803 ptr = SSPOPPTR;
3280af22
NIS
804 if (PL_comppad)
805 PL_curpad = AvARRAY(PL_comppad);
8990e307
LW
806 op_free((OP*)ptr);
807 break;
808 case SAVEt_FREEPV:
809 ptr = SSPOPPTR;
810 Safefree((char*)ptr);
811 break;
812 case SAVEt_CLEARSV:
3280af22 813 ptr = (void*)&PL_curpad[SSPOPLONG];
8990e307 814 sv = *(SV**)ptr;
bc44cdaf
GS
815 /* Can clear pad variable in place? */
816 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
6fc92669 817 if (SvTHINKFIRST(sv))
840a7b70 818 sv_force_normal_flags(sv, SV_IMMEDIATE_UNREF);
a0d0e21e
LW
819 if (SvMAGICAL(sv))
820 mg_free(sv);
8990e307
LW
821
822 switch (SvTYPE(sv)) {
823 case SVt_NULL:
824 break;
825 case SVt_PVAV:
44a8e56a 826 av_clear((AV*)sv);
8990e307
LW
827 break;
828 case SVt_PVHV:
44a8e56a 829 hv_clear((HV*)sv);
8990e307
LW
830 break;
831 case SVt_PVCV:
cea2e8a9 832 Perl_croak(aTHX_ "panic: leave_scope pad code");
5377b701
CS
833 case SVt_RV:
834 case SVt_IV:
835 case SVt_NV:
836 (void)SvOK_off(sv);
8990e307
LW
837 break;
838 default:
a0d0e21e 839 (void)SvOK_off(sv);
5377b701 840 (void)SvOOK_off(sv);
8990e307
LW
841 break;
842 }
843 }
844 else { /* Someone has a claim on this, so abandon it. */
4aa0a1f7 845 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
8990e307
LW
846 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
847 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
848 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
849 default: *(SV**)ptr = NEWSV(0,0); break;
850 }
53868620 851 SvREFCNT_dec(sv); /* Cast current value to the winds. */
4aa0a1f7 852 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
8990e307
LW
853 }
854 break;
855 case SAVEt_DELETE:
856 ptr = SSPOPPTR;
857 hv = (HV*)ptr;
858 ptr = SSPOPPTR;
748a9306 859 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
4e4c362e 860 SvREFCNT_dec(hv);
57813020 861 Safefree(ptr);
8990e307 862 break;
a0d0e21e
LW
863 case SAVEt_DESTRUCTOR:
864 ptr = SSPOPPTR;
146174a9
CB
865 (*SSPOPDPTR)(ptr);
866 break;
867 case SAVEt_DESTRUCTOR_X:
868 ptr = SSPOPPTR;
869 (*SSPOPDXPTR)(aTHXo_ ptr);
a0d0e21e
LW
870 break;
871 case SAVEt_REGCONTEXT:
455ece5e 872 case SAVEt_ALLOC:
161b7d16 873 i = SSPOPINT;
3280af22 874 PL_savestack_ix -= i; /* regexp must have croaked */
a0d0e21e 875 break;
55497cff 876 case SAVEt_STACK_POS: /* Position on Perl stack */
161b7d16 877 i = SSPOPINT;
3280af22 878 PL_stack_sp = PL_stack_base + i;
55497cff 879 break;
161b7d16
SM
880 case SAVEt_AELEM: /* array element */
881 value = (SV*)SSPOPPTR;
882 i = SSPOPINT;
883 av = (AV*)SSPOPPTR;
884 ptr = av_fetch(av,i,1);
4e4c362e
GS
885 if (ptr) {
886 sv = *(SV**)ptr;
3280af22 887 if (sv && sv != &PL_sv_undef) {
33c27489 888 if (SvTIED_mg((SV*)av, 'P'))
4e4c362e
GS
889 (void)SvREFCNT_inc(sv);
890 SvREFCNT_dec(av);
891 goto restore_sv;
892 }
893 }
894 SvREFCNT_dec(av);
895 SvREFCNT_dec(value);
896 break;
161b7d16
SM
897 case SAVEt_HELEM: /* hash element */
898 value = (SV*)SSPOPPTR;
9002cb76 899 sv = (SV*)SSPOPPTR;
161b7d16
SM
900 hv = (HV*)SSPOPPTR;
901 ptr = hv_fetch_ent(hv, sv, 1, 0);
4e4c362e
GS
902 if (ptr) {
903 SV *oval = HeVAL((HE*)ptr);
3280af22 904 if (oval && oval != &PL_sv_undef) {
4e4c362e 905 ptr = &HeVAL((HE*)ptr);
33c27489 906 if (SvTIED_mg((SV*)hv, 'P'))
4e4c362e
GS
907 (void)SvREFCNT_inc(*(SV**)ptr);
908 SvREFCNT_dec(hv);
909 SvREFCNT_dec(sv);
910 goto restore_sv;
911 }
912 }
913 SvREFCNT_dec(hv);
914 SvREFCNT_dec(sv);
915 SvREFCNT_dec(value);
916 break;
462e5cf6 917 case SAVEt_OP:
533c011a 918 PL_op = (OP*)SSPOPPTR;
462e5cf6 919 break;
25eaa213 920 case SAVEt_HINTS:
3280af22 921 *(I32*)&PL_hints = (I32)SSPOPINT;
b3ac6de7 922 break;
cb50131a
CB
923 case SAVEt_COMPPAD:
924 PL_comppad = (AV*)SSPOPPTR;
925 if (PL_comppad)
926 PL_curpad = AvARRAY(PL_comppad);
927 else
928 PL_curpad = Null(SV**);
929 break;
c3564e5c
GS
930 case SAVEt_PADSV:
931 {
932 PADOFFSET off = (PADOFFSET)SSPOPLONG;
933 ptr = SSPOPPTR;
934 if (ptr)
935 ((SV**)ptr)[off] = (SV*)SSPOPPTR;
936 }
937 break;
79072805 938 default:
cea2e8a9 939 Perl_croak(aTHX_ "panic: leave_scope inconsistency");
79072805
LW
940 }
941 }
942}
8990e307 943
8990e307 944void
864dbfa3 945Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
8990e307 946{
35ff7856 947#ifdef DEBUGGING
22c35a8c 948 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
6b35e009 949 if (CxTYPE(cx) != CXt_SUBST) {
760ac839 950 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
146174a9
CB
951 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%"UVxf"\n",
952 PTR2UV(cx->blk_oldcop));
760ac839
LW
953 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
954 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
955 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
146174a9
CB
956 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%"UVxf"\n",
957 PTR2UV(cx->blk_oldpm));
760ac839 958 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
8990e307 959 }
6b35e009 960 switch (CxTYPE(cx)) {
8990e307
LW
961 case CXt_NULL:
962 case CXt_BLOCK:
963 break;
146174a9
CB
964 case CXt_FORMAT:
965 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
966 PTR2UV(cx->blk_sub.cv));
967 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%"UVxf"\n",
968 PTR2UV(cx->blk_sub.gv));
969 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%"UVxf"\n",
970 PTR2UV(cx->blk_sub.dfoutgv));
971 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
972 (int)cx->blk_sub.hasargs);
973 break;
8990e307 974 case CXt_SUB:
146174a9
CB
975 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%"UVxf"\n",
976 PTR2UV(cx->blk_sub.cv));
760ac839 977 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
8990e307 978 (long)cx->blk_sub.olddepth);
760ac839 979 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
8990e307 980 (int)cx->blk_sub.hasargs);
146174a9
CB
981 PerlIO_printf(Perl_debug_log, "BLK_SUB.LVAL = %d\n",
982 (int)cx->blk_sub.lval);
8990e307
LW
983 break;
984 case CXt_EVAL:
760ac839 985 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
8990e307 986 (long)cx->blk_eval.old_in_eval);
760ac839 987 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
22c35a8c
GS
988 PL_op_name[cx->blk_eval.old_op_type],
989 PL_op_desc[cx->blk_eval.old_op_type]);
0f79a09d
GS
990 if (cx->blk_eval.old_namesv)
991 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
992 SvPVX(cx->blk_eval.old_namesv));
146174a9
CB
993 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%"UVxf"\n",
994 PTR2UV(cx->blk_eval.old_eval_root));
8990e307
LW
995 break;
996
997 case CXt_LOOP:
760ac839 998 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
8990e307 999 cx->blk_loop.label);
760ac839 1000 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
8990e307 1001 (long)cx->blk_loop.resetsp);
146174a9
CB
1002 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%"UVxf"\n",
1003 PTR2UV(cx->blk_loop.redo_op));
1004 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%"UVxf"\n",
1005 PTR2UV(cx->blk_loop.next_op));
1006 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%"UVxf"\n",
1007 PTR2UV(cx->blk_loop.last_op));
760ac839 1008 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
8990e307 1009 (long)cx->blk_loop.iterix);
146174a9
CB
1010 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%"UVxf"\n",
1011 PTR2UV(cx->blk_loop.iterary));
1012 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%"UVxf"\n",
1013 PTR2UV(CxITERVAR(cx)));
1014 if (CxITERVAR(cx))
1015 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%"UVxf"\n",
1016 PTR2UV(cx->blk_loop.itersave));
1017 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%"UVxf"\n",
1018 PTR2UV(cx->blk_loop.iterlval));
8990e307
LW
1019 break;
1020
1021 case CXt_SUBST:
760ac839 1022 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
8990e307 1023 (long)cx->sb_iters);
760ac839 1024 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
8990e307 1025 (long)cx->sb_maxiters);
35ef4773
GS
1026 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
1027 (long)cx->sb_rflags);
760ac839 1028 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
8990e307 1029 (long)cx->sb_once);
760ac839 1030 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
8990e307 1031 cx->sb_orig);
146174a9
CB
1032 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%"UVxf"\n",
1033 PTR2UV(cx->sb_dstr));
1034 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%"UVxf"\n",
1035 PTR2UV(cx->sb_targ));
1036 PerlIO_printf(Perl_debug_log, "SB_S = 0x%"UVxf"\n",
1037 PTR2UV(cx->sb_s));
1038 PerlIO_printf(Perl_debug_log, "SB_M = 0x%"UVxf"\n",
1039 PTR2UV(cx->sb_m));
1040 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%"UVxf"\n",
1041 PTR2UV(cx->sb_strend));
1042 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%"UVxf"\n",
1043 PTR2UV(cx->sb_rxres));
8990e307
LW
1044 break;
1045 }
17c3b450 1046#endif /* DEBUGGING */
35ff7856 1047}