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_list(register SV **sarg, I32 maxsarg)
442 for (i = 1; i <= maxsarg; i++) {
444 sv_setsv(sv,sarg[i]);
446 SSPUSHPTR(sarg[i]); /* remember the pointer */
447 SSPUSHPTR(sv); /* remember the value */
448 SSPUSHINT(SAVEt_ITEM);
453 save_destructor(void (*f) (void *), void *p)
459 SSPUSHINT(SAVEt_DESTRUCTOR);
472 leave_scope(I32 base)
483 croak("panic: corrupt saved stack index");
484 while (savestack_ix > base) {
486 case SAVEt_ITEM: /* normal string */
487 value = (SV*)SSPOPPTR;
489 sv_replace(sv,value);
494 case SAVEt_SV: /* scalar reference */
495 value = (SV*)SSPOPPTR;
499 case SAVEt_SVREF: /* scalar reference */
500 value = (SV*)SSPOPPTR;
504 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
505 "restore svref: %p %p:%s -> %p:%s\n",
506 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
507 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
508 SvTYPE(sv) != SVt_PVGV)
510 (void)SvUPGRADE(value, SvTYPE(sv));
511 SvMAGIC(value) = SvMAGIC(sv);
512 SvFLAGS(value) |= SvMAGICAL(sv);
516 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
517 SvTYPE(value) != SVt_PVGV)
519 SvFLAGS(value) |= (SvFLAGS(value) &
520 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
521 SvMAGICAL_off(value);
530 case SAVEt_AV: /* array reference */
534 AV *goner = GvAV(gv);
535 SvMAGIC(av) = SvMAGIC(goner);
536 SvFLAGS(av) |= SvMAGICAL(goner);
537 SvMAGICAL_off(goner);
548 case SAVEt_HV: /* hash reference */
552 HV *goner = GvHV(gv);
553 SvMAGIC(hv) = SvMAGIC(goner);
554 SvFLAGS(hv) |= SvMAGICAL(goner);
555 SvMAGICAL_off(goner);
566 case SAVEt_INT: /* int reference */
568 *(int*)ptr = (int)SSPOPINT;
570 case SAVEt_LONG: /* long reference */
572 *(long*)ptr = (long)SSPOPLONG;
574 case SAVEt_I32: /* I32 reference */
576 *(I32*)ptr = (I32)SSPOPINT;
578 case SAVEt_I16: /* I16 reference */
580 *(I16*)ptr = (I16)SSPOPINT;
582 case SAVEt_IV: /* IV reference */
584 *(IV*)ptr = (IV)SSPOPIV;
586 case SAVEt_SPTR: /* SV* reference */
588 *(SV**)ptr = (SV*)SSPOPPTR;
590 case SAVEt_PPTR: /* char* reference */
592 *(char**)ptr = (char*)SSPOPPTR;
594 case SAVEt_HPTR: /* HV* reference */
596 *(HV**)ptr = (HV*)SSPOPPTR;
598 case SAVEt_APTR: /* AV* reference */
600 *(AV**)ptr = (AV*)SSPOPPTR;
604 (void)sv_clear((SV*)gv);
606 case SAVEt_GP: /* scalar reference */
609 if (SvPOK(gv) && SvLEN(gv) > 0) {
612 SvPVX(gv) = (char *)SSPOPPTR;
613 SvCUR(gv) = (STRLEN)SSPOPIV;
614 SvLEN(gv) = (STRLEN)SSPOPIV;
621 SvREFCNT_dec((SV*)ptr);
626 curpad = AvARRAY(comppad);
631 Safefree((char*)ptr);
634 ptr = (void*)&curpad[SSPOPLONG];
636 /* Can clear pad variable in place? */
637 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
638 if (SvTHINKFIRST(sv)) {
640 croak("panic: leave_scope clearsv");
647 switch (SvTYPE(sv)) {
657 croak("panic: leave_scope pad code");
669 else { /* Someone has a claim on this, so abandon it. */
670 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
671 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
672 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
673 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
674 default: *(SV**)ptr = NEWSV(0,0); break;
676 SvREFCNT_dec(sv); /* Cast current value to the winds. */
677 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
684 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
687 case SAVEt_DESTRUCTOR:
691 case SAVEt_REGCONTEXT:
693 I32 delta = SSPOPINT;
694 savestack_ix -= delta; /* regexp must have croaked */
697 case SAVEt_STACK_POS: /* Position on Perl stack */
699 I32 delta = SSPOPINT;
700 stack_sp = stack_base + delta;
707 croak("panic: leave_scope inconsistency");
715 cx_dump(PERL_CONTEXT *cx)
718 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), block_type[cx->cx_type]);
719 if (cx->cx_type != CXt_SUBST) {
720 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
721 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
722 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
723 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
724 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
725 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
726 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
728 switch (cx->cx_type) {
733 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
734 (long)cx->blk_sub.cv);
735 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
736 (long)cx->blk_sub.gv);
737 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
738 (long)cx->blk_sub.dfoutgv);
739 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
740 (long)cx->blk_sub.olddepth);
741 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
742 (int)cx->blk_sub.hasargs);
745 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
746 (long)cx->blk_eval.old_in_eval);
747 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
748 op_name[cx->blk_eval.old_op_type],
749 op_desc[cx->blk_eval.old_op_type]);
750 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
751 cx->blk_eval.old_name);
752 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
753 (long)cx->blk_eval.old_eval_root);
757 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
759 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
760 (long)cx->blk_loop.resetsp);
761 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
762 (long)cx->blk_loop.redo_op);
763 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
764 (long)cx->blk_loop.next_op);
765 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
766 (long)cx->blk_loop.last_op);
767 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
768 (long)cx->blk_loop.iterix);
769 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
770 (long)cx->blk_loop.iterary);
771 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
772 (long)cx->blk_loop.itervar);
773 if (cx->blk_loop.itervar)
774 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
775 (long)cx->blk_loop.itersave);
776 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
777 (long)cx->blk_loop.iterlval);
781 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
783 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
784 (long)cx->sb_maxiters);
785 PerlIO_printf(Perl_debug_log, "SB_SAFEBASE = %ld\n",
786 (long)cx->sb_safebase);
787 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
789 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
791 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
793 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
795 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
797 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
799 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
800 (long)cx->sb_strend);
801 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",