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