This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
5.002 beta 1
[perl5.git] / cop.h
1 /*    cop.h
2  *
3  *    Copyright (c) 1991-1994, 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 };
19
20 #define Nullcop Null(COP*)
21
22 /*
23  * Here we have some enormously heavy (or at least ponderous) wizardry.
24  */
25
26 /* subroutine context */
27 struct block_sub {
28     CV *        cv;
29     GV *        gv;
30     GV *        dfoutgv;
31     AV *        savearray;
32     AV *        argarray;
33     U16         olddepth;
34     U8          hasargs;
35 };
36
37 #define PUSHSUB(cx)                                                     \
38         cx->blk_sub.cv = cv;                                            \
39         cx->blk_sub.olddepth = CvDEPTH(cv);                             \
40         cx->blk_sub.hasargs = hasargs;
41
42 #define PUSHFORMAT(cx)                                                  \
43         cx->blk_sub.cv = cv;                                            \
44         cx->blk_sub.gv = gv;                                            \
45         cx->blk_sub.hasargs = 0;                                        \
46         cx->blk_sub.dfoutgv = defoutgv;                                 \
47         (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
48
49 #define POPSUB(cx)                                                      \
50         if (cx->blk_sub.hasargs) {   /* put back old @_ */              \
51             GvAV(defgv) = cx->blk_sub.savearray;                        \
52         }                                                               \
53         if (cx->blk_sub.cv) {                                           \
54             if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) {    \
55                 SvREFCNT_dec((SV*)cx->blk_sub.cv);                      \
56             }                                                           \
57         }
58
59 #define POPFORMAT(cx)                                                   \
60         setdefout(cx->blk_sub.dfoutgv);                                 \
61         SvREFCNT_dec(cx->blk_sub.dfoutgv);
62
63 /* eval context */
64 struct block_eval {
65     I32         old_in_eval;
66     I32         old_op_type;
67     char *      old_name;
68     OP *        old_eval_root;
69     SV *        cur_text;
70 };
71
72 #define PUSHEVAL(cx,n,fgv)                                              \
73         cx->blk_eval.old_in_eval = in_eval;                             \
74         cx->blk_eval.old_op_type = op->op_type;                         \
75         cx->blk_eval.old_name = n;                                      \
76         cx->blk_eval.old_eval_root = eval_root;                         \
77         cx->blk_eval.cur_text = linestr;
78
79 #define POPEVAL(cx)                                                     \
80         in_eval = cx->blk_eval.old_in_eval;                             \
81         optype = cx->blk_eval.old_op_type;                              \
82         eval_root = cx->blk_eval.old_eval_root;
83
84 /* loop context */
85 struct block_loop {
86     char *      label;
87     I32         resetsp;
88     OP *        redo_op;
89     OP *        next_op;
90     OP *        last_op;
91     SV **       itervar;
92     SV *        itersave;
93     AV *        iterary;
94     I32         iterix;
95 };
96
97 #define PUSHLOOP(cx, ivar, s)                                           \
98         cx->blk_loop.label = curcop->cop_label;                         \
99         cx->blk_loop.resetsp = s - stack_base;                          \
100         cx->blk_loop.redo_op = cLOOP->op_redoop;                        \
101         cx->blk_loop.next_op = cLOOP->op_nextop;                        \
102         cx->blk_loop.last_op = cLOOP->op_lastop;                        \
103         cx->blk_loop.itervar = ivar;                                    \
104         if (ivar)                                                       \
105             cx->blk_loop.itersave = *cx->blk_loop.itervar;
106
107 #define POPLOOP(cx)                                                     \
108         newsp           = stack_base + cx->blk_loop.resetsp;
109
110 /* context common to subroutines, evals and loops */
111 struct block {
112     I32         blku_oldsp;     /* stack pointer to copy stuff down to */
113     COP *       blku_oldcop;    /* old curcop pointer */
114     I32         blku_oldretsp;  /* return stack index */
115     I32         blku_oldmarksp; /* mark stack index */
116     I32         blku_oldscopesp;        /* scope stack index */
117     PMOP *      blku_oldpm;     /* values of pattern match vars */
118     U8          blku_gimme;     /* is this block running in list context? */
119
120     union {
121         struct block_sub        blku_sub;
122         struct block_eval       blku_eval;
123         struct block_loop       blku_loop;
124     } blk_u;
125 };
126 #define blk_oldsp       cx_u.cx_blk.blku_oldsp
127 #define blk_oldcop      cx_u.cx_blk.blku_oldcop
128 #define blk_oldretsp    cx_u.cx_blk.blku_oldretsp
129 #define blk_oldmarksp   cx_u.cx_blk.blku_oldmarksp
130 #define blk_oldscopesp  cx_u.cx_blk.blku_oldscopesp
131 #define blk_oldpm       cx_u.cx_blk.blku_oldpm
132 #define blk_gimme       cx_u.cx_blk.blku_gimme
133 #define blk_sub         cx_u.cx_blk.blk_u.blku_sub
134 #define blk_eval        cx_u.cx_blk.blk_u.blku_eval
135 #define blk_loop        cx_u.cx_blk.blk_u.blku_loop
136
137 /* Enter a block. */
138 #define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix],            \
139         cx->cx_type             = t,                                    \
140         cx->blk_oldsp           = sp - stack_base,                      \
141         cx->blk_oldcop          = curcop,                               \
142         cx->blk_oldmarksp       = markstack_ptr - markstack,            \
143         cx->blk_oldscopesp      = scopestack_ix,                        \
144         cx->blk_oldretsp        = retstack_ix,                          \
145         cx->blk_oldpm           = curpm,                                \
146         cx->blk_gimme           = gimme;                                \
147         DEBUG_l( fprintf(stderr,"Entering block %ld, type %s\n",        \
148                     (long)cxstack_ix, block_type[t]); )
149
150 /* Exit a block (RETURN and LAST). */
151 #define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--],                    \
152         newsp           = stack_base + cx->blk_oldsp,                   \
153         curcop          = cx->blk_oldcop,                               \
154         markstack_ptr   = markstack + cx->blk_oldmarksp,                \
155         scopestack_ix   = cx->blk_oldscopesp,                           \
156         retstack_ix     = cx->blk_oldretsp,                             \
157         pm              = cx->blk_oldpm,                                \
158         gimme           = cx->blk_gimme;                                \
159         DEBUG_l( fprintf(stderr,"Leaving block %ld, type %s\n",         \
160                     (long)cxstack_ix+1,block_type[cx->cx_type]); )
161
162 /* Continue a block elsewhere (NEXT and REDO). */
163 #define TOPBLOCK(cx) cx = &cxstack[cxstack_ix],                         \
164         stack_sp        = stack_base + cx->blk_oldsp,                   \
165         markstack_ptr   = markstack + cx->blk_oldmarksp,                \
166         scopestack_ix   = cx->blk_oldscopesp,                           \
167         retstack_ix     = cx->blk_oldretsp
168
169 /* substitution context */
170 struct subst {
171     I32         sbu_iters;
172     I32         sbu_maxiters;
173     I32         sbu_safebase;
174     I32         sbu_once;
175     I32         sbu_oldsave;
176     char *      sbu_orig;
177     SV *        sbu_dstr;
178     SV *        sbu_targ;
179     char *      sbu_s;
180     char *      sbu_m;
181     char *      sbu_strend;
182     char *      sbu_subbase;
183 };
184 #define sb_iters        cx_u.cx_subst.sbu_iters
185 #define sb_maxiters     cx_u.cx_subst.sbu_maxiters
186 #define sb_safebase     cx_u.cx_subst.sbu_safebase
187 #define sb_once         cx_u.cx_subst.sbu_once
188 #define sb_oldsave      cx_u.cx_subst.sbu_oldsave
189 #define sb_orig         cx_u.cx_subst.sbu_orig
190 #define sb_dstr         cx_u.cx_subst.sbu_dstr
191 #define sb_targ         cx_u.cx_subst.sbu_targ
192 #define sb_s            cx_u.cx_subst.sbu_s
193 #define sb_m            cx_u.cx_subst.sbu_m
194 #define sb_strend       cx_u.cx_subst.sbu_strend
195 #define sb_subbase      cx_u.cx_subst.sbu_subbase
196
197 #define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix],                 \
198         cx->sb_iters            = iters,                                \
199         cx->sb_maxiters         = maxiters,                             \
200         cx->sb_safebase         = safebase,                             \
201         cx->sb_once             = once,                                 \
202         cx->sb_oldsave          = oldsave,                              \
203         cx->sb_orig             = orig,                                 \
204         cx->sb_dstr             = dstr,                                 \
205         cx->sb_targ             = targ,                                 \
206         cx->sb_s                = s,                                    \
207         cx->sb_m                = m,                                    \
208         cx->sb_strend           = strend,                               \
209         cx->cx_type             = CXt_SUBST
210
211 #define POPSUBST(cx) cxstack_ix--
212
213 struct context {
214     I32         cx_type;        /* what kind of context this is */
215     union {
216         struct block    cx_blk;
217         struct subst    cx_subst;
218     } cx_u;
219 };
220 #define CXt_NULL        0
221 #define CXt_SUB         1
222 #define CXt_EVAL        2
223 #define CXt_LOOP        3
224 #define CXt_SUBST       4
225 #define CXt_BLOCK       5
226
227 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
228
229 /* "gimme" values */
230 #define G_SCALAR        0
231 #define G_ARRAY         1
232
233 /* extra flags for perl_call_* routines */
234 #define G_DISCARD       2       /* Call FREETMPS. */
235 #define G_EVAL          4       /* Assume eval {} around subroutine call. */
236 #define G_NOARGS        8       /* Don't construct a @_ array. */
237 #define G_KEEPERR      16       /* Append errors to $@ rather than overwriting it */