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 av_extend(curstack, (p - stack_base) + (n) + 128);
29 #if defined(DEBUGGING) && !defined(USE_THREADS)
39 cxstack_max = cxstack_max * 3 / 2;
40 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
41 return cxstack_ix + 1;
45 push_return(OP *retop)
48 if (retstack_ix == retstack_max) {
49 retstack_max = retstack_max * 3 / 2;
50 Renew(retstack, retstack_max, OP*);
52 retstack[retstack_ix++] = retop;
60 return retstack[--retstack_ix];
69 if (scopestack_ix == scopestack_max) {
70 scopestack_max = scopestack_max * 3 / 2;
71 Renew(scopestack, scopestack_max, I32);
73 scopestack[scopestack_ix++] = savestack_ix;
81 I32 oldsave = scopestack[--scopestack_ix];
89 I32 oldmax = markstack_max - markstack;
90 I32 newmax = oldmax * 3 / 2;
92 Renew(markstack, newmax, I32);
93 markstack_ptr = markstack + oldmax;
94 markstack_max = markstack + newmax;
101 savestack_max = savestack_max * 3 / 2;
102 Renew(savestack, savestack_max, ANY);
109 /* XXX should tmps_floor live in cxstack? */
110 I32 myfloor = tmps_floor;
111 while (tmps_ix > myfloor) { /* clean up after last statement */
112 SV* sv = tmps_stack[tmps_ix];
113 tmps_stack[tmps_ix--] = Nullsv;
118 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
124 save_scalar_at(SV **sptr)
130 sv = *sptr = NEWSV(0,0);
131 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
132 sv_upgrade(sv, SvTYPE(osv));
133 if (SvGMAGICAL(osv)) {
135 bool oldtainted = tainted;
137 if (tainting && tainted && (mg = mg_find(osv, 't'))) {
138 SAVESPTR(mg->mg_obj);
141 SvFLAGS(osv) |= (SvFLAGS(osv) &
142 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
143 tainted = oldtainted;
145 SvMAGIC(sv) = SvMAGIC(osv);
146 SvFLAGS(sv) |= SvMAGICAL(osv);
162 return save_scalar_at(&GvSV(gv));
166 save_svref(SV **sptr)
172 SSPUSHINT(SAVEt_SVREF);
173 return save_scalar_at(sptr);
177 save_gp(GV *gv, I32 empty)
181 SSPUSHIV((IV)SvLEN(gv));
182 SvLEN(gv) = 0; /* forget that anything was allocated here */
183 SSPUSHIV((IV)SvCUR(gv));
184 SSPUSHPTR(SvPVX(gv));
186 SSPUSHPTR(SvREFCNT_inc(gv));
192 Newz(602, gp, 1, GP);
193 GvGP(gv) = gp_ref(gp);
194 GvSV(gv) = NEWSV(72,0);
195 GvLINE(gv) = curcop->cop_line;
211 if (!AvREAL(oav) && AvREIFY(oav))
218 GvAV(gv) = Null(AV*);
221 SvMAGIC(av) = SvMAGIC(oav);
222 SvFLAGS(av) |= SvMAGICAL(oav);
240 SSPUSHPTR(ohv = GvHVn(gv));
243 GvHV(gv) = Null(HV*);
246 SvMAGIC(hv) = SvMAGIC(ohv);
247 SvFLAGS(hv) |= SvMAGICAL(ohv);
258 save_item(register SV *item)
261 register SV *sv = NEWSV(0,0);
265 SSPUSHPTR(item); /* remember the pointer */
266 SSPUSHPTR(sv); /* remember the value */
267 SSPUSHINT(SAVEt_ITEM);
277 SSPUSHINT(SAVEt_INT);
281 save_long(long int *longp)
287 SSPUSHINT(SAVEt_LONG);
297 SSPUSHINT(SAVEt_I32);
307 SSPUSHINT(SAVEt_I16);
320 /* Cannot use save_sptr() to store a char* since the SV** cast will
321 * force word-alignment and we'll miss the pointer.
324 save_pptr(char **pptr)
330 SSPUSHINT(SAVEt_PPTR);
340 SSPUSHINT(SAVEt_SPTR);
344 save_threadsv(PADOFFSET i)
348 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
349 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
350 i, svp, *svp, SvPEEK(*svp)));
354 croak("panic: save_threadsv called in non-threaded perl");
356 #endif /* USE_THREADS */
365 SSPUSHINT(SAVEt_NSTAB);
375 SSPUSHINT(SAVEt_HPTR);
385 SSPUSHINT(SAVEt_APTR);
394 SSPUSHINT(SAVEt_FREESV);
403 SSPUSHINT(SAVEt_FREEOP);
407 save_freepv(char *pv)
412 SSPUSHINT(SAVEt_FREEPV);
416 save_clearsv(SV **svp)
420 SSPUSHLONG((long)(svp-curpad));
421 SSPUSHINT(SAVEt_CLEARSV);
425 save_delete(HV *hv, char *key, I32 klen)
432 SSPUSHINT(SAVEt_DELETE);
436 save_aelem(AV *av, I32 idx, SV **sptr)
443 SSPUSHINT(SAVEt_AELEM);
444 save_scalar_at(sptr);
448 save_helem(HV *hv, SV *key, SV **sptr)
455 SSPUSHINT(SAVEt_HELEM);
456 save_scalar_at(sptr);
460 save_list(register SV **sarg, I32 maxsarg)
466 for (i = 1; i <= maxsarg; i++) {
468 sv_setsv(sv,sarg[i]);
470 SSPUSHPTR(sarg[i]); /* remember the pointer */
471 SSPUSHPTR(sv); /* remember the value */
472 SSPUSHINT(SAVEt_ITEM);
478 save_destructor(DESTRUCTORFUNC f, void* p)
480 save_destructor(void (*f) (void *), void *p)
487 SSPUSHINT(SAVEt_DESTRUCTOR);
500 leave_scope(I32 base)
512 croak("panic: corrupt saved stack index");
513 while (savestack_ix > base) {
515 case SAVEt_ITEM: /* normal string */
516 value = (SV*)SSPOPPTR;
518 sv_replace(sv,value);
523 case SAVEt_SV: /* scalar reference */
524 value = (SV*)SSPOPPTR;
528 case SAVEt_SVREF: /* scalar reference */
529 value = (SV*)SSPOPPTR;
533 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
534 "restore svref: %p %p:%s -> %p:%s\n",
535 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
536 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
537 SvTYPE(sv) != SVt_PVGV)
539 (void)SvUPGRADE(value, SvTYPE(sv));
540 SvMAGIC(value) = SvMAGIC(sv);
541 SvFLAGS(value) |= SvMAGICAL(sv);
545 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
546 SvTYPE(value) != SVt_PVGV)
548 SvFLAGS(value) |= (SvFLAGS(value) &
549 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
550 SvMAGICAL_off(value);
559 case SAVEt_AV: /* array reference */
563 AV *goner = GvAV(gv);
564 SvMAGIC(av) = SvMAGIC(goner);
565 SvFLAGS(av) |= SvMAGICAL(goner);
566 SvMAGICAL_off(goner);
577 case SAVEt_HV: /* hash reference */
581 HV *goner = GvHV(gv);
582 SvMAGIC(hv) = SvMAGIC(goner);
583 SvFLAGS(hv) |= SvMAGICAL(goner);
584 SvMAGICAL_off(goner);
595 case SAVEt_INT: /* int reference */
597 *(int*)ptr = (int)SSPOPINT;
599 case SAVEt_LONG: /* long reference */
601 *(long*)ptr = (long)SSPOPLONG;
603 case SAVEt_I32: /* I32 reference */
605 *(I32*)ptr = (I32)SSPOPINT;
607 case SAVEt_I16: /* I16 reference */
609 *(I16*)ptr = (I16)SSPOPINT;
611 case SAVEt_IV: /* IV reference */
613 *(IV*)ptr = (IV)SSPOPIV;
615 case SAVEt_SPTR: /* SV* reference */
617 *(SV**)ptr = (SV*)SSPOPPTR;
619 case SAVEt_PPTR: /* char* reference */
621 *(char**)ptr = (char*)SSPOPPTR;
623 case SAVEt_HPTR: /* HV* reference */
625 *(HV**)ptr = (HV*)SSPOPPTR;
627 case SAVEt_APTR: /* AV* reference */
629 *(AV**)ptr = (AV*)SSPOPPTR;
633 (void)sv_clear((SV*)gv);
635 case SAVEt_GP: /* scalar reference */
638 if (SvPOK(gv) && SvLEN(gv) > 0) {
641 SvPVX(gv) = (char *)SSPOPPTR;
642 SvCUR(gv) = (STRLEN)SSPOPIV;
643 SvLEN(gv) = (STRLEN)SSPOPIV;
650 SvREFCNT_dec((SV*)ptr);
655 curpad = AvARRAY(comppad);
660 Safefree((char*)ptr);
663 ptr = (void*)&curpad[SSPOPLONG];
665 /* Can clear pad variable in place? */
666 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
667 if (SvTHINKFIRST(sv)) {
669 croak("panic: leave_scope clearsv");
676 switch (SvTYPE(sv)) {
686 croak("panic: leave_scope pad code");
698 else { /* Someone has a claim on this, so abandon it. */
699 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
700 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
701 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
702 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
703 default: *(SV**)ptr = NEWSV(0,0); break;
705 SvREFCNT_dec(sv); /* Cast current value to the winds. */
706 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
713 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
716 case SAVEt_DESTRUCTOR:
718 (CALLDESTRUCTOR)(ptr);
720 case SAVEt_REGCONTEXT:
722 savestack_ix -= i; /* regexp must have croaked */
724 case SAVEt_STACK_POS: /* Position on Perl stack */
726 stack_sp = stack_base + i;
728 case SAVEt_AELEM: /* array element */
729 value = (SV*)SSPOPPTR;
732 ptr = av_fetch(av,i,1);
734 case SAVEt_HELEM: /* hash element */
735 value = (SV*)SSPOPPTR;
738 ptr = hv_fetch_ent(hv, sv, 1, 0);
739 ptr = &HeVAL((HE*)ptr);
745 croak("panic: leave_scope inconsistency");
753 cx_dump(PERL_CONTEXT *cx)
756 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), block_type[cx->cx_type]);
757 if (cx->cx_type != CXt_SUBST) {
758 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
759 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
760 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
761 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
762 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
763 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
764 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
766 switch (cx->cx_type) {
771 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
772 (long)cx->blk_sub.cv);
773 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
774 (long)cx->blk_sub.gv);
775 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
776 (long)cx->blk_sub.dfoutgv);
777 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
778 (long)cx->blk_sub.olddepth);
779 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
780 (int)cx->blk_sub.hasargs);
783 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
784 (long)cx->blk_eval.old_in_eval);
785 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
786 op_name[cx->blk_eval.old_op_type],
787 op_desc[cx->blk_eval.old_op_type]);
788 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
789 cx->blk_eval.old_name);
790 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
791 (long)cx->blk_eval.old_eval_root);
795 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
797 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
798 (long)cx->blk_loop.resetsp);
799 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
800 (long)cx->blk_loop.redo_op);
801 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
802 (long)cx->blk_loop.next_op);
803 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
804 (long)cx->blk_loop.last_op);
805 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
806 (long)cx->blk_loop.iterix);
807 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
808 (long)cx->blk_loop.iterary);
809 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
810 (long)cx->blk_loop.itervar);
811 if (cx->blk_loop.itervar)
812 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
813 (long)cx->blk_loop.itersave);
814 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
815 (long)cx->blk_loop.iterlval);
819 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
821 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
822 (long)cx->sb_maxiters);
823 PerlIO_printf(Perl_debug_log, "SB_SAFEBASE = %ld\n",
824 (long)cx->sb_safebase);
825 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
827 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
829 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
831 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
833 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
835 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
837 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
838 (long)cx->sb_strend);
839 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",