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