3 * Copyright (c) 1991-1999, Larry Wall
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.
11 * "For the fashion of Minas Tirith was such that it was built on seven
16 #define PERL_IN_SCOPE_C
20 Perl_default_protect(pTHX_ int *excpt, protect_body_t body, ...)
28 DEBUG_l(deb("Setting up local jumplevel %p, was %p\n",
29 &cur_env, PL_top_env));
35 ret = CALL_FPTR(body)(args);
44 Perl_stack_grow(pTHX_ SV **sp, SV **p, int n)
47 #if defined(DEBUGGING) && !defined(USE_THREADS)
48 static int growing = 0;
53 #ifndef STRESS_REALLOC
54 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
56 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
58 #if defined(DEBUGGING) && !defined(USE_THREADS)
64 #ifndef STRESS_REALLOC
65 #define GROW(old) ((old) * 3 / 2)
67 #define GROW(old) ((old) + 1)
71 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
75 New(56, si, 1, PERL_SI);
76 si->si_stack = newAV();
77 AvREAL_off(si->si_stack);
78 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
79 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
80 AvFILLp(si->si_stack) = 0;
83 si->si_cxmax = cxitems - 1;
85 si->si_type = PERLSI_UNDEF;
86 New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
94 cxstack_max = GROW(cxstack_max);
95 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
96 return cxstack_ix + 1;
100 Perl_push_return(pTHX_ OP *retop)
103 if (PL_retstack_ix == PL_retstack_max) {
104 PL_retstack_max = GROW(PL_retstack_max);
105 Renew(PL_retstack, PL_retstack_max, OP*);
107 PL_retstack[PL_retstack_ix++] = retop;
111 Perl_pop_return(pTHX)
114 if (PL_retstack_ix > 0)
115 return PL_retstack[--PL_retstack_ix];
121 Perl_push_scope(pTHX)
124 if (PL_scopestack_ix == PL_scopestack_max) {
125 PL_scopestack_max = GROW(PL_scopestack_max);
126 Renew(PL_scopestack, PL_scopestack_max, I32);
128 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
136 I32 oldsave = PL_scopestack[--PL_scopestack_ix];
137 LEAVE_SCOPE(oldsave);
141 Perl_markstack_grow(pTHX)
144 I32 oldmax = PL_markstack_max - PL_markstack;
145 I32 newmax = GROW(oldmax);
147 Renew(PL_markstack, newmax, I32);
148 PL_markstack_ptr = PL_markstack + oldmax;
149 PL_markstack_max = PL_markstack + newmax;
153 Perl_savestack_grow(pTHX)
156 PL_savestack_max = GROW(PL_savestack_max) + 4;
157 Renew(PL_savestack, PL_savestack_max, ANY);
163 Perl_tmps_grow(pTHX_ I32 n)
166 #ifndef STRESS_REALLOC
168 n = (PL_tmps_max < 512) ? 128 : 512;
170 PL_tmps_max = PL_tmps_ix + n + 1;
171 Renew(PL_tmps_stack, PL_tmps_max, SV*);
179 /* XXX should tmps_floor live in cxstack? */
180 I32 myfloor = PL_tmps_floor;
181 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
182 SV* sv = PL_tmps_stack[PL_tmps_ix];
183 PL_tmps_stack[PL_tmps_ix--] = Nullsv;
186 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
192 save_scalar_at(pTHX_ SV **sptr)
198 sv = *sptr = NEWSV(0,0);
199 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
200 sv_upgrade(sv, SvTYPE(osv));
201 if (SvGMAGICAL(osv)) {
203 bool oldtainted = PL_tainted;
205 if (PL_tainting && PL_tainted && (mg = mg_find(osv, 't'))) {
206 SAVESPTR(mg->mg_obj);
209 SvFLAGS(osv) |= (SvFLAGS(osv) &
210 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
211 PL_tainted = oldtainted;
213 SvMAGIC(sv) = SvMAGIC(osv);
214 SvFLAGS(sv) |= SvMAGICAL(osv);
223 Perl_save_scalar(pTHX_ GV *gv)
226 SV **sptr = &GvSV(gv);
228 SSPUSHPTR(SvREFCNT_inc(gv));
229 SSPUSHPTR(SvREFCNT_inc(*sptr));
231 return save_scalar_at(sptr);
235 Perl_save_svref(pTHX_ SV **sptr)
240 SSPUSHPTR(SvREFCNT_inc(*sptr));
241 SSPUSHINT(SAVEt_SVREF);
242 return save_scalar_at(sptr);
245 /* Like save_svref(), but doesn't deal with magic. Can be used to
246 * restore a global SV to its prior contents, freeing new value. */
248 Perl_save_generic_svref(pTHX_ SV **sptr)
253 SSPUSHPTR(SvREFCNT_inc(*sptr));
254 SSPUSHINT(SAVEt_GENERIC_SVREF);
258 Perl_save_gp(pTHX_ GV *gv, I32 empty)
262 SSPUSHIV((IV)SvLEN(gv));
263 SvLEN(gv) = 0; /* forget that anything was allocated here */
264 SSPUSHIV((IV)SvCUR(gv));
265 SSPUSHPTR(SvPVX(gv));
267 SSPUSHPTR(SvREFCNT_inc(gv));
275 PL_sub_generation++; /* taking a method out of circulation */
276 Newz(602, gp, 1, GP);
277 GvGP(gv) = gp_ref(gp);
278 GvSV(gv) = NEWSV(72,0);
279 GvLINE(gv) = PL_curcop->cop_line;
289 Perl_save_ary(pTHX_ GV *gv)
295 if (!AvREAL(oav) && AvREIFY(oav))
302 GvAV(gv) = Null(AV*);
305 SvMAGIC(av) = SvMAGIC(oav);
306 SvFLAGS(av) |= SvMAGICAL(oav);
317 Perl_save_hash(pTHX_ GV *gv)
324 SSPUSHPTR(ohv = GvHVn(gv));
327 GvHV(gv) = Null(HV*);
330 SvMAGIC(hv) = SvMAGIC(ohv);
331 SvFLAGS(hv) |= SvMAGICAL(ohv);
342 Perl_save_item(pTHX_ register SV *item)
345 register SV *sv = NEWSV(0,0);
349 SSPUSHPTR(item); /* remember the pointer */
350 SSPUSHPTR(sv); /* remember the value */
351 SSPUSHINT(SAVEt_ITEM);
355 Perl_save_int(pTHX_ int *intp)
361 SSPUSHINT(SAVEt_INT);
365 Perl_save_long(pTHX_ long int *longp)
371 SSPUSHINT(SAVEt_LONG);
375 Perl_save_I32(pTHX_ I32 *intp)
381 SSPUSHINT(SAVEt_I32);
385 Perl_save_I16(pTHX_ I16 *intp)
391 SSPUSHINT(SAVEt_I16);
395 Perl_save_iv(pTHX_ IV *ivp)
404 /* Cannot use save_sptr() to store a char* since the SV** cast will
405 * force word-alignment and we'll miss the pointer.
408 Perl_save_pptr(pTHX_ char **pptr)
414 SSPUSHINT(SAVEt_PPTR);
418 Perl_save_sptr(pTHX_ SV **sptr)
424 SSPUSHINT(SAVEt_SPTR);
428 Perl_save_threadsv(pTHX_ PADOFFSET i)
432 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
433 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
434 i, svp, *svp, SvPEEK(*svp)));
438 croak("panic: save_threadsv called in non-threaded perl");
440 #endif /* USE_THREADS */
444 Perl_save_nogv(pTHX_ GV *gv)
449 SSPUSHINT(SAVEt_NSTAB);
453 Perl_save_hptr(pTHX_ HV **hptr)
459 SSPUSHINT(SAVEt_HPTR);
463 Perl_save_aptr(pTHX_ AV **aptr)
469 SSPUSHINT(SAVEt_APTR);
473 Perl_save_freesv(pTHX_ SV *sv)
478 SSPUSHINT(SAVEt_FREESV);
482 Perl_save_freeop(pTHX_ OP *o)
487 SSPUSHINT(SAVEt_FREEOP);
491 Perl_save_freepv(pTHX_ char *pv)
496 SSPUSHINT(SAVEt_FREEPV);
500 Perl_save_clearsv(pTHX_ SV **svp)
504 SSPUSHLONG((long)(svp-PL_curpad));
505 SSPUSHINT(SAVEt_CLEARSV);
509 Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
515 SSPUSHPTR(SvREFCNT_inc(hv));
516 SSPUSHINT(SAVEt_DELETE);
520 Perl_save_list(pTHX_ register SV **sarg, I32 maxsarg)
526 for (i = 1; i <= maxsarg; i++) {
528 sv_setsv(sv,sarg[i]);
530 SSPUSHPTR(sarg[i]); /* remember the pointer */
531 SSPUSHPTR(sv); /* remember the value */
532 SSPUSHINT(SAVEt_ITEM);
538 Perl_save_destructor(pTHX_ DESTRUCTORFUNC f, void* p)
540 Perl_save_destructor(pTHX_ void (*f) (void *), void *p)
547 SSPUSHINT(SAVEt_DESTRUCTOR);
551 Perl_save_aelem(pTHX_ AV *av, I32 idx, SV **sptr)
555 SSPUSHPTR(SvREFCNT_inc(av));
557 SSPUSHPTR(SvREFCNT_inc(*sptr));
558 SSPUSHINT(SAVEt_AELEM);
559 save_scalar_at(sptr);
563 Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
567 SSPUSHPTR(SvREFCNT_inc(hv));
568 SSPUSHPTR(SvREFCNT_inc(key));
569 SSPUSHPTR(SvREFCNT_inc(*sptr));
570 SSPUSHINT(SAVEt_HELEM);
571 save_scalar_at(sptr);
584 Perl_save_alloc(pTHX_ I32 size, I32 pad)
587 register I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
588 - (char*)PL_savestack);
589 register I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
591 /* SSCHECK may not be good enough */
592 while (PL_savestack_ix + elems + 2 > PL_savestack_max)
595 PL_savestack_ix += elems;
597 SSPUSHINT(SAVEt_ALLOC);
602 Perl_leave_scope(pTHX_ I32 base)
614 croak("panic: corrupt saved stack index");
615 while (PL_savestack_ix > base) {
617 case SAVEt_ITEM: /* normal string */
618 value = (SV*)SSPOPPTR;
620 sv_replace(sv,value);
625 case SAVEt_SV: /* scalar reference */
626 value = (SV*)SSPOPPTR;
631 case SAVEt_GENERIC_SVREF: /* generic sv */
632 value = (SV*)SSPOPPTR;
641 case SAVEt_SVREF: /* scalar reference */
642 value = (SV*)SSPOPPTR;
646 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
647 "restore svref: %p %p:%s -> %p:%s\n",
648 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
649 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
650 SvTYPE(sv) != SVt_PVGV)
652 (void)SvUPGRADE(value, SvTYPE(sv));
653 SvMAGIC(value) = SvMAGIC(sv);
654 SvFLAGS(value) |= SvMAGICAL(sv);
658 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
659 SvTYPE(value) != SVt_PVGV)
661 SvFLAGS(value) |= (SvFLAGS(value) &
662 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
663 SvMAGICAL_off(value);
673 case SAVEt_AV: /* array reference */
677 AV *goner = GvAV(gv);
678 SvMAGIC(av) = SvMAGIC(goner);
679 SvFLAGS(av) |= SvMAGICAL(goner);
680 SvMAGICAL_off(goner);
691 case SAVEt_HV: /* hash reference */
695 HV *goner = GvHV(gv);
696 SvMAGIC(hv) = SvMAGIC(goner);
697 SvFLAGS(hv) |= SvMAGICAL(goner);
698 SvMAGICAL_off(goner);
709 case SAVEt_INT: /* int reference */
711 *(int*)ptr = (int)SSPOPINT;
713 case SAVEt_LONG: /* long reference */
715 *(long*)ptr = (long)SSPOPLONG;
717 case SAVEt_I32: /* I32 reference */
719 *(I32*)ptr = (I32)SSPOPINT;
721 case SAVEt_I16: /* I16 reference */
723 *(I16*)ptr = (I16)SSPOPINT;
725 case SAVEt_IV: /* IV reference */
727 *(IV*)ptr = (IV)SSPOPIV;
729 case SAVEt_SPTR: /* SV* reference */
731 *(SV**)ptr = (SV*)SSPOPPTR;
733 case SAVEt_PPTR: /* char* reference */
735 *(char**)ptr = (char*)SSPOPPTR;
737 case SAVEt_HPTR: /* HV* reference */
739 *(HV**)ptr = (HV*)SSPOPPTR;
741 case SAVEt_APTR: /* AV* reference */
743 *(AV**)ptr = (AV*)SSPOPPTR;
747 (void)sv_clear((SV*)gv);
749 case SAVEt_GP: /* scalar reference */
752 if (SvPVX(gv) && SvLEN(gv) > 0) {
755 SvPVX(gv) = (char *)SSPOPPTR;
756 SvCUR(gv) = (STRLEN)SSPOPIV;
757 SvLEN(gv) = (STRLEN)SSPOPIV;
761 PL_sub_generation++; /* putting a method back into circulation */
766 SvREFCNT_dec((SV*)ptr);
771 PL_curpad = AvARRAY(PL_comppad);
776 Safefree((char*)ptr);
779 ptr = (void*)&PL_curpad[SSPOPLONG];
781 /* Can clear pad variable in place? */
782 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
783 if (SvTHINKFIRST(sv))
788 switch (SvTYPE(sv)) {
798 croak("panic: leave_scope pad code");
810 else { /* Someone has a claim on this, so abandon it. */
811 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
812 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
813 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
814 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
815 default: *(SV**)ptr = NEWSV(0,0); break;
817 SvREFCNT_dec(sv); /* Cast current value to the winds. */
818 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
825 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
829 case SAVEt_DESTRUCTOR:
833 case SAVEt_REGCONTEXT:
836 PL_savestack_ix -= i; /* regexp must have croaked */
838 case SAVEt_STACK_POS: /* Position on Perl stack */
840 PL_stack_sp = PL_stack_base + i;
842 case SAVEt_AELEM: /* array element */
843 value = (SV*)SSPOPPTR;
846 ptr = av_fetch(av,i,1);
849 if (sv && sv != &PL_sv_undef) {
850 if (SvTIED_mg((SV*)av, 'P'))
851 (void)SvREFCNT_inc(sv);
859 case SAVEt_HELEM: /* hash element */
860 value = (SV*)SSPOPPTR;
863 ptr = hv_fetch_ent(hv, sv, 1, 0);
865 SV *oval = HeVAL((HE*)ptr);
866 if (oval && oval != &PL_sv_undef) {
867 ptr = &HeVAL((HE*)ptr);
868 if (SvTIED_mg((SV*)hv, 'P'))
869 (void)SvREFCNT_inc(*(SV**)ptr);
880 PL_op = (OP*)SSPOPPTR;
883 if (GvHV(PL_hintgv)) {
884 SvREFCNT_dec((SV*)GvHV(PL_hintgv));
885 GvHV(PL_hintgv) = NULL;
887 *(I32*)&PL_hints = (I32)SSPOPINT;
890 croak("panic: leave_scope inconsistency");
896 Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
900 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
901 if (CxTYPE(cx) != CXt_SUBST) {
902 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
903 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
904 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
905 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
906 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
907 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
908 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
910 switch (CxTYPE(cx)) {
915 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
916 (long)cx->blk_sub.cv);
917 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
918 (long)cx->blk_sub.gv);
919 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
920 (long)cx->blk_sub.dfoutgv);
921 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
922 (long)cx->blk_sub.olddepth);
923 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
924 (int)cx->blk_sub.hasargs);
927 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
928 (long)cx->blk_eval.old_in_eval);
929 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
930 PL_op_name[cx->blk_eval.old_op_type],
931 PL_op_desc[cx->blk_eval.old_op_type]);
932 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
933 cx->blk_eval.old_name);
934 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
935 (long)cx->blk_eval.old_eval_root);
939 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
941 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
942 (long)cx->blk_loop.resetsp);
943 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
944 (long)cx->blk_loop.redo_op);
945 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
946 (long)cx->blk_loop.next_op);
947 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
948 (long)cx->blk_loop.last_op);
949 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
950 (long)cx->blk_loop.iterix);
951 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
952 (long)cx->blk_loop.iterary);
953 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
954 (long)cx->blk_loop.itervar);
955 if (cx->blk_loop.itervar)
956 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
957 (long)cx->blk_loop.itersave);
958 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
959 (long)cx->blk_loop.iterlval);
963 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
965 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
966 (long)cx->sb_maxiters);
967 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
968 (long)cx->sb_rflags);
969 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
971 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
973 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
975 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
977 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
979 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
981 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
982 (long)cx->sb_strend);
983 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",
987 #endif /* DEBUGGING */