This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate changes#2978,2979 from mainline
[perl5.git] / cop.h
CommitLineData
a0d0e21e 1/* cop.h
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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;
6d4ff0d2 31#ifndef USE_THREADS
79072805 32 AV * savearray;
6d4ff0d2 33#endif /* USE_THREADS */
79072805
LW
34 AV * argarray;
35 U16 olddepth;
36 U8 hasargs;
37};
38
39#define PUSHSUB(cx) \
40 cx->blk_sub.cv = cv; \
79072805
LW
41 cx->blk_sub.olddepth = CvDEPTH(cv); \
42 cx->blk_sub.hasargs = hasargs;
43
44#define PUSHFORMAT(cx) \
45 cx->blk_sub.cv = cv; \
46 cx->blk_sub.gv = gv; \
4633a7c4 47 cx->blk_sub.hasargs = 0; \
3280af22 48 cx->blk_sub.dfoutgv = PL_defoutgv; \
4633a7c4 49 (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
79072805
LW
50
51#define POPSUB(cx) \
f86702cc 52 { struct block_sub cxsub; \
53 POPSUB1(cx); \
54 POPSUB2(); }
55
56#define POPSUB1(cx) \
57 cxsub = cx->blk_sub; /* because DESTROY may clobber *cx */
58
6d4ff0d2
MB
59#ifdef USE_THREADS
60#define POPSAVEARRAY() NOOP
61#else
62#define POPSAVEARRAY() \
63 STMT_START { \
3280af22
NIS
64 SvREFCNT_dec(GvAV(PL_defgv)); \
65 GvAV(PL_defgv) = cxsub.savearray; \
6d4ff0d2
MB
66 } STMT_END
67#endif /* USE_THREADS */
68
f86702cc 69#define POPSUB2() \
70 if (cxsub.hasargs) { \
6d4ff0d2 71 POPSAVEARRAY(); \
774d564b 72 /* destroy arg array */ \
f86702cc 73 av_clear(cxsub.argarray); \
74 AvREAL_off(cxsub.argarray); \
79072805 75 } \
f86702cc 76 if (cxsub.cv) { \
77 if (!(CvDEPTH(cxsub.cv) = cxsub.olddepth)) \
78 SvREFCNT_dec(cxsub.cv); \
79072805
LW
79 }
80
81#define POPFORMAT(cx) \
4633a7c4
LW
82 setdefout(cx->blk_sub.dfoutgv); \
83 SvREFCNT_dec(cx->blk_sub.dfoutgv);
79072805
LW
84
85/* eval context */
86struct block_eval {
87 I32 old_in_eval;
88 I32 old_op_type;
89 char * old_name;
90 OP * old_eval_root;
748a9306 91 SV * cur_text;
79072805
LW
92};
93
8990e307 94#define PUSHEVAL(cx,n,fgv) \
3280af22 95 cx->blk_eval.old_in_eval = PL_in_eval; \
533c011a 96 cx->blk_eval.old_op_type = PL_op->op_type; \
79072805 97 cx->blk_eval.old_name = n; \
3280af22
NIS
98 cx->blk_eval.old_eval_root = PL_eval_root; \
99 cx->blk_eval.cur_text = PL_linestr;
79072805
LW
100
101#define POPEVAL(cx) \
3280af22 102 PL_in_eval = cx->blk_eval.old_in_eval; \
79072805 103 optype = cx->blk_eval.old_op_type; \
3280af22 104 PL_eval_root = cx->blk_eval.old_eval_root;
79072805
LW
105
106/* loop context */
107struct block_loop {
108 char * label;
109 I32 resetsp;
110 OP * redo_op;
111 OP * next_op;
112 OP * last_op;
113 SV ** itervar;
114 SV * itersave;
5f05dabc 115 SV * iterlval;
79072805 116 AV * iterary;
89ea2908
GA
117 IV iterix;
118 IV itermax;
79072805
LW
119};
120
121#define PUSHLOOP(cx, ivar, s) \
3280af22
NIS
122 cx->blk_loop.label = PL_curcop->cop_label; \
123 cx->blk_loop.resetsp = s - PL_stack_base; \
79072805
LW
124 cx->blk_loop.redo_op = cLOOP->op_redoop; \
125 cx->blk_loop.next_op = cLOOP->op_nextop; \
126 cx->blk_loop.last_op = cLOOP->op_lastop; \
7a4c00b4 127 if (cx->blk_loop.itervar = (ivar)) \
44a8e56a 128 cx->blk_loop.itersave = SvREFCNT_inc(*cx->blk_loop.itervar);\
129 cx->blk_loop.iterlval = Nullsv; \
130 cx->blk_loop.iterary = Nullav; \
131 cx->blk_loop.iterix = -1;
79072805
LW
132
133#define POPLOOP(cx) \
f86702cc 134 { struct block_loop cxloop; \
135 POPLOOP1(cx); \
136 POPLOOP2(); }
137
138#define POPLOOP1(cx) \
4fdae800 139 cxloop = cx->blk_loop; /* because DESTROY may clobber *cx */ \
3280af22 140 newsp = PL_stack_base + cxloop.resetsp;
f86702cc 141
142#define POPLOOP2() \
f86702cc 143 SvREFCNT_dec(cxloop.iterlval); \
144 if (cxloop.itervar) { \
35aa8965 145 sv_2mortal(*cxloop.itervar); \
f86702cc 146 *cxloop.itervar = cxloop.itersave; \
44a8e56a 147 } \
3280af22 148 if (cxloop.iterary && cxloop.iterary != PL_curstack) \
f86702cc 149 SvREFCNT_dec(cxloop.iterary);
79072805
LW
150
151/* context common to subroutines, evals and loops */
152struct 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. */
8990e307 179#define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix], \
79072805 180 cx->cx_type = t, \
3280af22
NIS
181 cx->blk_oldsp = sp - PL_stack_base, \
182 cx->blk_oldcop = PL_curcop, \
a375155a 183 cx->blk_oldmarksp = PL_markstack_ptr - PL_markstack, \
3280af22 184 cx->blk_oldscopesp = PL_scopestack_ix, \
a375155a 185 cx->blk_oldretsp = PL_retstack_ix, \
3280af22 186 cx->blk_oldpm = PL_curpm, \
79072805 187 cx->blk_gimme = gimme; \
760ac839 188 DEBUG_l( PerlIO_printf(PerlIO_stderr(), "Entering block %ld, type %s\n", \
a375155a 189 (long)cxstack_ix, block_type[CxTYPE(cx)]); )
79072805
LW
190
191/* Exit a block (RETURN and LAST). */
a0d0e21e 192#define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--], \
a375155a 193 newsp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
194 PL_curcop = cx->blk_oldcop, \
195 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
196 PL_scopestack_ix = cx->blk_oldscopesp, \
197 PL_retstack_ix = cx->blk_oldretsp, \
198 pm = cx->blk_oldpm, \
199 gimme = cx->blk_gimme; \
760ac839 200 DEBUG_l( PerlIO_printf(PerlIO_stderr(), "Leaving block %ld, type %s\n", \
4e43f39e 201 (long)cxstack_ix+1,block_type[CxTYPE(cx)]); )
79072805
LW
202
203/* Continue a block elsewhere (NEXT and REDO). */
3280af22 204#define TOPBLOCK(cx) cx = &cxstack[cxstack_ix], \
a375155a 205 PL_stack_sp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
206 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
207 PL_scopestack_ix = cx->blk_oldscopesp, \
2c3f7d30
GB
208 PL_retstack_ix = cx->blk_oldretsp, \
209 PL_curpm = cx->blk_oldpm
79072805
LW
210
211/* substitution context */
212struct subst {
213 I32 sbu_iters;
214 I32 sbu_maxiters;
215 I32 sbu_safebase;
4633a7c4 216 I32 sbu_oldsave;
71be2cbc 217 bool sbu_once;
218 bool sbu_rxtainted;
79072805
LW
219 char * sbu_orig;
220 SV * sbu_dstr;
221 SV * sbu_targ;
222 char * sbu_s;
223 char * sbu_m;
224 char * sbu_strend;
c90c0ff4 225 void * sbu_rxres;
c07a80fd 226 REGEXP * sbu_rx;
79072805
LW
227};
228#define sb_iters cx_u.cx_subst.sbu_iters
229#define sb_maxiters cx_u.cx_subst.sbu_maxiters
230#define sb_safebase cx_u.cx_subst.sbu_safebase
4633a7c4 231#define sb_oldsave cx_u.cx_subst.sbu_oldsave
71be2cbc 232#define sb_once cx_u.cx_subst.sbu_once
233#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
234#define sb_orig cx_u.cx_subst.sbu_orig
235#define sb_dstr cx_u.cx_subst.sbu_dstr
236#define sb_targ cx_u.cx_subst.sbu_targ
237#define sb_s cx_u.cx_subst.sbu_s
238#define sb_m cx_u.cx_subst.sbu_m
239#define sb_strend cx_u.cx_subst.sbu_strend
c90c0ff4 240#define sb_rxres cx_u.cx_subst.sbu_rxres
c07a80fd 241#define sb_rx cx_u.cx_subst.sbu_rx
79072805
LW
242
243#define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix], \
244 cx->sb_iters = iters, \
245 cx->sb_maxiters = maxiters, \
246 cx->sb_safebase = safebase, \
4633a7c4 247 cx->sb_oldsave = oldsave, \
71be2cbc 248 cx->sb_once = once, \
249 cx->sb_rxtainted = rxtainted, \
79072805
LW
250 cx->sb_orig = orig, \
251 cx->sb_dstr = dstr, \
252 cx->sb_targ = targ, \
253 cx->sb_s = s, \
254 cx->sb_m = m, \
255 cx->sb_strend = strend, \
c90c0ff4 256 cx->sb_rxres = Null(void*), \
c07a80fd 257 cx->sb_rx = rx, \
c90c0ff4 258 cx->cx_type = CXt_SUBST; \
259 rxres_save(&cx->sb_rxres, rx)
79072805 260
c90c0ff4 261#define POPSUBST(cx) cx = &cxstack[cxstack_ix--]; \
262 rxres_free(&cx->sb_rxres)
79072805
LW
263
264struct context {
4e43f39e 265 U32 cx_type; /* what kind of context this is */
79072805
LW
266 union {
267 struct block cx_blk;
268 struct subst cx_subst;
269 } cx_u;
270};
4e43f39e
GB
271
272#define CXTYPEMASK 0xff
79072805
LW
273#define CXt_NULL 0
274#define CXt_SUB 1
275#define CXt_EVAL 2
276#define CXt_LOOP 3
277#define CXt_SUBST 4
278#define CXt_BLOCK 5
279
4e43f39e
GB
280/* private flags for CXt_EVAL */
281#define CXp_REAL 0x00000100 /* truly eval'', not a lookalike */
282
283#define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
284#define CxREALEVAL(c) (((c)->cx_type & (CXt_EVAL|CXp_REAL)) == (CXt_EVAL|CXp_REAL))
285
79072805
LW
286#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
287
288/* "gimme" values */
a0d0e21e
LW
289#define G_SCALAR 0
290#define G_ARRAY 1
54310121 291#define G_VOID 128 /* skip this bit when adding flags below */
79072805 292
a0d0e21e
LW
293/* extra flags for perl_call_* routines */
294#define G_DISCARD 2 /* Call FREETMPS. */
295#define G_EVAL 4 /* Assume eval {} around subroutine call. */
296#define G_NOARGS 8 /* Don't construct a @_ array. */
54310121 297#define G_KEEPERR 16 /* Append errors to $@, don't overwrite it */
491527d0 298#define G_NODEBUG 32 /* Disable debugging at toplevel. */
e336de0d
GS
299
300/* Support for switching (stack and block) contexts.
301 * This ensures magic doesn't invalidate local stack and cx pointers.
302 */
303
e788e7d3
GS
304#define PERLSI_UNKNOWN -1
305#define PERLSI_UNDEF 0
306#define PERLSI_MAIN 1
307#define PERLSI_MAGIC 2
308#define PERLSI_SORT 3
309#define PERLSI_SIGNAL 4
310#define PERLSI_OVERLOAD 5
311#define PERLSI_DESTROY 6
312#define PERLSI_WARNHOOK 7
313#define PERLSI_DIEHOOK 8
314#define PERLSI_REQUIRE 9
e336de0d
GS
315
316struct stackinfo {
317 AV * si_stack; /* stack for current runlevel */
318 PERL_CONTEXT * si_cxstack; /* context stack for runlevel */
319 I32 si_cxix; /* current context index */
320 I32 si_cxmax; /* maximum allocated index */
321 I32 si_type; /* type of runlevel */
322 struct stackinfo * si_prev;
323 struct stackinfo * si_next;
324 I32 * si_markbase; /* where markstack begins for us.
325 * currently used only with DEBUGGING,
326 * but not #ifdef-ed for bincompat */
327};
328
329typedef struct stackinfo PERL_SI;
330
3280af22
NIS
331#define cxstack (PL_curstackinfo->si_cxstack)
332#define cxstack_ix (PL_curstackinfo->si_cxix)
333#define cxstack_max (PL_curstackinfo->si_cxmax)
e336de0d
GS
334
335#ifdef DEBUGGING
3280af22 336# define SET_MARKBASE PL_curstackinfo->si_markbase = PL_markstack_ptr
e336de0d
GS
337#else
338# define SET_MARKBASE NOOP
339#endif
340
d3acc0f7 341#define PUSHSTACKi(type) \
e336de0d 342 STMT_START { \
3280af22 343 PERL_SI *next = PL_curstackinfo->si_next; \
e336de0d
GS
344 if (!next) { \
345 next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \
3280af22
NIS
346 next->si_prev = PL_curstackinfo; \
347 PL_curstackinfo->si_next = next; \
e336de0d
GS
348 } \
349 next->si_type = type; \
350 next->si_cxix = -1; \
351 AvFILLp(next->si_stack) = 0; \
3280af22
NIS
352 SWITCHSTACK(PL_curstack,next->si_stack); \
353 PL_curstackinfo = next; \
e336de0d
GS
354 SET_MARKBASE; \
355 } STMT_END
356
e788e7d3 357#define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
d3acc0f7
JP
358
359#define POPSTACK \
e336de0d 360 STMT_START { \
3280af22 361 PERL_SI *prev = PL_curstackinfo->si_prev; \
e336de0d
GS
362 if (!prev) { \
363 PerlIO_printf(PerlIO_stderr(), "panic: POPSTACK\n"); \
364 my_exit(1); \
365 } \
3280af22 366 SWITCHSTACK(PL_curstack,prev->si_stack); \
e336de0d 367 /* don't free prev here, free them all at the END{} */ \
3280af22 368 PL_curstackinfo = prev; \
e336de0d
GS
369 } STMT_END
370
371#define POPSTACK_TO(s) \
372 STMT_START { \
3280af22 373 while (PL_curstack != s) { \
bac4b2ad 374 dounwind(-1); \
d3acc0f7 375 POPSTACK; \
bac4b2ad 376 } \
e336de0d 377 } STMT_END