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