This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / cop.h
CommitLineData
a0d0e21e 1/* cop.h
79072805 2 *
e6906430 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
ae53e38e 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006 by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
d7afa7f5 9 * Control ops (cops) are one of the three ops OP_NEXTSTATE, OP_DBSTATE,
c293eb2b
NC
10 * and OP_SETSTATE that (loosely speaking) are separate statements.
11 * They hold information important for lexical state and error reporting.
12 * At run time, PL_curcop is set to point to the most recently executed cop,
d7afa7f5 13 * and thus can be used to determine our current state.
79072805
LW
14 */
15
2f36f949
NC
16/* A jmpenv packages the state required to perform a proper non-local jump.
17 * Note that there is a start_env initialized when perl starts, and top_env
18 * points to this initially, so top_env should always be non-null.
19 *
20 * Existence of a non-null top_env->je_prev implies it is valid to call
21 * longjmp() at that runlevel (we make sure start_env.je_prev is always
22 * null to ensure this).
23 *
24 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
25 * establish a local jmpenv to handle exception traps. Care must be taken
26 * to restore the previous value of je_mustcatch before exiting the
27 * stack frame iff JMPENV_PUSH was not called in that stack frame.
28 * GSAR 97-03-27
29 */
30
31struct jmpenv {
32 struct jmpenv * je_prev;
33 Sigjmp_buf je_buf; /* only for use if !je_throw */
34 int je_ret; /* last exception thrown */
35 bool je_mustcatch; /* need to call longjmp()? */
36#ifdef PERL_FLEXIBLE_EXCEPTIONS
37 void (*je_throw)(int v); /* last for bincompat */
38 bool je_noset; /* no need for setjmp() */
39#endif
40};
41
42typedef struct jmpenv JMPENV;
43
44#ifdef OP_IN_REGISTER
45#define OP_REG_TO_MEM PL_opsave = op
46#define OP_MEM_TO_REG op = PL_opsave
47#else
48#define OP_REG_TO_MEM NOOP
49#define OP_MEM_TO_REG NOOP
50#endif
51
52/*
53 * How to build the first jmpenv.
54 *
55 * top_env needs to be non-zero. It points to an area
56 * in which longjmp() stuff is stored, as C callstack
57 * info there at least is thread specific this has to
58 * be per-thread. Otherwise a 'die' in a thread gives
59 * that thread the C stack of last thread to do an eval {}!
60 */
61
62#define JMPENV_BOOTSTRAP \
63 STMT_START { \
64 Zero(&PL_start_env, 1, JMPENV); \
65 PL_start_env.je_ret = -1; \
66 PL_start_env.je_mustcatch = TRUE; \
67 PL_top_env = &PL_start_env; \
68 } STMT_END
69
70#ifdef PERL_FLEXIBLE_EXCEPTIONS
71
72/*
73 * These exception-handling macros are split up to
74 * ease integration with C++ exceptions.
75 *
76 * To use C++ try+catch to catch Perl exceptions, an extension author
77 * needs to first write an extern "C" function to throw an appropriate
78 * exception object; typically it will be or contain an integer,
79 * because Perl's internals use integers to track exception types:
80 * extern "C" { static void thrower(int i) { throw i; } }
81 *
82 * Then (as shown below) the author needs to use, not the simple
83 * JMPENV_PUSH, but several of its constitutent macros, to arrange for
84 * the Perl internals to call thrower() rather than longjmp() to
85 * report exceptions:
86 *
87 * dJMPENV;
88 * JMPENV_PUSH_INIT(thrower);
89 * try {
90 * ... stuff that may throw exceptions ...
91 * }
92 * catch (int why) { // or whatever matches thrower()
93 * JMPENV_POST_CATCH;
94 * EXCEPT_SET(why);
95 * switch (why) {
96 * ... // handle various Perl exception codes
97 * }
98 * }
99 * JMPENV_POP; // don't forget this!
100 */
101
102/*
103 * Function that catches/throws, and its callback for the
104 * body of protected processing.
105 */
106typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
107typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
108 int *, protect_body_t, ...);
109
110#define dJMPENV JMPENV cur_env; \
111 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
112
113#define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
114 STMT_START { \
115 (ce).je_throw = (THROWFUNC); \
116 (ce).je_ret = -1; \
117 (ce).je_mustcatch = FALSE; \
118 (ce).je_prev = PL_top_env; \
119 PL_top_env = &(ce); \
120 OP_REG_TO_MEM; \
121 } STMT_END
122
123#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
124
125#define JMPENV_POST_CATCH_ENV(ce) \
126 STMT_START { \
127 OP_MEM_TO_REG; \
128 PL_top_env = &(ce); \
129 } STMT_END
130
131#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
132
133#define JMPENV_PUSH_ENV(ce,v) \
134 STMT_START { \
135 if (!(ce).je_noset) { \
136 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
137 (void*)ce, (void*)PL_top_env)); \
138 JMPENV_PUSH_INIT_ENV(ce,NULL); \
139 EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
140 (ce).je_noset = 1; \
141 } \
142 else \
143 EXCEPT_SET_ENV(ce,0); \
144 JMPENV_POST_CATCH_ENV(ce); \
145 (v) = EXCEPT_GET_ENV(ce); \
146 } STMT_END
147
148#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
149
150#define JMPENV_POP_ENV(ce) \
151 STMT_START { \
152 if (PL_top_env == &(ce)) \
153 PL_top_env = (ce).je_prev; \
154 } STMT_END
155
156#define JMPENV_POP JMPENV_POP_ENV(*(JMPENV*)pcur_env)
157
158#define JMPENV_JUMP(v) \
159 STMT_START { \
160 OP_REG_TO_MEM; \
161 if (PL_top_env->je_prev) { \
162 if (PL_top_env->je_throw) \
163 PL_top_env->je_throw(v); \
164 else \
165 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
166 } \
167 if ((v) == 2) \
168 PerlProc_exit(STATUS_EXIT); \
169 PerlIO_printf(Perl_error_log, "panic: top_env\n"); \
170 PerlProc_exit(1); \
171 } STMT_END
172
173#define EXCEPT_GET_ENV(ce) ((ce).je_ret)
174#define EXCEPT_GET EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
175#define EXCEPT_SET_ENV(ce,v) ((ce).je_ret = (v))
176#define EXCEPT_SET(v) EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
177
178#else /* !PERL_FLEXIBLE_EXCEPTIONS */
179
180#define dJMPENV JMPENV cur_env
181
182#define JMPENV_PUSH(v) \
183 STMT_START { \
184 DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
185 (void*)&cur_env, (void*)PL_top_env)); \
186 cur_env.je_prev = PL_top_env; \
187 OP_REG_TO_MEM; \
188 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
189 OP_MEM_TO_REG; \
190 PL_top_env = &cur_env; \
191 cur_env.je_mustcatch = FALSE; \
192 (v) = cur_env.je_ret; \
193 } STMT_END
194
195#define JMPENV_POP \
196 STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
197
198#define JMPENV_JUMP(v) \
199 STMT_START { \
200 OP_REG_TO_MEM; \
201 if (PL_top_env->je_prev) \
202 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
203 if ((v) == 2) \
204 PerlProc_exit(STATUS_EXIT); \
205 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n"); \
206 PerlProc_exit(1); \
207 } STMT_END
208
209#endif /* PERL_FLEXIBLE_EXCEPTIONS */
210
211#define CATCH_GET (PL_top_env->je_mustcatch)
212#define CATCH_SET(v) (PL_top_env->je_mustcatch = (v))
213
214
79072805
LW
215struct cop {
216 BASEOP
a0d0e21e 217 char * cop_label; /* label for this construct */
57843af0 218#ifdef USE_ITHREADS
11faa288 219 char * cop_stashpv; /* package line was compiled in */
57843af0
GS
220 char * cop_file; /* file name the following line # is from */
221#else
11faa288 222 HV * cop_stash; /* package line was compiled in */
79072805 223 GV * cop_filegv; /* file the following line # is from */
57843af0 224#endif
a0d0e21e
LW
225 U32 cop_seq; /* parse sequence number */
226 I32 cop_arybase; /* array base this line was compiled with */
79072805 227 line_t cop_line; /* line # of this command */
599cee73 228 SV * cop_warnings; /* lexical warnings bitmask */
ac27b0f5 229 SV * cop_io; /* lexical IO defaults */
79072805
LW
230};
231
57843af0
GS
232#ifdef USE_ITHREADS
233# define CopFILE(c) ((c)->cop_file)
234# define CopFILEGV(c) (CopFILE(c) \
0e2d6244 235 ? gv_fetchfile(CopFILE(c)) : NULL)
083fcd59 236
56b65a99
AT
237# ifdef NETWARE
238# define CopFILE_set(c,pv) ((c)->cop_file = savepv(pv))
239# else
240# define CopFILE_set(c,pv) ((c)->cop_file = savesharedpv(pv))
241# endif
083fcd59 242
57843af0 243# define CopFILESV(c) (CopFILE(c) \
0e2d6244 244 ? GvSV(gv_fetchfile(CopFILE(c))) : NULL)
57843af0 245# define CopFILEAV(c) (CopFILE(c) \
0e2d6244 246 ? GvAV(gv_fetchfile(CopFILE(c))) : NULL)
22d66770 247# define CopFILEAVx(c) (GvAV(gv_fetchfile(CopFILE(c))))
57843af0 248# define CopSTASHPV(c) ((c)->cop_stashpv)
083fcd59 249
56b65a99 250# ifdef NETWARE
0e2d6244 251# define CopSTASHPV_set(c,pv) ((c)->cop_stashpv = ((pv) ? savepv(pv) : NULL))
56b65a99
AT
252# else
253# define CopSTASHPV_set(c,pv) ((c)->cop_stashpv = savesharedpv(pv))
254# endif
083fcd59 255
11faa288 256# define CopSTASH(c) (CopSTASHPV(c) \
0e2d6244
SS
257 ? gv_stashpv(CopSTASHPV(c),GV_ADD) : NULL)
258# define CopSTASH_set(c,hv) CopSTASHPV_set(c, (hv) ? HvNAME_get(hv) : NULL)
585f2c41 259# define CopSTASH_eq(c,hv) ((hv) && stashpv_hvname_match(c,hv))
56b65a99
AT
260# ifdef NETWARE
261# define CopSTASH_free(c) SAVECOPSTASH_FREE(c)
56b65a99
AT
262# define CopFILE_free(c) SAVECOPFILE_FREE(c)
263# else
585f2c41 264# define CopSTASH_free(c) PerlMemShared_free(CopSTASHPV(c))
0e2d6244 265# define CopFILE_free(c) (PerlMemShared_free(CopFILE(c)),(CopFILE(c) = NULL))
56b65a99 266# endif
57843af0
GS
267#else
268# define CopFILEGV(c) ((c)->cop_filegv)
f4dd75d9
GS
269# define CopFILEGV_set(c,gv) ((c)->cop_filegv = (GV*)SvREFCNT_inc(gv))
270# define CopFILE_set(c,pv) CopFILEGV_set((c), gv_fetchfile(pv))
0e2d6244
SS
271# define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : NULL)
272# define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : NULL)
22d66770 273# define CopFILEAVx(c) (GvAV(CopFILEGV(c)))
0e2d6244 274# define CopFILE(c) (CopFILESV(c) ? SvPVX(CopFILESV(c)) : NULL)
57843af0 275# define CopSTASH(c) ((c)->cop_stash)
f4dd75d9 276# define CopSTASH_set(c,hv) ((c)->cop_stash = (hv))
0e2d6244 277# define CopSTASHPV(c) (CopSTASH(c) ? HvNAME_get(CopSTASH(c)) : NULL)
f4dd75d9
GS
278 /* cop_stash is not refcounted */
279# define CopSTASHPV_set(c,pv) CopSTASH_set((c), gv_stashpv(pv,GV_ADD))
280# define CopSTASH_eq(c,hv) (CopSTASH(c) == (hv))
05ec9bb3 281# define CopSTASH_free(c)
0e2d6244 282# define CopFILE_free(c) (SvREFCNT_dec(CopFILEGV(c)),(CopFILEGV(c) = NULL))
05ec9bb3 283
57843af0
GS
284#endif /* USE_ITHREADS */
285
ed094faf 286#define CopSTASH_ne(c,hv) (!CopSTASH_eq(c,hv))
cc49e20b 287#define CopLINE(c) ((c)->cop_line)
57843af0
GS
288#define CopLINE_inc(c) (++CopLINE(c))
289#define CopLINE_dec(c) (--CopLINE(c))
290#define CopLINE_set(c,l) (CopLINE(c) = (l))
cc49e20b 291
248c2a4d
CN
292/* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */
293#ifdef MACOS_TRADITIONAL
294# define OutCopFILE(c) MacPerl_MPWFileName(CopFILE(c))
295#else
296# define OutCopFILE(c) CopFILE(c)
297#endif
298
2cb86c03
NC
299/* CopARYBASE is likely to be removed soon. */
300#define CopARYBASE(c) ((c)->cop_arybase)
301#define CopARYBASE_get(c) ((c)->cop_arybase + 0)
302#define CopARYBASE_set(c, b) STMT_START { (c)->cop_arybase = (b); } STMT_END
303
304/* FIXME NATIVE_HINTS if this is changed from op_private (see perl.h) */
305#define CopHINTS_get(c) ((c)->op_private + 0)
306#define CopHINTS_set(c, h) STMT_START { \
307 (c)->op_private \
308 = (U8)((h) & HINT_PRIVATE_MASK); \
309 } STMT_END
310
79072805
LW
311/*
312 * Here we have some enormously heavy (or at least ponderous) wizardry.
313 */
314
315/* subroutine context */
316struct block_sub {
317 CV * cv;
318 GV * gv;
463ee0b2 319 GV * dfoutgv;
4d1ff10f 320#ifndef USE_5005THREADS
79072805 321 AV * savearray;
4d1ff10f 322#endif /* USE_5005THREADS */
79072805 323 AV * argarray;
5b7ea690 324 long olddepth;
79072805 325 U8 hasargs;
cd06dffe 326 U8 lval; /* XXX merge lval and hasargs? */
d7afa7f5 327 PAD *oldcomppad;
79072805
LW
328};
329
c2ef0052
NC
330/* base for the next two macros. Don't use directly.
331 * Note that the refcnt of the cv is incremented twice; The CX one is
332 * decremented by LEAVESUB, the other by LEAVE. */
333
338501c1 334#define PUSHSUB_BASE(cx) \
79072805 335 cx->blk_sub.cv = cv; \
5b7ea690 336 cx->blk_sub.olddepth = CvDEPTH(cv); \
c2ef0052
NC
337 cx->blk_sub.hasargs = hasargs; \
338 if (!CvDEPTH(cv)) { \
2190c7e7
AL
339 SvREFCNT_inc_simple_void_NN(cv); \
340 SvREFCNT_inc_simple_void_NN(cv); \
c2ef0052
NC
341 SAVEFREESV(cv); \
342 }
343
338501c1
JH
344
345#define PUSHSUB(cx) \
346 PUSHSUB_BASE(cx) \
cd06dffe
GS
347 cx->blk_sub.lval = PL_op->op_private & \
348 (OPpLVAL_INTRO|OPpENTERSUB_INARGS);
79072805 349
338501c1
JH
350/* variant for use by OP_DBSTATE, where op_private holds hint bits */
351#define PUSHSUB_DB(cx) \
352 PUSHSUB_BASE(cx) \
353 cx->blk_sub.lval = 0;
354
355
79072805
LW
356#define PUSHFORMAT(cx) \
357 cx->blk_sub.cv = cv; \
358 cx->blk_sub.gv = gv; \
4633a7c4 359 cx->blk_sub.hasargs = 0; \
3280af22 360 cx->blk_sub.dfoutgv = PL_defoutgv; \
be2d5e07 361 SvREFCNT_inc_void(cx->blk_sub.dfoutgv)
79072805 362
4d1ff10f 363#ifdef USE_5005THREADS
7766f137 364# define POP_SAVEARRAY() NOOP
6d4ff0d2 365#else
7766f137 366# define POP_SAVEARRAY() \
6d4ff0d2 367 STMT_START { \
3280af22 368 SvREFCNT_dec(GvAV(PL_defgv)); \
a8bba7fa 369 GvAV(PL_defgv) = cx->blk_sub.savearray; \
6d4ff0d2 370 } STMT_END
4d1ff10f 371#endif /* USE_5005THREADS */
6d4ff0d2 372
ecf8e9dd 373/* junk in @_ spells trouble when cloning CVs and in pp_caller(), so don't
8e09340b
GS
374 * leave any (a fast av_clear(ary), basically) */
375#define CLEAR_ARGARRAY(ary) \
376 STMT_START { \
377 AvMAX(ary) += AvARRAY(ary) - AvALLOC(ary); \
0da6cfda 378 SvPV_set(ary, (char*)AvALLOC(ary)); \
8e09340b
GS
379 AvFILLp(ary) = -1; \
380 } STMT_END
7766f137 381
b0d9ce38
GS
382#define POPSUB(cx,sv) \
383 STMT_START { \
a8bba7fa 384 if (cx->blk_sub.hasargs) { \
7766f137 385 POP_SAVEARRAY(); \
d8b46c1b 386 /* abandon @_ if it got reified */ \
a8bba7fa 387 if (AvREAL(cx->blk_sub.argarray)) { \
8e7b0921 388 const SSize_t fill = AvFILLp(cx->blk_sub.argarray); \
a8bba7fa
GS
389 SvREFCNT_dec(cx->blk_sub.argarray); \
390 cx->blk_sub.argarray = newAV(); \
391 av_extend(cx->blk_sub.argarray, fill); \
392 AvFLAGS(cx->blk_sub.argarray) = AVf_REIFY; \
9755d405 393 CX_CURPAD_SV(cx->blk_sub, 0) = (SV*)cx->blk_sub.argarray; \
d8b46c1b 394 } \
7766f137 395 else { \
8e09340b 396 CLEAR_ARGARRAY(cx->blk_sub.argarray); \
7766f137 397 } \
79072805 398 } \
b0d9ce38
GS
399 sv = (SV*)cx->blk_sub.cv; \
400 if (sv && (CvDEPTH((CV*)sv) = cx->blk_sub.olddepth)) \
0e2d6244 401 sv = NULL; \
b0d9ce38
GS
402 } STMT_END
403
404#define LEAVESUB(sv) \
405 STMT_START { \
406 if (sv) \
407 SvREFCNT_dec(sv); \
408 } STMT_END
79072805
LW
409
410#define POPFORMAT(cx) \
4633a7c4
LW
411 setdefout(cx->blk_sub.dfoutgv); \
412 SvREFCNT_dec(cx->blk_sub.dfoutgv);
79072805
LW
413
414/* eval context */
415struct block_eval {
416 I32 old_in_eval;
417 I32 old_op_type;
0f79a09d 418 SV * old_namesv;
79072805 419 OP * old_eval_root;
748a9306 420 SV * cur_text;
2090ab20 421 CV * cv;
2f36f949 422 JMPENV * cur_top_env; /* value of PL_top_env when eval CX created */
79072805
LW
423};
424
8990e307 425#define PUSHEVAL(cx,n,fgv) \
0f79a09d 426 STMT_START { \
3280af22 427 cx->blk_eval.old_in_eval = PL_in_eval; \
a8bba7fa 428 cx->blk_eval.old_op_type = PL_op->op_type; \
0e2d6244 429 cx->blk_eval.old_namesv = (n ? newSVpv(n,0) : NULL); \
a8bba7fa 430 cx->blk_eval.old_eval_root = PL_eval_root; \
0f79a09d 431 cx->blk_eval.cur_text = PL_linestr; \
5900599a 432 cx->blk_eval.cv = NULL; /* set by doeval(), as applicable */ \
2f36f949 433 cx->blk_eval.cur_top_env = PL_top_env; \
0f79a09d 434 } STMT_END
79072805
LW
435
436#define POPEVAL(cx) \
0f79a09d 437 STMT_START { \
3280af22 438 PL_in_eval = cx->blk_eval.old_in_eval; \
79072805 439 optype = cx->blk_eval.old_op_type; \
7766f137 440 PL_eval_root = cx->blk_eval.old_eval_root; \
0f79a09d
GS
441 if (cx->blk_eval.old_namesv) \
442 sv_2mortal(cx->blk_eval.old_namesv); \
443 } STMT_END
79072805
LW
444
445/* loop context */
446struct block_loop {
447 char * label;
448 I32 resetsp;
449 OP * redo_op;
450 OP * next_op;
451 OP * last_op;
7766f137
GS
452#ifdef USE_ITHREADS
453 void * iterdata;
d7afa7f5 454 PAD *oldcomppad;
7766f137 455#else
79072805 456 SV ** itervar;
7766f137 457#endif
79072805 458 SV * itersave;
5f05dabc 459 SV * iterlval;
79072805 460 AV * iterary;
89ea2908
GA
461 IV iterix;
462 IV itermax;
79072805
LW
463};
464
7766f137
GS
465#ifdef USE_ITHREADS
466# define CxITERVAR(c) \
467 ((c)->blk_loop.iterdata \
468 ? (CxPADLOOP(cx) \
9755d405
JH
469 ? &CX_CURPAD_SV( (c)->blk_loop, \
470 INT2PTR(PADOFFSET, (c)->blk_loop.iterdata)) \
7766f137
GS
471 : &GvSV((GV*)(c)->blk_loop.iterdata)) \
472 : (SV**)NULL)
473# define CX_ITERDATA_SET(cx,idata) \
9755d405 474 CX_CURPAD_SAVE(cx->blk_loop); \
155aba94 475 if ((cx->blk_loop.iterdata = (idata))) \
d10edb75
KC
476 cx->blk_loop.itersave = SvREFCNT_inc(*CxITERVAR(cx)); \
477 else \
0e2d6244 478 cx->blk_loop.itersave = NULL;
7766f137
GS
479#else
480# define CxITERVAR(c) ((c)->blk_loop.itervar)
481# define CX_ITERDATA_SET(cx,ivar) \
155aba94 482 if ((cx->blk_loop.itervar = (SV**)(ivar))) \
d10edb75
KC
483 cx->blk_loop.itersave = SvREFCNT_inc(*CxITERVAR(cx)); \
484 else \
0e2d6244 485 cx->blk_loop.itersave = NULL;
7766f137
GS
486#endif
487
488#define PUSHLOOP(cx, dat, s) \
38a230cb
GS
489 cx->blk_loop.label = PL_curcop->cop_label; \
490 cx->blk_loop.resetsp = s - PL_stack_base; \
79072805
LW
491 cx->blk_loop.redo_op = cLOOP->op_redoop; \
492 cx->blk_loop.next_op = cLOOP->op_nextop; \
493 cx->blk_loop.last_op = cLOOP->op_lastop; \
0e2d6244
SS
494 cx->blk_loop.iterlval = NULL; \
495 cx->blk_loop.iterary = NULL; \
7766f137
GS
496 cx->blk_loop.iterix = -1; \
497 CX_ITERDATA_SET(cx,dat);
79072805
LW
498
499#define POPLOOP(cx) \
a8bba7fa 500 SvREFCNT_dec(cx->blk_loop.iterlval); \
7766f137 501 if (CxITERVAR(cx)) { \
d64ae468
NC
502 if (SvPADMY(cx->blk_loop.itersave)) { \
503 SV ** const s_v_p = CxITERVAR(cx); \
504 sv_2mortal(*s_v_p); \
505 *s_v_p = cx->blk_loop.itersave; \
506 } \
507 else { \
508 SvREFCNT_dec(cx->blk_loop.itersave); \
509 } \
44a8e56a 510 } \
a8bba7fa
GS
511 if (cx->blk_loop.iterary && cx->blk_loop.iterary != PL_curstack)\
512 SvREFCNT_dec(cx->blk_loop.iterary);
79072805
LW
513
514/* context common to subroutines, evals and loops */
515struct block {
516 I32 blku_oldsp; /* stack pointer to copy stuff down to */
517 COP * blku_oldcop; /* old curcop pointer */
518 I32 blku_oldretsp; /* return stack index */
519 I32 blku_oldmarksp; /* mark stack index */
520 I32 blku_oldscopesp; /* scope stack index */
521 PMOP * blku_oldpm; /* values of pattern match vars */
522 U8 blku_gimme; /* is this block running in list context? */
523
524 union {
525 struct block_sub blku_sub;
526 struct block_eval blku_eval;
527 struct block_loop blku_loop;
528 } blk_u;
529};
530#define blk_oldsp cx_u.cx_blk.blku_oldsp
531#define blk_oldcop cx_u.cx_blk.blku_oldcop
532#define blk_oldretsp cx_u.cx_blk.blku_oldretsp
533#define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
534#define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
535#define blk_oldpm cx_u.cx_blk.blku_oldpm
536#define blk_gimme cx_u.cx_blk.blku_gimme
537#define blk_sub cx_u.cx_blk.blk_u.blku_sub
538#define blk_eval cx_u.cx_blk.blk_u.blku_eval
539#define blk_loop cx_u.cx_blk.blk_u.blku_loop
540
541/* Enter a block. */
8990e307 542#define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix], \
79072805 543 cx->cx_type = t, \
3280af22
NIS
544 cx->blk_oldsp = sp - PL_stack_base, \
545 cx->blk_oldcop = PL_curcop, \
1aff0e91 546 cx->blk_oldmarksp = PL_markstack_ptr - PL_markstack, \
3280af22 547 cx->blk_oldscopesp = PL_scopestack_ix, \
1aff0e91 548 cx->blk_oldretsp = PL_retstack_ix, \
3280af22 549 cx->blk_oldpm = PL_curpm, \
eb160463 550 cx->blk_gimme = (U8)gimme; \
bf49b057 551 DEBUG_l( PerlIO_printf(Perl_debug_log, "Entering block %ld, type %s\n", \
1aff0e91 552 (long)cxstack_ix, PL_block_type[CxTYPE(cx)]); )
79072805
LW
553
554/* Exit a block (RETURN and LAST). */
a0d0e21e 555#define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--], \
1aff0e91 556 newsp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
557 PL_curcop = cx->blk_oldcop, \
558 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
559 PL_scopestack_ix = cx->blk_oldscopesp, \
560 PL_retstack_ix = cx->blk_oldretsp, \
561 pm = cx->blk_oldpm, \
562 gimme = cx->blk_gimme; \
1aa6899f 563 DEBUG_SCOPE("POPBLOCK"); \
bf49b057 564 DEBUG_l( PerlIO_printf(Perl_debug_log, "Leaving block %ld, type %s\n", \
22c35a8c 565 (long)cxstack_ix+1,PL_block_type[CxTYPE(cx)]); )
79072805
LW
566
567/* Continue a block elsewhere (NEXT and REDO). */
3280af22 568#define TOPBLOCK(cx) cx = &cxstack[cxstack_ix], \
1aff0e91 569 PL_stack_sp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
570 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
571 PL_scopestack_ix = cx->blk_oldscopesp, \
2158dd9b 572 PL_retstack_ix = cx->blk_oldretsp, \
1aa6899f
JH
573 PL_curpm = cx->blk_oldpm; \
574 DEBUG_SCOPE("TOPBLOCK");
79072805
LW
575
576/* substitution context */
577struct subst {
578 I32 sbu_iters;
579 I32 sbu_maxiters;
22e551b9 580 I32 sbu_rflags;
4633a7c4 581 I32 sbu_oldsave;
71be2cbc 582 bool sbu_once;
583 bool sbu_rxtainted;
79072805
LW
584 char * sbu_orig;
585 SV * sbu_dstr;
586 SV * sbu_targ;
587 char * sbu_s;
588 char * sbu_m;
589 char * sbu_strend;
c90c0ff4 590 void * sbu_rxres;
c07a80fd 591 REGEXP * sbu_rx;
79072805
LW
592};
593#define sb_iters cx_u.cx_subst.sbu_iters
594#define sb_maxiters cx_u.cx_subst.sbu_maxiters
22e551b9 595#define sb_rflags cx_u.cx_subst.sbu_rflags
4633a7c4 596#define sb_oldsave cx_u.cx_subst.sbu_oldsave
71be2cbc 597#define sb_once cx_u.cx_subst.sbu_once
598#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
599#define sb_orig cx_u.cx_subst.sbu_orig
600#define sb_dstr cx_u.cx_subst.sbu_dstr
601#define sb_targ cx_u.cx_subst.sbu_targ
602#define sb_s cx_u.cx_subst.sbu_s
603#define sb_m cx_u.cx_subst.sbu_m
604#define sb_strend cx_u.cx_subst.sbu_strend
c90c0ff4 605#define sb_rxres cx_u.cx_subst.sbu_rxres
c07a80fd 606#define sb_rx cx_u.cx_subst.sbu_rx
79072805
LW
607
608#define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix], \
609 cx->sb_iters = iters, \
610 cx->sb_maxiters = maxiters, \
22e551b9 611 cx->sb_rflags = r_flags, \
4633a7c4 612 cx->sb_oldsave = oldsave, \
71be2cbc 613 cx->sb_once = once, \
614 cx->sb_rxtainted = rxtainted, \
79072805
LW
615 cx->sb_orig = orig, \
616 cx->sb_dstr = dstr, \
617 cx->sb_targ = targ, \
618 cx->sb_s = s, \
619 cx->sb_m = m, \
620 cx->sb_strend = strend, \
0e2d6244 621 cx->sb_rxres = NULL, \
c07a80fd 622 cx->sb_rx = rx, \
c90c0ff4 623 cx->cx_type = CXt_SUBST; \
96b78c84
NC
624 rxres_save(&cx->sb_rxres, rx); \
625 ReREFCNT_inc(rx)
79072805 626
c90c0ff4 627#define POPSUBST(cx) cx = &cxstack[cxstack_ix--]; \
96b78c84
NC
628 rxres_free(&cx->sb_rxres); \
629 ReREFCNT_dec(cx->sb_rx)
79072805
LW
630
631struct context {
6b35e009 632 U32 cx_type; /* what kind of context this is */
79072805
LW
633 union {
634 struct block cx_blk;
635 struct subst cx_subst;
636 } cx_u;
637};
6b35e009
GS
638
639#define CXTYPEMASK 0xff
79072805
LW
640#define CXt_NULL 0
641#define CXt_SUB 1
642#define CXt_EVAL 2
643#define CXt_LOOP 3
644#define CXt_SUBST 4
645#define CXt_BLOCK 5
7766f137 646#define CXt_FORMAT 6
79072805 647
4127a31c
RH
648/* private flags for CXt_SUB and CXt_NULL */
649#define CXp_MULTICALL 0x00000400 /* part of a multicall (so don't
650 tear down context on exit). */
651
6b35e009
GS
652/* private flags for CXt_EVAL */
653#define CXp_REAL 0x00000100 /* truly eval'', not a lookalike */
1d76a5c3 654#define CXp_TRYBLOCK 0x00000200 /* eval{}, not eval'' or similar */
6b35e009 655
7766f137
GS
656#ifdef USE_ITHREADS
657/* private flags for CXt_LOOP */
658# define CXp_PADVAR 0x00000100 /* itervar lives on pad, iterdata
659 has pad offset; if not set,
660 iterdata holds GV* */
661# define CxPADLOOP(c) (((c)->cx_type & (CXt_LOOP|CXp_PADVAR)) \
662 == (CXt_LOOP|CXp_PADVAR))
663#endif
664
6b35e009 665#define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
4127a31c
RH
666#define CxMULTICALL(c) (((c)->cx_type & CXp_MULTICALL) \
667 == CXp_MULTICALL)
7766f137
GS
668#define CxREALEVAL(c) (((c)->cx_type & (CXt_EVAL|CXp_REAL)) \
669 == (CXt_EVAL|CXp_REAL))
1d76a5c3
GS
670#define CxTRYBLOCK(c) (((c)->cx_type & (CXt_EVAL|CXp_TRYBLOCK)) \
671 == (CXt_EVAL|CXp_TRYBLOCK))
6b35e009 672
79072805
LW
673#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
674
ccfc67b7
JH
675/*
676=head1 "Gimme" Values
677*/
954c1994
GS
678
679/*
680=for apidoc AmU||G_SCALAR
681Used to indicate scalar context. See C<GIMME_V>, C<GIMME>, and
682L<perlcall>.
683
684=for apidoc AmU||G_ARRAY
91e74348 685Used to indicate list context. See C<GIMME_V>, C<GIMME> and
954c1994
GS
686L<perlcall>.
687
688=for apidoc AmU||G_VOID
689Used to indicate void context. See C<GIMME_V> and L<perlcall>.
690
691=for apidoc AmU||G_DISCARD
692Indicates that arguments returned from a callback should be discarded. See
693L<perlcall>.
694
695=for apidoc AmU||G_EVAL
696
697Used to force a Perl C<eval> wrapper around a callback. See
698L<perlcall>.
699
700=for apidoc AmU||G_NOARGS
701
702Indicates that no arguments are being sent to a callback. See
703L<perlcall>.
704
705=cut
706*/
707
a0d0e21e
LW
708#define G_SCALAR 0
709#define G_ARRAY 1
54310121 710#define G_VOID 128 /* skip this bit when adding flags below */
79072805 711
864dbfa3 712/* extra flags for Perl_call_* routines */
a0d0e21e
LW
713#define G_DISCARD 2 /* Call FREETMPS. */
714#define G_EVAL 4 /* Assume eval {} around subroutine call. */
715#define G_NOARGS 8 /* Don't construct a @_ array. */
54310121 716#define G_KEEPERR 16 /* Append errors to $@, don't overwrite it */
491527d0 717#define G_NODEBUG 32 /* Disable debugging at toplevel. */
968b3946 718#define G_METHOD 64 /* Calling method. */
6d1a3977
NC
719#define G_FAKINGEVAL 256 /* Faking en eval context for call_sv or
720 fold_constants. */
e336de0d 721
faef0170
HS
722/* flag bits for PL_in_eval */
723#define EVAL_NULL 0 /* not in an eval */
724#define EVAL_INEVAL 1 /* some enclosing scope is an eval */
725#define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */
864dbfa3 726#define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */
6dc8a9e4 727#define EVAL_INREQUIRE 8 /* The code is being required. */
faef0170 728
e336de0d
GS
729/* Support for switching (stack and block) contexts.
730 * This ensures magic doesn't invalidate local stack and cx pointers.
731 */
732
e788e7d3
GS
733#define PERLSI_UNKNOWN -1
734#define PERLSI_UNDEF 0
735#define PERLSI_MAIN 1
736#define PERLSI_MAGIC 2
737#define PERLSI_SORT 3
738#define PERLSI_SIGNAL 4
739#define PERLSI_OVERLOAD 5
740#define PERLSI_DESTROY 6
741#define PERLSI_WARNHOOK 7
742#define PERLSI_DIEHOOK 8
743#define PERLSI_REQUIRE 9
e336de0d
GS
744
745struct stackinfo {
746 AV * si_stack; /* stack for current runlevel */
747 PERL_CONTEXT * si_cxstack; /* context stack for runlevel */
748 I32 si_cxix; /* current context index */
749 I32 si_cxmax; /* maximum allocated index */
750 I32 si_type; /* type of runlevel */
751 struct stackinfo * si_prev;
752 struct stackinfo * si_next;
ce2f7c3b 753 I32 si_markoff; /* offset where markstack begins for us.
e336de0d
GS
754 * currently used only with DEBUGGING,
755 * but not #ifdef-ed for bincompat */
756};
757
758typedef struct stackinfo PERL_SI;
759
3280af22
NIS
760#define cxstack (PL_curstackinfo->si_cxstack)
761#define cxstack_ix (PL_curstackinfo->si_cxix)
762#define cxstack_max (PL_curstackinfo->si_cxmax)
e336de0d
GS
763
764#ifdef DEBUGGING
ce2f7c3b
GS
765# define SET_MARK_OFFSET \
766 PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack
e336de0d 767#else
ce2f7c3b 768# define SET_MARK_OFFSET NOOP
e336de0d
GS
769#endif
770
d3acc0f7 771#define PUSHSTACKi(type) \
e336de0d 772 STMT_START { \
3280af22 773 PERL_SI *next = PL_curstackinfo->si_next; \
e336de0d
GS
774 if (!next) { \
775 next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \
3280af22
NIS
776 next->si_prev = PL_curstackinfo; \
777 PL_curstackinfo->si_next = next; \
e336de0d
GS
778 } \
779 next->si_type = type; \
780 next->si_cxix = -1; \
781 AvFILLp(next->si_stack) = 0; \
3280af22
NIS
782 SWITCHSTACK(PL_curstack,next->si_stack); \
783 PL_curstackinfo = next; \
ce2f7c3b 784 SET_MARK_OFFSET; \
e336de0d
GS
785 } STMT_END
786
e788e7d3 787#define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
d3acc0f7 788
3095d977
GS
789/* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
790 * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
d3acc0f7 791#define POPSTACK \
e336de0d 792 STMT_START { \
39644a26 793 dSP; \
1a9219e7 794 PERL_SI * const prev = PL_curstackinfo->si_prev; \
e336de0d 795 if (!prev) { \
bf49b057 796 PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); \
e336de0d
GS
797 my_exit(1); \
798 } \
3280af22 799 SWITCHSTACK(PL_curstack,prev->si_stack); \
e336de0d 800 /* don't free prev here, free them all at the END{} */ \
3280af22 801 PL_curstackinfo = prev; \
e336de0d
GS
802 } STMT_END
803
804#define POPSTACK_TO(s) \
805 STMT_START { \
3280af22 806 while (PL_curstack != s) { \
bac4b2ad 807 dounwind(-1); \
d3acc0f7 808 POPSTACK; \
bac4b2ad 809 } \
e336de0d 810 } STMT_END
ef7b71f0
JH
811
812#define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
813#define IN_PERL_RUNTIME (PL_curcop != &PL_compiling)
814
4127a31c
RH
815/*
816=head1 Multicall Functions
817
818=for apidoc Ams||dMULTICALL
819Declare local variables for a multicall. See L<perlcall/Lightweight Callbacks>.
820
821=for apidoc Ams||PUSH_MULTICALL
822Opening bracket for a lightweight callback.
823See L<perlcall/Lightweight Callbacks>.
824
825=for apidoc Ams||MULTICALL
826Make a lightweight callback. See L<perlcall/Lightweight Callbacks>.
827
828=for apidoc Ams||POP_MULTICALL
829Closing bracket for a lightweight callback.
830See L<perlcall/Lightweight Callbacks>.
831
832=cut
833*/
834
835#define dMULTICALL \
836 SV **newsp; /* set by POPBLOCK */ \
837 PERL_CONTEXT *cx; \
838 CV *multicall_cv; \
839 OP *multicall_cop; \
840 bool multicall_oldcatch; \
841 U8 hasargs = 0 /* used by PUSHSUB */
842
843#define PUSH_MULTICALL(the_cv) \
844 STMT_START { \
7508116b
AL
845 CV * const _nOnclAshIngNamE_ = the_cv; \
846 CV * const cv = _nOnclAshIngNamE_; \
847 AV * const padlist = CvPADLIST(cv); \
4127a31c
RH
848 ENTER; \
849 multicall_oldcatch = CATCH_GET; \
850 SAVETMPS; SAVEVPTR(PL_op); \
851 CATCH_SET(TRUE); \
852 PUSHBLOCK(cx, CXt_SUB|CXp_MULTICALL, PL_stack_sp); \
853 PUSHSUB(cx); \
854 if (++CvDEPTH(cv) >= 2) { \
855 PERL_STACK_OVERFLOW_CHECK(); \
856 Perl_pad_push(aTHX_ padlist, CvDEPTH(cv), 1); \
857 } \
858 SAVECOMPPAD(); \
859 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \
860 multicall_cv = cv; \
861 multicall_cop = CvSTART(cv); \
862 } STMT_END
863
864#define MULTICALL \
865 STMT_START { \
866 PL_op = multicall_cop; \
867 CALLRUNOPS(aTHX); \
868 } STMT_END
869
870#define POP_MULTICALL \
871 STMT_START { \
872 LEAVESUB(multicall_cv); \
873 CvDEPTH(multicall_cv)--; \
874 POPBLOCK(cx,PL_curpm); \
875 CATCH_SET(multicall_oldcatch); \
876 LEAVE; \
877 } STMT_END