This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
revert POP{SUB,LOOP}{1,2} logic to the simpler pre-5.003_24
[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
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 */
599cee73 18 SV * cop_warnings; /* lexical warnings bitmask */
79072805
LW
19};
20
21#define Nullcop Null(COP*)
22
23/*
24 * Here we have some enormously heavy (or at least ponderous) wizardry.
25 */
26
27/* subroutine context */
28struct block_sub {
29 CV * cv;
30 GV * gv;
463ee0b2 31 GV * dfoutgv;
6d4ff0d2 32#ifndef USE_THREADS
79072805 33 AV * savearray;
6d4ff0d2 34#endif /* USE_THREADS */
79072805
LW
35 AV * argarray;
36 U16 olddepth;
37 U8 hasargs;
cd06dffe 38 U8 lval; /* XXX merge lval and hasargs? */
79072805
LW
39};
40
41#define PUSHSUB(cx) \
42 cx->blk_sub.cv = cv; \
79072805 43 cx->blk_sub.olddepth = CvDEPTH(cv); \
cd06dffe
GS
44 cx->blk_sub.hasargs = hasargs; \
45 cx->blk_sub.lval = PL_op->op_private & \
46 (OPpLVAL_INTRO|OPpENTERSUB_INARGS);
79072805
LW
47
48#define PUSHFORMAT(cx) \
49 cx->blk_sub.cv = cv; \
50 cx->blk_sub.gv = gv; \
4633a7c4 51 cx->blk_sub.hasargs = 0; \
3280af22 52 cx->blk_sub.dfoutgv = PL_defoutgv; \
4633a7c4 53 (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
79072805 54
6d4ff0d2
MB
55#ifdef USE_THREADS
56#define POPSAVEARRAY() NOOP
57#else
58#define POPSAVEARRAY() \
59 STMT_START { \
3280af22 60 SvREFCNT_dec(GvAV(PL_defgv)); \
a8bba7fa 61 GvAV(PL_defgv) = cx->blk_sub.savearray; \
6d4ff0d2
MB
62 } STMT_END
63#endif /* USE_THREADS */
64
a8bba7fa
GS
65#define POPSUB(cx) \
66 if (cx->blk_sub.hasargs) { \
6d4ff0d2 67 POPSAVEARRAY(); \
d8b46c1b 68 /* abandon @_ if it got reified */ \
a8bba7fa
GS
69 if (AvREAL(cx->blk_sub.argarray)) { \
70 SSize_t fill = AvFILLp(cx->blk_sub.argarray); \
71 SvREFCNT_dec(cx->blk_sub.argarray); \
72 cx->blk_sub.argarray = newAV(); \
73 av_extend(cx->blk_sub.argarray, fill); \
74 AvFLAGS(cx->blk_sub.argarray) = AVf_REIFY; \
75 PL_curpad[0] = (SV*)cx->blk_sub.argarray; \
d8b46c1b 76 } \
79072805 77 } \
a8bba7fa
GS
78 if (cx->blk_sub.cv) { \
79 if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth)) \
80 SvREFCNT_dec(cx->blk_sub.cv); \
79072805
LW
81 }
82
83#define POPFORMAT(cx) \
4633a7c4
LW
84 setdefout(cx->blk_sub.dfoutgv); \
85 SvREFCNT_dec(cx->blk_sub.dfoutgv);
79072805
LW
86
87/* eval context */
88struct block_eval {
89 I32 old_in_eval;
90 I32 old_op_type;
91 char * old_name;
92 OP * old_eval_root;
748a9306 93 SV * cur_text;
79072805
LW
94};
95
8990e307 96#define PUSHEVAL(cx,n,fgv) \
3280af22 97 cx->blk_eval.old_in_eval = PL_in_eval; \
a8bba7fa 98 cx->blk_eval.old_op_type = PL_op->op_type; \
79072805 99 cx->blk_eval.old_name = n; \
a8bba7fa 100 cx->blk_eval.old_eval_root = PL_eval_root; \
3280af22 101 cx->blk_eval.cur_text = PL_linestr;
79072805
LW
102
103#define POPEVAL(cx) \
3280af22 104 PL_in_eval = cx->blk_eval.old_in_eval; \
79072805 105 optype = cx->blk_eval.old_op_type; \
3280af22 106 PL_eval_root = cx->blk_eval.old_eval_root;
79072805
LW
107
108/* loop context */
109struct block_loop {
110 char * label;
111 I32 resetsp;
112 OP * redo_op;
113 OP * next_op;
114 OP * last_op;
115 SV ** itervar;
116 SV * itersave;
5f05dabc 117 SV * iterlval;
79072805 118 AV * iterary;
89ea2908
GA
119 IV iterix;
120 IV itermax;
79072805
LW
121};
122
123#define PUSHLOOP(cx, ivar, s) \
38a230cb
GS
124 cx->blk_loop.label = PL_curcop->cop_label; \
125 cx->blk_loop.resetsp = s - PL_stack_base; \
79072805
LW
126 cx->blk_loop.redo_op = cLOOP->op_redoop; \
127 cx->blk_loop.next_op = cLOOP->op_nextop; \
128 cx->blk_loop.last_op = cLOOP->op_lastop; \
7a4c00b4 129 if (cx->blk_loop.itervar = (ivar)) \
44a8e56a
PP
130 cx->blk_loop.itersave = SvREFCNT_inc(*cx->blk_loop.itervar);\
131 cx->blk_loop.iterlval = Nullsv; \
132 cx->blk_loop.iterary = Nullav; \
133 cx->blk_loop.iterix = -1;
79072805
LW
134
135#define POPLOOP(cx) \
a8bba7fa
GS
136 SvREFCNT_dec(cx->blk_loop.iterlval); \
137 if (cx->blk_loop.itervar) { \
138 sv_2mortal(*(cx->blk_loop.itervar)); \
139 *(cx->blk_loop.itervar) = cx->blk_loop.itersave; \
44a8e56a 140 } \
a8bba7fa
GS
141 if (cx->blk_loop.iterary && cx->blk_loop.iterary != PL_curstack)\
142 SvREFCNT_dec(cx->blk_loop.iterary);
79072805
LW
143
144/* context common to subroutines, evals and loops */
145struct block {
146 I32 blku_oldsp; /* stack pointer to copy stuff down to */
147 COP * blku_oldcop; /* old curcop pointer */
148 I32 blku_oldretsp; /* return stack index */
149 I32 blku_oldmarksp; /* mark stack index */
150 I32 blku_oldscopesp; /* scope stack index */
151 PMOP * blku_oldpm; /* values of pattern match vars */
152 U8 blku_gimme; /* is this block running in list context? */
153
154 union {
155 struct block_sub blku_sub;
156 struct block_eval blku_eval;
157 struct block_loop blku_loop;
158 } blk_u;
159};
160#define blk_oldsp cx_u.cx_blk.blku_oldsp
161#define blk_oldcop cx_u.cx_blk.blku_oldcop
162#define blk_oldretsp cx_u.cx_blk.blku_oldretsp
163#define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
164#define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
165#define blk_oldpm cx_u.cx_blk.blku_oldpm
166#define blk_gimme cx_u.cx_blk.blku_gimme
167#define blk_sub cx_u.cx_blk.blk_u.blku_sub
168#define blk_eval cx_u.cx_blk.blk_u.blku_eval
169#define blk_loop cx_u.cx_blk.blk_u.blku_loop
170
171/* Enter a block. */
8990e307 172#define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix], \
79072805 173 cx->cx_type = t, \
3280af22
NIS
174 cx->blk_oldsp = sp - PL_stack_base, \
175 cx->blk_oldcop = PL_curcop, \
1aff0e91 176 cx->blk_oldmarksp = PL_markstack_ptr - PL_markstack, \
3280af22 177 cx->blk_oldscopesp = PL_scopestack_ix, \
1aff0e91 178 cx->blk_oldretsp = PL_retstack_ix, \
3280af22 179 cx->blk_oldpm = PL_curpm, \
79072805 180 cx->blk_gimme = gimme; \
bf49b057 181 DEBUG_l( PerlIO_printf(Perl_debug_log, "Entering block %ld, type %s\n", \
1aff0e91 182 (long)cxstack_ix, PL_block_type[CxTYPE(cx)]); )
79072805
LW
183
184/* Exit a block (RETURN and LAST). */
a0d0e21e 185#define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--], \
1aff0e91 186 newsp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
187 PL_curcop = cx->blk_oldcop, \
188 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
189 PL_scopestack_ix = cx->blk_oldscopesp, \
190 PL_retstack_ix = cx->blk_oldretsp, \
191 pm = cx->blk_oldpm, \
192 gimme = cx->blk_gimme; \
bf49b057 193 DEBUG_l( PerlIO_printf(Perl_debug_log, "Leaving block %ld, type %s\n", \
22c35a8c 194 (long)cxstack_ix+1,PL_block_type[CxTYPE(cx)]); )
79072805
LW
195
196/* Continue a block elsewhere (NEXT and REDO). */
3280af22 197#define TOPBLOCK(cx) cx = &cxstack[cxstack_ix], \
1aff0e91 198 PL_stack_sp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
199 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
200 PL_scopestack_ix = cx->blk_oldscopesp, \
2158dd9b
GS
201 PL_retstack_ix = cx->blk_oldretsp, \
202 PL_curpm = cx->blk_oldpm
79072805
LW
203
204/* substitution context */
205struct subst {
206 I32 sbu_iters;
207 I32 sbu_maxiters;
22e551b9 208 I32 sbu_rflags;
4633a7c4 209 I32 sbu_oldsave;
71be2cbc
PP
210 bool sbu_once;
211 bool sbu_rxtainted;
79072805
LW
212 char * sbu_orig;
213 SV * sbu_dstr;
214 SV * sbu_targ;
215 char * sbu_s;
216 char * sbu_m;
217 char * sbu_strend;
c90c0ff4 218 void * sbu_rxres;
c07a80fd 219 REGEXP * sbu_rx;
79072805
LW
220};
221#define sb_iters cx_u.cx_subst.sbu_iters
222#define sb_maxiters cx_u.cx_subst.sbu_maxiters
22e551b9 223#define sb_rflags cx_u.cx_subst.sbu_rflags
4633a7c4 224#define sb_oldsave cx_u.cx_subst.sbu_oldsave
71be2cbc
PP
225#define sb_once cx_u.cx_subst.sbu_once
226#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
227#define sb_orig cx_u.cx_subst.sbu_orig
228#define sb_dstr cx_u.cx_subst.sbu_dstr
229#define sb_targ cx_u.cx_subst.sbu_targ
230#define sb_s cx_u.cx_subst.sbu_s
231#define sb_m cx_u.cx_subst.sbu_m
232#define sb_strend cx_u.cx_subst.sbu_strend
c90c0ff4 233#define sb_rxres cx_u.cx_subst.sbu_rxres
c07a80fd 234#define sb_rx cx_u.cx_subst.sbu_rx
79072805
LW
235
236#define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix], \
237 cx->sb_iters = iters, \
238 cx->sb_maxiters = maxiters, \
22e551b9 239 cx->sb_rflags = r_flags, \
4633a7c4 240 cx->sb_oldsave = oldsave, \
71be2cbc
PP
241 cx->sb_once = once, \
242 cx->sb_rxtainted = rxtainted, \
79072805
LW
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, \
c90c0ff4 249 cx->sb_rxres = Null(void*), \
c07a80fd 250 cx->sb_rx = rx, \
c90c0ff4
PP
251 cx->cx_type = CXt_SUBST; \
252 rxres_save(&cx->sb_rxres, rx)
79072805 253
c90c0ff4
PP
254#define POPSUBST(cx) cx = &cxstack[cxstack_ix--]; \
255 rxres_free(&cx->sb_rxres)
79072805
LW
256
257struct context {
6b35e009 258 U32 cx_type; /* what kind of context this is */
79072805
LW
259 union {
260 struct block cx_blk;
261 struct subst cx_subst;
262 } cx_u;
263};
6b35e009
GS
264
265#define CXTYPEMASK 0xff
79072805
LW
266#define CXt_NULL 0
267#define CXt_SUB 1
268#define CXt_EVAL 2
269#define CXt_LOOP 3
270#define CXt_SUBST 4
271#define CXt_BLOCK 5
272
6b35e009
GS
273/* private flags for CXt_EVAL */
274#define CXp_REAL 0x00000100 /* truly eval'', not a lookalike */
275
276#define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
277#define CxREALEVAL(c) (((c)->cx_type & (CXt_EVAL|CXp_REAL)) == (CXt_EVAL|CXp_REAL))
278
79072805
LW
279#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
280
281/* "gimme" values */
a0d0e21e
LW
282#define G_SCALAR 0
283#define G_ARRAY 1
54310121 284#define G_VOID 128 /* skip this bit when adding flags below */
79072805 285
864dbfa3 286/* extra flags for Perl_call_* routines */
a0d0e21e
LW
287#define G_DISCARD 2 /* Call FREETMPS. */
288#define G_EVAL 4 /* Assume eval {} around subroutine call. */
289#define G_NOARGS 8 /* Don't construct a @_ array. */
54310121 290#define G_KEEPERR 16 /* Append errors to $@, don't overwrite it */
491527d0 291#define G_NODEBUG 32 /* Disable debugging at toplevel. */
a7c6d244 292#define G_NOCATCH 64 /* Don't do CATCH_SET() */
e336de0d 293
faef0170
HS
294/* flag bits for PL_in_eval */
295#define EVAL_NULL 0 /* not in an eval */
296#define EVAL_INEVAL 1 /* some enclosing scope is an eval */
297#define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */
864dbfa3 298#define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */
faef0170 299
e336de0d
GS
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 358
3095d977
GS
359/* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
360 * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
d3acc0f7 361#define POPSTACK \
e336de0d 362 STMT_START { \
de616352 363 djSP; \
3280af22 364 PERL_SI *prev = PL_curstackinfo->si_prev; \
e336de0d 365 if (!prev) { \
bf49b057 366 PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); \
e336de0d
GS
367 my_exit(1); \
368 } \
3280af22 369 SWITCHSTACK(PL_curstack,prev->si_stack); \
e336de0d 370 /* don't free prev here, free them all at the END{} */ \
3280af22 371 PL_curstackinfo = prev; \
e336de0d
GS
372 } STMT_END
373
374#define POPSTACK_TO(s) \
375 STMT_START { \
3280af22 376 while (PL_curstack != s) { \
bac4b2ad 377 dounwind(-1); \
d3acc0f7 378 POPSTACK; \
bac4b2ad 379 } \
e336de0d 380 } STMT_END