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)
23 av_extend(curstack, (p - stack_base) + (n) + 128);
31 cxstack_max = cxstack_max * 3 / 2;
32 Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */
33 return cxstack_ix + 1;
37 push_return(OP *retop)
40 if (retstack_ix == retstack_max) {
41 retstack_max = retstack_max * 3 / 2;
42 Renew(retstack, retstack_max, OP*);
44 retstack[retstack_ix++] = retop;
52 return retstack[--retstack_ix];
61 if (scopestack_ix == scopestack_max) {
62 scopestack_max = scopestack_max * 3 / 2;
63 Renew(scopestack, scopestack_max, I32);
65 scopestack[scopestack_ix++] = savestack_ix;
73 I32 oldsave = scopestack[--scopestack_ix];
81 I32 oldmax = markstack_max - markstack;
82 I32 newmax = oldmax * 3 / 2;
84 Renew(markstack, newmax, I32);
85 markstack_ptr = markstack + oldmax;
86 markstack_max = markstack + newmax;
93 savestack_max = savestack_max * 3 / 2;
94 Renew(savestack, savestack_max, ANY);
101 /* XXX should tmps_floor live in cxstack? */
102 I32 myfloor = tmps_floor;
103 while (tmps_ix > myfloor) { /* clean up after last statement */
104 SV* sv = tmps_stack[tmps_ix];
105 tmps_stack[tmps_ix--] = Nullsv;
110 SvREFCNT_dec(sv); /* note, can modify tmps_ix!!! */
116 save_scalar_at(SV **sptr)
122 sv = *sptr = NEWSV(0,0);
123 if (SvTYPE(osv) >= SVt_PVMG && SvMAGIC(osv) && SvTYPE(osv) != SVt_PVGV) {
124 sv_upgrade(sv, SvTYPE(osv));
125 if (SvGMAGICAL(osv)) {
127 bool oldtainted = tainted;
129 if (tainting && tainted && (mg = mg_find(osv, 't'))) {
130 SAVESPTR(mg->mg_obj);
133 SvFLAGS(osv) |= (SvFLAGS(osv) &
134 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
135 tainted = oldtainted;
137 SvMAGIC(sv) = SvMAGIC(osv);
138 SvFLAGS(sv) |= SvMAGICAL(osv);
154 return save_scalar_at(&GvSV(gv));
158 save_svref(SV **sptr)
164 SSPUSHINT(SAVEt_SVREF);
165 return save_scalar_at(sptr);
169 save_gp(GV *gv, I32 empty)
173 SSPUSHIV((IV)SvLEN(gv));
174 SvLEN(gv) = 0; /* forget that anything was allocated here */
175 SSPUSHIV((IV)SvCUR(gv));
176 SSPUSHPTR(SvPVX(gv));
178 SSPUSHPTR(SvREFCNT_inc(gv));
184 Newz(602, gp, 1, GP);
185 GvGP(gv) = gp_ref(gp);
186 GvSV(gv) = NEWSV(72,0);
187 GvLINE(gv) = curcop->cop_line;
204 SSPUSHPTR(oav = GvAVn(gv));
207 GvAV(gv) = Null(AV*);
210 SvMAGIC(av) = SvMAGIC(oav);
211 SvFLAGS(av) |= SvMAGICAL(oav);
229 SSPUSHPTR(ohv = GvHVn(gv));
232 GvHV(gv) = Null(HV*);
235 SvMAGIC(hv) = SvMAGIC(ohv);
236 SvFLAGS(hv) |= SvMAGICAL(ohv);
247 save_item(register SV *item)
253 SSPUSHPTR(item); /* remember the pointer */
256 SSPUSHPTR(sv); /* remember the value */
257 SSPUSHINT(SAVEt_ITEM);
267 SSPUSHINT(SAVEt_INT);
271 save_long(long int *longp)
277 SSPUSHINT(SAVEt_LONG);
287 SSPUSHINT(SAVEt_I32);
297 SSPUSHINT(SAVEt_I16);
310 /* Cannot use save_sptr() to store a char* since the SV** cast will
311 * force word-alignment and we'll miss the pointer.
314 save_pptr(char **pptr)
320 SSPUSHINT(SAVEt_PPTR);
330 SSPUSHINT(SAVEt_SPTR);
334 save_threadsv(PADOFFSET i)
338 SV **svp = &THREADSV(i); /* XXX Change to save by offset */
339 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "save_threadsv %u: %p %p:%s\n",
340 i, svp, *svp, SvPEEK(*svp)));
344 croak("panic: save_threadsv called in non-threaded perl");
346 #endif /* USE_THREADS */
355 SSPUSHINT(SAVEt_NSTAB);
365 SSPUSHINT(SAVEt_HPTR);
375 SSPUSHINT(SAVEt_APTR);
384 SSPUSHINT(SAVEt_FREESV);
393 SSPUSHINT(SAVEt_FREEOP);
397 save_freepv(char *pv)
402 SSPUSHINT(SAVEt_FREEPV);
406 save_clearsv(SV **svp)
410 SSPUSHLONG((long)(svp-curpad));
411 SSPUSHINT(SAVEt_CLEARSV);
415 save_delete(HV *hv, char *key, I32 klen)
422 SSPUSHINT(SAVEt_DELETE);
426 save_list(register SV **sarg, I32 maxsarg)
432 SSCHECK(3 * maxsarg);
433 for (i = 1; i <= maxsarg; i++) {
434 SSPUSHPTR(sarg[i]); /* remember the pointer */
436 sv_setsv(sv,sarg[i]);
437 SSPUSHPTR(sv); /* remember the value */
438 SSPUSHINT(SAVEt_ITEM);
443 save_destructor(void (*f) (void *), void *p)
449 SSPUSHINT(SAVEt_DESTRUCTOR);
462 leave_scope(I32 base)
473 croak("panic: corrupt saved stack index");
474 while (savestack_ix > base) {
476 case SAVEt_ITEM: /* normal string */
477 value = (SV*)SSPOPPTR;
479 sv_replace(sv,value);
484 case SAVEt_SV: /* scalar reference */
485 value = (SV*)SSPOPPTR;
489 case SAVEt_SVREF: /* scalar reference */
490 value = (SV*)SSPOPPTR;
494 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
495 "restore svref: %p %p:%s -> %p:%s\n",
496 ptr, sv, SvPEEK(sv), value, SvPEEK(value)));
497 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) &&
498 SvTYPE(sv) != SVt_PVGV)
500 (void)SvUPGRADE(value, SvTYPE(sv));
501 SvMAGIC(value) = SvMAGIC(sv);
502 SvFLAGS(value) |= SvMAGICAL(sv);
506 else if (SvTYPE(value) >= SVt_PVMG && SvMAGIC(value) &&
507 SvTYPE(value) != SVt_PVGV)
509 SvFLAGS(value) |= (SvFLAGS(value) &
510 (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
511 SvMAGICAL_off(value);
520 case SAVEt_AV: /* array reference */
524 AV *goner = GvAV(gv);
525 SvMAGIC(av) = SvMAGIC(goner);
526 SvFLAGS(av) |= SvMAGICAL(goner);
527 SvMAGICAL_off(goner);
538 case SAVEt_HV: /* hash reference */
542 HV *goner = GvHV(gv);
543 SvMAGIC(hv) = SvMAGIC(goner);
544 SvFLAGS(hv) |= SvMAGICAL(goner);
545 SvMAGICAL_off(goner);
556 case SAVEt_INT: /* int reference */
558 *(int*)ptr = (int)SSPOPINT;
560 case SAVEt_LONG: /* long reference */
562 *(long*)ptr = (long)SSPOPLONG;
564 case SAVEt_I32: /* I32 reference */
566 *(I32*)ptr = (I32)SSPOPINT;
568 case SAVEt_I16: /* I16 reference */
570 *(I16*)ptr = (I16)SSPOPINT;
572 case SAVEt_IV: /* IV reference */
574 *(IV*)ptr = (IV)SSPOPIV;
576 case SAVEt_SPTR: /* SV* reference */
578 *(SV**)ptr = (SV*)SSPOPPTR;
580 case SAVEt_PPTR: /* char* reference */
582 *(char**)ptr = (char*)SSPOPPTR;
584 case SAVEt_HPTR: /* HV* reference */
586 *(HV**)ptr = (HV*)SSPOPPTR;
588 case SAVEt_APTR: /* AV* reference */
590 *(AV**)ptr = (AV*)SSPOPPTR;
594 (void)sv_clear((SV*)gv);
596 case SAVEt_GP: /* scalar reference */
601 if (SvPOK(gv) && SvLEN(gv) > 0) {
604 SvPVX(gv) = (char *)SSPOPPTR;
605 SvCUR(gv) = (STRLEN)SSPOPIV;
606 SvLEN(gv) = (STRLEN)SSPOPIV;
611 SvREFCNT_dec((SV*)ptr);
616 curpad = AvARRAY(comppad);
621 Safefree((char*)ptr);
624 ptr = (void*)&curpad[SSPOPLONG];
626 /* Can clear pad variable in place? */
627 if (SvREFCNT(sv) <= 1 && !SvOBJECT(sv)) {
628 if (SvTHINKFIRST(sv)) {
630 croak("panic: leave_scope clearsv");
637 switch (SvTYPE(sv)) {
647 croak("panic: leave_scope pad code");
659 else { /* Someone has a claim on this, so abandon it. */
660 U32 padflags = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
661 switch (SvTYPE(sv)) { /* Console ourselves with a new value */
662 case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
663 case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
664 default: *(SV**)ptr = NEWSV(0,0); break;
666 SvREFCNT_dec(sv); /* Cast current value to the winds. */
667 SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
674 (void)hv_delete(hv, (char*)ptr, (U32)SSPOPINT, G_DISCARD);
677 case SAVEt_DESTRUCTOR:
681 case SAVEt_REGCONTEXT:
683 I32 delta = SSPOPINT;
684 savestack_ix -= delta; /* regexp must have croaked */
687 case SAVEt_STACK_POS: /* Position on Perl stack */
689 I32 delta = SSPOPINT;
690 stack_sp = stack_base + delta;
697 croak("panic: leave_scope inconsistency");
705 cx_dump(PERL_CONTEXT *cx)
708 PerlIO_printf(Perl_debug_log, "CX %ld = %s\n", (long)(cx - cxstack), block_type[cx->cx_type]);
709 if (cx->cx_type != CXt_SUBST) {
710 PerlIO_printf(Perl_debug_log, "BLK_OLDSP = %ld\n", (long)cx->blk_oldsp);
711 PerlIO_printf(Perl_debug_log, "BLK_OLDCOP = 0x%lx\n", (long)cx->blk_oldcop);
712 PerlIO_printf(Perl_debug_log, "BLK_OLDMARKSP = %ld\n", (long)cx->blk_oldmarksp);
713 PerlIO_printf(Perl_debug_log, "BLK_OLDSCOPESP = %ld\n", (long)cx->blk_oldscopesp);
714 PerlIO_printf(Perl_debug_log, "BLK_OLDRETSP = %ld\n", (long)cx->blk_oldretsp);
715 PerlIO_printf(Perl_debug_log, "BLK_OLDPM = 0x%lx\n", (long)cx->blk_oldpm);
716 PerlIO_printf(Perl_debug_log, "BLK_GIMME = %s\n", cx->blk_gimme ? "LIST" : "SCALAR");
718 switch (cx->cx_type) {
723 PerlIO_printf(Perl_debug_log, "BLK_SUB.CV = 0x%lx\n",
724 (long)cx->blk_sub.cv);
725 PerlIO_printf(Perl_debug_log, "BLK_SUB.GV = 0x%lx\n",
726 (long)cx->blk_sub.gv);
727 PerlIO_printf(Perl_debug_log, "BLK_SUB.DFOUTGV = 0x%lx\n",
728 (long)cx->blk_sub.dfoutgv);
729 PerlIO_printf(Perl_debug_log, "BLK_SUB.OLDDEPTH = %ld\n",
730 (long)cx->blk_sub.olddepth);
731 PerlIO_printf(Perl_debug_log, "BLK_SUB.HASARGS = %d\n",
732 (int)cx->blk_sub.hasargs);
735 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_IN_EVAL = %ld\n",
736 (long)cx->blk_eval.old_in_eval);
737 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_OP_TYPE = %s (%s)\n",
738 op_name[cx->blk_eval.old_op_type],
739 op_desc[cx->blk_eval.old_op_type]);
740 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_NAME = %s\n",
741 cx->blk_eval.old_name);
742 PerlIO_printf(Perl_debug_log, "BLK_EVAL.OLD_EVAL_ROOT = 0x%lx\n",
743 (long)cx->blk_eval.old_eval_root);
747 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LABEL = %s\n",
749 PerlIO_printf(Perl_debug_log, "BLK_LOOP.RESETSP = %ld\n",
750 (long)cx->blk_loop.resetsp);
751 PerlIO_printf(Perl_debug_log, "BLK_LOOP.REDO_OP = 0x%lx\n",
752 (long)cx->blk_loop.redo_op);
753 PerlIO_printf(Perl_debug_log, "BLK_LOOP.NEXT_OP = 0x%lx\n",
754 (long)cx->blk_loop.next_op);
755 PerlIO_printf(Perl_debug_log, "BLK_LOOP.LAST_OP = 0x%lx\n",
756 (long)cx->blk_loop.last_op);
757 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERIX = %ld\n",
758 (long)cx->blk_loop.iterix);
759 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERARY = 0x%lx\n",
760 (long)cx->blk_loop.iterary);
761 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERVAR = 0x%lx\n",
762 (long)cx->blk_loop.itervar);
763 if (cx->blk_loop.itervar)
764 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERSAVE = 0x%lx\n",
765 (long)cx->blk_loop.itersave);
766 PerlIO_printf(Perl_debug_log, "BLK_LOOP.ITERLVAL = 0x%lx\n",
767 (long)cx->blk_loop.iterlval);
771 PerlIO_printf(Perl_debug_log, "SB_ITERS = %ld\n",
773 PerlIO_printf(Perl_debug_log, "SB_MAXITERS = %ld\n",
774 (long)cx->sb_maxiters);
775 PerlIO_printf(Perl_debug_log, "SB_SAFEBASE = %ld\n",
776 (long)cx->sb_safebase);
777 PerlIO_printf(Perl_debug_log, "SB_ONCE = %ld\n",
779 PerlIO_printf(Perl_debug_log, "SB_ORIG = %s\n",
781 PerlIO_printf(Perl_debug_log, "SB_DSTR = 0x%lx\n",
783 PerlIO_printf(Perl_debug_log, "SB_TARG = 0x%lx\n",
785 PerlIO_printf(Perl_debug_log, "SB_S = 0x%lx\n",
787 PerlIO_printf(Perl_debug_log, "SB_M = 0x%lx\n",
789 PerlIO_printf(Perl_debug_log, "SB_STREND = 0x%lx\n",
790 (long)cx->sb_strend);
791 PerlIO_printf(Perl_debug_log, "SB_RXRES = 0x%lx\n",