This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
another change towards a shareable optree: avoid pointer to filegv
[perl5.git] / cop.h
CommitLineData
a0d0e21e 1/* cop.h
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, 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 13 HV * cop_stash; /* package line was compiled in */
57843af0
GS
14#ifdef USE_ITHREADS
15 char * cop_file; /* file name the following line # is from */
16#else
79072805 17 GV * cop_filegv; /* file the following line # is from */
57843af0 18#endif
a0d0e21e
LW
19 U32 cop_seq; /* parse sequence number */
20 I32 cop_arybase; /* array base this line was compiled with */
79072805 21 line_t cop_line; /* line # of this command */
599cee73 22 SV * cop_warnings; /* lexical warnings bitmask */
79072805
LW
23};
24
25#define Nullcop Null(COP*)
26
57843af0
GS
27#ifdef USE_ITHREADS
28# define CopFILE(c) ((c)->cop_file)
29# define CopFILEGV(c) (CopFILE(c) \
30 ? gv_fetchfile(CopFILE(c)) : Nullgv)
31# define CopFILE_set(c,pv) ((c)->cop_file = savepv(pv)) /* XXX */
32# define CopFILESV(c) (CopFILE(c) \
33 ? GvSV(gv_fetchfile(CopFILE(c))) : Nullsv)
34# define CopFILEAV(c) (CopFILE(c) \
35 ? GvAV(gv_fetchfile(CopFILE(c))) : Nullav)
36# define CopSTASH(c) (CopSTASHPV(c) \
37 ? gv_fetchstash(CopSTASHPV(c)) : Nullhv)
38# define CopSTASH_set(c,hv) ((c)->cop_stashpv = HvNAME(hv)) /* XXX */
39# define CopSTASHPV_set(c,pv) ((c)->cop_stashpv = pv)
40# define CopSTASHPV(c) ((c)->cop_stashpv)
41#else
42# define CopFILEGV(c) ((c)->cop_filegv)
43# define CopFILEGV_set(c,gv) ((c)->cop_filegv = gv)
44# define CopFILE_set(c,pv) ((c)->cop_filegv = gv_fetchfile(pv))
45# define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : Nullsv)
46# define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : Nullav)
47# define CopFILE(c) (CopFILESV(c) ? SvPVX(CopFILESV(c)) : Nullch)
48# define CopSTASH(c) ((c)->cop_stash)
49# define CopSTASH_set(c,hv) ((c)->cop_stash = hv)
50# define CopSTASHPV(c) (CopSTASH(c) ? HvNAME(CopSTASH(c)) : Nullch)
51#endif /* USE_ITHREADS */
52
cc49e20b 53#define CopLINE(c) ((c)->cop_line)
57843af0
GS
54#define CopLINE_inc(c) (++CopLINE(c))
55#define CopLINE_dec(c) (--CopLINE(c))
56#define CopLINE_set(c,l) (CopLINE(c) = (l))
cc49e20b 57
79072805
LW
58/*
59 * Here we have some enormously heavy (or at least ponderous) wizardry.
60 */
61
62/* subroutine context */
63struct block_sub {
64 CV * cv;
65 GV * gv;
463ee0b2 66 GV * dfoutgv;
6d4ff0d2 67#ifndef USE_THREADS
79072805 68 AV * savearray;
6d4ff0d2 69#endif /* USE_THREADS */
79072805
LW
70 AV * argarray;
71 U16 olddepth;
72 U8 hasargs;
cd06dffe 73 U8 lval; /* XXX merge lval and hasargs? */
79072805
LW
74};
75
76#define PUSHSUB(cx) \
77 cx->blk_sub.cv = cv; \
79072805 78 cx->blk_sub.olddepth = CvDEPTH(cv); \
cd06dffe
GS
79 cx->blk_sub.hasargs = hasargs; \
80 cx->blk_sub.lval = PL_op->op_private & \
81 (OPpLVAL_INTRO|OPpENTERSUB_INARGS);
79072805
LW
82
83#define PUSHFORMAT(cx) \
84 cx->blk_sub.cv = cv; \
85 cx->blk_sub.gv = gv; \
4633a7c4 86 cx->blk_sub.hasargs = 0; \
3280af22 87 cx->blk_sub.dfoutgv = PL_defoutgv; \
4633a7c4 88 (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
79072805 89
6d4ff0d2
MB
90#ifdef USE_THREADS
91#define POPSAVEARRAY() NOOP
92#else
93#define POPSAVEARRAY() \
94 STMT_START { \
3280af22 95 SvREFCNT_dec(GvAV(PL_defgv)); \
a8bba7fa 96 GvAV(PL_defgv) = cx->blk_sub.savearray; \
6d4ff0d2
MB
97 } STMT_END
98#endif /* USE_THREADS */
99
b0d9ce38
GS
100#define POPSUB(cx,sv) \
101 STMT_START { \
a8bba7fa 102 if (cx->blk_sub.hasargs) { \
6d4ff0d2 103 POPSAVEARRAY(); \
d8b46c1b 104 /* abandon @_ if it got reified */ \
a8bba7fa
GS
105 if (AvREAL(cx->blk_sub.argarray)) { \
106 SSize_t fill = AvFILLp(cx->blk_sub.argarray); \
107 SvREFCNT_dec(cx->blk_sub.argarray); \
108 cx->blk_sub.argarray = newAV(); \
109 av_extend(cx->blk_sub.argarray, fill); \
110 AvFLAGS(cx->blk_sub.argarray) = AVf_REIFY; \
111 PL_curpad[0] = (SV*)cx->blk_sub.argarray; \
d8b46c1b 112 } \
79072805 113 } \
b0d9ce38
GS
114 sv = (SV*)cx->blk_sub.cv; \
115 if (sv && (CvDEPTH((CV*)sv) = cx->blk_sub.olddepth)) \
116 sv = Nullsv; \
117 } STMT_END
118
119#define LEAVESUB(sv) \
120 STMT_START { \
121 if (sv) \
122 SvREFCNT_dec(sv); \
123 } STMT_END
79072805
LW
124
125#define POPFORMAT(cx) \
4633a7c4
LW
126 setdefout(cx->blk_sub.dfoutgv); \
127 SvREFCNT_dec(cx->blk_sub.dfoutgv);
79072805
LW
128
129/* eval context */
130struct block_eval {
131 I32 old_in_eval;
132 I32 old_op_type;
133 char * old_name;
134 OP * old_eval_root;
748a9306 135 SV * cur_text;
79072805
LW
136};
137
8990e307 138#define PUSHEVAL(cx,n,fgv) \
3280af22 139 cx->blk_eval.old_in_eval = PL_in_eval; \
a8bba7fa 140 cx->blk_eval.old_op_type = PL_op->op_type; \
79072805 141 cx->blk_eval.old_name = n; \
a8bba7fa 142 cx->blk_eval.old_eval_root = PL_eval_root; \
3280af22 143 cx->blk_eval.cur_text = PL_linestr;
79072805
LW
144
145#define POPEVAL(cx) \
3280af22 146 PL_in_eval = cx->blk_eval.old_in_eval; \
79072805 147 optype = cx->blk_eval.old_op_type; \
3280af22 148 PL_eval_root = cx->blk_eval.old_eval_root;
79072805
LW
149
150/* loop context */
151struct block_loop {
152 char * label;
153 I32 resetsp;
154 OP * redo_op;
155 OP * next_op;
156 OP * last_op;
157 SV ** itervar;
158 SV * itersave;
5f05dabc 159 SV * iterlval;
79072805 160 AV * iterary;
89ea2908
GA
161 IV iterix;
162 IV itermax;
79072805
LW
163};
164
165#define PUSHLOOP(cx, ivar, s) \
38a230cb
GS
166 cx->blk_loop.label = PL_curcop->cop_label; \
167 cx->blk_loop.resetsp = s - PL_stack_base; \
79072805
LW
168 cx->blk_loop.redo_op = cLOOP->op_redoop; \
169 cx->blk_loop.next_op = cLOOP->op_nextop; \
170 cx->blk_loop.last_op = cLOOP->op_lastop; \
7a4c00b4 171 if (cx->blk_loop.itervar = (ivar)) \
44a8e56a 172 cx->blk_loop.itersave = SvREFCNT_inc(*cx->blk_loop.itervar);\
173 cx->blk_loop.iterlval = Nullsv; \
174 cx->blk_loop.iterary = Nullav; \
175 cx->blk_loop.iterix = -1;
79072805
LW
176
177#define POPLOOP(cx) \
a8bba7fa
GS
178 SvREFCNT_dec(cx->blk_loop.iterlval); \
179 if (cx->blk_loop.itervar) { \
180 sv_2mortal(*(cx->blk_loop.itervar)); \
181 *(cx->blk_loop.itervar) = cx->blk_loop.itersave; \
44a8e56a 182 } \
a8bba7fa
GS
183 if (cx->blk_loop.iterary && cx->blk_loop.iterary != PL_curstack)\
184 SvREFCNT_dec(cx->blk_loop.iterary);
79072805
LW
185
186/* context common to subroutines, evals and loops */
187struct block {
188 I32 blku_oldsp; /* stack pointer to copy stuff down to */
189 COP * blku_oldcop; /* old curcop pointer */
190 I32 blku_oldretsp; /* return stack index */
191 I32 blku_oldmarksp; /* mark stack index */
192 I32 blku_oldscopesp; /* scope stack index */
193 PMOP * blku_oldpm; /* values of pattern match vars */
194 U8 blku_gimme; /* is this block running in list context? */
195
196 union {
197 struct block_sub blku_sub;
198 struct block_eval blku_eval;
199 struct block_loop blku_loop;
200 } blk_u;
201};
202#define blk_oldsp cx_u.cx_blk.blku_oldsp
203#define blk_oldcop cx_u.cx_blk.blku_oldcop
204#define blk_oldretsp cx_u.cx_blk.blku_oldretsp
205#define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
206#define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
207#define blk_oldpm cx_u.cx_blk.blku_oldpm
208#define blk_gimme cx_u.cx_blk.blku_gimme
209#define blk_sub cx_u.cx_blk.blk_u.blku_sub
210#define blk_eval cx_u.cx_blk.blk_u.blku_eval
211#define blk_loop cx_u.cx_blk.blk_u.blku_loop
212
213/* Enter a block. */
8990e307 214#define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix], \
79072805 215 cx->cx_type = t, \
3280af22
NIS
216 cx->blk_oldsp = sp - PL_stack_base, \
217 cx->blk_oldcop = PL_curcop, \
1aff0e91 218 cx->blk_oldmarksp = PL_markstack_ptr - PL_markstack, \
3280af22 219 cx->blk_oldscopesp = PL_scopestack_ix, \
1aff0e91 220 cx->blk_oldretsp = PL_retstack_ix, \
3280af22 221 cx->blk_oldpm = PL_curpm, \
79072805 222 cx->blk_gimme = gimme; \
bf49b057 223 DEBUG_l( PerlIO_printf(Perl_debug_log, "Entering block %ld, type %s\n", \
1aff0e91 224 (long)cxstack_ix, PL_block_type[CxTYPE(cx)]); )
79072805
LW
225
226/* Exit a block (RETURN and LAST). */
a0d0e21e 227#define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--], \
1aff0e91 228 newsp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
229 PL_curcop = cx->blk_oldcop, \
230 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
231 PL_scopestack_ix = cx->blk_oldscopesp, \
232 PL_retstack_ix = cx->blk_oldretsp, \
233 pm = cx->blk_oldpm, \
234 gimme = cx->blk_gimme; \
bf49b057 235 DEBUG_l( PerlIO_printf(Perl_debug_log, "Leaving block %ld, type %s\n", \
22c35a8c 236 (long)cxstack_ix+1,PL_block_type[CxTYPE(cx)]); )
79072805
LW
237
238/* Continue a block elsewhere (NEXT and REDO). */
3280af22 239#define TOPBLOCK(cx) cx = &cxstack[cxstack_ix], \
1aff0e91 240 PL_stack_sp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
241 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
242 PL_scopestack_ix = cx->blk_oldscopesp, \
2158dd9b
GS
243 PL_retstack_ix = cx->blk_oldretsp, \
244 PL_curpm = cx->blk_oldpm
79072805
LW
245
246/* substitution context */
247struct subst {
248 I32 sbu_iters;
249 I32 sbu_maxiters;
22e551b9 250 I32 sbu_rflags;
4633a7c4 251 I32 sbu_oldsave;
71be2cbc 252 bool sbu_once;
253 bool sbu_rxtainted;
79072805
LW
254 char * sbu_orig;
255 SV * sbu_dstr;
256 SV * sbu_targ;
257 char * sbu_s;
258 char * sbu_m;
259 char * sbu_strend;
c90c0ff4 260 void * sbu_rxres;
c07a80fd 261 REGEXP * sbu_rx;
79072805
LW
262};
263#define sb_iters cx_u.cx_subst.sbu_iters
264#define sb_maxiters cx_u.cx_subst.sbu_maxiters
22e551b9 265#define sb_rflags cx_u.cx_subst.sbu_rflags
4633a7c4 266#define sb_oldsave cx_u.cx_subst.sbu_oldsave
71be2cbc 267#define sb_once cx_u.cx_subst.sbu_once
268#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
269#define sb_orig cx_u.cx_subst.sbu_orig
270#define sb_dstr cx_u.cx_subst.sbu_dstr
271#define sb_targ cx_u.cx_subst.sbu_targ
272#define sb_s cx_u.cx_subst.sbu_s
273#define sb_m cx_u.cx_subst.sbu_m
274#define sb_strend cx_u.cx_subst.sbu_strend
c90c0ff4 275#define sb_rxres cx_u.cx_subst.sbu_rxres
c07a80fd 276#define sb_rx cx_u.cx_subst.sbu_rx
79072805
LW
277
278#define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix], \
279 cx->sb_iters = iters, \
280 cx->sb_maxiters = maxiters, \
22e551b9 281 cx->sb_rflags = r_flags, \
4633a7c4 282 cx->sb_oldsave = oldsave, \
71be2cbc 283 cx->sb_once = once, \
284 cx->sb_rxtainted = rxtainted, \
79072805
LW
285 cx->sb_orig = orig, \
286 cx->sb_dstr = dstr, \
287 cx->sb_targ = targ, \
288 cx->sb_s = s, \
289 cx->sb_m = m, \
290 cx->sb_strend = strend, \
c90c0ff4 291 cx->sb_rxres = Null(void*), \
c07a80fd 292 cx->sb_rx = rx, \
c90c0ff4 293 cx->cx_type = CXt_SUBST; \
294 rxres_save(&cx->sb_rxres, rx)
79072805 295
c90c0ff4 296#define POPSUBST(cx) cx = &cxstack[cxstack_ix--]; \
297 rxres_free(&cx->sb_rxres)
79072805
LW
298
299struct context {
6b35e009 300 U32 cx_type; /* what kind of context this is */
79072805
LW
301 union {
302 struct block cx_blk;
303 struct subst cx_subst;
304 } cx_u;
305};
6b35e009
GS
306
307#define CXTYPEMASK 0xff
79072805
LW
308#define CXt_NULL 0
309#define CXt_SUB 1
310#define CXt_EVAL 2
311#define CXt_LOOP 3
312#define CXt_SUBST 4
313#define CXt_BLOCK 5
314
6b35e009
GS
315/* private flags for CXt_EVAL */
316#define CXp_REAL 0x00000100 /* truly eval'', not a lookalike */
317
318#define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
319#define CxREALEVAL(c) (((c)->cx_type & (CXt_EVAL|CXp_REAL)) == (CXt_EVAL|CXp_REAL))
320
79072805
LW
321#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
322
323/* "gimme" values */
a0d0e21e
LW
324#define G_SCALAR 0
325#define G_ARRAY 1
54310121 326#define G_VOID 128 /* skip this bit when adding flags below */
79072805 327
864dbfa3 328/* extra flags for Perl_call_* routines */
a0d0e21e
LW
329#define G_DISCARD 2 /* Call FREETMPS. */
330#define G_EVAL 4 /* Assume eval {} around subroutine call. */
331#define G_NOARGS 8 /* Don't construct a @_ array. */
54310121 332#define G_KEEPERR 16 /* Append errors to $@, don't overwrite it */
491527d0 333#define G_NODEBUG 32 /* Disable debugging at toplevel. */
e336de0d 334
faef0170
HS
335/* flag bits for PL_in_eval */
336#define EVAL_NULL 0 /* not in an eval */
337#define EVAL_INEVAL 1 /* some enclosing scope is an eval */
338#define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */
864dbfa3 339#define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */
faef0170 340
e336de0d
GS
341/* Support for switching (stack and block) contexts.
342 * This ensures magic doesn't invalidate local stack and cx pointers.
343 */
344
e788e7d3
GS
345#define PERLSI_UNKNOWN -1
346#define PERLSI_UNDEF 0
347#define PERLSI_MAIN 1
348#define PERLSI_MAGIC 2
349#define PERLSI_SORT 3
350#define PERLSI_SIGNAL 4
351#define PERLSI_OVERLOAD 5
352#define PERLSI_DESTROY 6
353#define PERLSI_WARNHOOK 7
354#define PERLSI_DIEHOOK 8
355#define PERLSI_REQUIRE 9
e336de0d
GS
356
357struct stackinfo {
358 AV * si_stack; /* stack for current runlevel */
359 PERL_CONTEXT * si_cxstack; /* context stack for runlevel */
360 I32 si_cxix; /* current context index */
361 I32 si_cxmax; /* maximum allocated index */
362 I32 si_type; /* type of runlevel */
363 struct stackinfo * si_prev;
364 struct stackinfo * si_next;
365 I32 * si_markbase; /* where markstack begins for us.
366 * currently used only with DEBUGGING,
367 * but not #ifdef-ed for bincompat */
368};
369
370typedef struct stackinfo PERL_SI;
371
3280af22
NIS
372#define cxstack (PL_curstackinfo->si_cxstack)
373#define cxstack_ix (PL_curstackinfo->si_cxix)
374#define cxstack_max (PL_curstackinfo->si_cxmax)
e336de0d
GS
375
376#ifdef DEBUGGING
3280af22 377# define SET_MARKBASE PL_curstackinfo->si_markbase = PL_markstack_ptr
e336de0d
GS
378#else
379# define SET_MARKBASE NOOP
380#endif
381
d3acc0f7 382#define PUSHSTACKi(type) \
e336de0d 383 STMT_START { \
3280af22 384 PERL_SI *next = PL_curstackinfo->si_next; \
e336de0d
GS
385 if (!next) { \
386 next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \
3280af22
NIS
387 next->si_prev = PL_curstackinfo; \
388 PL_curstackinfo->si_next = next; \
e336de0d
GS
389 } \
390 next->si_type = type; \
391 next->si_cxix = -1; \
392 AvFILLp(next->si_stack) = 0; \
3280af22
NIS
393 SWITCHSTACK(PL_curstack,next->si_stack); \
394 PL_curstackinfo = next; \
e336de0d
GS
395 SET_MARKBASE; \
396 } STMT_END
397
e788e7d3 398#define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
d3acc0f7 399
3095d977
GS
400/* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
401 * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
d3acc0f7 402#define POPSTACK \
e336de0d 403 STMT_START { \
de616352 404 djSP; \
3280af22 405 PERL_SI *prev = PL_curstackinfo->si_prev; \
e336de0d 406 if (!prev) { \
bf49b057 407 PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); \
e336de0d
GS
408 my_exit(1); \
409 } \
3280af22 410 SWITCHSTACK(PL_curstack,prev->si_stack); \
e336de0d 411 /* don't free prev here, free them all at the END{} */ \
3280af22 412 PL_curstackinfo = prev; \
e336de0d
GS
413 } STMT_END
414
415#define POPSTACK_TO(s) \
416 STMT_START { \
3280af22 417 while (PL_curstack != s) { \
bac4b2ad 418 dounwind(-1); \
d3acc0f7 419 POPSTACK; \
bac4b2ad 420 } \
e336de0d 421 } STMT_END