4c269a72dee217cc9c0abea942612cdd15678e98
[perl.git] / cop.h
1 /*    cop.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
5  *
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.
8  *
9  * Control ops (cops) are one of the two ops OP_NEXTSTATE and OP_DBSTATE,
10  * that (loosely speaking) are separate statements.
11  * They hold information important for lexical state and error reporting.
12  * At run time, PL_curcop is set to point to the most recently executed cop,
13  * and thus can be used to determine our current state.
14  */
15
16 /* A jmpenv packages the state required to perform a proper non-local jump.
17  * Note that there is a PL_start_env initialized when perl starts, and
18  * PL_top_env points to this initially, so PL_top_env should always be
19  * non-null.
20  *
21  * Existence of a non-null PL_top_env->je_prev implies it is valid to call
22  * longjmp() at that runlevel (we make sure PL_start_env.je_prev is always
23  * null to ensure this).
24  *
25  * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
26  * establish a local jmpenv to handle exception traps.  Care must be taken
27  * to restore the previous value of je_mustcatch before exiting the
28  * stack frame iff JMPENV_PUSH was not called in that stack frame.
29  * GSAR 97-03-27
30  */
31
32 struct jmpenv {
33     struct jmpenv *     je_prev;
34     Sigjmp_buf          je_buf;         /* only for use if !je_throw */
35     int                 je_ret;         /* last exception thrown */
36     bool                je_mustcatch;   /* need to call longjmp()? */
37 };
38
39 typedef struct jmpenv JMPENV;
40
41 #ifdef OP_IN_REGISTER
42 #define OP_REG_TO_MEM   PL_opsave = op
43 #define OP_MEM_TO_REG   op = PL_opsave
44 #else
45 #define OP_REG_TO_MEM   NOOP
46 #define OP_MEM_TO_REG   NOOP
47 #endif
48
49 /*
50  * How to build the first jmpenv.
51  *
52  * top_env needs to be non-zero. It points to an area
53  * in which longjmp() stuff is stored, as C callstack
54  * info there at least is thread specific this has to
55  * be per-thread. Otherwise a 'die' in a thread gives
56  * that thread the C stack of last thread to do an eval {}!
57  */
58
59 #define JMPENV_BOOTSTRAP \
60     STMT_START {                                \
61         Zero(&PL_start_env, 1, JMPENV);         \
62         PL_start_env.je_ret = -1;               \
63         PL_start_env.je_mustcatch = TRUE;       \
64         PL_top_env = &PL_start_env;             \
65     } STMT_END
66
67 /*
68  *   PERL_FLEXIBLE_EXCEPTIONS
69  * 
70  * All the flexible exceptions code has been removed.
71  * See the following threads for details:
72  *
73  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-07/msg00378.html
74  * 
75  * Joshua's original patches (which weren't applied) and discussion:
76  * 
77  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01396.html
78  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01489.html
79  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01491.html
80  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01608.html
81  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02144.html
82  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02998.html
83  * 
84  * Chip's reworked patch and discussion:
85  * 
86  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-03/msg00520.html
87  * 
88  * The flaw in these patches (which went unnoticed at the time) was
89  * that they moved some code that could potentially die() out of the
90  * region protected by the setjmp()s.  This caused exceptions within
91  * END blocks and such to not be handled by the correct setjmp().
92  * 
93  * The original patches that introduces flexible exceptions were:
94  *
95  *   http://public.activestate.com/cgi-bin/perlbrowse?patch=3386
96  *   http://public.activestate.com/cgi-bin/perlbrowse?patch=5162
97  */
98
99 #define dJMPENV         JMPENV cur_env
100
101 #define JMPENV_PUSH(v) \
102     STMT_START {                                                        \
103         DEBUG_l({                                                       \
104             int i = 0; JMPENV *p = PL_top_env;                          \
105             while (p) { i++; p = p->je_prev; }                          \
106             Perl_deb(aTHX_ "JUMPENV_PUSH level=%d at %s:%d\n",          \
107                          i,  __FILE__, __LINE__);})                     \
108         cur_env.je_prev = PL_top_env;                                   \
109         OP_REG_TO_MEM;                                                  \
110         cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK);              \
111         OP_MEM_TO_REG;                                                  \
112         PL_top_env = &cur_env;                                          \
113         cur_env.je_mustcatch = FALSE;                                   \
114         (v) = cur_env.je_ret;                                           \
115     } STMT_END
116
117 #define JMPENV_POP \
118     STMT_START {                                                        \
119         DEBUG_l({                                                       \
120             int i = -1; JMPENV *p = PL_top_env;                         \
121             while (p) { i++; p = p->je_prev; }                          \
122             Perl_deb(aTHX_ "JUMPENV_POP level=%d at %s:%d\n",           \
123                          i, __FILE__, __LINE__);})                      \
124         assert(PL_top_env == &cur_env);                                 \
125         PL_top_env = cur_env.je_prev;                                   \
126     } STMT_END
127
128 #define JMPENV_JUMP(v) \
129     STMT_START {                                                \
130         DEBUG_l({                                               \
131             int i = -1; JMPENV *p = PL_top_env;                 \
132             while (p) { i++; p = p->je_prev; }                  \
133             Perl_deb(aTHX_ "JUMPENV_JUMP(%d) level=%d at %s:%d\n", \
134                          (int)v, i, __FILE__, __LINE__);})      \
135         OP_REG_TO_MEM;                                          \
136         if (PL_top_env->je_prev)                                \
137             PerlProc_longjmp(PL_top_env->je_buf, (v));          \
138         if ((v) == 2)                                           \
139             PerlProc_exit(STATUS_EXIT);                         \
140         PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");     \
141         PerlProc_exit(1);                                       \
142     } STMT_END
143
144 #define CATCH_GET               (PL_top_env->je_mustcatch)
145 #define CATCH_SET(v) \
146     STMT_START {                                                        \
147         DEBUG_l(                                                        \
148             Perl_deb(aTHX_                                              \
149                 "JUMPLEVEL set catch %d => %d (for %p) at %s:%d\n",     \
150                  PL_top_env->je_mustcatch, v, (void*)PL_top_env,        \
151                  __FILE__, __LINE__);)                                  \
152         PL_top_env->je_mustcatch = (v);                                 \
153     } STMT_END
154
155
156 #include "mydtrace.h"
157
158 struct cop {
159     BASEOP
160     /* On LP64 putting this here takes advantage of the fact that BASEOP isn't
161        an exact multiple of 8 bytes to save structure padding.  */
162     line_t      cop_line;       /* line # of this command */
163     /* label for this construct is now stored in cop_hints_hash */
164 #ifdef USE_ITHREADS
165     char *      cop_stashpv;    /* package line was compiled in */
166     char *      cop_file;       /* file name the following line # is from */
167 #else
168     HV *        cop_stash;      /* package line was compiled in */
169     GV *        cop_filegv;     /* file the following line # is from */
170 #endif
171     U32         cop_hints;      /* hints bits from pragmata */
172     U32         cop_seq;        /* parse sequence number */
173     /* Beware. mg.c and warnings.pl assume the type of this is STRLEN *:  */
174     STRLEN *    cop_warnings;   /* lexical warnings bitmask */
175     /* compile time state of %^H.  See the comment in op.c for how this is
176        used to recreate a hash to return from caller.  */
177     struct refcounted_he * cop_hints_hash;
178 };
179
180 #ifdef USE_ITHREADS
181 #  define CopFILE(c)            ((c)->cop_file)
182 #  define CopFILEGV(c)          (CopFILE(c) \
183                                  ? gv_fetchfile(CopFILE(c)) : NULL)
184                                  
185 #  ifdef NETWARE
186 #    define CopFILE_set(c,pv)   ((c)->cop_file = savepv(pv))
187 #    define CopFILE_setn(c,pv,l)  ((c)->cop_file = savepv((pv),(l)))
188 #  else
189 #    define CopFILE_set(c,pv)   ((c)->cop_file = savesharedpv(pv))
190 #    define CopFILE_setn(c,pv,l)  ((c)->cop_file = savesharedpvn((pv),(l)))
191 #  endif
192
193 #  define CopFILESV(c)          (CopFILE(c) \
194                                  ? GvSV(gv_fetchfile(CopFILE(c))) : NULL)
195 #  define CopFILEAV(c)          (CopFILE(c) \
196                                  ? GvAV(gv_fetchfile(CopFILE(c))) : NULL)
197 #  ifdef DEBUGGING
198 #    define CopFILEAVx(c)       (assert(CopFILE(c)), \
199                                    GvAV(gv_fetchfile(CopFILE(c))))
200 #  else
201 #    define CopFILEAVx(c)       (GvAV(gv_fetchfile(CopFILE(c))))
202 #  endif
203 #  define CopSTASHPV(c)         ((c)->cop_stashpv)
204
205 #  ifdef NETWARE
206 #    define CopSTASHPV_set(c,pv)        ((c)->cop_stashpv = ((pv) ? savepv(pv) : NULL))
207 #  else
208 #    define CopSTASHPV_set(c,pv)        ((c)->cop_stashpv = savesharedpv(pv))
209 #  endif
210
211 #  define CopSTASH(c)           (CopSTASHPV(c) \
212                                  ? gv_stashpv(CopSTASHPV(c),GV_ADD) : NULL)
213 #  define CopSTASH_set(c,hv)    CopSTASHPV_set(c, (hv) ? HvNAME_get(hv) : NULL)
214 #  define CopSTASH_eq(c,hv)     ((hv) && stashpv_hvname_match(c,hv))
215 #  ifdef NETWARE
216 #    define CopSTASH_free(c) SAVECOPSTASH_FREE(c)
217 #    define CopFILE_free(c) SAVECOPFILE_FREE(c)
218 #  else
219 #    define CopSTASH_free(c)    PerlMemShared_free(CopSTASHPV(c))
220 #    define CopFILE_free(c)     (PerlMemShared_free(CopFILE(c)),(CopFILE(c) = NULL))
221 #  endif
222 #else
223 #  define CopFILEGV(c)          ((c)->cop_filegv)
224 #  define CopFILEGV_set(c,gv)   ((c)->cop_filegv = (GV*)SvREFCNT_inc(gv))
225 #  define CopFILE_set(c,pv)     CopFILEGV_set((c), gv_fetchfile(pv))
226 #  define CopFILE_setn(c,pv,l)  CopFILEGV_set((c), gv_fetchfile_flags((pv),(l),0))
227 #  define CopFILESV(c)          (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : NULL)
228 #  define CopFILEAV(c)          (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : NULL)
229 #  ifdef DEBUGGING
230 #    define CopFILEAVx(c)       (assert(CopFILEGV(c)), GvAV(CopFILEGV(c)))
231 #  else
232 #    define CopFILEAVx(c)       (GvAV(CopFILEGV(c)))
233 # endif
234 #  define CopFILE(c)            (CopFILEGV(c) && GvSV(CopFILEGV(c)) \
235                                     ? SvPVX(GvSV(CopFILEGV(c))) : NULL)
236 #  define CopSTASH(c)           ((c)->cop_stash)
237 #  define CopSTASH_set(c,hv)    ((c)->cop_stash = (hv))
238 #  define CopSTASHPV(c)         (CopSTASH(c) ? HvNAME_get(CopSTASH(c)) : NULL)
239    /* cop_stash is not refcounted */
240 #  define CopSTASHPV_set(c,pv)  CopSTASH_set((c), gv_stashpv(pv,GV_ADD))
241 #  define CopSTASH_eq(c,hv)     (CopSTASH(c) == (hv))
242 #  define CopSTASH_free(c)      
243 #  define CopFILE_free(c)       (SvREFCNT_dec(CopFILEGV(c)),(CopFILEGV(c) = NULL))
244
245 #endif /* USE_ITHREADS */
246 #define CopLABEL(c)  Perl_fetch_cop_label(aTHX_ (c), NULL, NULL)
247 #define CopLABEL_alloc(pv)      ((pv)?savepv(pv):NULL)
248
249 #define CopSTASH_ne(c,hv)       (!CopSTASH_eq(c,hv))
250 #define CopLINE(c)              ((c)->cop_line)
251 #define CopLINE_inc(c)          (++CopLINE(c))
252 #define CopLINE_dec(c)          (--CopLINE(c))
253 #define CopLINE_set(c,l)        (CopLINE(c) = (l))
254
255 /* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */
256 #define OutCopFILE(c) CopFILE(c)
257
258 /* If $[ is non-zero, it's stored in cop_hints under the key "$[", and
259    HINT_ARYBASE is set to indicate this.
260    Setting it is ineficient due to the need to create 2 mortal SVs, but as
261    using $[ is highly discouraged, no sane Perl code will be using it.  */
262 #define CopARYBASE_get(c)       \
263         ((CopHINTS_get(c) & HINT_ARYBASE)                               \
264          ? SvIV(Perl_refcounted_he_fetch(aTHX_ (c)->cop_hints_hash, 0,  \
265                                          "$[", 2, 0, 0))                \
266          : 0)
267 #define CopARYBASE_set(c, b) STMT_START { \
268         if (b || ((c)->cop_hints & HINT_ARYBASE)) {                     \
269             (c)->cop_hints |= HINT_ARYBASE;                             \
270             if ((c) == &PL_compiling) {                                 \
271                 SV *val = newSViv(b);                                   \
272                 (void)hv_stores(GvHV(PL_hintgv), "$[", val);            \
273                 mg_set(val);                                            \
274                 PL_hints |= HINT_ARYBASE;                               \
275             } else {                                                    \
276                 (c)->cop_hints_hash                                     \
277                    = Perl_refcounted_he_new(aTHX_ (c)->cop_hints_hash,  \
278                                         newSVpvs_flags("$[", SVs_TEMP), \
279                                         sv_2mortal(newSViv(b)));        \
280             }                                                           \
281         }                                                               \
282     } STMT_END
283
284 /* FIXME NATIVE_HINTS if this is changed from op_private (see perl.h)  */
285 #define CopHINTS_get(c)         ((c)->cop_hints + 0)
286 #define CopHINTS_set(c, h)      STMT_START {                            \
287                                     (c)->cop_hints = (h);               \
288                                 } STMT_END
289
290 /*
291  * Here we have some enormously heavy (or at least ponderous) wizardry.
292  */
293
294 /* subroutine context */
295 struct block_sub {
296     OP *        retop;  /* op to execute on exit from sub */
297     /* Above here is the same for sub, format and eval.  */
298     CV *        cv;
299     /* Above here is the same for sub and format.  */
300     AV *        savearray;
301     AV *        argarray;
302     I32         olddepth;
303     PAD         *oldcomppad;
304 };
305
306
307 /* format context */
308 struct block_format {
309     OP *        retop;  /* op to execute on exit from sub */
310     /* Above here is the same for sub, format and eval.  */
311     CV *        cv;
312     /* Above here is the same for sub and format.  */
313     GV *        gv;
314     GV *        dfoutgv;
315 };
316
317 /* base for the next two macros. Don't use directly.
318  * Note that the refcnt of the cv is incremented twice;  The CX one is
319  * decremented by LEAVESUB, the other by LEAVE. */
320
321 #define PUSHSUB_BASE(cx)                                                \
322         ENTRY_PROBE(GvENAME(CvGV(cv)),                                  \
323                 CopFILE((const COP *)CvSTART(cv)),                      \
324                 CopLINE((const COP *)CvSTART(cv)));                     \
325                                                                         \
326         cx->blk_sub.cv = cv;                                            \
327         cx->blk_sub.olddepth = CvDEPTH(cv);                             \
328         cx->cx_type |= (hasargs) ? CXp_HASARGS : 0;                     \
329         cx->blk_sub.retop = NULL;                                       \
330         if (!CvDEPTH(cv)) {                                             \
331             SvREFCNT_inc_simple_void_NN(cv);                            \
332             SvREFCNT_inc_simple_void_NN(cv);                            \
333             SAVEFREESV(cv);                                             \
334         }
335
336
337 #define PUSHSUB(cx)                                                     \
338         PUSHSUB_BASE(cx)                                                \
339         cx->blk_u16 = PL_op->op_private &                               \
340                               (OPpLVAL_INTRO|OPpENTERSUB_INARGS);
341
342 /* variant for use by OP_DBSTATE, where op_private holds hint bits */
343 #define PUSHSUB_DB(cx)                                                  \
344         PUSHSUB_BASE(cx)                                                \
345         cx->blk_u16 = 0;
346
347
348 #define PUSHFORMAT(cx, retop)                                           \
349         cx->blk_format.cv = cv;                                         \
350         cx->blk_format.gv = gv;                                         \
351         cx->blk_format.retop = (retop);                                 \
352         cx->blk_format.dfoutgv = PL_defoutgv;                           \
353         SvREFCNT_inc_void(cx->blk_format.dfoutgv)
354
355 #define POP_SAVEARRAY()                                         \
356     STMT_START {                                                        \
357         SvREFCNT_dec(GvAV(PL_defgv));                                   \
358         GvAV(PL_defgv) = cx->blk_sub.savearray;                         \
359     } STMT_END
360
361 /* junk in @_ spells trouble when cloning CVs and in pp_caller(), so don't
362  * leave any (a fast av_clear(ary), basically) */
363 #define CLEAR_ARGARRAY(ary) \
364     STMT_START {                                                        \
365         AvMAX(ary) += AvARRAY(ary) - AvALLOC(ary);                      \
366         AvARRAY(ary) = AvALLOC(ary);                                    \
367         AvFILLp(ary) = -1;                                              \
368     } STMT_END
369
370 #define POPSUB(cx,sv)                                                   \
371     STMT_START {                                                        \
372         RETURN_PROBE(GvENAME(CvGV((const CV*)cx->blk_sub.cv)),          \
373                 CopFILE((COP*)CvSTART((const CV*)cx->blk_sub.cv)),      \
374                 CopLINE((COP*)CvSTART((const CV*)cx->blk_sub.cv)));     \
375                                                                         \
376         if (CxHASARGS(cx)) {                                            \
377             POP_SAVEARRAY();                                            \
378             /* abandon @_ if it got reified */                          \
379             if (AvREAL(cx->blk_sub.argarray)) {                         \
380                 const SSize_t fill = AvFILLp(cx->blk_sub.argarray);     \
381                 SvREFCNT_dec(cx->blk_sub.argarray);                     \
382                 cx->blk_sub.argarray = newAV();                         \
383                 av_extend(cx->blk_sub.argarray, fill);                  \
384                 AvREIFY_only(cx->blk_sub.argarray);                     \
385                 CX_CURPAD_SV(cx->blk_sub, 0) = MUTABLE_SV(cx->blk_sub.argarray); \
386             }                                                           \
387             else {                                                      \
388                 CLEAR_ARGARRAY(cx->blk_sub.argarray);                   \
389             }                                                           \
390         }                                                               \
391         sv = MUTABLE_SV(cx->blk_sub.cv);                                \
392         if (sv && (CvDEPTH((const CV*)sv) = cx->blk_sub.olddepth))      \
393             sv = NULL;                                          \
394     } STMT_END
395
396 #define LEAVESUB(sv)                                                    \
397     STMT_START {                                                        \
398         if (sv)                                                         \
399             SvREFCNT_dec(sv);                                           \
400     } STMT_END
401
402 #define POPFORMAT(cx)                                                   \
403         setdefout(cx->blk_format.dfoutgv);                              \
404         SvREFCNT_dec(cx->blk_format.dfoutgv);
405
406 /* eval context */
407 struct block_eval {
408     OP *        retop;  /* op to execute on exit from eval */
409     /* Above here is the same for sub, format and eval.  */
410     SV *        old_namesv;
411     OP *        old_eval_root;
412     SV *        cur_text;
413     CV *        cv;
414     JMPENV *    cur_top_env; /* value of PL_top_env when eval CX created */
415 };
416
417 /* If we ever need more than 512 op types, change the shift from 7.
418    blku_gimme is actually also only 2 bits, so could be merged with something.
419 */
420
421 #define CxOLD_IN_EVAL(cx)       (((cx)->blk_u16) & 0x7F)
422 #define CxOLD_OP_TYPE(cx)       (((cx)->blk_u16) >> 7)
423
424 #define PUSHEVAL(cx,n)                                                  \
425     STMT_START {                                                        \
426         assert(!(PL_in_eval & ~0x7F));                                  \
427         assert(!(PL_op->op_type & ~0x1FF));                             \
428         cx->blk_u16 = (PL_in_eval & 0x7F) | ((U16)PL_op->op_type << 7); \
429         cx->blk_eval.old_namesv = (n ? newSVpv(n,0) : NULL);            \
430         cx->blk_eval.old_eval_root = PL_eval_root;                      \
431         cx->blk_eval.cur_text = PL_parser ? PL_parser->linestr : NULL;  \
432         cx->blk_eval.cv = NULL; /* set by doeval(), as applicable */    \
433         cx->blk_eval.retop = NULL;                                      \
434         cx->blk_eval.cur_top_env = PL_top_env;                          \
435     } STMT_END
436
437 #define POPEVAL(cx)                                                     \
438     STMT_START {                                                        \
439         PL_in_eval = CxOLD_IN_EVAL(cx);                                 \
440         optype = CxOLD_OP_TYPE(cx);                                     \
441         PL_eval_root = cx->blk_eval.old_eval_root;                      \
442         if (cx->blk_eval.old_namesv)                                    \
443             sv_2mortal(cx->blk_eval.old_namesv);                        \
444     } STMT_END
445
446 /* loop context */
447 struct block_loop {
448     I32         resetsp;
449     LOOP *      my_op;  /* My op, that contains redo, next and last ops.  */
450 #ifdef USE_ITHREADS
451     PAD         *oldcomppad; /* Also used for the GV, if my_op->op_targ is 0 */
452 #else
453     SV **       itervar;
454 #endif
455     union {
456         struct { /* valid if type is LOOP_FOR or LOOP_PLAIN (but {NULL,0})*/
457             AV * ary; /* use the stack if this is NULL */
458             IV ix;
459         } ary;
460         struct { /* valid if type is LOOP_LAZYIV */
461             IV cur;
462             IV end;
463         } lazyiv;
464         struct { /* valid if type if LOOP_LAZYSV */
465             SV * cur;
466             SV * end; /* maxiumum value (or minimum in reverse) */
467         } lazysv;
468     } state_u;
469 };
470
471 #ifdef USE_ITHREADS
472 #  define CxITERVAR(c)                                                  \
473         ((c)->blk_loop.oldcomppad                                       \
474          ? (CxPADLOOP(c)                                                \
475             ? &CX_CURPAD_SV( (c)->blk_loop, (c)->blk_loop.my_op->op_targ) \
476             : &GvSV((GV*)(c)->blk_loop.oldcomppad))                     \
477          : (SV**)NULL)
478 #  define CX_ITERDATA_SET(cx,idata,o)                                   \
479         if (cx->blk_loop.my_op->op_targ)                                \
480             CX_CURPAD_SAVE(cx->blk_loop);                               \
481         else                                                            \
482             cx->blk_loop.oldcomppad = (idata);
483 #else
484 #  define CxITERVAR(c)          ((c)->blk_loop.itervar)
485 #  define CX_ITERDATA_SET(cx,ivar,o)                                    \
486         cx->blk_loop.itervar = (SV**)(ivar);
487 #endif
488 #define CxLABEL(c)      (0 + CopLABEL((c)->blk_oldcop))
489 #define CxHASARGS(c)    (((c)->cx_type & CXp_HASARGS) == CXp_HASARGS)
490 #define CxLVAL(c)       (0 + (c)->blk_u16)
491
492 #define PUSHLOOP_PLAIN(cx, s)                                           \
493         cx->blk_loop.resetsp = s - PL_stack_base;                       \
494         cx->blk_loop.my_op = cLOOP;                                     \
495         cx->blk_loop.state_u.ary.ary = NULL;                            \
496         cx->blk_loop.state_u.ary.ix = 0;                                \
497         CX_ITERDATA_SET(cx, NULL, 0);
498
499 #define PUSHLOOP_FOR(cx, dat, s, offset)                                \
500         cx->blk_loop.resetsp = s - PL_stack_base;                       \
501         cx->blk_loop.my_op = cLOOP;                                     \
502         cx->blk_loop.state_u.ary.ary = NULL;                            \
503         cx->blk_loop.state_u.ary.ix = 0;                                \
504         CX_ITERDATA_SET(cx, dat, offset);
505
506 #define POPLOOP(cx)                                                     \
507         if (CxTYPE(cx) == CXt_LOOP_LAZYSV) {                            \
508             SvREFCNT_dec(cx->blk_loop.state_u.lazysv.cur);              \
509             SvREFCNT_dec(cx->blk_loop.state_u.lazysv.end);              \
510         }                                                               \
511         if (CxTYPE(cx) == CXt_LOOP_FOR)                                 \
512             SvREFCNT_dec(cx->blk_loop.state_u.ary.ary);
513
514 /* given/when context */
515 struct block_givwhen {
516         OP *leave_op;
517 };
518
519 #define PUSHGIVEN(cx)                                                   \
520         cx->blk_givwhen.leave_op = cLOGOP->op_other;
521
522 #define PUSHWHEN PUSHGIVEN
523
524 /* context common to subroutines, evals and loops */
525 struct block {
526     U8          blku_type;      /* what kind of context this is */
527     U8          blku_gimme;     /* is this block running in list context? */
528     U16         blku_u16;       /* used by block_sub and block_eval (so far) */
529     I32         blku_oldsp;     /* stack pointer to copy stuff down to */
530     COP *       blku_oldcop;    /* old curcop pointer */
531     I32         blku_oldmarksp; /* mark stack index */
532     I32         blku_oldscopesp;        /* scope stack index */
533     PMOP *      blku_oldpm;     /* values of pattern match vars */
534
535     union {
536         struct block_sub        blku_sub;
537         struct block_format     blku_format;
538         struct block_eval       blku_eval;
539         struct block_loop       blku_loop;
540         struct block_givwhen    blku_givwhen;
541     } blk_u;
542 };
543 #define blk_oldsp       cx_u.cx_blk.blku_oldsp
544 #define blk_oldcop      cx_u.cx_blk.blku_oldcop
545 #define blk_oldmarksp   cx_u.cx_blk.blku_oldmarksp
546 #define blk_oldscopesp  cx_u.cx_blk.blku_oldscopesp
547 #define blk_oldpm       cx_u.cx_blk.blku_oldpm
548 #define blk_gimme       cx_u.cx_blk.blku_gimme
549 #define blk_u16         cx_u.cx_blk.blku_u16
550 #define blk_sub         cx_u.cx_blk.blk_u.blku_sub
551 #define blk_format      cx_u.cx_blk.blk_u.blku_format
552 #define blk_eval        cx_u.cx_blk.blk_u.blku_eval
553 #define blk_loop        cx_u.cx_blk.blk_u.blku_loop
554 #define blk_givwhen     cx_u.cx_blk.blk_u.blku_givwhen
555
556 #define DEBUG_CX(action)                                                \
557     DEBUG_l(                                                            \
558         Perl_deb(aTHX_ "CX %ld %s %s (scope %ld,%ld) at %s:%d\n",       \
559                     (long)cxstack_ix,                                   \
560                     action,                                             \
561                     PL_block_type[CxTYPE(&cxstack[cxstack_ix])],        \
562                     (long)PL_scopestack_ix,                             \
563                     (long)(cxstack[cxstack_ix].blk_oldscopesp),         \
564                     __FILE__, __LINE__));
565
566 /* Enter a block. */
567 #define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix],            \
568         cx->cx_type             = t,                                    \
569         cx->blk_oldsp           = sp - PL_stack_base,                   \
570         cx->blk_oldcop          = PL_curcop,                            \
571         cx->blk_oldmarksp       = PL_markstack_ptr - PL_markstack,      \
572         cx->blk_oldscopesp      = PL_scopestack_ix,                     \
573         cx->blk_oldpm           = PL_curpm,                             \
574         cx->blk_gimme           = (U8)gimme;                            \
575         DEBUG_CX("PUSH");
576
577 /* Exit a block (RETURN and LAST). */
578 #define POPBLOCK(cx,pm)                                                 \
579         DEBUG_CX("POP");                                                \
580         cx = &cxstack[cxstack_ix--],                                    \
581         newsp            = PL_stack_base + cx->blk_oldsp,               \
582         PL_curcop        = cx->blk_oldcop,                              \
583         PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp,            \
584         PL_scopestack_ix = cx->blk_oldscopesp,                          \
585         pm               = cx->blk_oldpm,                               \
586         gimme            = cx->blk_gimme;
587
588 /* Continue a block elsewhere (NEXT and REDO). */
589 #define TOPBLOCK(cx)                                                    \
590         DEBUG_CX("TOP");                                                \
591         cx  = &cxstack[cxstack_ix],                                     \
592         PL_stack_sp      = PL_stack_base + cx->blk_oldsp,               \
593         PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp,            \
594         PL_scopestack_ix = cx->blk_oldscopesp,                          \
595         PL_curpm         = cx->blk_oldpm;
596
597 /* substitution context */
598 struct subst {
599     U8          sbu_type;       /* what kind of context this is */
600     U8          sbu_rflags;
601     U16         sbu_rxtainted;  /* matches struct block */
602     I32         sbu_iters;
603     I32         sbu_maxiters;
604     I32         sbu_oldsave;
605     char *      sbu_orig;
606     SV *        sbu_dstr;
607     SV *        sbu_targ;
608     char *      sbu_s;
609     char *      sbu_m;
610     char *      sbu_strend;
611     void *      sbu_rxres;
612     REGEXP *    sbu_rx;
613 };
614 #define sb_iters        cx_u.cx_subst.sbu_iters
615 #define sb_maxiters     cx_u.cx_subst.sbu_maxiters
616 #define sb_rflags       cx_u.cx_subst.sbu_rflags
617 #define sb_oldsave      cx_u.cx_subst.sbu_oldsave
618 #define sb_once         cx_u.cx_subst.sbu_once
619 #define sb_rxtainted    cx_u.cx_subst.sbu_rxtainted
620 #define sb_orig         cx_u.cx_subst.sbu_orig
621 #define sb_dstr         cx_u.cx_subst.sbu_dstr
622 #define sb_targ         cx_u.cx_subst.sbu_targ
623 #define sb_s            cx_u.cx_subst.sbu_s
624 #define sb_m            cx_u.cx_subst.sbu_m
625 #define sb_strend       cx_u.cx_subst.sbu_strend
626 #define sb_rxres        cx_u.cx_subst.sbu_rxres
627 #define sb_rx           cx_u.cx_subst.sbu_rx
628
629 #ifdef PERL_CORE
630 #  define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix],               \
631         cx->sb_iters            = iters,                                \
632         cx->sb_maxiters         = maxiters,                             \
633         cx->sb_rflags           = r_flags,                              \
634         cx->sb_oldsave          = oldsave,                              \
635         cx->sb_rxtainted        = rxtainted,                            \
636         cx->sb_orig             = orig,                                 \
637         cx->sb_dstr             = dstr,                                 \
638         cx->sb_targ             = targ,                                 \
639         cx->sb_s                = s,                                    \
640         cx->sb_m                = m,                                    \
641         cx->sb_strend           = strend,                               \
642         cx->sb_rxres            = NULL,                                 \
643         cx->sb_rx               = rx,                                   \
644         cx->cx_type             = CXt_SUBST | (once ? CXp_ONCE : 0);    \
645         rxres_save(&cx->sb_rxres, rx);                                  \
646         (void)ReREFCNT_inc(rx)
647
648 #  define POPSUBST(cx) cx = &cxstack[cxstack_ix--];                     \
649         rxres_free(&cx->sb_rxres);                                      \
650         ReREFCNT_dec(cx->sb_rx)
651 #endif
652
653 #define CxONCE(cx)              ((cx)->cx_type & CXp_ONCE)
654
655 struct context {
656     union {
657         struct block    cx_blk;
658         struct subst    cx_subst;
659     } cx_u;
660 };
661 #define cx_type cx_u.cx_subst.sbu_type
662
663 /* If you re-order these, there is also an array of uppercase names in perl.h
664    and a static array of context names in pp_ctl.c  */
665 #define CXTYPEMASK      0xf
666 #define CXt_NULL        0
667 #define CXt_WHEN        1
668 #define CXt_BLOCK       2
669 /* When micro-optimising :-) keep GIVEN next to the LOOPs, as these 5 share a
670    jump table in pp_ctl.c
671    The first 4 don't have a 'case' in at least one switch statement in pp_ctl.c
672 */
673 #define CXt_GIVEN       3
674 /* This is first so that CXt_LOOP_FOR|CXt_LOOP_LAZYIV is CXt_LOOP_LAZYIV */
675 #define CXt_LOOP_FOR    4
676 #define CXt_LOOP_PLAIN  5
677 #define CXt_LOOP_LAZYSV 6
678 #define CXt_LOOP_LAZYIV 7
679 #define CXt_SUB         8
680 #define CXt_FORMAT      9
681 #define CXt_EVAL       10
682 #define CXt_SUBST      11
683 /* SUBST doesn't feature in all switch statements.  */
684
685 /* private flags for CXt_SUB and CXt_NULL
686    However, this is checked in many places which do not check the type, so
687    this bit needs to be kept clear for most everything else. For reasons I
688    haven't investigated, it can coexist with CXp_FOR_DEF */
689 #define CXp_MULTICALL   0x10    /* part of a multicall (so don't
690                                    tear down context on exit). */ 
691
692 /* private flags for CXt_SUB and CXt_FORMAT */
693 #define CXp_HASARGS     0x20
694
695 /* private flags for CXt_EVAL */
696 #define CXp_REAL        0x20    /* truly eval'', not a lookalike */
697 #define CXp_TRYBLOCK    0x40    /* eval{}, not eval'' or similar */
698
699 /* private flags for CXt_LOOP */
700 #define CXp_FOR_DEF     0x10    /* foreach using $_ */
701 #ifdef USE_ITHREADS
702 #  define CxPADLOOP(c)  ((c)->blk_loop.my_op->op_targ)
703 #endif
704
705 /* private flags for CXt_SUBST */
706 #define CXp_ONCE        0x10    /* What was sbu_once in struct subst */
707
708 #define CxTYPE(c)       ((c)->cx_type & CXTYPEMASK)
709 #define CxTYPE_is_LOOP(c)       (((c)->cx_type & 0xC) == 0x4)
710 #define CxMULTICALL(c)  (((c)->cx_type & CXp_MULTICALL)                 \
711                          == CXp_MULTICALL)
712 #define CxREALEVAL(c)   (((c)->cx_type & (CXTYPEMASK|CXp_REAL))         \
713                          == (CXt_EVAL|CXp_REAL))
714 #define CxTRYBLOCK(c)   (((c)->cx_type & (CXTYPEMASK|CXp_TRYBLOCK))     \
715                          == (CXt_EVAL|CXp_TRYBLOCK))
716 #define CxFOREACH(c)    (CxTYPE_is_LOOP(c) && CxTYPE(c) != CXt_LOOP_PLAIN)
717 #define CxFOREACHDEF(c) ((CxTYPE_is_LOOP(c) && CxTYPE(c) != CXt_LOOP_PLAIN) \
718                          && ((c)->cx_type & CXp_FOR_DEF))
719
720 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
721
722 /* 
723 =head1 "Gimme" Values
724 */
725
726 /*
727 =for apidoc AmU||G_SCALAR
728 Used to indicate scalar context.  See C<GIMME_V>, C<GIMME>, and
729 L<perlcall>.
730
731 =for apidoc AmU||G_ARRAY
732 Used to indicate list context.  See C<GIMME_V>, C<GIMME> and
733 L<perlcall>.
734
735 =for apidoc AmU||G_VOID
736 Used to indicate void context.  See C<GIMME_V> and L<perlcall>.
737
738 =for apidoc AmU||G_DISCARD
739 Indicates that arguments returned from a callback should be discarded.  See
740 L<perlcall>.
741
742 =for apidoc AmU||G_EVAL
743
744 Used to force a Perl C<eval> wrapper around a callback.  See
745 L<perlcall>.
746
747 =for apidoc AmU||G_NOARGS
748
749 Indicates that no arguments are being sent to a callback.  See
750 L<perlcall>.
751
752 =cut
753 */
754
755 #define G_SCALAR        2
756 #define G_ARRAY         3
757 #define G_VOID          1
758 #define G_WANT          3
759
760 /* extra flags for Perl_call_* routines */
761 #define G_DISCARD       4       /* Call FREETMPS.
762                                    Don't change this without consulting the
763                                    hash actions codes defined in hv.h */
764 #define G_EVAL          8       /* Assume eval {} around subroutine call. */
765 #define G_NOARGS       16       /* Don't construct a @_ array. */
766 #define G_KEEPERR      32       /* Warn for errors, don't overwrite $@ */
767 #define G_NODEBUG      64       /* Disable debugging at toplevel.  */
768 #define G_METHOD      128       /* Calling method. */
769 #define G_FAKINGEVAL  256       /* Faking an eval context for call_sv or
770                                    fold_constants. */
771 #define G_UNDEF_FILL  512       /* Fill the stack with &PL_sv_undef
772                                    A special case for UNSHIFT in
773                                    Perl_magic_methcall().  */
774
775 /* flag bits for PL_in_eval */
776 #define EVAL_NULL       0       /* not in an eval */
777 #define EVAL_INEVAL     1       /* some enclosing scope is an eval */
778 #define EVAL_WARNONLY   2       /* used by yywarn() when calling yyerror() */
779 #define EVAL_KEEPERR    4       /* set by Perl_call_sv if G_KEEPERR */
780 #define EVAL_INREQUIRE  8       /* The code is being required. */
781
782 /* Support for switching (stack and block) contexts.
783  * This ensures magic doesn't invalidate local stack and cx pointers.
784  */
785
786 #define PERLSI_UNKNOWN          -1
787 #define PERLSI_UNDEF            0
788 #define PERLSI_MAIN             1
789 #define PERLSI_MAGIC            2
790 #define PERLSI_SORT             3
791 #define PERLSI_SIGNAL           4
792 #define PERLSI_OVERLOAD         5
793 #define PERLSI_DESTROY          6
794 #define PERLSI_WARNHOOK         7
795 #define PERLSI_DIEHOOK          8
796 #define PERLSI_REQUIRE          9
797
798 struct stackinfo {
799     AV *                si_stack;       /* stack for current runlevel */
800     PERL_CONTEXT *      si_cxstack;     /* context stack for runlevel */
801     struct stackinfo *  si_prev;
802     struct stackinfo *  si_next;
803     I32                 si_cxix;        /* current context index */
804     I32                 si_cxmax;       /* maximum allocated index */
805     I32                 si_type;        /* type of runlevel */
806     I32                 si_markoff;     /* offset where markstack begins for us.
807                                          * currently used only with DEBUGGING,
808                                          * but not #ifdef-ed for bincompat */
809 };
810
811 typedef struct stackinfo PERL_SI;
812
813 #define cxstack         (PL_curstackinfo->si_cxstack)
814 #define cxstack_ix      (PL_curstackinfo->si_cxix)
815 #define cxstack_max     (PL_curstackinfo->si_cxmax)
816
817 #ifdef DEBUGGING
818 #  define       SET_MARK_OFFSET \
819     PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack
820 #else
821 #  define       SET_MARK_OFFSET NOOP
822 #endif
823
824 #define PUSHSTACKi(type) \
825     STMT_START {                                                        \
826         PERL_SI *next = PL_curstackinfo->si_next;                       \
827         DEBUG_l({                                                       \
828             int i = 0; PERL_SI *p = PL_curstackinfo;                    \
829             while (p) { i++; p = p->si_prev; }                          \
830             Perl_deb(aTHX_ "push STACKINFO %d at %s:%d\n",              \
831                          i, __FILE__, __LINE__);})                      \
832         if (!next) {                                                    \
833             next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1);    \
834             next->si_prev = PL_curstackinfo;                            \
835             PL_curstackinfo->si_next = next;                            \
836         }                                                               \
837         next->si_type = type;                                           \
838         next->si_cxix = -1;                                             \
839         AvFILLp(next->si_stack) = 0;                                    \
840         SWITCHSTACK(PL_curstack,next->si_stack);                        \
841         PL_curstackinfo = next;                                         \
842         SET_MARK_OFFSET;                                                \
843     } STMT_END
844
845 #define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
846
847 /* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
848  * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
849 #define POPSTACK \
850     STMT_START {                                                        \
851         dSP;                                                            \
852         PERL_SI * const prev = PL_curstackinfo->si_prev;                \
853         DEBUG_l({                                                       \
854             int i = -1; PERL_SI *p = PL_curstackinfo;                   \
855             while (p) { i++; p = p->si_prev; }                          \
856             Perl_deb(aTHX_ "pop  STACKINFO %d at %s:%d\n",              \
857                          i, __FILE__, __LINE__);})                      \
858         if (!prev) {                                                    \
859             PerlIO_printf(Perl_error_log, "panic: POPSTACK\n");         \
860             my_exit(1);                                                 \
861         }                                                               \
862         SWITCHSTACK(PL_curstack,prev->si_stack);                        \
863         /* don't free prev here, free them all at the END{} */          \
864         PL_curstackinfo = prev;                                         \
865     } STMT_END
866
867 #define POPSTACK_TO(s) \
868     STMT_START {                                                        \
869         while (PL_curstack != s) {                                      \
870             dounwind(-1);                                               \
871             POPSTACK;                                                   \
872         }                                                               \
873     } STMT_END
874
875 #define IN_PERL_COMPILETIME     (PL_curcop == &PL_compiling)
876 #define IN_PERL_RUNTIME         (PL_curcop != &PL_compiling)
877
878 /*
879 =head1 Multicall Functions
880
881 =for apidoc Ams||dMULTICALL
882 Declare local variables for a multicall. See L<perlcall/Lightweight Callbacks>.
883
884 =for apidoc Ams||PUSH_MULTICALL
885 Opening bracket for a lightweight callback.
886 See L<perlcall/Lightweight Callbacks>.
887
888 =for apidoc Ams||MULTICALL
889 Make a lightweight callback. See L<perlcall/Lightweight Callbacks>.
890
891 =for apidoc Ams||POP_MULTICALL
892 Closing bracket for a lightweight callback.
893 See L<perlcall/Lightweight Callbacks>.
894
895 =cut
896 */
897
898 #define dMULTICALL \
899     SV **newsp;                 /* set by POPBLOCK */                   \
900     PERL_CONTEXT *cx;                                                   \
901     CV *multicall_cv;                                                   \
902     OP *multicall_cop;                                                  \
903     bool multicall_oldcatch;                                            \
904     U8 hasargs = 0              /* used by PUSHSUB */
905
906 #define PUSH_MULTICALL(the_cv) \
907     STMT_START {                                                        \
908         CV * const _nOnclAshIngNamE_ = the_cv;                          \
909         CV * const cv = _nOnclAshIngNamE_;                              \
910         AV * const padlist = CvPADLIST(cv);                             \
911         ENTER;                                                          \
912         multicall_oldcatch = CATCH_GET;                                 \
913         SAVETMPS; SAVEVPTR(PL_op);                                      \
914         CATCH_SET(TRUE);                                                \
915         PUSHSTACKi(PERLSI_SORT);                                        \
916         PUSHBLOCK(cx, CXt_SUB|CXp_MULTICALL, PL_stack_sp);              \
917         PUSHSUB(cx);                                                    \
918         if (++CvDEPTH(cv) >= 2) {                                       \
919             PERL_STACK_OVERFLOW_CHECK();                                \
920             Perl_pad_push(aTHX_ padlist, CvDEPTH(cv));                  \
921         }                                                               \
922         SAVECOMPPAD();                                                  \
923         PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));                       \
924         multicall_cv = cv;                                              \
925         multicall_cop = CvSTART(cv);                                    \
926     } STMT_END
927
928 #define MULTICALL \
929     STMT_START {                                                        \
930         PL_op = multicall_cop;                                          \
931         CALLRUNOPS(aTHX);                                               \
932     } STMT_END
933
934 #define POP_MULTICALL \
935     STMT_START {                                                        \
936         LEAVESUB(multicall_cv);                                         \
937         CvDEPTH(multicall_cv)--;                                        \
938         POPBLOCK(cx,PL_curpm);                                          \
939         POPSTACK;                                                       \
940         CATCH_SET(multicall_oldcatch);                                  \
941         LEAVE;                                                          \
942         SPAGAIN;                                                        \
943     } STMT_END
944
945 /*
946  * Local variables:
947  * c-indentation-style: bsd
948  * c-basic-offset: 4
949  * indent-tabs-mode: t
950  * End:
951  *
952  * ex: set ts=8 sts=4 sw=4 noet:
953  */