0b1868b6317e9bea1370983dbe03f995c327a534
[perl.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     U32         cop_seq;        /* parse sequence number */
53     short       cop_slen;       /* len of cop_short, if not null */
54     VOL short   cop_flags;      /* optimization flags--see above */
55     HV *        cop_stash;      /* package line was compiled in */
56     GV *        cop_filegv;     /* file the following line # is from */
57     line_t      cop_line;       /* line # of this command */
58     char        cop_type;       /* what this command does */
59 };
60
61 #define Nullcop Null(COP*)
62
63 /*
64  * Here we have some enormously heavy (or at least ponderous) wizardry.
65  */
66
67 /* subroutine context */
68 struct block_sub {
69     CV *        cv;
70     GV *        gv;
71     GV *        dfoutgv;
72     AV *        savearray;
73     AV *        argarray;
74     AV *        comppad;
75     U16         olddepth;
76     U8          hasargs;
77 };
78
79 #define PUSHSUB(cx)                                                     \
80         cx->blk_sub.cv = cv;                                            \
81         cx->blk_sub.gv = gv;                                            \
82         cx->blk_sub.olddepth = CvDEPTH(cv);                             \
83         cx->blk_sub.hasargs = hasargs;
84
85 #define PUSHFORMAT(cx)                                                  \
86         cx->blk_sub.cv = cv;                                            \
87         cx->blk_sub.gv = gv;                                            \
88         cx->blk_sub.dfoutgv = defoutgv;                                 \
89         cx->blk_sub.hasargs = 0;
90
91 #define POPSUB(cx)                                                      \
92         if (cx->blk_sub.hasargs) {   /* put back old @_ */              \
93             av_free(cx->blk_sub.argarray);                              \
94             GvAV(defgv) = cx->blk_sub.savearray;                        \
95         }                                                               \
96         if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) {        \
97             if (CvDELETED(cx->blk_sub.cv))                              \
98                 sv_free((SV*)cx->blk_sub.cv);                           \
99         }
100
101 #define POPFORMAT(cx)                                                   \
102         defoutgv = cx->blk_sub.dfoutgv;
103
104 /* eval context */
105 struct block_eval {
106     I32         old_in_eval;
107     I32         old_op_type;
108     char *      old_name;
109     OP *        old_eval_root;
110 };
111
112 #define PUSHEVAL(cx,n)                                                  \
113         cx->blk_eval.old_in_eval = in_eval;                             \
114         cx->blk_eval.old_op_type = op->op_type;                         \
115         cx->blk_eval.old_name = n;                                      \
116         cx->blk_eval.old_eval_root = eval_root;
117
118 #define POPEVAL(cx)                                                     \
119         in_eval = cx->blk_eval.old_in_eval;                             \
120         optype = cx->blk_eval.old_op_type;                              \
121         eval_root = cx->blk_eval.old_eval_root;
122
123 /* loop context */
124 struct block_loop {
125     char *      label;
126     I32         resetsp;
127     OP *        redo_op;
128     OP *        next_op;
129     OP *        last_op;
130     SV **       itervar;
131     SV *        itersave;
132     AV *        iterary;
133     I32         iterix;
134 };
135
136 #define PUSHLOOP(cx, ivar, s)                                           \
137         cx->blk_loop.label = curcop->cop_label;                         \
138         cx->blk_loop.resetsp = s - stack_base;                          \
139         cx->blk_loop.redo_op = cLOOP->op_redoop;                        \
140         cx->blk_loop.next_op = cLOOP->op_nextop;                        \
141         cx->blk_loop.last_op = cLOOP->op_lastop;                        \
142         cx->blk_loop.itervar = ivar;                                    \
143         if (ivar)                                                       \
144             cx->blk_loop.itersave = *cx->blk_loop.itervar;
145
146 #define POPLOOP(cx)                                                     \
147         newsp           = stack_base + cx->blk_loop.resetsp;            \
148         if (cx->blk_loop.itervar)                                       \
149             *cx->blk_loop.itervar = cx->blk_loop.itersave;
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,s) CXINC, cx = &cxstack[cxstack_ix],             \
180         cx->cx_type             = t,                                    \
181         cx->blk_oldsp           = s - stack_base,                       \
182         cx->blk_oldcop          = curcop,                               \
183         cx->blk_oldmarksp       = markstack_ptr - markstack,            \
184         cx->blk_oldscopesp      = scopestack_ix,                        \
185         cx->blk_oldretsp        = retstack_ix,                          \
186         cx->blk_oldpm           = curpm,                                \
187         cx->blk_gimme           = gimme;                                \
188         if (debug & 4)                                                  \
189             fprintf(stderr,"Entering block %d, type %d\n",              \
190                 cxstack_ix, t); 
191
192 /* Exit a block (RETURN and LAST). */
193 #define POPBLOCK(cx) cx = &cxstack[cxstack_ix--],                       \
194         newsp           = stack_base + cx->blk_oldsp,                   \
195         curcop          = cx->blk_oldcop,                               \
196         markstack_ptr   = markstack + cx->blk_oldmarksp,                \
197         scopestack_ix   = cx->blk_oldscopesp,                           \
198         retstack_ix     = cx->blk_oldretsp,                             \
199         curpm           = cx->blk_oldpm,                                \
200         gimme           = cx->blk_gimme;                                \
201         if (debug & 4)                                                  \
202             fprintf(stderr,"Leaving block %d, type %d\n",               \
203                 cxstack_ix+1,cx->cx_type);
204
205 /* Continue a block elsewhere (NEXT and REDO). */
206 #define TOPBLOCK(cx) cx = &cxstack[cxstack_ix],                         \
207         stack_sp        = stack_base + cx->blk_oldsp,                   \
208         markstack_ptr   = markstack + cx->blk_oldmarksp,                \
209         scopestack_ix   = cx->blk_oldscopesp,                           \
210         retstack_ix     = cx->blk_oldretsp
211
212 /* substitution context */
213 struct subst {
214     I32         sbu_iters;
215     I32         sbu_maxiters;
216     I32         sbu_safebase;
217     I32         sbu_once;
218     char *      sbu_orig;
219     SV *        sbu_dstr;
220     SV *        sbu_targ;
221     char *      sbu_s;
222     char *      sbu_m;
223     char *      sbu_strend;
224     char *      sbu_subbase;
225 };
226 #define sb_iters        cx_u.cx_subst.sbu_iters
227 #define sb_maxiters     cx_u.cx_subst.sbu_maxiters
228 #define sb_safebase     cx_u.cx_subst.sbu_safebase
229 #define sb_once         cx_u.cx_subst.sbu_once
230 #define sb_orig         cx_u.cx_subst.sbu_orig
231 #define sb_dstr         cx_u.cx_subst.sbu_dstr
232 #define sb_targ         cx_u.cx_subst.sbu_targ
233 #define sb_s            cx_u.cx_subst.sbu_s
234 #define sb_m            cx_u.cx_subst.sbu_m
235 #define sb_strend       cx_u.cx_subst.sbu_strend
236 #define sb_subbase      cx_u.cx_subst.sbu_subbase
237
238 #define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix],                 \
239         cx->sb_iters            = iters,                                \
240         cx->sb_maxiters         = maxiters,                             \
241         cx->sb_safebase         = safebase,                             \
242         cx->sb_once             = once,                                 \
243         cx->sb_orig             = orig,                                 \
244         cx->sb_dstr             = dstr,                                 \
245         cx->sb_targ             = targ,                                 \
246         cx->sb_s                = s,                                    \
247         cx->sb_m                = m,                                    \
248         cx->sb_strend           = strend,                               \
249         cx->cx_type             = CXt_SUBST
250
251 #define POPSUBST(cx) cxstack_ix--
252
253 struct context {
254     I32         cx_type;        /* what kind of context this is */
255     union {
256         struct block    cx_blk;
257         struct subst    cx_subst;
258     } cx_u;
259 };
260 #define CXt_NULL        0
261 #define CXt_SUB         1
262 #define CXt_EVAL        2
263 #define CXt_LOOP        3
264 #define CXt_SUBST       4
265 #define CXt_BLOCK       5
266
267 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
268
269 /* "gimme" values */
270 #define G_SCALAR 0
271 #define G_ARRAY 1
272