9f07457f58808b7bd85af0dda95a4acace6c2c08
[p5sagit/p5-mst-13.2.git] / cop.h
1 /* $RCSfile: cmd.h,v $$Revision: 4.1 $$Date: 92/08/07 17:19:19 $
2  *
3  *    Copyright (c) 1991, 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  * $Log:        cmd.h,v $
9  * Revision 4.1  92/08/07  17:19:19  lwall
10  * Stage 6 Snapshot
11  * 
12  * Revision 4.0.1.2  92/06/08  12:01:02  lwall
13  * patch20: removed implicit int declarations on funcions
14  * 
15  * Revision 4.0.1.1  91/06/07  10:28:50  lwall
16  * patch4: new copyright notice
17  * patch4: length($`), length($&), length($') now optimized to avoid string copy
18  * 
19  * Revision 4.0  91/03/20  01:04:34  lwall
20  * 4.0 baseline.
21  * 
22  */
23
24 struct acop {
25     GV          *acop_gv;       /* a symbol table entry */
26     OP          *acop_expr;     /* any associated expression */
27 };
28
29 struct ccop {
30     OP          *ccop_true;     /* normal code to do on if and while */
31     OP          *ccop_alt;      /* else cmd ptr or continue code */
32 };
33
34 struct scop {
35     OP          **scop_next;    /* array of pointers to commands */
36     short       scop_offset;    /* first value - 1 */
37     short       scop_max;       /* last value + 1 */
38 };
39
40 struct cop {
41     BASEOP
42     OP          *cop_expr;      /* conditional expression */
43     OP          *cop_head;      /* head of this command list */
44     SV          *cop_short;     /* string to match as shortcut */
45     GV          *cop_gv;        /* a symbol table entry, mostly for fp */
46     char        *cop_label;     /* label for this construct */
47     union uop {
48         struct acop acop;       /* normal command */
49         struct ccop ccop;       /* compound command */
50         struct scop scop;       /* switch command */
51     } uop;
52     short       cop_slen;       /* len of cop_short, if not null */
53     VOL short   cop_flags;      /* optimization flags--see above */
54     HV *        cop_stash;      /* package line was compiled in */
55     GV *        cop_filegv;     /* file the following line # is from */
56     line_t      cop_line;       /* line # of this command */
57     char        cop_type;       /* what this command does */
58 };
59
60 #define Nullcop Null(COP*)
61
62 /*
63  * Here we have some enormously heavy (or at least ponderous) wizardry.
64  */
65
66 /* subroutine context */
67 struct block_sub {
68     CV *        cv;
69     GV *        gv;
70     GV *        defgv;
71     AV *        savearray;
72     AV *        argarray;
73     U16         olddepth;
74     U8          hasargs;
75 };
76
77 #define PUSHSUB(cx)                                                     \
78         cx->blk_sub.cv = cv;                                            \
79         cx->blk_sub.gv = gv;                                            \
80         cx->blk_sub.olddepth = CvDEPTH(cv);                             \
81         cx->blk_sub.hasargs = hasargs;
82
83 #define PUSHFORMAT(cx)                                                  \
84         cx->blk_sub.cv = cv;                                            \
85         cx->blk_sub.gv = gv;                                            \
86         cx->blk_sub.defgv = defoutgv;                                   \
87         cx->blk_sub.hasargs = 0;
88
89 #define POPSUB(cx)                                                      \
90         if (cx->blk_sub.hasargs) {   /* put back old @_ */              \
91             av_free(cx->blk_sub.argarray);                              \
92             GvAV(defgv) = cx->blk_sub.savearray;                        \
93         }                                                               \
94         if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) {        \
95             if (CvDELETED(cx->blk_sub.cv))                              \
96                 cv_free(cx->blk_sub.cv);                                \
97         }
98
99 #define POPFORMAT(cx)                                                   \
100         defoutgv = cx->blk_sub.defgv;
101
102 /* eval context */
103 struct block_eval {
104     I32         old_in_eval;
105     I32         old_op_type;
106     char *      old_name;
107     OP *        old_eval_root;
108 };
109
110 #define PUSHEVAL(cx,n)                                                  \
111         cx->blk_eval.old_in_eval = in_eval;                             \
112         cx->blk_eval.old_op_type = op->op_type;                         \
113         cx->blk_eval.old_name = n;                                      \
114         cx->blk_eval.old_eval_root = eval_root;
115
116 #define POPEVAL(cx)                                                     \
117         in_eval = cx->blk_eval.old_in_eval;                             \
118         optype = cx->blk_eval.old_op_type;                              \
119         eval_root = cx->blk_eval.old_eval_root;
120
121 /* loop context */
122 struct block_loop {
123     char *      label;
124     I32         resetsp;
125     OP *        redo_op;
126     OP *        next_op;
127     OP *        last_op;
128     SV **       itervar;
129     SV *        itersave;
130     AV *        iterary;
131     I32         iterix;
132 };
133
134 #define PUSHLOOP(cx, ivar, s)                                           \
135         cx->blk_loop.label = curcop->cop_label;                         \
136         cx->blk_loop.resetsp = s - stack_base;                          \
137         cx->blk_loop.redo_op = cLOOP->op_redoop;                        \
138         cx->blk_loop.next_op = cLOOP->op_nextop;                        \
139         cx->blk_loop.last_op = cLOOP->op_lastop;                        \
140         cx->blk_loop.itervar = ivar;                                    \
141         if (ivar)                                                       \
142             cx->blk_loop.itersave = *cx->blk_loop.itervar;
143
144 #define POPLOOP(cx)                                                     \
145         newsp           = stack_base + cx->blk_loop.resetsp;            \
146         if (cx->blk_loop.itervar)                                       \
147             *cx->blk_loop.itervar = cx->blk_loop.itersave;
148
149 /* context common to subroutines, evals and loops */
150 struct block {
151     I32         blku_oldsp;     /* stack pointer to copy stuff down to */
152     COP *       blku_oldcop;    /* old curcop pointer */
153     I32         blku_oldretsp;  /* return stack index */
154     I32         blku_oldmarksp; /* mark stack index */
155     I32         blku_oldscopesp;        /* scope stack index */
156     PMOP *      blku_oldpm;     /* values of pattern match vars */
157     U8          blku_gimme;     /* is this block running in list context? */
158
159     union {
160         struct block_sub        blku_sub;
161         struct block_eval       blku_eval;
162         struct block_loop       blku_loop;
163     } blk_u;
164 };
165 #define blk_oldsp       cx_u.cx_blk.blku_oldsp
166 #define blk_oldcop      cx_u.cx_blk.blku_oldcop
167 #define blk_oldretsp    cx_u.cx_blk.blku_oldretsp
168 #define blk_oldmarksp   cx_u.cx_blk.blku_oldmarksp
169 #define blk_oldscopesp  cx_u.cx_blk.blku_oldscopesp
170 #define blk_oldpm       cx_u.cx_blk.blku_oldpm
171 #define blk_gimme       cx_u.cx_blk.blku_gimme
172 #define blk_sub         cx_u.cx_blk.blk_u.blku_sub
173 #define blk_eval        cx_u.cx_blk.blk_u.blku_eval
174 #define blk_loop        cx_u.cx_blk.blk_u.blku_loop
175
176 /* Enter a block. */
177 #define PUSHBLOCK(cx,t,s) CXINC, cx = &cxstack[cxstack_ix],             \
178         cx->cx_type             = t,                                    \
179         cx->blk_oldsp           = s - stack_base,                       \
180         cx->blk_oldcop          = curcop,                               \
181         cx->blk_oldmarksp       = markstack_ptr - markstack,            \
182         cx->blk_oldscopesp      = scopestack_ix,                        \
183         cx->blk_oldretsp        = retstack_ix,                          \
184         cx->blk_oldpm           = curpm,                                \
185         cx->blk_gimme           = gimme;                                \
186         if (debug & 4)                                                  \
187             fprintf(stderr,"Entering block %d, type %d\n",              \
188                 cxstack_ix, t); 
189
190 /* Exit a block (RETURN and LAST). */
191 #define POPBLOCK(cx) cx = &cxstack[cxstack_ix--],                       \
192         newsp           = stack_base + cx->blk_oldsp,                   \
193         curcop          = cx->blk_oldcop,                               \
194         markstack_ptr   = markstack + cx->blk_oldmarksp,                \
195         scopestack_ix   = cx->blk_oldscopesp,                           \
196         retstack_ix     = cx->blk_oldretsp,                             \
197         curpm           = cx->blk_oldpm,                                \
198         gimme           = cx->blk_gimme;                                \
199         if (debug & 4)                                                  \
200             fprintf(stderr,"Leaving block %d, type %d\n",               \
201                 cxstack_ix+1,cx->cx_type);
202
203 /* Continue a block elsewhere (NEXT and REDO). */
204 #define TOPBLOCK(cx) cx = &cxstack[cxstack_ix],                         \
205         stack_sp        = stack_base + cx->blk_oldsp,                   \
206         markstack_ptr   = markstack + cx->blk_oldmarksp,                \
207         scopestack_ix   = cx->blk_oldscopesp,                           \
208         retstack_ix     = cx->blk_oldretsp
209
210 /* substitution context */
211 struct subst {
212     I32         sbu_iters;
213     I32         sbu_maxiters;
214     I32         sbu_safebase;
215     I32         sbu_once;
216     char *      sbu_orig;
217     SV *        sbu_dstr;
218     SV *        sbu_targ;
219     char *      sbu_s;
220     char *      sbu_m;
221     char *      sbu_strend;
222     char *      sbu_subbase;
223 };
224 #define sb_iters        cx_u.cx_subst.sbu_iters
225 #define sb_maxiters     cx_u.cx_subst.sbu_maxiters
226 #define sb_safebase     cx_u.cx_subst.sbu_safebase
227 #define sb_once         cx_u.cx_subst.sbu_once
228 #define sb_orig         cx_u.cx_subst.sbu_orig
229 #define sb_dstr         cx_u.cx_subst.sbu_dstr
230 #define sb_targ         cx_u.cx_subst.sbu_targ
231 #define sb_s            cx_u.cx_subst.sbu_s
232 #define sb_m            cx_u.cx_subst.sbu_m
233 #define sb_strend       cx_u.cx_subst.sbu_strend
234 #define sb_subbase      cx_u.cx_subst.sbu_subbase
235
236 #define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix],                 \
237         cx->sb_iters            = iters,                                \
238         cx->sb_maxiters         = maxiters,                             \
239         cx->sb_safebase         = safebase,                             \
240         cx->sb_once             = once,                                 \
241         cx->sb_orig             = orig,                                 \
242         cx->sb_dstr             = dstr,                                 \
243         cx->sb_targ             = targ,                                 \
244         cx->sb_s                = s,                                    \
245         cx->sb_m                = m,                                    \
246         cx->sb_strend           = strend,                               \
247         cx->cx_type             = CXt_SUBST
248
249 #define POPSUBST(cx) cxstack_ix--
250
251 struct context {
252     I32         cx_type;        /* what kind of context this is */
253     union {
254         struct block    cx_blk;
255         struct subst    cx_subst;
256     } cx_u;
257 };
258 #define CXt_NULL        0
259 #define CXt_SUB         1
260 #define CXt_EVAL        2
261 #define CXt_LOOP        3
262 #define CXt_SUBST       4
263 #define CXt_BLOCK       5
264
265 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
266
267 /* "gimme" values */
268 #define G_SCALAR 0
269 #define G_ARRAY 1
270