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(curstack, (p - stack_base) + (n) + 128);
31 av_extend(curstack, (p - 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] = &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 (retstack_ix == retstack_max) {
79 retstack_max = GROW(retstack_max);
80 Renew(retstack, retstack_max, OP*);
82 retstack[retstack_ix++] = retop;
90 return retstack[--retstack_ix];
99 if (scopestack_ix == scopestack_max) {
100 scopestack_max = GROW(scopestack_max);
101 Renew(scopestack, scopestack_max, I32);
103 scopestack[scopestack_ix++] = savestack_ix;
111 I32 oldsave = scopestack[--scopestack_ix];
112 LEAVE_SCOPE(oldsave);
119 I32 oldmax = markstack_max - markstack;
120 I32 newmax = GROW(oldmax);
122 Renew(markstack, newmax, I32);
123 markstack_ptr = markstack + oldmax;
124 markstack_max = markstack + newmax;
131 savestack_max = GROW(savestack_max) + 4;
132 Renew(savestack, savestack_max, ANY);
141 /* XXX should tmps_floor live in cxstack? */
142 I32 myfloor = tmps_floor;
143 while (tmps_ix > myfloor) { /* clean up after last statement */
144 SV* sv = tmps_stack[tmps_ix];
145 tmps_stack[tmps_ix--] = Nullsv;
150 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
156 save_scalar_at(SV **sptr)
162 sv = *sptr = NEWSV(0,0);
163 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
164 sv_upgrade(sv, SvTYPE(osv));
165 if (SvGMAGICAL(osv)) {
167 bool oldtainted = tainted;
169 if (tainting && tainted && (mg = mg_find(osv, 't'))) {
170 SAVESPTR(mg->mg_obj);
173 SvFLAGS(osv) |= (SvFLAGS(osv) &
174 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
175 tainted = oldtainted;
177 SvMAGIC(sv) = SvMAGIC(osv);
178 SvFLAGS(sv) |= SvMAGICAL(osv);
190 SV **sptr = &GvSV(gv);
192 SSPUSHPTR(SvREFCNT_inc(gv));
193 SSPUSHPTR(SvREFCNT_inc(*sptr));
195 return save_scalar_at(sptr);
199 save_svref(SV **sptr)
204 SSPUSHPTR(SvREFCNT_inc(*sptr));
205 SSPUSHINT(SAVEt_SVREF);
206 return save_scalar_at(sptr);
210 save_gp(GV *gv, I32 empty)
214 SSPUSHIV((IV)SvLEN(gv));
215 SvLEN(gv) = 0; /* forget that anything was allocated here */
216 SSPUSHIV((IV)SvCUR(gv));
217 SSPUSHPTR(SvPVX(gv));
219 SSPUSHPTR(SvREFCNT_inc(gv));
227 sub_generation++; /* taking a method out of circulation */
228 Newz(602, gp, 1, GP);
229 GvGP(gv) = gp_ref(gp);
230 GvSV(gv) = NEWSV(72,0);
231 GvLINE(gv) = curcop->cop_line;
247 if (!AvREAL(oav) && AvREIFY(oav))
254 GvAV(gv) = Null(AV*);
257 SvMAGIC(av) = SvMAGIC(oav);
258 SvFLAGS(av) |= SvMAGICAL(oav);
276 SSPUSHPTR(ohv = GvHVn(gv));
279 GvHV(gv) = Null(HV*);
282 SvMAGIC(hv) = SvMAGIC(ohv);
283 SvFLAGS(hv) |= SvMAGICAL(ohv);
294 save_item(register SV *item)
297 register SV *sv = NEWSV(0,0);
301 SSPUSHPTR(item); /* remember the pointer */
302 SSPUSHPTR(sv); /* remember the value */
303 SSPUSHINT(SAVEt_ITEM);
313 SSPUSHINT(SAVEt_INT);
317 save_long(long int *longp)
323 SSPUSHINT(SAVEt_LONG);
333 SSPUSHINT(SAVEt_I32);
343 SSPUSHINT(SAVEt_I16);
356 /* Cannot use save_sptr() to store a char* since the SV** cast will
357 * force word-alignment and we'll miss the pointer.
360 save_pptr(char **pptr)
366 SSPUSHINT(SAVEt_PPTR);
376 SSPUSHINT(SAVEt_SPTR);
380 save_threadsv(PADOFFSET i)
384 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
385 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
386 i, svp, *svp, SvPEEK(*svp)));
390 croak("panic: save_threadsv called in non-threaded perl");
392 #endif /* USE_THREADS */
401 SSPUSHINT(SAVEt_NSTAB);
411 SSPUSHINT(SAVEt_HPTR);
421 SSPUSHINT(SAVEt_APTR);
430 SSPUSHINT(SAVEt_FREESV);
439 SSPUSHINT(SAVEt_FREEOP);
443 save_freepv(char *pv)
448 SSPUSHINT(SAVEt_FREEPV);
452 save_clearsv(SV **svp)
456 SSPUSHLONG((long)(svp-curpad));
457 SSPUSHINT(SAVEt_CLEARSV);
461 save_delete(HV *hv, char *key, I32 klen)
467 SSPUSHPTR(SvREFCNT_inc(hv));
468 SSPUSHINT(SAVEt_DELETE);
472 save_list(register SV **sarg, I32 maxsarg)
478 for (i = 1; i <= maxsarg; i++) {
480 sv_setsv(sv,sarg[i]);
482 SSPUSHPTR(sarg[i]); /* remember the pointer */
483 SSPUSHPTR(sv); /* remember the value */
484 SSPUSHINT(SAVEt_ITEM);
490 save_destructor(DESTRUCTORFUNC f, void* p)
492 save_destructor(void (*f) (void *), void *p)
499 SSPUSHINT(SAVEt_DESTRUCTOR);
503 save_aelem(AV *av, I32 idx, SV **sptr)
507 SSPUSHPTR(SvREFCNT_inc(av));
509 SSPUSHPTR(SvREFCNT_inc(*sptr));
510 SSPUSHINT(SAVEt_AELEM);
511 save_scalar_at(sptr);
515 save_helem(HV *hv, SV *key, SV **sptr)
519 SSPUSHPTR(SvREFCNT_inc(hv));
520 SSPUSHPTR(SvREFCNT_inc(key));
521 SSPUSHPTR(SvREFCNT_inc(*sptr));
522 SSPUSHINT(SAVEt_HELEM);
523 save_scalar_at(sptr);
536 leave_scope(I32 base)
548 croak("panic: corrupt saved stack index");
549 while (savestack_ix > base) {
551 case SAVEt_ITEM: /* normal string */
552 value = (SV*)SSPOPPTR;
554 sv_replace(sv,value);
559 case SAVEt_SV: /* scalar reference */
560 value = (SV*)SSPOPPTR;
565 case SAVEt_SVREF: /* scalar reference */
566 value = (SV*)SSPOPPTR;
570 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
571 "restore svref: %p %p:%s -> %p:%s\n",
572 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
573 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
574 SvTYPE(sv) != SVt_PVGV)
576 (void)SvUPGRADE(value, SvTYPE(sv));
577 SvMAGIC(value) = SvMAGIC(sv);
578 SvFLAGS(value) |= SvMAGICAL(sv);
582 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
583 SvTYPE(value) != SVt_PVGV)
585 SvFLAGS(value) |= (SvFLAGS(value) &
586 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
587 SvMAGICAL_off(value);
597 case SAVEt_AV: /* array reference */
601 AV *goner = GvAV(gv);
602 SvMAGIC(av) = SvMAGIC(goner);
603 SvFLAGS(av) |= SvMAGICAL(goner);
604 SvMAGICAL_off(goner);
615 case SAVEt_HV: /* hash reference */
619 HV *goner = GvHV(gv);
620 SvMAGIC(hv) = SvMAGIC(goner);
621 SvFLAGS(hv) |= SvMAGICAL(goner);
622 SvMAGICAL_off(goner);
633 case SAVEt_INT: /* int reference */
635 *(int*)ptr = (int)SSPOPINT;
637 case SAVEt_LONG: /* long reference */
639 *(long*)ptr = (long)SSPOPLONG;
641 case SAVEt_I32: /* I32 reference */
643 *(I32*)ptr = (I32)SSPOPINT;
645 case SAVEt_I16: /* I16 reference */
647 *(I16*)ptr = (I16)SSPOPINT;
649 case SAVEt_IV: /* IV reference */
651 *(IV*)ptr = (IV)SSPOPIV;
653 case SAVEt_SPTR: /* SV* reference */
655 *(SV**)ptr = (SV*)SSPOPPTR;
657 case SAVEt_PPTR: /* char* reference */
659 *(char**)ptr = (char*)SSPOPPTR;
661 case SAVEt_HPTR: /* HV* reference */
663 *(HV**)ptr = (HV*)SSPOPPTR;
665 case SAVEt_APTR: /* AV* reference */
667 *(AV**)ptr = (AV*)SSPOPPTR;
671 (void)sv_clear((SV*)gv);
673 case SAVEt_GP: /* scalar reference */
676 if (SvPOK(gv) && SvLEN(gv) > 0) {
679 SvPVX(gv) = (char *)SSPOPPTR;
680 SvCUR(gv) = (STRLEN)SSPOPIV;
681 SvLEN(gv) = (STRLEN)SSPOPIV;
685 sub_generation++; /* putting a method back into circulation */
690 SvREFCNT_dec((SV*)ptr);
695 curpad = AvARRAY(comppad);
700 Safefree((char*)ptr);
703 ptr = (void*)&curpad[SSPOPLONG];
705 /* Can clear pad variable in place? */
706 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
707 if (SvTHINKFIRST(sv)) {
709 croak("panic: leave_scope clearsv");
716 switch (SvTYPE(sv)) {
726 croak("panic: leave_scope pad code");
738 else { /* Someone has a claim on this, so abandon it. */
739 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
740 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
741 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
742 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
743 default: *(SV**)ptr = NEWSV(0,0); break;
745 SvREFCNT_dec(sv); /* Cast current value to the winds. */
746 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
753 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
757 case SAVEt_DESTRUCTOR:
759 (CALLDESTRUCTOR)(ptr);
761 case SAVEt_REGCONTEXT:
763 savestack_ix -= i; /* regexp must have croaked */
765 case SAVEt_STACK_POS: /* Position on Perl stack */
767 stack_sp = stack_base + i;
769 case SAVEt_AELEM: /* array element */
770 value = (SV*)SSPOPPTR;
773 ptr = av_fetch(av,i,1);
776 if (sv && sv != &sv_undef) {
777 if (SvRMAGICAL(av) && mg_find((SV*)av, 'P'))
778 (void)SvREFCNT_inc(sv);
786 case SAVEt_HELEM: /* hash element */
787 value = (SV*)SSPOPPTR;
790 ptr = hv_fetch_ent(hv, sv, 1, 0);
792 SV *oval = HeVAL((HE*)ptr);
793 if (oval && oval != &sv_undef) {
794 ptr = &HeVAL((HE*)ptr);
795 if (SvRMAGICAL(hv) && mg_find((SV*)hv, 'P'))
796 (void)SvREFCNT_inc(*(SV**)ptr);
811 SvREFCNT_dec((SV*)GvHV(hintgv));
814 *(I32*)&hints = (I32)SSPOPINT;
817 croak("panic: leave_scope inconsistency");
823 cx_dump(PERL_CONTEXT *cx)
827 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), block_type[cx->cx_type]);
828 if (cx->cx_type != CXt_SUBST) {
829 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
830 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
831 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
832 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
833 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
834 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
835 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
837 switch (cx->cx_type) {
842 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
843 (long)cx->blk_sub.cv);
844 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
845 (long)cx->blk_sub.gv);
846 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
847 (long)cx->blk_sub.dfoutgv);
848 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
849 (long)cx->blk_sub.olddepth);
850 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
851 (int)cx->blk_sub.hasargs);
854 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
855 (long)cx->blk_eval.old_in_eval);
856 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
857 op_name[cx->blk_eval.old_op_type],
858 op_desc[cx->blk_eval.old_op_type]);
859 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
860 cx->blk_eval.old_name);
861 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
862 (long)cx->blk_eval.old_eval_root);
866 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
868 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
869 (long)cx->blk_loop.resetsp);
870 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
871 (long)cx->blk_loop.redo_op);
872 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
873 (long)cx->blk_loop.next_op);
874 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
875 (long)cx->blk_loop.last_op);
876 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
877 (long)cx->blk_loop.iterix);
878 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
879 (long)cx->blk_loop.iterary);
880 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
881 (long)cx->blk_loop.itervar);
882 if (cx->blk_loop.itervar)
883 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
884 (long)cx->blk_loop.itersave);
885 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
886 (long)cx->blk_loop.iterlval);
890 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
892 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
893 (long)cx->sb_maxiters);
894 PerlIO_printf(Perl_debug_log, "SB_SAFEBASE = %ld\n",
895 (long)cx->sb_safebase);
896 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
898 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
900 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
902 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
904 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
906 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
908 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
909 (long)cx->sb_strend);
910 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",
914 #endif /* DEBUGGING */