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