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, ...)
25 ret = vdefault_protect(excpt, body, &args);
31 Perl_vdefault_protect(pTHX_ int *excpt, protect_body_t body, va_list *args)
38 DEBUG_l(Perl_deb(aTHX_ "Setting up local jumplevel %p, was %p\n",
39 &cur_env, PL_top_env));
44 ret = CALL_FPTR(body)(aTHX_ *args);
51 Perl_stack_grow(pTHX_ SV **sp, SV **p, int n)
54 #if defined(DEBUGGING) && !defined(USE_THREADS)
55 static int growing = 0;
60 #ifndef STRESS_REALLOC
61 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
63 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
65 #if defined(DEBUGGING) && !defined(USE_THREADS)
71 #ifndef STRESS_REALLOC
72 #define GROW(old) ((old) * 3 / 2)
74 #define GROW(old) ((old) + 1)
78 Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems)
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);
86 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
87 AvFILLp(si->si_stack) = 0;
90 si->si_cxmax = cxitems - 1;
92 si->si_type = PERLSI_UNDEF;
93 New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
101 cxstack_max = GROW(cxstack_max);
102 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
103 return cxstack_ix + 1;
107 Perl_push_return(pTHX_ OP *retop)
110 if (PL_retstack_ix == PL_retstack_max) {
111 PL_retstack_max = GROW(PL_retstack_max);
112 Renew(PL_retstack, PL_retstack_max, OP*);
114 PL_retstack[PL_retstack_ix++] = retop;
118 Perl_pop_return(pTHX)
121 if (PL_retstack_ix > 0)
122 return PL_retstack[--PL_retstack_ix];
128 Perl_push_scope(pTHX)
131 if (PL_scopestack_ix == PL_scopestack_max) {
132 PL_scopestack_max = GROW(PL_scopestack_max);
133 Renew(PL_scopestack, PL_scopestack_max, I32);
135 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
143 I32 oldsave = PL_scopestack[--PL_scopestack_ix];
144 LEAVE_SCOPE(oldsave);
148 Perl_markstack_grow(pTHX)
151 I32 oldmax = PL_markstack_max - PL_markstack;
152 I32 newmax = GROW(oldmax);
154 Renew(PL_markstack, newmax, I32);
155 PL_markstack_ptr = PL_markstack + oldmax;
156 PL_markstack_max = PL_markstack + newmax;
160 Perl_savestack_grow(pTHX)
163 PL_savestack_max = GROW(PL_savestack_max) + 4;
164 Renew(PL_savestack, PL_savestack_max, ANY);
170 Perl_tmps_grow(pTHX_ I32 n)
173 #ifndef STRESS_REALLOC
175 n = (PL_tmps_max < 512) ? 128 : 512;
177 PL_tmps_max = PL_tmps_ix + n + 1;
178 Renew(PL_tmps_stack, PL_tmps_max, SV*);
186 /* XXX should tmps_floor live in cxstack? */
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;
193 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
199 S_save_scalar_at(pTHX_ SV **sptr)
205 sv = *sptr = NEWSV(0,0);
206 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
207 sv_upgrade(sv, SvTYPE(osv));
208 if (SvGMAGICAL(osv)) {
210 bool oldtainted = PL_tainted;
212 if (PL_tainting && PL_tainted && (mg = mg_find(osv, 't'))) {
213 SAVESPTR(mg->mg_obj);
216 SvFLAGS(osv) |= (SvFLAGS(osv) &
217 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
218 PL_tainted = oldtainted;
220 SvMAGIC(sv) = SvMAGIC(osv);
221 SvFLAGS(sv) |= SvMAGICAL(osv);
230 Perl_save_scalar(pTHX_ GV *gv)
233 SV **sptr = &GvSV(gv);
235 SSPUSHPTR(SvREFCNT_inc(gv));
236 SSPUSHPTR(SvREFCNT_inc(*sptr));
238 return save_scalar_at(sptr);
242 Perl_save_svref(pTHX_ SV **sptr)
247 SSPUSHPTR(SvREFCNT_inc(*sptr));
248 SSPUSHINT(SAVEt_SVREF);
249 return save_scalar_at(sptr);
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. */
255 Perl_save_generic_svref(pTHX_ SV **sptr)
260 SSPUSHPTR(SvREFCNT_inc(*sptr));
261 SSPUSHINT(SAVEt_GENERIC_SVREF);
265 Perl_save_gp(pTHX_ GV *gv, I32 empty)
269 SSPUSHIV((IV)SvLEN(gv));
270 SvLEN(gv) = 0; /* forget that anything was allocated here */
271 SSPUSHIV((IV)SvCUR(gv));
272 SSPUSHPTR(SvPVX(gv));
274 SSPUSHPTR(SvREFCNT_inc(gv));
282 PL_sub_generation++; /* taking a method out of circulation */
283 Newz(602, gp, 1, GP);
284 GvGP(gv) = gp_ref(gp);
285 GvSV(gv) = NEWSV(72,0);
286 GvLINE(gv) = PL_curcop->cop_line;
296 Perl_save_ary(pTHX_ GV *gv)
302 if (!AvREAL(oav) && AvREIFY(oav))
309 GvAV(gv) = Null(AV*);
312 SvMAGIC(av) = SvMAGIC(oav);
313 SvFLAGS(av) |= SvMAGICAL(oav);
324 Perl_save_hash(pTHX_ GV *gv)
331 SSPUSHPTR(ohv = GvHVn(gv));
334 GvHV(gv) = Null(HV*);
337 SvMAGIC(hv) = SvMAGIC(ohv);
338 SvFLAGS(hv) |= SvMAGICAL(ohv);
349 Perl_save_item(pTHX_ register SV *item)
352 register SV *sv = NEWSV(0,0);
356 SSPUSHPTR(item); /* remember the pointer */
357 SSPUSHPTR(sv); /* remember the value */
358 SSPUSHINT(SAVEt_ITEM);
362 Perl_save_int(pTHX_ int *intp)
368 SSPUSHINT(SAVEt_INT);
372 Perl_save_long(pTHX_ long int *longp)
378 SSPUSHINT(SAVEt_LONG);
382 Perl_save_I32(pTHX_ I32 *intp)
388 SSPUSHINT(SAVEt_I32);
392 Perl_save_I16(pTHX_ I16 *intp)
398 SSPUSHINT(SAVEt_I16);
402 Perl_save_iv(pTHX_ IV *ivp)
411 /* Cannot use save_sptr() to store a char* since the SV** cast will
412 * force word-alignment and we'll miss the pointer.
415 Perl_save_pptr(pTHX_ char **pptr)
421 SSPUSHINT(SAVEt_PPTR);
425 Perl_save_sptr(pTHX_ SV **sptr)
431 SSPUSHINT(SAVEt_SPTR);
435 Perl_save_threadsv(pTHX_ PADOFFSET i)
439 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
440 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
441 i, svp, *svp, SvPEEK(*svp)));
445 Perl_croak(aTHX_ "panic: save_threadsv called in non-threaded perl");
447 #endif /* USE_THREADS */
451 Perl_save_nogv(pTHX_ GV *gv)
456 SSPUSHINT(SAVEt_NSTAB);
460 Perl_save_hptr(pTHX_ HV **hptr)
466 SSPUSHINT(SAVEt_HPTR);
470 Perl_save_aptr(pTHX_ AV **aptr)
476 SSPUSHINT(SAVEt_APTR);
480 Perl_save_freesv(pTHX_ SV *sv)
485 SSPUSHINT(SAVEt_FREESV);
489 Perl_save_freeop(pTHX_ OP *o)
494 SSPUSHINT(SAVEt_FREEOP);
498 Perl_save_freepv(pTHX_ char *pv)
503 SSPUSHINT(SAVEt_FREEPV);
507 Perl_save_clearsv(pTHX_ SV **svp)
511 SSPUSHLONG((long)(svp-PL_curpad));
512 SSPUSHINT(SAVEt_CLEARSV);
516 Perl_save_delete(pTHX_ HV *hv, char *key, I32 klen)
522 SSPUSHPTR(SvREFCNT_inc(hv));
523 SSPUSHINT(SAVEt_DELETE);
527 Perl_save_list(pTHX_ register SV **sarg, I32 maxsarg)
533 for (i = 1; i <= maxsarg; i++) {
535 sv_setsv(sv,sarg[i]);
537 SSPUSHPTR(sarg[i]); /* remember the pointer */
538 SSPUSHPTR(sv); /* remember the value */
539 SSPUSHINT(SAVEt_ITEM);
544 Perl_save_destructor(pTHX_ DESTRUCTORFUNC_t f, void* p)
550 SSPUSHINT(SAVEt_DESTRUCTOR);
554 Perl_save_aelem(pTHX_ AV *av, I32 idx, SV **sptr)
558 SSPUSHPTR(SvREFCNT_inc(av));
560 SSPUSHPTR(SvREFCNT_inc(*sptr));
561 SSPUSHINT(SAVEt_AELEM);
562 save_scalar_at(sptr);
566 Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
570 SSPUSHPTR(SvREFCNT_inc(hv));
571 SSPUSHPTR(SvREFCNT_inc(key));
572 SSPUSHPTR(SvREFCNT_inc(*sptr));
573 SSPUSHINT(SAVEt_HELEM);
574 save_scalar_at(sptr);
587 Perl_save_alloc(pTHX_ I32 size, I32 pad)
590 register I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
591 - (char*)PL_savestack);
592 register I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
594 /* SSCHECK may not be good enough */
595 while (PL_savestack_ix + elems + 2 > PL_savestack_max)
598 PL_savestack_ix += elems;
600 SSPUSHINT(SAVEt_ALLOC);
605 Perl_leave_scope(pTHX_ I32 base)
617 Perl_croak(aTHX_ "panic: corrupt saved stack index");
618 while (PL_savestack_ix > base) {
620 case SAVEt_ITEM: /* normal string */
621 value = (SV*)SSPOPPTR;
623 sv_replace(sv,value);
628 case SAVEt_SV: /* scalar reference */
629 value = (SV*)SSPOPPTR;
634 case SAVEt_GENERIC_SVREF: /* generic sv */
635 value = (SV*)SSPOPPTR;
644 case SAVEt_SVREF: /* scalar reference */
645 value = (SV*)SSPOPPTR;
649 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
650 "restore svref: %p %p:%s -> %p:%s\n",
651 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
652 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
653 SvTYPE(sv) != SVt_PVGV)
655 (void)SvUPGRADE(value, SvTYPE(sv));
656 SvMAGIC(value) = SvMAGIC(sv);
657 SvFLAGS(value) |= SvMAGICAL(sv);
661 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
662 SvTYPE(value) != SVt_PVGV)
664 SvFLAGS(value) |= (SvFLAGS(value) &
665 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
666 SvMAGICAL_off(value);
676 case SAVEt_AV: /* array reference */
680 AV *goner = GvAV(gv);
681 SvMAGIC(av) = SvMAGIC(goner);
682 SvFLAGS(av) |= SvMAGICAL(goner);
683 SvMAGICAL_off(goner);
694 case SAVEt_HV: /* hash reference */
698 HV *goner = GvHV(gv);
699 SvMAGIC(hv) = SvMAGIC(goner);
700 SvFLAGS(hv) |= SvMAGICAL(goner);
701 SvMAGICAL_off(goner);
712 case SAVEt_INT: /* int reference */
714 *(int*)ptr = (int)SSPOPINT;
716 case SAVEt_LONG: /* long reference */
718 *(long*)ptr = (long)SSPOPLONG;
720 case SAVEt_I32: /* I32 reference */
722 *(I32*)ptr = (I32)SSPOPINT;
724 case SAVEt_I16: /* I16 reference */
726 *(I16*)ptr = (I16)SSPOPINT;
728 case SAVEt_IV: /* IV reference */
730 *(IV*)ptr = (IV)SSPOPIV;
732 case SAVEt_SPTR: /* SV* reference */
734 *(SV**)ptr = (SV*)SSPOPPTR;
736 case SAVEt_PPTR: /* char* reference */
738 *(char**)ptr = (char*)SSPOPPTR;
740 case SAVEt_HPTR: /* HV* reference */
742 *(HV**)ptr = (HV*)SSPOPPTR;
744 case SAVEt_APTR: /* AV* reference */
746 *(AV**)ptr = (AV*)SSPOPPTR;
750 (void)sv_clear((SV*)gv);
752 case SAVEt_GP: /* scalar reference */
755 if (SvPVX(gv) && SvLEN(gv) > 0) {
758 SvPVX(gv) = (char *)SSPOPPTR;
759 SvCUR(gv) = (STRLEN)SSPOPIV;
760 SvLEN(gv) = (STRLEN)SSPOPIV;
764 PL_sub_generation++; /* putting a method back into circulation */
769 SvREFCNT_dec((SV*)ptr);
774 PL_curpad = AvARRAY(PL_comppad);
779 Safefree((char*)ptr);
782 ptr = (void*)&PL_curpad[SSPOPLONG];
784 /* Can clear pad variable in place? */
785 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
786 if (SvTHINKFIRST(sv))
791 switch (SvTYPE(sv)) {
801 Perl_croak(aTHX_ "panic: leave_scope pad code");
813 else { /* Someone has a claim on this, so abandon it. */
814 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
815 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
816 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
817 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
818 default: *(SV**)ptr = NEWSV(0,0); break;
820 SvREFCNT_dec(sv); /* Cast current value to the winds. */
821 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
828 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
832 case SAVEt_DESTRUCTOR:
834 CALLDESTRUCTOR(aTHXo_ ptr);
836 case SAVEt_REGCONTEXT:
839 PL_savestack_ix -= i; /* regexp must have croaked */
841 case SAVEt_STACK_POS: /* Position on Perl stack */
843 PL_stack_sp = PL_stack_base + i;
845 case SAVEt_AELEM: /* array element */
846 value = (SV*)SSPOPPTR;
849 ptr = av_fetch(av,i,1);
852 if (sv && sv != &PL_sv_undef) {
853 if (SvTIED_mg((SV*)av, 'P'))
854 (void)SvREFCNT_inc(sv);
862 case SAVEt_HELEM: /* hash element */
863 value = (SV*)SSPOPPTR;
866 ptr = hv_fetch_ent(hv, sv, 1, 0);
868 SV *oval = HeVAL((HE*)ptr);
869 if (oval && oval != &PL_sv_undef) {
870 ptr = &HeVAL((HE*)ptr);
871 if (SvTIED_mg((SV*)hv, 'P'))
872 (void)SvREFCNT_inc(*(SV**)ptr);
883 PL_op = (OP*)SSPOPPTR;
886 if (GvHV(PL_hintgv)) {
887 SvREFCNT_dec((SV*)GvHV(PL_hintgv));
888 GvHV(PL_hintgv) = NULL;
890 *(I32*)&PL_hints = (I32)SSPOPINT;
893 Perl_croak(aTHX_ "panic: leave_scope inconsistency");
899 Perl_cx_dump(pTHX_ PERL_CONTEXT *cx)
903 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), PL_block_type[CxTYPE(cx)]);
904 if (CxTYPE(cx) != CXt_SUBST) {
905 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
906 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
907 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
908 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
909 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
910 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
911 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
913 switch (CxTYPE(cx)) {
918 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
919 (long)cx->blk_sub.cv);
920 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
921 (long)cx->blk_sub.gv);
922 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
923 (long)cx->blk_sub.dfoutgv);
924 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
925 (long)cx->blk_sub.olddepth);
926 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
927 (int)cx->blk_sub.hasargs);
930 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
931 (long)cx->blk_eval.old_in_eval);
932 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
933 PL_op_name[cx->blk_eval.old_op_type],
934 PL_op_desc[cx->blk_eval.old_op_type]);
935 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
936 cx->blk_eval.old_name);
937 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
938 (long)cx->blk_eval.old_eval_root);
942 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
944 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
945 (long)cx->blk_loop.resetsp);
946 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
947 (long)cx->blk_loop.redo_op);
948 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
949 (long)cx->blk_loop.next_op);
950 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
951 (long)cx->blk_loop.last_op);
952 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
953 (long)cx->blk_loop.iterix);
954 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
955 (long)cx->blk_loop.iterary);
956 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
957 (long)cx->blk_loop.itervar);
958 if (cx->blk_loop.itervar)
959 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
960 (long)cx->blk_loop.itersave);
961 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
962 (long)cx->blk_loop.iterlval);
966 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
968 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
969 (long)cx->sb_maxiters);
970 PerlIO_printf(Perl_debug_log, "SB_RFLAGS = %ld\n",
971 (long)cx->sb_rflags);
972 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
974 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
976 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
978 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
980 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
982 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
984 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
985 (long)cx->sb_strend);
986 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",
990 #endif /* DEBUGGING */