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