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