This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new (Feb 1) perlembed.pod
[perl5.git] / cop.h
CommitLineData
a0d0e21e 1/* cop.h
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, Larry Wall
79072805
LW
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 *
79072805
LW
8 */
9
79072805
LW
10struct cop {
11 BASEOP
a0d0e21e 12 char * cop_label; /* label for this construct */
79072805
LW
13 HV * cop_stash; /* package line was compiled in */
14 GV * cop_filegv; /* file the following line # is from */
a0d0e21e
LW
15 U32 cop_seq; /* parse sequence number */
16 I32 cop_arybase; /* array base this line was compiled with */
79072805 17 line_t cop_line; /* line # of this command */
79072805
LW
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 */
27struct block_sub {
28 CV * cv;
29 GV * gv;
463ee0b2 30 GV * dfoutgv;
79072805
LW
31 AV * savearray;
32 AV * argarray;
33 U16 olddepth;
34 U8 hasargs;
35};
36
37#define PUSHSUB(cx) \
38 cx->blk_sub.cv = cv; \
79072805
LW
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; \
4633a7c4 45 cx->blk_sub.hasargs = 0; \
463ee0b2 46 cx->blk_sub.dfoutgv = defoutgv; \
4633a7c4 47 (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
79072805 48
44a8e56a
PP
49/* We muck with cxstack_ix since _dec may call a DESTROY, overwriting cx. */
50
79072805 51#define POPSUB(cx) \
774d564b
PP
52 if (cx->blk_sub.hasargs) { \
53 /* put back old @_ */ \
a6006777 54 SvREFCNT_dec(GvAV(defgv)); \
79072805 55 GvAV(defgv) = cx->blk_sub.savearray; \
774d564b
PP
56 /* destroy arg array */ \
57 av_clear(cx->blk_sub.argarray); \
58 AvREAL_off(cx->blk_sub.argarray); \
79072805 59 } \
8990e307 60 if (cx->blk_sub.cv) { \
44a8e56a
PP
61 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) { \
62 cxstack_ix++; \
a0d0e21e 63 SvREFCNT_dec((SV*)cx->blk_sub.cv); \
44a8e56a
PP
64 cxstack_ix--; \
65 } \
79072805
LW
66 }
67
68#define POPFORMAT(cx) \
4633a7c4
LW
69 setdefout(cx->blk_sub.dfoutgv); \
70 SvREFCNT_dec(cx->blk_sub.dfoutgv);
79072805
LW
71
72/* eval context */
73struct block_eval {
74 I32 old_in_eval;
75 I32 old_op_type;
76 char * old_name;
77 OP * old_eval_root;
748a9306 78 SV * cur_text;
79072805
LW
79};
80
8990e307 81#define PUSHEVAL(cx,n,fgv) \
79072805
LW
82 cx->blk_eval.old_in_eval = in_eval; \
83 cx->blk_eval.old_op_type = op->op_type; \
84 cx->blk_eval.old_name = n; \
748a9306
LW
85 cx->blk_eval.old_eval_root = eval_root; \
86 cx->blk_eval.cur_text = linestr;
79072805
LW
87
88#define POPEVAL(cx) \
89 in_eval = cx->blk_eval.old_in_eval; \
90 optype = cx->blk_eval.old_op_type; \
91 eval_root = cx->blk_eval.old_eval_root;
92
93/* loop context */
94struct block_loop {
95 char * label;
96 I32 resetsp;
97 OP * redo_op;
98 OP * next_op;
99 OP * last_op;
100 SV ** itervar;
101 SV * itersave;
5f05dabc 102 SV * iterlval;
79072805
LW
103 AV * iterary;
104 I32 iterix;
105};
106
107#define PUSHLOOP(cx, ivar, s) \
108 cx->blk_loop.label = curcop->cop_label; \
109 cx->blk_loop.resetsp = s - stack_base; \
110 cx->blk_loop.redo_op = cLOOP->op_redoop; \
111 cx->blk_loop.next_op = cLOOP->op_nextop; \
112 cx->blk_loop.last_op = cLOOP->op_lastop; \
7a4c00b4 113 if (cx->blk_loop.itervar = (ivar)) \
44a8e56a
PP
114 cx->blk_loop.itersave = SvREFCNT_inc(*cx->blk_loop.itervar);\
115 cx->blk_loop.iterlval = Nullsv; \
116 cx->blk_loop.iterary = Nullav; \
117 cx->blk_loop.iterix = -1;
79072805
LW
118
119#define POPLOOP(cx) \
5f05dabc 120 newsp = stack_base + cx->blk_loop.resetsp; \
7a4c00b4
PP
121 SvREFCNT_dec(cx->blk_loop.iterlval); \
122 if (cx->blk_loop.itervar) { \
123 SvREFCNT_dec(*cx->blk_loop.itervar); \
124 *cx->blk_loop.itervar = cx->blk_loop.itersave; \
44a8e56a
PP
125 } \
126 if (cx->blk_loop.iterary && cx->blk_loop.iterary != curstack) \
127 SvREFCNT_dec(cx->blk_loop.iterary);
79072805
LW
128
129/* context common to subroutines, evals and loops */
130struct block {
131 I32 blku_oldsp; /* stack pointer to copy stuff down to */
132 COP * blku_oldcop; /* old curcop pointer */
133 I32 blku_oldretsp; /* return stack index */
134 I32 blku_oldmarksp; /* mark stack index */
135 I32 blku_oldscopesp; /* scope stack index */
136 PMOP * blku_oldpm; /* values of pattern match vars */
137 U8 blku_gimme; /* is this block running in list context? */
138
139 union {
140 struct block_sub blku_sub;
141 struct block_eval blku_eval;
142 struct block_loop blku_loop;
143 } blk_u;
144};
145#define blk_oldsp cx_u.cx_blk.blku_oldsp
146#define blk_oldcop cx_u.cx_blk.blku_oldcop
147#define blk_oldretsp cx_u.cx_blk.blku_oldretsp
148#define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
149#define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
150#define blk_oldpm cx_u.cx_blk.blku_oldpm
151#define blk_gimme cx_u.cx_blk.blku_gimme
152#define blk_sub cx_u.cx_blk.blk_u.blku_sub
153#define blk_eval cx_u.cx_blk.blk_u.blku_eval
154#define blk_loop cx_u.cx_blk.blk_u.blku_loop
155
156/* Enter a block. */
8990e307 157#define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix], \
79072805 158 cx->cx_type = t, \
8990e307 159 cx->blk_oldsp = sp - stack_base, \
79072805
LW
160 cx->blk_oldcop = curcop, \
161 cx->blk_oldmarksp = markstack_ptr - markstack, \
162 cx->blk_oldscopesp = scopestack_ix, \
163 cx->blk_oldretsp = retstack_ix, \
164 cx->blk_oldpm = curpm, \
165 cx->blk_gimme = gimme; \
760ac839 166 DEBUG_l( PerlIO_printf(PerlIO_stderr(), "Entering block %ld, type %s\n", \
a0d0e21e 167 (long)cxstack_ix, block_type[t]); )
79072805
LW
168
169/* Exit a block (RETURN and LAST). */
a0d0e21e 170#define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--], \
79072805
LW
171 newsp = stack_base + cx->blk_oldsp, \
172 curcop = cx->blk_oldcop, \
173 markstack_ptr = markstack + cx->blk_oldmarksp, \
174 scopestack_ix = cx->blk_oldscopesp, \
175 retstack_ix = cx->blk_oldretsp, \
a0d0e21e 176 pm = cx->blk_oldpm, \
79072805 177 gimme = cx->blk_gimme; \
760ac839 178 DEBUG_l( PerlIO_printf(PerlIO_stderr(), "Leaving block %ld, type %s\n", \
a0d0e21e 179 (long)cxstack_ix+1,block_type[cx->cx_type]); )
79072805
LW
180
181/* Continue a block elsewhere (NEXT and REDO). */
182#define TOPBLOCK(cx) cx = &cxstack[cxstack_ix], \
183 stack_sp = stack_base + cx->blk_oldsp, \
184 markstack_ptr = markstack + cx->blk_oldmarksp, \
185 scopestack_ix = cx->blk_oldscopesp, \
186 retstack_ix = cx->blk_oldretsp
187
188/* substitution context */
189struct subst {
190 I32 sbu_iters;
191 I32 sbu_maxiters;
192 I32 sbu_safebase;
4633a7c4 193 I32 sbu_oldsave;
71be2cbc
PP
194 bool sbu_once;
195 bool sbu_rxtainted;
79072805
LW
196 char * sbu_orig;
197 SV * sbu_dstr;
198 SV * sbu_targ;
199 char * sbu_s;
200 char * sbu_m;
201 char * sbu_strend;
202 char * sbu_subbase;
c07a80fd 203 REGEXP * sbu_rx;
79072805
LW
204};
205#define sb_iters cx_u.cx_subst.sbu_iters
206#define sb_maxiters cx_u.cx_subst.sbu_maxiters
207#define sb_safebase cx_u.cx_subst.sbu_safebase
4633a7c4 208#define sb_oldsave cx_u.cx_subst.sbu_oldsave
71be2cbc
PP
209#define sb_once cx_u.cx_subst.sbu_once
210#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
211#define sb_orig cx_u.cx_subst.sbu_orig
212#define sb_dstr cx_u.cx_subst.sbu_dstr
213#define sb_targ cx_u.cx_subst.sbu_targ
214#define sb_s cx_u.cx_subst.sbu_s
215#define sb_m cx_u.cx_subst.sbu_m
216#define sb_strend cx_u.cx_subst.sbu_strend
217#define sb_subbase cx_u.cx_subst.sbu_subbase
c07a80fd 218#define sb_rx cx_u.cx_subst.sbu_rx
79072805
LW
219
220#define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix], \
221 cx->sb_iters = iters, \
222 cx->sb_maxiters = maxiters, \
223 cx->sb_safebase = safebase, \
4633a7c4 224 cx->sb_oldsave = oldsave, \
71be2cbc
PP
225 cx->sb_once = once, \
226 cx->sb_rxtainted = rxtainted, \
79072805
LW
227 cx->sb_orig = orig, \
228 cx->sb_dstr = dstr, \
229 cx->sb_targ = targ, \
230 cx->sb_s = s, \
231 cx->sb_m = m, \
232 cx->sb_strend = strend, \
c07a80fd 233 cx->sb_rx = rx, \
79072805
LW
234 cx->cx_type = CXt_SUBST
235
236#define POPSUBST(cx) cxstack_ix--
237
238struct context {
239 I32 cx_type; /* what kind of context this is */
240 union {
241 struct block cx_blk;
242 struct subst cx_subst;
243 } cx_u;
244};
245#define CXt_NULL 0
246#define CXt_SUB 1
247#define CXt_EVAL 2
248#define CXt_LOOP 3
249#define CXt_SUBST 4
250#define CXt_BLOCK 5
251
252#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
253
254/* "gimme" values */
a0d0e21e
LW
255#define G_SCALAR 0
256#define G_ARRAY 1
79072805 257
a0d0e21e
LW
258/* extra flags for perl_call_* routines */
259#define G_DISCARD 2 /* Call FREETMPS. */
260#define G_EVAL 4 /* Assume eval {} around subroutine call. */
261#define G_NOARGS 8 /* Don't construct a @_ array. */
4633a7c4 262#define G_KEEPERR 16 /* Append errors to $@ rather than overwriting it */