3 * Copyright (c) 1991-1997, 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
19 stack_grow(SV **sp, SV **p, int n)
22 #if defined(DEBUGGING) && !defined(USE_THREADS)
23 static int growing = 0;
28 #ifndef STRESS_REALLOC
29 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 128);
31 av_extend(PL_curstack, (p - PL_stack_base) + (n) + 1);
33 #if defined(DEBUGGING) && !defined(USE_THREADS)
39 #ifndef STRESS_REALLOC
40 #define GROW(old) ((old) * 3 / 2)
42 #define GROW(old) ((old) + 1)
46 new_stackinfo(I32 stitems, I32 cxitems)
50 New(56, si, 1, PERL_SI);
51 si->si_stack = newAV();
52 AvREAL_off(si->si_stack);
53 av_extend(si->si_stack, stitems > 0 ? stitems-1 : 0);
54 AvALLOC(si->si_stack)[0] = &PL_sv_undef;
55 AvFILLp(si->si_stack) = 0;
58 si->si_cxmax = cxitems - 1;
60 si->si_type = PERLSI_UNDEF;
61 New(56, si->si_cxstack, cxitems, PERL_CONTEXT);
69 cxstack_max = GROW(cxstack_max);
70 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
71 return cxstack_ix + 1;
75 push_return(OP *retop)
78 if (PL_retstack_ix == PL_retstack_max) {
79 PL_retstack_max = GROW(PL_retstack_max);
80 Renew(PL_retstack, PL_retstack_max, OP*);
82 PL_retstack[PL_retstack_ix++] = retop;
89 if (PL_retstack_ix > 0)
90 return PL_retstack[--PL_retstack_ix];
99 if (PL_scopestack_ix == PL_scopestack_max) {
100 PL_scopestack_max = GROW(PL_scopestack_max);
101 Renew(PL_scopestack, PL_scopestack_max, I32);
103 PL_scopestack[PL_scopestack_ix++] = PL_savestack_ix;
111 I32 oldsave = PL_scopestack[--PL_scopestack_ix];
112 LEAVE_SCOPE(oldsave);
119 I32 oldmax = PL_markstack_max - PL_markstack;
120 I32 newmax = GROW(oldmax);
122 Renew(PL_markstack, newmax, I32);
123 PL_markstack_ptr = PL_markstack + oldmax;
124 PL_markstack_max = PL_markstack + newmax;
131 PL_savestack_max = GROW(PL_savestack_max) + 4;
132 Renew(PL_savestack, PL_savestack_max, ANY);
141 /* XXX should tmps_floor live in cxstack? */
142 I32 myfloor = PL_tmps_floor;
143 while (PL_tmps_ix > myfloor) { /* clean up after last statement */
144 SV* sv = PL_tmps_stack[PL_tmps_ix];
145 PL_tmps_stack[PL_tmps_ix--] = Nullsv;
148 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
154 save_scalar_at(SV **sptr)
160 sv = *sptr = NEWSV(0,0);
161 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
162 sv_upgrade(sv, SvTYPE(osv));
163 if (SvGMAGICAL(osv)) {
165 bool oldtainted = PL_tainted;
167 if (PL_tainting && PL_tainted && (mg = mg_find(osv, 't'))) {
168 SAVESPTR(mg->mg_obj);
171 SvFLAGS(osv) |= (SvFLAGS(osv) &
172 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
173 PL_tainted = oldtainted;
175 SvMAGIC(sv) = SvMAGIC(osv);
176 SvFLAGS(sv) |= SvMAGICAL(osv);
188 SV **sptr = &GvSV(gv);
190 SSPUSHPTR(SvREFCNT_inc(gv));
191 SSPUSHPTR(SvREFCNT_inc(*sptr));
193 return save_scalar_at(sptr);
197 save_svref(SV **sptr)
202 SSPUSHPTR(SvREFCNT_inc(*sptr));
203 SSPUSHINT(SAVEt_SVREF);
204 return save_scalar_at(sptr);
207 /* Like save_svref(), but doesn't deal with magic. Can be used to
208 * restore a global SV to its prior contents, freeing new value. */
210 save_generic_svref(SV **sptr)
215 SSPUSHPTR(SvREFCNT_inc(*sptr));
216 SSPUSHINT(SAVEt_GENERIC_SVREF);
220 save_gp(GV *gv, I32 empty)
224 SSPUSHIV((IV)SvLEN(gv));
225 SvLEN(gv) = 0; /* forget that anything was allocated here */
226 SSPUSHIV((IV)SvCUR(gv));
227 SSPUSHPTR(SvPVX(gv));
229 SSPUSHPTR(SvREFCNT_inc(gv));
237 PL_sub_generation++; /* taking a method out of circulation */
238 Newz(602, gp, 1, GP);
239 GvGP(gv) = gp_ref(gp);
240 GvSV(gv) = NEWSV(72,0);
241 GvLINE(gv) = PL_curcop->cop_line;
257 if (!AvREAL(oav) && AvREIFY(oav))
264 GvAV(gv) = Null(AV*);
267 SvMAGIC(av) = SvMAGIC(oav);
268 SvFLAGS(av) |= SvMAGICAL(oav);
286 SSPUSHPTR(ohv = GvHVn(gv));
289 GvHV(gv) = Null(HV*);
292 SvMAGIC(hv) = SvMAGIC(ohv);
293 SvFLAGS(hv) |= SvMAGICAL(ohv);
304 save_item(register SV *item)
307 register SV *sv = NEWSV(0,0);
311 SSPUSHPTR(item); /* remember the pointer */
312 SSPUSHPTR(sv); /* remember the value */
313 SSPUSHINT(SAVEt_ITEM);
323 SSPUSHINT(SAVEt_INT);
327 save_long(long int *longp)
333 SSPUSHINT(SAVEt_LONG);
343 SSPUSHINT(SAVEt_I32);
353 SSPUSHINT(SAVEt_I16);
366 /* Cannot use save_sptr() to store a char* since the SV** cast will
367 * force word-alignment and we'll miss the pointer.
370 save_pptr(char **pptr)
376 SSPUSHINT(SAVEt_PPTR);
386 SSPUSHINT(SAVEt_SPTR);
390 save_threadsv(PADOFFSET i)
394 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
395 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
396 i, svp, *svp, SvPEEK(*svp)));
400 croak("panic: save_threadsv called in non-threaded perl");
402 #endif /* USE_THREADS */
411 SSPUSHINT(SAVEt_NSTAB);
421 SSPUSHINT(SAVEt_HPTR);
431 SSPUSHINT(SAVEt_APTR);
440 SSPUSHINT(SAVEt_FREESV);
449 SSPUSHINT(SAVEt_FREEOP);
453 save_freepv(char *pv)
458 SSPUSHINT(SAVEt_FREEPV);
462 save_clearsv(SV **svp)
466 SSPUSHLONG((long)(svp-PL_curpad));
467 SSPUSHINT(SAVEt_CLEARSV);
471 save_delete(HV *hv, char *key, I32 klen)
477 SSPUSHPTR(SvREFCNT_inc(hv));
478 SSPUSHINT(SAVEt_DELETE);
482 save_list(register SV **sarg, I32 maxsarg)
488 for (i = 1; i <= maxsarg; i++) {
490 sv_setsv(sv,sarg[i]);
492 SSPUSHPTR(sarg[i]); /* remember the pointer */
493 SSPUSHPTR(sv); /* remember the value */
494 SSPUSHINT(SAVEt_ITEM);
500 save_destructor(DESTRUCTORFUNC f, void* p)
502 save_destructor(void (*f) (void *), void *p)
509 SSPUSHINT(SAVEt_DESTRUCTOR);
513 save_aelem(AV *av, I32 idx, SV **sptr)
517 SSPUSHPTR(SvREFCNT_inc(av));
519 SSPUSHPTR(SvREFCNT_inc(*sptr));
520 SSPUSHINT(SAVEt_AELEM);
521 save_scalar_at(sptr);
525 save_helem(HV *hv, SV *key, SV **sptr)
529 SSPUSHPTR(SvREFCNT_inc(hv));
530 SSPUSHPTR(SvREFCNT_inc(key));
531 SSPUSHPTR(SvREFCNT_inc(*sptr));
532 SSPUSHINT(SAVEt_HELEM);
533 save_scalar_at(sptr);
546 save_alloc(I32 size, I32 pad)
549 register I32 start = pad + ((char*)&PL_savestack[PL_savestack_ix]
550 - (char*)PL_savestack);
551 register I32 elems = 1 + ((size + pad - 1) / sizeof(*PL_savestack));
553 /* SSCHECK may not be good enough */
554 while (PL_savestack_ix + elems + 2 > PL_savestack_max)
557 PL_savestack_ix += elems;
559 SSPUSHINT(SAVEt_ALLOC);
564 leave_scope(I32 base)
576 croak("panic: corrupt saved stack index");
577 while (PL_savestack_ix > base) {
579 case SAVEt_ITEM: /* normal string */
580 value = (SV*)SSPOPPTR;
582 sv_replace(sv,value);
587 case SAVEt_SV: /* scalar reference */
588 value = (SV*)SSPOPPTR;
593 case SAVEt_GENERIC_SVREF: /* generic sv */
594 value = (SV*)SSPOPPTR;
603 case SAVEt_SVREF: /* scalar reference */
604 value = (SV*)SSPOPPTR;
608 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
609 "restore svref: %p %p:%s -> %p:%s\n",
610 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
611 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
612 SvTYPE(sv) != SVt_PVGV)
614 (void)SvUPGRADE(value, SvTYPE(sv));
615 SvMAGIC(value) = SvMAGIC(sv);
616 SvFLAGS(value) |= SvMAGICAL(sv);
620 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
621 SvTYPE(value) != SVt_PVGV)
623 SvFLAGS(value) |= (SvFLAGS(value) &
624 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
625 SvMAGICAL_off(value);
635 case SAVEt_AV: /* array reference */
639 AV *goner = GvAV(gv);
640 SvMAGIC(av) = SvMAGIC(goner);
641 SvFLAGS(av) |= SvMAGICAL(goner);
642 SvMAGICAL_off(goner);
653 case SAVEt_HV: /* hash reference */
657 HV *goner = GvHV(gv);
658 SvMAGIC(hv) = SvMAGIC(goner);
659 SvFLAGS(hv) |= SvMAGICAL(goner);
660 SvMAGICAL_off(goner);
671 case SAVEt_INT: /* int reference */
673 *(int*)ptr = (int)SSPOPINT;
675 case SAVEt_LONG: /* long reference */
677 *(long*)ptr = (long)SSPOPLONG;
679 case SAVEt_I32: /* I32 reference */
681 *(I32*)ptr = (I32)SSPOPINT;
683 case SAVEt_I16: /* I16 reference */
685 *(I16*)ptr = (I16)SSPOPINT;
687 case SAVEt_IV: /* IV reference */
689 *(IV*)ptr = (IV)SSPOPIV;
691 case SAVEt_SPTR: /* SV* reference */
693 *(SV**)ptr = (SV*)SSPOPPTR;
695 case SAVEt_PPTR: /* char* reference */
697 *(char**)ptr = (char*)SSPOPPTR;
699 case SAVEt_HPTR: /* HV* reference */
701 *(HV**)ptr = (HV*)SSPOPPTR;
703 case SAVEt_APTR: /* AV* reference */
705 *(AV**)ptr = (AV*)SSPOPPTR;
709 (void)sv_clear((SV*)gv);
711 case SAVEt_GP: /* scalar reference */
714 if (SvPVX(gv) && SvLEN(gv) > 0) {
717 SvPVX(gv) = (char *)SSPOPPTR;
718 SvCUR(gv) = (STRLEN)SSPOPIV;
719 SvLEN(gv) = (STRLEN)SSPOPIV;
723 PL_sub_generation++; /* putting a method back into circulation */
728 SvREFCNT_dec((SV*)ptr);
733 PL_curpad = AvARRAY(PL_comppad);
738 Safefree((char*)ptr);
741 ptr = (void*)&PL_curpad[SSPOPLONG];
743 /* Can clear pad variable in place? */
744 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
745 if (SvTHINKFIRST(sv)) {
747 croak("panic: leave_scope clearsv");
754 switch (SvTYPE(sv)) {
764 croak("panic: leave_scope pad code");
776 else { /* Someone has a claim on this, so abandon it. */
777 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
778 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
779 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
780 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
781 default: *(SV**)ptr = NEWSV(0,0); break;
783 SvREFCNT_dec(sv); /* Cast current value to the winds. */
784 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
791 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
795 case SAVEt_DESTRUCTOR:
797 (CALLDESTRUCTOR)(ptr);
799 case SAVEt_REGCONTEXT:
802 PL_savestack_ix -= i; /* regexp must have croaked */
804 case SAVEt_STACK_POS: /* Position on Perl stack */
806 PL_stack_sp = PL_stack_base + i;
808 case SAVEt_AELEM: /* array element */
809 value = (SV*)SSPOPPTR;
812 ptr = av_fetch(av,i,1);
815 if (sv && sv != &PL_sv_undef) {
816 if (SvTIED_mg((SV*)av, 'P'))
817 (void)SvREFCNT_inc(sv);
825 case SAVEt_HELEM: /* hash element */
826 value = (SV*)SSPOPPTR;
829 ptr = hv_fetch_ent(hv, sv, 1, 0);
831 SV *oval = HeVAL((HE*)ptr);
832 if (oval && oval != &PL_sv_undef) {
833 ptr = &HeVAL((HE*)ptr);
834 if (SvTIED_mg((SV*)hv, 'P'))
835 (void)SvREFCNT_inc(*(SV**)ptr);
846 PL_op = (OP*)SSPOPPTR;
849 if (GvHV(PL_hintgv)) {
850 SvREFCNT_dec((SV*)GvHV(PL_hintgv));
851 GvHV(PL_hintgv) = NULL;
853 *(I32*)&PL_hints = (I32)SSPOPINT;
856 croak("panic: leave_scope inconsistency");
862 cx_dump(PERL_CONTEXT *cx)
866 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), block_type[CxTYPE(cx)]);
867 if (CxTYPE(cx) != CXt_SUBST) {
868 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
869 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
870 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
871 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
872 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
873 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
874 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
876 switch (CxTYPE(cx)) {
881 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
882 (long)cx->blk_sub.cv);
883 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
884 (long)cx->blk_sub.gv);
885 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
886 (long)cx->blk_sub.dfoutgv);
887 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
888 (long)cx->blk_sub.olddepth);
889 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
890 (int)cx->blk_sub.hasargs);
893 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
894 (long)cx->blk_eval.old_in_eval);
895 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
896 op_name[cx->blk_eval.old_op_type],
897 op_desc[cx->blk_eval.old_op_type]);
898 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
899 cx->blk_eval.old_name);
900 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
901 (long)cx->blk_eval.old_eval_root);
905 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
907 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
908 (long)cx->blk_loop.resetsp);
909 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
910 (long)cx->blk_loop.redo_op);
911 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
912 (long)cx->blk_loop.next_op);
913 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
914 (long)cx->blk_loop.last_op);
915 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
916 (long)cx->blk_loop.iterix);
917 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
918 (long)cx->blk_loop.iterary);
919 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
920 (long)cx->blk_loop.itervar);
921 if (cx->blk_loop.itervar)
922 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
923 (long)cx->blk_loop.itersave);
924 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
925 (long)cx->blk_loop.iterlval);
929 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
931 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
932 (long)cx->sb_maxiters);
933 PerlIO_printf(Perl_debug_log, "SB_SAFEBASE = %ld\n",
934 (long)cx->sb_safebase);
935 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
937 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
939 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
941 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
943 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
945 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
947 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
948 (long)cx->sb_strend);
949 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",
953 #endif /* DEBUGGING */