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