3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
11 /* This file contains the main Perl opcode execution loop. It just
12 * calls the pp_foo() function associated with each op, and expects that
13 * function to return a pointer to the next op to be executed, or null if
14 * it's the end of the sub or program or whatever.
16 * There is a similar loop in dump.c, Perl_runops_debug(), which does
17 * the same, but also checks for various debug flags each time round the
20 * Why this function requires a file all of its own is anybody's guess.
29 * 'Away now, Shadowfax! Run, greatheart, run as you have never run before!
30 * Now we are come to the lands where you were foaled, and every stone you
31 * know. Run now! Hope is in speed!' --Gandalf
33 * [p.600 of _The Lord of the Rings_, III/xi: "The PalantÃr"]
37 Perl_runops_standard(pTHX)
40 PERL_DTRACE_PROBE_OP(op);
41 while ((PL_op = op = op->op_ppaddr(aTHX))) {
42 PERL_DTRACE_PROBE_OP(op);
53 /* this is a wrapper for all runops-style functions. It temporarily
54 * reifies the stack if necessary, then calls the real runops function
57 Perl_runops_wrap(pTHX)
59 /* runops loops assume a ref-counted stack. If we have been called via a
60 * wrapper (pp_wrap or xs_wrap) with the top half of the stack not
61 * reference-counted, or with a non-real stack, temporarily convert it
62 * to reference-counted. This is because the si_stack_nonrc_base
63 * mechanism only allows a single split in the stack, not multiple
65 * At the end, we revert the stack (or part thereof) to non-refcounted
66 * to keep whoever our caller is happy.
68 * If what we call croaks, catch it, revert, then rethrow.
71 I32 cut; /* the cut point between refcnted and non-refcnted */
72 bool was_real = cBOOL(AvREAL(PL_curstack));
73 I32 old_base = PL_curstackinfo->si_stack_nonrc_base;
75 if (was_real && !old_base) {
76 PL_runops(aTHX); /* call the real loop */
82 assert(PL_stack_base + cut <= PL_stack_sp + 1);
83 PL_curstackinfo->si_stack_nonrc_base = 0;
87 assert(!AvREIFY(PL_curstack));
88 AvREAL_on(PL_curstack);
89 /* skip the PL_sv_undef guard at PL_stack_base[0] but still
90 * signal adjusting may be needed on return by setting to a
91 * non-zero value - even if stack is empty */
96 SV **svp = PL_stack_base + cut;
97 while (svp <= PL_stack_sp) {
98 SvREFCNT_inc_simple_void(*svp);
103 AV * old_curstack = PL_curstack;
105 /* run the real loop while catching exceptions */
110 case 0: /* normal return from JMPENV_PUSH */
111 cur_env.je_mustcatch = cur_env.je_prev->je_mustcatch;
112 PL_runops(aTHX); /* call the real loop */
115 /* revert stack back its non-ref-counted state */
116 assert(AvREAL(PL_curstack));
119 /* undo the stack reification that took place at the beginning of
121 if (UNLIKELY(!was_real))
122 AvREAL_off(PL_curstack);
124 SSize_t n = PL_stack_sp - (PL_stack_base + cut) + 1;
126 /* we need to decrement the refcount of every SV from cut
127 * upwards; but this may prematurely free them, so
128 * mortalise them instead */
130 for (SSize_t i = 0; i < n; i ++) {
131 SV* sv = PL_stack_base[cut + i];
133 PL_tmps_stack[++PL_tmps_ix] = sv;
137 I32 sp1 = PL_stack_sp - PL_stack_base + 1;
138 PL_curstackinfo->si_stack_nonrc_base =
139 old_base > sp1 ? sp1 : old_base;
143 case 3: /* exception trapped by eval - stack only partially unwound */
145 /* if the exception has already unwound to before the current
146 * stack, no need to fix it up */
147 if (old_curstack == PL_curstack)
158 JMPENV_JUMP(ret); /* re-throw the exception */
159 NOT_REACHED; /* NOTREACHED */
168 * ex: set ts=8 sts=4 sw=4 et: