sundry cleanups for cloned interpreters (only known failure mode
[p5sagit/p5-mst-13.2.git] / cop.h
1 /*    cop.h
2  *
3  *    Copyright (c) 1991-1999, Larry Wall
4  *
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.
7  *
8  */
9
10 struct cop {
11     BASEOP
12     char *      cop_label;      /* label for this construct */
13 #ifdef USE_ITHREADS
14     char *      cop_stashpv;    /* package line was compiled in */
15     char *      cop_file;       /* file name the following line # is from */
16 #else
17     HV *        cop_stash;      /* package line was compiled in */
18     GV *        cop_filegv;     /* file the following line # is from */
19 #endif
20     U32         cop_seq;        /* parse sequence number */
21     I32         cop_arybase;    /* array base this line was compiled with */
22     line_t      cop_line;       /* line # of this command */
23     SV *        cop_warnings;   /* lexical warnings bitmask */
24 };
25
26 #define Nullcop Null(COP*)
27
28 #ifdef USE_ITHREADS
29 #  define CopFILE(c)            ((c)->cop_file)
30 #  define CopFILEGV(c)          (CopFILE(c) \
31                                  ? gv_fetchfile(CopFILE(c)) : Nullgv)
32 #  define CopFILE_set(c,pv)     ((c)->cop_file = savepv(pv))    /* XXX */
33 #  define CopFILESV(c)          (CopFILE(c) \
34                                  ? GvSV(gv_fetchfile(CopFILE(c))) : Nullsv)
35 #  define CopFILEAV(c)          (CopFILE(c) \
36                                  ? GvAV(gv_fetchfile(CopFILE(c))) : Nullav)
37 #  define CopSTASHPV(c)         ((c)->cop_stashpv)
38 #  define CopSTASHPV_set(c,pv)  ((c)->cop_stashpv = savepv(pv)) /* XXX */
39 #  define CopSTASH(c)           (CopSTASHPV(c) \
40                                  ? gv_stashpv(CopSTASHPV(c),GV_ADD) : Nullhv)
41 #  define CopSTASH_set(c,hv)    CopSTASHPV_set(c, HvNAME(hv))
42 #  define CopSTASH_eq(c,hv)     (hv                                     \
43                                  && (CopSTASHPV(c) == HvNAME(hv)        \
44                                      || (CopSTASHPV(c) && HvNAME(hv)    \
45                                          && strEQ(CopSTASHPV(c), HvNAME(hv)))))
46 #else
47 #  define CopFILEGV(c)          ((c)->cop_filegv)
48 #  define CopFILEGV_set(c,gv)   ((c)->cop_filegv = gv)
49 #  define CopFILE_set(c,pv)     ((c)->cop_filegv = gv_fetchfile(pv))
50 #  define CopFILESV(c)          (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : Nullsv)
51 #  define CopFILEAV(c)          (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : Nullav)
52 #  define CopFILE(c)            (CopFILESV(c) ? SvPVX(CopFILESV(c)) : Nullch)
53 #  define CopSTASH(c)           ((c)->cop_stash)
54 #  define CopSTASH_set(c,hv)    ((c)->cop_stash = hv)
55 #  define CopSTASHPV(c)         (CopSTASH(c) ? HvNAME(CopSTASH(c)) : Nullch)
56 #  define CopSTASHPV_set(c,pv)  CopSTASH_set(c, gv_stashpv(pv,GV_ADD))
57 #  define CopSTASH_eq(c,hv)     (CopSTASH(c) == hv)
58 #endif /* USE_ITHREADS */
59
60 #define CopSTASH_ne(c,hv)       (!CopSTASH_eq(c,hv))
61 #define CopLINE(c)              ((c)->cop_line)
62 #define CopLINE_inc(c)          (++CopLINE(c))
63 #define CopLINE_dec(c)          (--CopLINE(c))
64 #define CopLINE_set(c,l)        (CopLINE(c) = (l))
65
66 /*
67  * Here we have some enormously heavy (or at least ponderous) wizardry.
68  */
69
70 /* subroutine context */
71 struct block_sub {
72     CV *        cv;
73     GV *        gv;
74     GV *        dfoutgv;
75 #ifndef USE_THREADS
76     AV *        savearray;
77 #endif /* USE_THREADS */
78     AV *        argarray;
79     U16         olddepth;
80     U8          hasargs;
81     U8          lval;           /* XXX merge lval and hasargs? */
82 };
83
84 #define PUSHSUB(cx)                                                     \
85         cx->blk_sub.cv = cv;                                            \
86         cx->blk_sub.olddepth = CvDEPTH(cv);                             \
87         cx->blk_sub.hasargs = hasargs;                                  \
88         cx->blk_sub.lval = PL_op->op_private &                          \
89                               (OPpLVAL_INTRO|OPpENTERSUB_INARGS);
90
91 #define PUSHFORMAT(cx)                                                  \
92         cx->blk_sub.cv = cv;                                            \
93         cx->blk_sub.gv = gv;                                            \
94         cx->blk_sub.hasargs = 0;                                        \
95         cx->blk_sub.dfoutgv = PL_defoutgv;                              \
96         (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
97
98 #ifdef USE_THREADS
99 #define POPSAVEARRAY() NOOP
100 #else
101 #define POPSAVEARRAY()                                                  \
102     STMT_START {                                                        \
103         SvREFCNT_dec(GvAV(PL_defgv));                                   \
104         GvAV(PL_defgv) = cx->blk_sub.savearray;                         \
105     } STMT_END
106 #endif /* USE_THREADS */
107
108 #define POPSUB(cx,sv)                                                   \
109     STMT_START {                                                        \
110         if (cx->blk_sub.hasargs) {                                      \
111             POPSAVEARRAY();                                             \
112             /* abandon @_ if it got reified */                          \
113             if (AvREAL(cx->blk_sub.argarray)) {                         \
114                 SSize_t fill = AvFILLp(cx->blk_sub.argarray);           \
115                 SvREFCNT_dec(cx->blk_sub.argarray);                     \
116                 cx->blk_sub.argarray = newAV();                         \
117                 av_extend(cx->blk_sub.argarray, fill);                  \
118                 AvFLAGS(cx->blk_sub.argarray) = AVf_REIFY;              \
119                 PL_curpad[0] = (SV*)cx->blk_sub.argarray;               \
120             }                                                           \
121         }                                                               \
122         sv = (SV*)cx->blk_sub.cv;                                       \
123         if (sv && (CvDEPTH((CV*)sv) = cx->blk_sub.olddepth))            \
124             sv = Nullsv;                                                \
125     } STMT_END
126
127 #define LEAVESUB(sv)                                                    \
128     STMT_START {                                                        \
129         if (sv)                                                         \
130             SvREFCNT_dec(sv);                                           \
131     } STMT_END
132
133 #define POPFORMAT(cx)                                                   \
134         setdefout(cx->blk_sub.dfoutgv);                                 \
135         SvREFCNT_dec(cx->blk_sub.dfoutgv);
136
137 /* eval context */
138 struct block_eval {
139     I32         old_in_eval;
140     I32         old_op_type;
141     char *      old_name;
142     OP *        old_eval_root;
143     SV *        cur_text;
144 };
145
146 #define PUSHEVAL(cx,n,fgv)                                              \
147         cx->blk_eval.old_in_eval = PL_in_eval;                          \
148         cx->blk_eval.old_op_type = PL_op->op_type;                      \
149         cx->blk_eval.old_name = n;                                      \
150         cx->blk_eval.old_eval_root = PL_eval_root;                      \
151         cx->blk_eval.cur_text = PL_linestr;
152
153 #define POPEVAL(cx)                                                     \
154         PL_in_eval = cx->blk_eval.old_in_eval;                          \
155         optype = cx->blk_eval.old_op_type;                              \
156         PL_eval_root = cx->blk_eval.old_eval_root;
157
158 /* loop context */
159 struct block_loop {
160     char *      label;
161     I32         resetsp;
162     OP *        redo_op;
163     OP *        next_op;
164     OP *        last_op;
165     SV **       itervar;
166     SV *        itersave;
167     SV *        iterlval;
168     AV *        iterary;
169     IV          iterix;
170     IV          itermax;
171 };
172
173 #define PUSHLOOP(cx, ivar, s)                                           \
174         cx->blk_loop.label = PL_curcop->cop_label;                      \
175         cx->blk_loop.resetsp = s - PL_stack_base;                       \
176         cx->blk_loop.redo_op = cLOOP->op_redoop;                        \
177         cx->blk_loop.next_op = cLOOP->op_nextop;                        \
178         cx->blk_loop.last_op = cLOOP->op_lastop;                        \
179         if (cx->blk_loop.itervar = (ivar))                              \
180             cx->blk_loop.itersave = SvREFCNT_inc(*cx->blk_loop.itervar);\
181         cx->blk_loop.iterlval = Nullsv;                                 \
182         cx->blk_loop.iterary = Nullav;                                  \
183         cx->blk_loop.iterix = -1;
184
185 #define POPLOOP(cx)                                                     \
186         SvREFCNT_dec(cx->blk_loop.iterlval);                            \
187         if (cx->blk_loop.itervar) {                                     \
188             sv_2mortal(*(cx->blk_loop.itervar));                        \
189             *(cx->blk_loop.itervar) = cx->blk_loop.itersave;            \
190         }                                                               \
191         if (cx->blk_loop.iterary && cx->blk_loop.iterary != PL_curstack)\
192             SvREFCNT_dec(cx->blk_loop.iterary);
193
194 /* context common to subroutines, evals and loops */
195 struct block {
196     I32         blku_oldsp;     /* stack pointer to copy stuff down to */
197     COP *       blku_oldcop;    /* old curcop pointer */
198     I32         blku_oldretsp;  /* return stack index */
199     I32         blku_oldmarksp; /* mark stack index */
200     I32         blku_oldscopesp;        /* scope stack index */
201     PMOP *      blku_oldpm;     /* values of pattern match vars */
202     U8          blku_gimme;     /* is this block running in list context? */
203
204     union {
205         struct block_sub        blku_sub;
206         struct block_eval       blku_eval;
207         struct block_loop       blku_loop;
208     } blk_u;
209 };
210 #define blk_oldsp       cx_u.cx_blk.blku_oldsp
211 #define blk_oldcop      cx_u.cx_blk.blku_oldcop
212 #define blk_oldretsp    cx_u.cx_blk.blku_oldretsp
213 #define blk_oldmarksp   cx_u.cx_blk.blku_oldmarksp
214 #define blk_oldscopesp  cx_u.cx_blk.blku_oldscopesp
215 #define blk_oldpm       cx_u.cx_blk.blku_oldpm
216 #define blk_gimme       cx_u.cx_blk.blku_gimme
217 #define blk_sub         cx_u.cx_blk.blk_u.blku_sub
218 #define blk_eval        cx_u.cx_blk.blk_u.blku_eval
219 #define blk_loop        cx_u.cx_blk.blk_u.blku_loop
220
221 /* Enter a block. */
222 #define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix],            \
223         cx->cx_type             = t,                                    \
224         cx->blk_oldsp           = sp - PL_stack_base,                   \
225         cx->blk_oldcop          = PL_curcop,                            \
226         cx->blk_oldmarksp       = PL_markstack_ptr - PL_markstack,      \
227         cx->blk_oldscopesp      = PL_scopestack_ix,                     \
228         cx->blk_oldretsp        = PL_retstack_ix,                       \
229         cx->blk_oldpm           = PL_curpm,                             \
230         cx->blk_gimme           = gimme;                                \
231         DEBUG_l( PerlIO_printf(Perl_debug_log, "Entering block %ld, type %s\n", \
232                     (long)cxstack_ix, PL_block_type[CxTYPE(cx)]); )
233
234 /* Exit a block (RETURN and LAST). */
235 #define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--],                    \
236         newsp            = PL_stack_base + cx->blk_oldsp,               \
237         PL_curcop        = cx->blk_oldcop,                              \
238         PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp,            \
239         PL_scopestack_ix = cx->blk_oldscopesp,                          \
240         PL_retstack_ix   = cx->blk_oldretsp,                            \
241         pm               = cx->blk_oldpm,                               \
242         gimme            = cx->blk_gimme;                               \
243         DEBUG_l( PerlIO_printf(Perl_debug_log, "Leaving block %ld, type %s\n",          \
244                     (long)cxstack_ix+1,PL_block_type[CxTYPE(cx)]); )
245
246 /* Continue a block elsewhere (NEXT and REDO). */
247 #define TOPBLOCK(cx) cx  = &cxstack[cxstack_ix],                        \
248         PL_stack_sp      = PL_stack_base + cx->blk_oldsp,               \
249         PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp,            \
250         PL_scopestack_ix = cx->blk_oldscopesp,                          \
251         PL_retstack_ix   = cx->blk_oldretsp,                            \
252         PL_curpm         = cx->blk_oldpm
253
254 /* substitution context */
255 struct subst {
256     I32         sbu_iters;
257     I32         sbu_maxiters;
258     I32         sbu_rflags;
259     I32         sbu_oldsave;
260     bool        sbu_once;
261     bool        sbu_rxtainted;
262     char *      sbu_orig;
263     SV *        sbu_dstr;
264     SV *        sbu_targ;
265     char *      sbu_s;
266     char *      sbu_m;
267     char *      sbu_strend;
268     void *      sbu_rxres;
269     REGEXP *    sbu_rx;
270 };
271 #define sb_iters        cx_u.cx_subst.sbu_iters
272 #define sb_maxiters     cx_u.cx_subst.sbu_maxiters
273 #define sb_rflags       cx_u.cx_subst.sbu_rflags
274 #define sb_oldsave      cx_u.cx_subst.sbu_oldsave
275 #define sb_once         cx_u.cx_subst.sbu_once
276 #define sb_rxtainted    cx_u.cx_subst.sbu_rxtainted
277 #define sb_orig         cx_u.cx_subst.sbu_orig
278 #define sb_dstr         cx_u.cx_subst.sbu_dstr
279 #define sb_targ         cx_u.cx_subst.sbu_targ
280 #define sb_s            cx_u.cx_subst.sbu_s
281 #define sb_m            cx_u.cx_subst.sbu_m
282 #define sb_strend       cx_u.cx_subst.sbu_strend
283 #define sb_rxres        cx_u.cx_subst.sbu_rxres
284 #define sb_rx           cx_u.cx_subst.sbu_rx
285
286 #define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix],                 \
287         cx->sb_iters            = iters,                                \
288         cx->sb_maxiters         = maxiters,                             \
289         cx->sb_rflags           = r_flags,                              \
290         cx->sb_oldsave          = oldsave,                              \
291         cx->sb_once             = once,                                 \
292         cx->sb_rxtainted        = rxtainted,                            \
293         cx->sb_orig             = orig,                                 \
294         cx->sb_dstr             = dstr,                                 \
295         cx->sb_targ             = targ,                                 \
296         cx->sb_s                = s,                                    \
297         cx->sb_m                = m,                                    \
298         cx->sb_strend           = strend,                               \
299         cx->sb_rxres            = Null(void*),                          \
300         cx->sb_rx               = rx,                                   \
301         cx->cx_type             = CXt_SUBST;                            \
302         rxres_save(&cx->sb_rxres, rx)
303
304 #define POPSUBST(cx) cx = &cxstack[cxstack_ix--];                       \
305         rxres_free(&cx->sb_rxres)
306
307 struct context {
308     U32         cx_type;        /* what kind of context this is */
309     union {
310         struct block    cx_blk;
311         struct subst    cx_subst;
312     } cx_u;
313 };
314
315 #define CXTYPEMASK      0xff
316 #define CXt_NULL        0
317 #define CXt_SUB         1
318 #define CXt_EVAL        2
319 #define CXt_LOOP        3
320 #define CXt_SUBST       4
321 #define CXt_BLOCK       5
322
323 /* private flags for CXt_EVAL */
324 #define CXp_REAL        0x00000100      /* truly eval'', not a lookalike */
325
326 #define CxTYPE(c)       ((c)->cx_type & CXTYPEMASK)
327 #define CxREALEVAL(c)   (((c)->cx_type & (CXt_EVAL|CXp_REAL)) == (CXt_EVAL|CXp_REAL))
328
329 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
330
331 /* "gimme" values */
332 #define G_SCALAR        0
333 #define G_ARRAY         1
334 #define G_VOID          128     /* skip this bit when adding flags below */
335
336 /* extra flags for Perl_call_* routines */
337 #define G_DISCARD       2       /* Call FREETMPS. */
338 #define G_EVAL          4       /* Assume eval {} around subroutine call. */
339 #define G_NOARGS        8       /* Don't construct a @_ array. */
340 #define G_KEEPERR      16       /* Append errors to $@, don't overwrite it */
341 #define G_NODEBUG      32       /* Disable debugging at toplevel.  */
342
343 /* flag bits for PL_in_eval */
344 #define EVAL_NULL       0       /* not in an eval */
345 #define EVAL_INEVAL     1       /* some enclosing scope is an eval */
346 #define EVAL_WARNONLY   2       /* used by yywarn() when calling yyerror() */
347 #define EVAL_KEEPERR    4       /* set by Perl_call_sv if G_KEEPERR */
348
349 /* Support for switching (stack and block) contexts.
350  * This ensures magic doesn't invalidate local stack and cx pointers.
351  */
352
353 #define PERLSI_UNKNOWN          -1
354 #define PERLSI_UNDEF            0
355 #define PERLSI_MAIN             1
356 #define PERLSI_MAGIC            2
357 #define PERLSI_SORT             3
358 #define PERLSI_SIGNAL           4
359 #define PERLSI_OVERLOAD         5
360 #define PERLSI_DESTROY          6
361 #define PERLSI_WARNHOOK         7
362 #define PERLSI_DIEHOOK          8
363 #define PERLSI_REQUIRE          9
364
365 struct stackinfo {
366     AV *                si_stack;       /* stack for current runlevel */
367     PERL_CONTEXT *      si_cxstack;     /* context stack for runlevel */
368     I32                 si_cxix;        /* current context index */
369     I32                 si_cxmax;       /* maximum allocated index */
370     I32                 si_type;        /* type of runlevel */
371     struct stackinfo *  si_prev;
372     struct stackinfo *  si_next;
373     I32 *               si_markbase;    /* where markstack begins for us.
374                                          * currently used only with DEBUGGING,
375                                          * but not #ifdef-ed for bincompat */
376 };
377
378 typedef struct stackinfo PERL_SI;
379
380 #define cxstack         (PL_curstackinfo->si_cxstack)
381 #define cxstack_ix      (PL_curstackinfo->si_cxix)
382 #define cxstack_max     (PL_curstackinfo->si_cxmax)
383
384 #ifdef DEBUGGING
385 #  define       SET_MARKBASE PL_curstackinfo->si_markbase = PL_markstack_ptr
386 #else
387 #  define       SET_MARKBASE NOOP
388 #endif
389
390 #define PUSHSTACKi(type) \
391     STMT_START {                                                        \
392         PERL_SI *next = PL_curstackinfo->si_next;                       \
393         if (!next) {                                                    \
394             next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1);    \
395             next->si_prev = PL_curstackinfo;                            \
396             PL_curstackinfo->si_next = next;                            \
397         }                                                               \
398         next->si_type = type;                                           \
399         next->si_cxix = -1;                                             \
400         AvFILLp(next->si_stack) = 0;                                    \
401         SWITCHSTACK(PL_curstack,next->si_stack);                        \
402         PL_curstackinfo = next;                                         \
403         SET_MARKBASE;                                                   \
404     } STMT_END
405
406 #define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
407
408 /* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
409  * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
410 #define POPSTACK \
411     STMT_START {                                                        \
412         djSP;                                                           \
413         PERL_SI *prev = PL_curstackinfo->si_prev;                       \
414         if (!prev) {                                                    \
415             PerlIO_printf(Perl_error_log, "panic: POPSTACK\n");         \
416             my_exit(1);                                                 \
417         }                                                               \
418         SWITCHSTACK(PL_curstack,prev->si_stack);                        \
419         /* don't free prev here, free them all at the END{} */          \
420         PL_curstackinfo = prev;                                         \
421     } STMT_END
422
423 #define POPSTACK_TO(s) \
424     STMT_START {                                                        \
425         while (PL_curstack != s) {                                      \
426             dounwind(-1);                                               \
427             POPSTACK;                                                   \
428         }                                                               \
429     } STMT_END