This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix Storable test for pre-5.19.2 threaded perls
[perl5.git] / cop.h
CommitLineData
a0d0e21e 1/* cop.h
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
8622e0e2 4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 *
8622e0e2 9 * Control ops (cops) are one of the two ops OP_NEXTSTATE and OP_DBSTATE,
775e5718 10 * that (loosely speaking) are statement separators.
3ffa1527
JH
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,
a3985cdc 13 * and thus can be used to determine our current state.
79072805
LW
14 */
15
0d2925a6 16/* A jmpenv packages the state required to perform a proper non-local jump.
1c98cc53
DM
17 * Note that there is a PL_start_env initialized when perl starts, and
18 * PL_top_env points to this initially, so PL_top_env should always be
19 * non-null.
0d2925a6 20 *
1c98cc53
DM
21 * Existence of a non-null PL_top_env->je_prev implies it is valid to call
22 * longjmp() at that runlevel (we make sure PL_start_env.je_prev is always
0d2925a6
DM
23 * null to ensure this).
24 *
25 * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
26 * establish a local jmpenv to handle exception traps. Care must be taken
27 * to restore the previous value of je_mustcatch before exiting the
28 * stack frame iff JMPENV_PUSH was not called in that stack frame.
29 * GSAR 97-03-27
30 */
31
32struct jmpenv {
33 struct jmpenv * je_prev;
caa674f3 34 Sigjmp_buf je_buf; /* uninit if je_prev is NULL */
0d2925a6
DM
35 int je_ret; /* last exception thrown */
36 bool je_mustcatch; /* need to call longjmp()? */
a68090fe 37 U16 je_old_delaymagic; /* saved PL_delaymagic */
9449f0d6
DM
38#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
39 SSize_t je_old_stack_hwm;
40#endif
0d2925a6
DM
41};
42
43typedef struct jmpenv JMPENV;
44
9449f0d6
DM
45#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
46# define JE_OLD_STACK_HWM_zero PL_start_env.je_old_stack_hwm = 0
47# define JE_OLD_STACK_HWM_save(je) \
48 (je).je_old_stack_hwm = PL_curstackinfo->si_stack_hwm
49# define JE_OLD_STACK_HWM_restore(je) \
978b1859
DM
50 if (PL_curstackinfo->si_stack_hwm < (je).je_old_stack_hwm) \
51 PL_curstackinfo->si_stack_hwm = (je).je_old_stack_hwm
9449f0d6
DM
52#else
53# define JE_OLD_STACK_HWM_zero NOOP
54# define JE_OLD_STACK_HWM_save(je) NOOP
55# define JE_OLD_STACK_HWM_restore(je) NOOP
56#endif
57
0d2925a6
DM
58/*
59 * How to build the first jmpenv.
60 *
61 * top_env needs to be non-zero. It points to an area
62 * in which longjmp() stuff is stored, as C callstack
63 * info there at least is thread specific this has to
64 * be per-thread. Otherwise a 'die' in a thread gives
65 * that thread the C stack of last thread to do an eval {}!
66 */
67
68#define JMPENV_BOOTSTRAP \
69 STMT_START { \
caa674f3
DD
70 PERL_POISON_EXPR(PoisonNew(&PL_start_env, 1, JMPENV));\
71 PL_top_env = &PL_start_env; \
72 PL_start_env.je_prev = NULL; \
0d2925a6
DM
73 PL_start_env.je_ret = -1; \
74 PL_start_env.je_mustcatch = TRUE; \
a68090fe 75 PL_start_env.je_old_delaymagic = 0; \
9449f0d6 76 JE_OLD_STACK_HWM_zero; \
0d2925a6
DM
77 } STMT_END
78
79/*
80 * PERL_FLEXIBLE_EXCEPTIONS
81 *
82 * All the flexible exceptions code has been removed.
83 * See the following threads for details:
84 *
85 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-07/msg00378.html
86 *
87 * Joshua's original patches (which weren't applied) and discussion:
88 *
89 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01396.html
90 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01489.html
91 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01491.html
92 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01608.html
93 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02144.html
94 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02998.html
95 *
96 * Chip's reworked patch and discussion:
97 *
98 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-03/msg00520.html
99 *
100 * The flaw in these patches (which went unnoticed at the time) was
101 * that they moved some code that could potentially die() out of the
102 * region protected by the setjmp()s. This caused exceptions within
103 * END blocks and such to not be handled by the correct setjmp().
104 *
105 * The original patches that introduces flexible exceptions were:
106 *
ff7e9008
MS
107 * http://perl5.git.perl.org/perl.git/commit/312caa8e97f1c7ee342a9895c2f0e749625b4929
108 * http://perl5.git.perl.org/perl.git/commit/14dd3ad8c9bf82cf09798a22cc89a9862dfd6d1a
109 *
0d2925a6
DM
110 */
111
112#define dJMPENV JMPENV cur_env
113
114#define JMPENV_PUSH(v) \
115 STMT_START { \
1c98cc53
DM
116 DEBUG_l({ \
117 int i = 0; JMPENV *p = PL_top_env; \
118 while (p) { i++; p = p->je_prev; } \
91e35ba1
DM
119 Perl_deb(aTHX_ "JUMPENV_PUSH level=%d at %s:%d\n", \
120 i, __FILE__, __LINE__);}) \
0d2925a6 121 cur_env.je_prev = PL_top_env; \
9449f0d6 122 JE_OLD_STACK_HWM_save(cur_env); \
0d2925a6 123 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
9449f0d6 124 JE_OLD_STACK_HWM_restore(cur_env); \
0d2925a6
DM
125 PL_top_env = &cur_env; \
126 cur_env.je_mustcatch = FALSE; \
a68090fe 127 cur_env.je_old_delaymagic = PL_delaymagic; \
0d2925a6
DM
128 (v) = cur_env.je_ret; \
129 } STMT_END
130
131#define JMPENV_POP \
132 STMT_START { \
1c98cc53
DM
133 DEBUG_l({ \
134 int i = -1; JMPENV *p = PL_top_env; \
135 while (p) { i++; p = p->je_prev; } \
91e35ba1
DM
136 Perl_deb(aTHX_ "JUMPENV_POP level=%d at %s:%d\n", \
137 i, __FILE__, __LINE__);}) \
d3ab86be 138 assert(PL_top_env == &cur_env); \
a68090fe 139 PL_delaymagic = cur_env.je_old_delaymagic; \
0d2925a6
DM
140 PL_top_env = cur_env.je_prev; \
141 } STMT_END
142
143#define JMPENV_JUMP(v) \
144 STMT_START { \
1c98cc53
DM
145 DEBUG_l({ \
146 int i = -1; JMPENV *p = PL_top_env; \
147 while (p) { i++; p = p->je_prev; } \
91e35ba1
DM
148 Perl_deb(aTHX_ "JUMPENV_JUMP(%d) level=%d at %s:%d\n", \
149 (int)v, i, __FILE__, __LINE__);}) \
0d2925a6
DM
150 if (PL_top_env->je_prev) \
151 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
152 if ((v) == 2) \
37038d91 153 PerlProc_exit(STATUS_EXIT); \
5637ef5b 154 PerlIO_printf(PerlIO_stderr(), "panic: top_env, v=%d\n", (int)v); \
0d2925a6
DM
155 PerlProc_exit(1); \
156 } STMT_END
157
158#define CATCH_GET (PL_top_env->je_mustcatch)
1c98cc53
DM
159#define CATCH_SET(v) \
160 STMT_START { \
161 DEBUG_l( \
162 Perl_deb(aTHX_ \
163 "JUMPLEVEL set catch %d => %d (for %p) at %s:%d\n", \
164 PL_top_env->je_mustcatch, v, (void*)PL_top_env, \
165 __FILE__, __LINE__);) \
166 PL_top_env->je_mustcatch = (v); \
167 } STMT_END
0d2925a6 168
20439bc7
Z
169/*
170=head1 COP Hint Hashes
171*/
172
173typedef struct refcounted_he COPHH;
174
175#define COPHH_KEY_UTF8 REFCOUNTED_HE_KEY_UTF8
176
177/*
ffd08bbf 178=for apidoc Amx|SV *|cophh_fetch_pvn|const COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|U32 flags
20439bc7 179
2d7f6611
KW
180Look up the entry in the cop hints hash C<cophh> with the key specified by
181C<keypv> and C<keylen>. If C<flags> has the C<COPHH_KEY_UTF8> bit set,
20439bc7 182the key octets are interpreted as UTF-8, otherwise they are interpreted
2d7f6611 183as Latin-1. C<hash> is a precomputed hash of the key string, or zero if
20439bc7
Z
184it has not been precomputed. Returns a mortal scalar copy of the value
185associated with the key, or C<&PL_sv_placeholder> if there is no value
186associated with the key.
187
188=cut
189*/
190
191#define cophh_fetch_pvn(cophh, keypv, keylen, hash, flags) \
192 Perl_refcounted_he_fetch_pvn(aTHX_ cophh, keypv, keylen, hash, flags)
193
194/*
ffd08bbf 195=for apidoc Amx|SV *|cophh_fetch_pvs|const COPHH *cophh|const char *key|U32 flags
20439bc7 196
0c395ea5
KW
197Like L</cophh_fetch_pvn>, but takes a C<NUL>-terminated literal string instead
198of a string/length pair, and no precomputed hash.
20439bc7
Z
199
200=cut
201*/
202
203#define cophh_fetch_pvs(cophh, key, flags) \
204 Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, flags)
205
206/*
ffd08bbf 207=for apidoc Amx|SV *|cophh_fetch_pv|const COPHH *cophh|const char *key|U32 hash|U32 flags
20439bc7
Z
208
209Like L</cophh_fetch_pvn>, but takes a nul-terminated string instead of
210a string/length pair.
211
212=cut
213*/
214
215#define cophh_fetch_pv(cophh, key, hash, flags) \
216 Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, flags)
217
218/*
ffd08bbf 219=for apidoc Amx|SV *|cophh_fetch_sv|const COPHH *cophh|SV *key|U32 hash|U32 flags
20439bc7
Z
220
221Like L</cophh_fetch_pvn>, but takes a Perl scalar instead of a
222string/length pair.
223
224=cut
225*/
226
227#define cophh_fetch_sv(cophh, key, hash, flags) \
228 Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, flags)
229
230/*
ffd08bbf 231=for apidoc Amx|HV *|cophh_2hv|const COPHH *cophh|U32 flags
20439bc7
Z
232
233Generates and returns a standard Perl hash representing the full set of
2d7f6611 234key/value pairs in the cop hints hash C<cophh>. C<flags> is currently
20439bc7
Z
235unused and must be zero.
236
237=cut
238*/
239
240#define cophh_2hv(cophh, flags) \
241 Perl_refcounted_he_chain_2hv(aTHX_ cophh, flags)
242
243/*
ffd08bbf 244=for apidoc Amx|COPHH *|cophh_copy|COPHH *cophh
20439bc7 245
2d7f6611 246Make and return a complete copy of the cop hints hash C<cophh>.
20439bc7
Z
247
248=cut
249*/
250
251#define cophh_copy(cophh) Perl_refcounted_he_inc(aTHX_ cophh)
252
253/*
ffd08bbf 254=for apidoc Amx|void|cophh_free|COPHH *cophh
20439bc7 255
2d7f6611 256Discard the cop hints hash C<cophh>, freeing all resources associated
20439bc7
Z
257with it.
258
259=cut
260*/
261
262#define cophh_free(cophh) Perl_refcounted_he_free(aTHX_ cophh)
263
264/*
ffd08bbf 265=for apidoc Amx|COPHH *|cophh_new_empty
20439bc7
Z
266
267Generate and return a fresh cop hints hash containing no entries.
268
269=cut
270*/
271
272#define cophh_new_empty() ((COPHH *)NULL)
273
274/*
ffd08bbf 275=for apidoc Amx|COPHH *|cophh_store_pvn|COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|SV *value|U32 flags
20439bc7 276
2d7f6611 277Stores a value, associated with a key, in the cop hints hash C<cophh>,
20439bc7
Z
278and returns the modified hash. The returned hash pointer is in general
279not the same as the hash pointer that was passed in. The input hash is
280consumed by the function, and the pointer to it must not be subsequently
281used. Use L</cophh_copy> if you need both hashes.
282
2d7f6611 283The key is specified by C<keypv> and C<keylen>. If C<flags> has the
20439bc7 284C<COPHH_KEY_UTF8> bit set, the key octets are interpreted as UTF-8,
2d7f6611 285otherwise they are interpreted as Latin-1. C<hash> is a precomputed
20439bc7
Z
286hash of the key string, or zero if it has not been precomputed.
287
2d7f6611 288C<value> is the scalar value to store for this key. C<value> is copied
20439bc7
Z
289by this function, which thus does not take ownership of any reference
290to it, and later changes to the scalar will not be reflected in the
291value visible in the cop hints hash. Complex types of scalar will not
292be stored with referential integrity, but will be coerced to strings.
293
294=cut
295*/
296
297#define cophh_store_pvn(cophh, keypv, keylen, hash, value, flags) \
298 Perl_refcounted_he_new_pvn(aTHX_ cophh, keypv, keylen, hash, value, flags)
299
300/*
ffd08bbf 301=for apidoc Amx|COPHH *|cophh_store_pvs|const COPHH *cophh|const char *key|SV *value|U32 flags
20439bc7 302
0c395ea5
KW
303Like L</cophh_store_pvn>, but takes a C<NUL>-terminated literal string instead
304of a string/length pair, and no precomputed hash.
20439bc7
Z
305
306=cut
307*/
308
309#define cophh_store_pvs(cophh, key, value, flags) \
310 Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, value, flags)
311
312/*
ffd08bbf 313=for apidoc Amx|COPHH *|cophh_store_pv|const COPHH *cophh|const char *key|U32 hash|SV *value|U32 flags
20439bc7
Z
314
315Like L</cophh_store_pvn>, but takes a nul-terminated string instead of
316a string/length pair.
317
318=cut
319*/
320
321#define cophh_store_pv(cophh, key, hash, value, flags) \
322 Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, value, flags)
323
324/*
ffd08bbf 325=for apidoc Amx|COPHH *|cophh_store_sv|const COPHH *cophh|SV *key|U32 hash|SV *value|U32 flags
20439bc7
Z
326
327Like L</cophh_store_pvn>, but takes a Perl scalar instead of a
328string/length pair.
329
330=cut
331*/
332
333#define cophh_store_sv(cophh, key, hash, value, flags) \
334 Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, value, flags)
335
336/*
ffd08bbf 337=for apidoc Amx|COPHH *|cophh_delete_pvn|COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|U32 flags
20439bc7 338
2d7f6611 339Delete a key and its associated value from the cop hints hash C<cophh>,
20439bc7
Z
340and returns the modified hash. The returned hash pointer is in general
341not the same as the hash pointer that was passed in. The input hash is
342consumed by the function, and the pointer to it must not be subsequently
343used. Use L</cophh_copy> if you need both hashes.
344
2d7f6611 345The key is specified by C<keypv> and C<keylen>. If C<flags> has the
20439bc7 346C<COPHH_KEY_UTF8> bit set, the key octets are interpreted as UTF-8,
2d7f6611 347otherwise they are interpreted as Latin-1. C<hash> is a precomputed
20439bc7
Z
348hash of the key string, or zero if it has not been precomputed.
349
350=cut
351*/
352
353#define cophh_delete_pvn(cophh, keypv, keylen, hash, flags) \
354 Perl_refcounted_he_new_pvn(aTHX_ cophh, keypv, keylen, hash, \
355 (SV *)NULL, flags)
356
357/*
ffd08bbf 358=for apidoc Amx|COPHH *|cophh_delete_pvs|const COPHH *cophh|const char *key|U32 flags
20439bc7 359
0c395ea5
KW
360Like L</cophh_delete_pvn>, but takes a C<NUL>-terminated literal string instead
361of a string/length pair, and no precomputed hash.
20439bc7
Z
362
363=cut
364*/
365
366#define cophh_delete_pvs(cophh, key, flags) \
367 Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \
368 (SV *)NULL, flags)
369
370/*
ffd08bbf 371=for apidoc Amx|COPHH *|cophh_delete_pv|const COPHH *cophh|const char *key|U32 hash|U32 flags
20439bc7
Z
372
373Like L</cophh_delete_pvn>, but takes a nul-terminated string instead of
374a string/length pair.
375
376=cut
377*/
378
379#define cophh_delete_pv(cophh, key, hash, flags) \
380 Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, (SV *)NULL, flags)
381
382/*
ffd08bbf 383=for apidoc Amx|COPHH *|cophh_delete_sv|const COPHH *cophh|SV *key|U32 hash|U32 flags
20439bc7
Z
384
385Like L</cophh_delete_pvn>, but takes a Perl scalar instead of a
386string/length pair.
387
388=cut
389*/
390
391#define cophh_delete_sv(cophh, key, hash, flags) \
392 Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, (SV *)NULL, flags)
0d2925a6 393
5ac1e9b2 394#include "mydtrace.h"
0d2925a6 395
88df5f01
FC
396struct cop {
397 BASEOP
398 /* On LP64 putting this here takes advantage of the fact that BASEOP isn't
399 an exact multiple of 8 bytes to save structure padding. */
400 line_t cop_line; /* line # of this command */
401 /* label for this construct is now stored in cop_hints_hash */
57843af0 402#ifdef USE_ITHREADS
88df5f01
FC
403 PADOFFSET cop_stashoff; /* offset into PL_stashpad, for the
404 package the line was compiled in */
1dc74fdb 405 char * cop_file; /* file name the following line # is from */
57843af0 406#else
88df5f01 407 HV * cop_stash; /* package line was compiled in */
79072805 408 GV * cop_filegv; /* file the following line # is from */
57843af0 409#endif
88df5f01
FC
410 U32 cop_hints; /* hints bits from pragmata */
411 U32 cop_seq; /* parse sequence number */
412 /* Beware. mg.c and warnings.pl assume the type of this is STRLEN *: */
413 STRLEN * cop_warnings; /* lexical warnings bitmask */
414 /* compile time state of %^H. See the comment in op.c for how this is
415 used to recreate a hash to return from caller. */
20439bc7 416 COPHH * cop_hints_hash;
79072805
LW
417};
418
57843af0 419#ifdef USE_ITHREADS
1dc74fdb
FC
420# define CopFILE(c) ((c)->cop_file)
421# define CopFILEGV(c) (CopFILE(c) \
422 ? gv_fetchfile(CopFILE(c)) : NULL)
423
424# ifdef NETWARE
425# define CopFILE_set(c,pv) ((c)->cop_file = savepv(pv))
d166b3ad 426# define CopFILE_setn(c,pv,l) ((c)->cop_file = savepvn((pv),(l)))
1dc74fdb
FC
427# else
428# define CopFILE_set(c,pv) ((c)->cop_file = savesharedpv(pv))
429# define CopFILE_setn(c,pv,l) ((c)->cop_file = savesharedpvn((pv),(l)))
430# endif
431
432# define CopFILESV(c) (CopFILE(c) \
433 ? GvSV(gv_fetchfile(CopFILE(c))) : NULL)
434# define CopFILEAV(c) (CopFILE(c) \
435 ? GvAV(gv_fetchfile(CopFILE(c))) : NULL)
436# define CopFILEAVx(c) (assert_(CopFILE(c)) \
437 GvAV(gv_fetchfile(CopFILE(c))))
083fcd59 438
d4d03940
FC
439# define CopSTASH(c) PL_stashpad[(c)->cop_stashoff]
440# define CopSTASH_set(c,hv) ((c)->cop_stashoff = (hv) \
441 ? alloccopstash(hv) \
442 : 0)
1dc74fdb
FC
443# ifdef NETWARE
444# define CopFILE_free(c) SAVECOPFILE_FREE(c)
445# else
446# define CopFILE_free(c) (PerlMemShared_free(CopFILE(c)),(CopFILE(c) = NULL))
447# endif
57843af0
GS
448#else
449# define CopFILEGV(c) ((c)->cop_filegv)
f4dd75d9 450# define CopFILEGV_set(c,gv) ((c)->cop_filegv = (GV*)SvREFCNT_inc(gv))
1dc74fdb
FC
451# define CopFILE_set(c,pv) CopFILEGV_set((c), gv_fetchfile(pv))
452# define CopFILE_setn(c,pv,l) CopFILEGV_set((c), gv_fetchfile_flags((pv),(l),0))
453# define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : NULL)
454# define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : NULL)
455# ifdef DEBUGGING
456# define CopFILEAVx(c) (assert(CopFILEGV(c)), GvAV(CopFILEGV(c)))
457# else
458# define CopFILEAVx(c) (GvAV(CopFILEGV(c)))
459# endif
460# define CopFILE(c) (CopFILEGV(c) \
461 ? GvNAME(CopFILEGV(c))+2 : NULL)
57843af0 462# define CopSTASH(c) ((c)->cop_stash)
f4dd75d9 463# define CopSTASH_set(c,hv) ((c)->cop_stash = (hv))
a0714e2c 464# define CopFILE_free(c) (SvREFCNT_dec(CopFILEGV(c)),(CopFILEGV(c) = NULL))
05ec9bb3 465
57843af0 466#endif /* USE_ITHREADS */
20439bc7 467
d4d03940
FC
468#define CopSTASHPV(c) (CopSTASH(c) ? HvNAME_get(CopSTASH(c)) : NULL)
469 /* cop_stash is not refcounted */
470#define CopSTASHPV_set(c,pv) CopSTASH_set((c), gv_stashpv(pv,GV_ADD))
471#define CopSTASH_eq(c,hv) (CopSTASH(c) == (hv))
d4d03940 472
20439bc7
Z
473#define CopHINTHASH_get(c) ((COPHH*)((c)->cop_hints_hash))
474#define CopHINTHASH_set(c,h) ((c)->cop_hints_hash = (h))
475
476/*
477=head1 COP Hint Reading
478*/
479
480/*
481=for apidoc Am|SV *|cop_hints_fetch_pvn|const COP *cop|const char *keypv|STRLEN keylen|U32 hash|U32 flags
482
2d7f6611
KW
483Look up the hint entry in the cop C<cop> with the key specified by
484C<keypv> and C<keylen>. If C<flags> has the C<COPHH_KEY_UTF8> bit set,
20439bc7 485the key octets are interpreted as UTF-8, otherwise they are interpreted
2d7f6611 486as Latin-1. C<hash> is a precomputed hash of the key string, or zero if
20439bc7
Z
487it has not been precomputed. Returns a mortal scalar copy of the value
488associated with the key, or C<&PL_sv_placeholder> if there is no value
489associated with the key.
490
491=cut
492*/
493
494#define cop_hints_fetch_pvn(cop, keypv, keylen, hash, flags) \
495 cophh_fetch_pvn(CopHINTHASH_get(cop), keypv, keylen, hash, flags)
496
497/*
498=for apidoc Am|SV *|cop_hints_fetch_pvs|const COP *cop|const char *key|U32 flags
499
0c395ea5
KW
500Like L</cop_hints_fetch_pvn>, but takes a C<NUL>-terminated literal string
501instead of a string/length pair, and no precomputed hash.
20439bc7
Z
502
503=cut
504*/
505
506#define cop_hints_fetch_pvs(cop, key, flags) \
507 cophh_fetch_pvs(CopHINTHASH_get(cop), key, flags)
508
509/*
510=for apidoc Am|SV *|cop_hints_fetch_pv|const COP *cop|const char *key|U32 hash|U32 flags
511
512Like L</cop_hints_fetch_pvn>, but takes a nul-terminated string instead
513of a string/length pair.
514
515=cut
516*/
517
518#define cop_hints_fetch_pv(cop, key, hash, flags) \
519 cophh_fetch_pv(CopHINTHASH_get(cop), key, hash, flags)
520
521/*
522=for apidoc Am|SV *|cop_hints_fetch_sv|const COP *cop|SV *key|U32 hash|U32 flags
523
524Like L</cop_hints_fetch_pvn>, but takes a Perl scalar instead of a
525string/length pair.
526
527=cut
528*/
529
530#define cop_hints_fetch_sv(cop, key, hash, flags) \
531 cophh_fetch_sv(CopHINTHASH_get(cop), key, hash, flags)
532
533/*
534=for apidoc Am|HV *|cop_hints_2hv|const COP *cop|U32 flags
535
536Generates and returns a standard Perl hash representing the full set of
2d7f6611 537hint entries in the cop C<cop>. C<flags> is currently unused and must
20439bc7
Z
538be zero.
539
540=cut
541*/
542
543#define cop_hints_2hv(cop, flags) \
544 cophh_2hv(CopHINTHASH_get(cop), flags)
545
aebc0cbe 546#define CopLABEL(c) Perl_cop_fetch_label(aTHX_ (c), NULL, NULL)
5db1eb8d
BF
547#define CopLABEL_len(c,len) Perl_cop_fetch_label(aTHX_ (c), len, NULL)
548#define CopLABEL_len_flags(c,len,flags) Perl_cop_fetch_label(aTHX_ (c), len, flags)
dca6062a 549#define CopLABEL_alloc(pv) ((pv)?savepv(pv):NULL)
57843af0 550
ed094faf 551#define CopSTASH_ne(c,hv) (!CopSTASH_eq(c,hv))
cc49e20b 552#define CopLINE(c) ((c)->cop_line)
57843af0
GS
553#define CopLINE_inc(c) (++CopLINE(c))
554#define CopLINE_dec(c) (--CopLINE(c))
555#define CopLINE_set(c,l) (CopLINE(c) = (l))
cc49e20b 556
248c2a4d 557/* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */
e37778c2 558#define OutCopFILE(c) CopFILE(c)
248c2a4d 559
d5ec2987 560#define CopHINTS_get(c) ((c)->cop_hints + 0)
623e6609 561#define CopHINTS_set(c, h) STMT_START { \
d5ec2987 562 (c)->cop_hints = (h); \
623e6609
NC
563 } STMT_END
564
79072805
LW
565/*
566 * Here we have some enormously heavy (or at least ponderous) wizardry.
567 */
568
569/* subroutine context */
570struct block_sub {
f9c764c5 571 OP * retop; /* op to execute on exit from sub */
adcbf118 572 /* Above here is the same for sub, format and eval. */
9aa350eb 573 PAD *prevcomppad; /* the caller's PL_comppad */
8e663997 574 CV * cv;
f9c764c5 575 /* Above here is the same for sub and format. */
bb172083 576 I32 olddepth;
3073d04f 577 AV *savearray;
f9c764c5
NC
578};
579
580
581/* format context */
582struct block_format {
f39bc417 583 OP * retop; /* op to execute on exit from sub */
adcbf118 584 /* Above here is the same for sub, format and eval. */
9aa350eb 585 PAD *prevcomppad; /* the caller's PL_comppad */
8e663997 586 CV * cv;
f9c764c5
NC
587 /* Above here is the same for sub and format. */
588 GV * gv;
589 GV * dfoutgv;
79072805
LW
590};
591
4ebe6e95
DM
592/* return a pointer to the current context */
593
594#define CX_CUR() (&cxstack[cxstack_ix])
595
dfe0f39b
DM
596/* free all savestack items back to the watermark of the specified context */
597
e992140c 598#define CX_LEAVE_SCOPE(cx) LEAVE_SCOPE(cx->blk_oldsaveix)
dfe0f39b 599
5da525e9
DM
600#ifdef DEBUGGING
601/* on debugging builds, poison cx afterwards so we know no code
602 * uses it - because after doing cxstack_ix--, any ties, exceptions etc
603 * may overwrite the current stack frame */
604# define CX_POP(cx) \
4ebe6e95 605 assert(CX_CUR() == cx); \
5da525e9
DM
606 cxstack_ix--; \
607 cx = NULL;
608#else
609# define CX_POP(cx) cxstack_ix--;
610#endif
611
612
b36bdeca 613/* base for the next two macros. Don't use directly.
d6911e1b
DM
614 * The context frame holds a reference to the CV so that it can't be
615 * freed while we're executing it */
b36bdeca 616
b36bdeca 617
490576d1 618#define CX_PUSHSUB_GET_LVALUE_MASK(func) \
777d9014
FC
619 /* If the context is indeterminate, then only the lvalue */ \
620 /* flags that the caller also has are applicable. */ \
4587c532 621 ( \
777d9014
FC
622 (PL_op->op_flags & OPf_WANT) \
623 ? OPpENTERSUB_LVAL_MASK \
624 : !(PL_op->op_private & OPpENTERSUB_LVAL_MASK) \
4587c532
FC
625 ? 0 : (U8)func(aTHX) \
626 )
627
fad386cb 628/* Restore old @_ */
b405d38b 629#define CX_POP_SAVEARRAY(cx) \
6d4ff0d2 630 STMT_START { \
f2b9631d 631 AV *cx_pop_savearray_av = GvAV(PL_defgv); \
a8bba7fa 632 GvAV(PL_defgv) = cx->blk_sub.savearray; \
656457d0 633 cx->blk_sub.savearray = NULL; \
f2b9631d 634 SvREFCNT_dec(cx_pop_savearray_av); \
6d4ff0d2 635 } STMT_END
6d4ff0d2 636
ecf8e9dd 637/* junk in @_ spells trouble when cloning CVs and in pp_caller(), so don't
8e09340b
GS
638 * leave any (a fast av_clear(ary), basically) */
639#define CLEAR_ARGARRAY(ary) \
640 STMT_START { \
641 AvMAX(ary) += AvARRAY(ary) - AvALLOC(ary); \
9c6bc640 642 AvARRAY(ary) = AvALLOC(ary); \
8e09340b
GS
643 AvFILLp(ary) = -1; \
644 } STMT_END
7766f137 645
7e27c4a5 646
79072805
LW
647/* eval context */
648struct block_eval {
8e663997
NC
649 OP * retop; /* op to execute on exit from eval */
650 /* Above here is the same for sub, format and eval. */
0f79a09d 651 SV * old_namesv;
79072805 652 OP * old_eval_root;
748a9306 653 SV * cur_text;
2090ab20 654 CV * cv;
abd70938 655 JMPENV * cur_top_env; /* value of PL_top_env when eval CX created */
79072805
LW
656};
657
17347a51
NC
658/* If we ever need more than 512 op types, change the shift from 7.
659 blku_gimme is actually also only 2 bits, so could be merged with something.
660*/
661
4c57ced5
DM
662/* blk_u16 bit usage for eval contexts: */
663
664#define CxOLD_IN_EVAL(cx) (((cx)->blk_u16) & 0x3F) /* saved PL in_eval */
665#define CxEVAL_TXT_REFCNTED(cx) (((cx)->blk_u16) & 0x40) /* cur_text rc++ */
666#define CxOLD_OP_TYPE(cx) (((cx)->blk_u16) >> 7) /* type of eval op */
85a64632 667
79072805
LW
668/* loop context */
669struct block_loop {
022eaa24 670 LOOP * my_op; /* My op, that contains redo, next and last ops. */
df530c37 671 union { /* different ways of locating the iteration variable */
b52de964
DM
672 SV **svp; /* for lexicals: address of pad slot */
673 GV *gv; /* for package vars */
df530c37 674 } itervar_u;
f0bb9bf7 675 SV *itersave; /* the original iteration var */
493b0a3c 676 union {
93661e56
DM
677 struct { /* CXt_LOOP_ARY, C<for (@ary)> */
678 AV *ary; /* array being iterated over */
679 IV ix; /* index relative to base of array */
d01136d6 680 } ary;
93661e56
DM
681 struct { /* CXt_LOOP_LIST, C<for (list)> */
682 I32 basesp; /* first element of list on stack */
683 IV ix; /* index relative to basesp */
684 } stack;
685 struct { /* CXt_LOOP_LAZYIV, C<for (1..9)> */
d01136d6
BS
686 IV cur;
687 IV end;
688 } lazyiv;
93661e56 689 struct { /* CXt_LOOP_LAZYSV C<for ('a'..'z')> */
d01136d6
BS
690 SV * cur;
691 SV * end; /* maxiumum value (or minimum in reverse) */
692 } lazysv;
693 } state_u;
7766f137 694#ifdef USE_ITHREADS
b52de964 695 PAD *oldcomppad; /* needed to map itervar_u.svp during thread clone */
f83b46a0 696#endif
b52de964 697};
f83b46a0 698
9af0df0b
DM
699#define CxITERVAR(c) \
700 (CxPADLOOP(c) \
701 ? (c)->blk_loop.itervar_u.svp \
702 : ((c)->cx_type & CXp_FOR_GV) \
703 ? &GvSV((c)->blk_loop.itervar_u.gv) \
704 : (SV **)&(c)->blk_loop.itervar_u.gv)
f83b46a0 705
b1cf8b36 706#define CxLABEL(c) (0 + CopLABEL((c)->blk_oldcop))
5db1eb8d
BF
707#define CxLABEL_len(c,len) (0 + CopLABEL_len((c)->blk_oldcop, len))
708#define CxLABEL_len_flags(c,len,flags) (0 + CopLABEL_len_flags((c)->blk_oldcop, len, flags))
ae423868 709#define CxHASARGS(c) (((c)->cx_type & CXp_HASARGS) == CXp_HASARGS)
d9203e03
DM
710
711/* CxLVAL(): the lval flags of the call site: the relevant flag bits from
712 * the op_private field of the calling pp_entersub (or its caller's caller
713 * if the caller's lvalue context isn't known):
714 * OPpLVAL_INTRO: sub used in lvalue context, e.g. f() = 1;
715 * OPpENTERSUB_INARGS (in conjunction with OPpLVAL_INTRO): the
716 * function is being used as a sub arg or as a referent, e.g.
717 * g(...,f(),...) or $r = \f()
718 * OPpDEREF: 2-bit mask indicating e.g. f()->[0].
719 * Note the contrast with CvLVALUE(), which is a property of the sub
720 * rather than the call site.
721 */
1956db7e 722#define CxLVAL(c) (0 + ((c)->blk_u16 & 0xff))
1956db7e 723
7766f137 724
79072805 725
0d863452
RH
726/* given/when context */
727struct block_givwhen {
728 OP *leave_op;
b95eccd3 729 SV *defsv_save; /* the original $_ */
0d863452
RH
730};
731
0d863452 732
c5369ccc 733
79072805
LW
734/* context common to subroutines, evals and loops */
735struct block {
446d0787 736 U8 blku_type; /* what kind of context this is */
2e0df0e8 737 U8 blku_gimme; /* is this block running in list context? */
17347a51 738 U16 blku_u16; /* used by block_sub and block_eval (so far) */
e992140c
DM
739 I32 blku_oldsaveix; /* saved PL_savestack_ix */
740 /* all the fields above must be aligned with same-sized fields as sbu */
1bef65a2 741 I32 blku_oldsp; /* current sp floor: where nextstate pops to */
79072805 742 I32 blku_oldmarksp; /* mark stack index */
f284fecd 743 COP * blku_oldcop; /* old curcop pointer */
79072805 744 PMOP * blku_oldpm; /* values of pattern match vars */
bb3300d1 745 SSize_t blku_old_tmpsfloor; /* saved PL_tmps_floor */
f284fecd 746 I32 blku_oldscopesp; /* scope stack index */
79072805
LW
747
748 union {
749 struct block_sub blku_sub;
f9c764c5 750 struct block_format blku_format;
79072805
LW
751 struct block_eval blku_eval;
752 struct block_loop blku_loop;
0d863452 753 struct block_givwhen blku_givwhen;
79072805
LW
754 } blk_u;
755};
756#define blk_oldsp cx_u.cx_blk.blku_oldsp
757#define blk_oldcop cx_u.cx_blk.blku_oldcop
79072805
LW
758#define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
759#define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
760#define blk_oldpm cx_u.cx_blk.blku_oldpm
761#define blk_gimme cx_u.cx_blk.blku_gimme
446d0787 762#define blk_u16 cx_u.cx_blk.blku_u16
e992140c 763#define blk_oldsaveix cx_u.cx_blk.blku_oldsaveix
ce8bb8d8 764#define blk_old_tmpsfloor cx_u.cx_blk.blku_old_tmpsfloor
79072805 765#define blk_sub cx_u.cx_blk.blk_u.blku_sub
f9c764c5 766#define blk_format cx_u.cx_blk.blk_u.blku_format
79072805
LW
767#define blk_eval cx_u.cx_blk.blk_u.blku_eval
768#define blk_loop cx_u.cx_blk.blk_u.blku_loop
0d863452 769#define blk_givwhen cx_u.cx_blk.blk_u.blku_givwhen
79072805 770
5e691bc6 771#define CX_DEBUG(cx, action) \
d9f81b50 772 DEBUG_l( \
bb3300d1 773 Perl_deb(aTHX_ "CX %ld %s %s (scope %ld,%ld) (save %ld,%ld) at %s:%d\n",\
1c98cc53
DM
774 (long)cxstack_ix, \
775 action, \
5e691bc6 776 PL_block_type[CxTYPE(cx)], \
1c98cc53 777 (long)PL_scopestack_ix, \
5e691bc6 778 (long)(cx->blk_oldscopesp), \
bb3300d1 779 (long)PL_savestack_ix, \
5e691bc6 780 (long)(cx->blk_oldsaveix), \
d9f81b50 781 __FILE__, __LINE__));
1c98cc53 782
ed8ff0f3 783
79072805
LW
784
785/* substitution context */
786struct subst {
e992140c 787 U8 sbu_type; /* same as blku_type */
1ed74d04 788 U8 sbu_rflags;
e992140c
DM
789 U16 sbu_rxtainted;
790 I32 sbu_oldsaveix; /* same as blku_oldsaveix */
a7f94955 791 /* all the fields above must be aligned with same-sized fields as blk_u */
3c6ef0a5
FC
792 SSize_t sbu_iters;
793 SSize_t sbu_maxiters;
79072805
LW
794 char * sbu_orig;
795 SV * sbu_dstr;
796 SV * sbu_targ;
797 char * sbu_s;
798 char * sbu_m;
799 char * sbu_strend;
c90c0ff4 800 void * sbu_rxres;
c07a80fd 801 REGEXP * sbu_rx;
79072805
LW
802};
803#define sb_iters cx_u.cx_subst.sbu_iters
804#define sb_maxiters cx_u.cx_subst.sbu_maxiters
22e551b9 805#define sb_rflags cx_u.cx_subst.sbu_rflags
71be2cbc 806#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
807#define sb_orig cx_u.cx_subst.sbu_orig
808#define sb_dstr cx_u.cx_subst.sbu_dstr
809#define sb_targ cx_u.cx_subst.sbu_targ
810#define sb_s cx_u.cx_subst.sbu_s
811#define sb_m cx_u.cx_subst.sbu_m
812#define sb_strend cx_u.cx_subst.sbu_strend
c90c0ff4 813#define sb_rxres cx_u.cx_subst.sbu_rxres
c07a80fd 814#define sb_rx cx_u.cx_subst.sbu_rx
79072805 815
9c105995 816#ifdef PERL_CORE
490576d1 817# define CX_PUSHSUBST(cx) CXINC, cx = CX_CUR(), \
e992140c 818 cx->blk_oldsaveix = oldsave, \
79072805
LW
819 cx->sb_iters = iters, \
820 cx->sb_maxiters = maxiters, \
22e551b9 821 cx->sb_rflags = r_flags, \
71be2cbc 822 cx->sb_rxtainted = rxtainted, \
79072805
LW
823 cx->sb_orig = orig, \
824 cx->sb_dstr = dstr, \
825 cx->sb_targ = targ, \
826 cx->sb_s = s, \
827 cx->sb_m = m, \
828 cx->sb_strend = strend, \
4608196e 829 cx->sb_rxres = NULL, \
c07a80fd 830 cx->sb_rx = rx, \
1ed74d04 831 cx->cx_type = CXt_SUBST | (once ? CXp_ONCE : 0); \
e22ae1e2 832 rxres_save(&cx->sb_rxres, rx); \
cf69025f
TC
833 (void)ReREFCNT_inc(rx); \
834 SvREFCNT_inc_void_NN(targ)
79072805 835
b405d38b 836# define CX_POPSUBST(cx) \
77189b8c
DM
837 STMT_START { \
838 REGEXP *re; \
e4592eb4 839 assert(CxTYPE(cx) == CXt_SUBST); \
e22ae1e2 840 rxres_free(&cx->sb_rxres); \
77189b8c
DM
841 re = cx->sb_rx; \
842 cx->sb_rx = NULL; \
843 ReREFCNT_dec(re); \
844 SvREFCNT_dec_NN(cx->sb_targ); \
845 } STMT_END
9c105995
NC
846#endif
847
848#define CxONCE(cx) ((cx)->cx_type & CXp_ONCE)
79072805
LW
849
850struct context {
79072805
LW
851 union {
852 struct block cx_blk;
853 struct subst cx_subst;
854 } cx_u;
855};
2e0df0e8 856#define cx_type cx_u.cx_subst.sbu_type
6b35e009 857
76753e7f
NC
858/* If you re-order these, there is also an array of uppercase names in perl.h
859 and a static array of context names in pp_ctl.c */
3701055e 860#define CXTYPEMASK 0xf
79646418 861#define CXt_NULL 0 /* currently only used for sort BLOCK */
76753e7f
NC
862#define CXt_WHEN 1
863#define CXt_BLOCK 2
864/* When micro-optimising :-) keep GIVEN next to the LOOPs, as these 5 share a
865 jump table in pp_ctl.c
866 The first 4 don't have a 'case' in at least one switch statement in pp_ctl.c
867*/
868#define CXt_GIVEN 3
93661e56
DM
869
870/* be careful of the ordering of these five. Macros like CxTYPE_is_LOOP,
871 * CxFOREACH compare ranges */
c349b9a0
DM
872#define CXt_LOOP_ARY 4 /* for (@ary) { ...; } */
873#define CXt_LOOP_LAZYSV 5 /* for ('a'..'z') { ...; } */
874#define CXt_LOOP_LAZYIV 6 /* for (1..9) { ...; } */
875#define CXt_LOOP_LIST 7 /* for (1,2,3) { ...; } */
876#define CXt_LOOP_PLAIN 8 /* while (...) { ...; }
877 or plain block { ...; } */
93661e56
DM
878#define CXt_SUB 9
879#define CXt_FORMAT 10
880#define CXt_EVAL 11
881#define CXt_SUBST 12
76753e7f 882/* SUBST doesn't feature in all switch statements. */
79072805 883
ae423868 884/* private flags for CXt_SUB and CXt_FORMAT */
79646418
DM
885#define CXp_MULTICALL 0x10 /* part of a multicall (so don't tear down
886 context on exit). (not CXt_FORMAT) */
1f27d788 887#define CXp_HASARGS 0x20
b0065247
DM
888#define CXp_SUB_RE 0x40 /* code called within regex, i.e. (?{}) */
889#define CXp_SUB_RE_FAKE 0x80 /* fake sub CX for (?{}) in current scope */
ae423868 890
6b35e009 891/* private flags for CXt_EVAL */
1f27d788
NC
892#define CXp_REAL 0x20 /* truly eval'', not a lookalike */
893#define CXp_TRYBLOCK 0x40 /* eval{}, not eval'' or similar */
6b35e009 894
7766f137 895/* private flags for CXt_LOOP */
28e0cf42
DM
896
897/* this is only set in conjunction with CXp_FOR_GV */
1f27d788 898#define CXp_FOR_DEF 0x10 /* foreach using $_ */
28e0cf42 899/* these 3 are mutually exclusive */
d39c26a6 900#define CXp_FOR_LVREF 0x20 /* foreach using \$var */
9af0df0b
DM
901#define CXp_FOR_GV 0x40 /* foreach using package var */
902#define CXp_FOR_PAD 0x80 /* foreach using lexical var */
28e0cf42 903
9af0df0b 904#define CxPADLOOP(c) ((c)->cx_type & CXp_FOR_PAD)
e846cb92 905
1ed74d04
NC
906/* private flags for CXt_SUBST */
907#define CXp_ONCE 0x10 /* What was sbu_once in struct subst */
7766f137 908
6b35e009 909#define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
113ebcf5
DM
910#define CxTYPE_is_LOOP(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \
911 && CxTYPE(cx) <= CXt_LOOP_PLAIN)
79646418 912#define CxMULTICALL(c) ((c)->cx_type & CXp_MULTICALL)
0588a150 913#define CxREALEVAL(c) (((c)->cx_type & (CXTYPEMASK|CXp_REAL)) \
7766f137 914 == (CXt_EVAL|CXp_REAL))
0588a150 915#define CxTRYBLOCK(c) (((c)->cx_type & (CXTYPEMASK|CXp_TRYBLOCK)) \
1d76a5c3 916 == (CXt_EVAL|CXp_TRYBLOCK))
113ebcf5
DM
917#define CxFOREACH(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \
918 && CxTYPE(cx) <= CXt_LOOP_LIST)
6b35e009 919
109bf713 920#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
79072805 921
ccfc67b7
JH
922/*
923=head1 "Gimme" Values
924*/
954c1994
GS
925
926/*
927=for apidoc AmU||G_SCALAR
fbe13c60 928Used to indicate scalar context. See C<L</GIMME_V>>, C<L</GIMME>>, and
954c1994
GS
929L<perlcall>.
930
931=for apidoc AmU||G_ARRAY
fbe13c60 932Used to indicate list context. See C<L</GIMME_V>>, C<L</GIMME>> and
954c1994
GS
933L<perlcall>.
934
935=for apidoc AmU||G_VOID
fbe13c60 936Used to indicate void context. See C<L</GIMME_V>> and L<perlcall>.
954c1994
GS
937
938=for apidoc AmU||G_DISCARD
939Indicates that arguments returned from a callback should be discarded. See
940L<perlcall>.
941
942=for apidoc AmU||G_EVAL
943
944Used to force a Perl C<eval> wrapper around a callback. See
945L<perlcall>.
946
947=for apidoc AmU||G_NOARGS
948
949Indicates that no arguments are being sent to a callback. See
950L<perlcall>.
951
952=cut
953*/
954
2f8edad0
NC
955#define G_SCALAR 2
956#define G_ARRAY 3
957#define G_VOID 1
958#define G_WANT 3
79072805 959
864dbfa3 960/* extra flags for Perl_call_* routines */
2f8edad0 961#define G_DISCARD 4 /* Call FREETMPS.
b54b4831
NC
962 Don't change this without consulting the
963 hash actions codes defined in hv.h */
2f8edad0
NC
964#define G_EVAL 8 /* Assume eval {} around subroutine call. */
965#define G_NOARGS 16 /* Don't construct a @_ array. */
7ce09284 966#define G_KEEPERR 32 /* Warn for errors, don't overwrite $@ */
2f8edad0
NC
967#define G_NODEBUG 64 /* Disable debugging at toplevel. */
968#define G_METHOD 128 /* Calling method. */
c40f1585 969#define G_FAKINGEVAL 256 /* Faking an eval context for call_sv or
edb2152a 970 fold_constants. */
67549bd2
NC
971#define G_UNDEF_FILL 512 /* Fill the stack with &PL_sv_undef
972 A special case for UNSHIFT in
973 Perl_magic_methcall(). */
d1d7a15d
NC
974#define G_WRITING_TO_STDERR 1024 /* Perl_write_to_stderr() is calling
975 Perl_magic_methcall(). */
a1941760 976#define G_RE_REPARSING 0x800 /* compiling a run-time /(?{..})/ */
c106c2be 977#define G_METHOD_NAMED 4096 /* calling named method, eg without :: or ' */
e336de0d 978
faef0170
HS
979/* flag bits for PL_in_eval */
980#define EVAL_NULL 0 /* not in an eval */
981#define EVAL_INEVAL 1 /* some enclosing scope is an eval */
982#define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */
864dbfa3 983#define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */
6dc8a9e4 984#define EVAL_INREQUIRE 8 /* The code is being required. */
a1941760 985#define EVAL_RE_REPARSING 0x10 /* eval_sv() called with G_RE_REPARSING */
4c57ced5 986/* if adding extra bits, make sure they can fit in CxOLD_OP_TYPE() */
faef0170 987
e336de0d
GS
988/* Support for switching (stack and block) contexts.
989 * This ensures magic doesn't invalidate local stack and cx pointers.
990 */
991
e788e7d3
GS
992#define PERLSI_UNKNOWN -1
993#define PERLSI_UNDEF 0
994#define PERLSI_MAIN 1
995#define PERLSI_MAGIC 2
996#define PERLSI_SORT 3
997#define PERLSI_SIGNAL 4
998#define PERLSI_OVERLOAD 5
999#define PERLSI_DESTROY 6
1000#define PERLSI_WARNHOOK 7
1001#define PERLSI_DIEHOOK 8
1002#define PERLSI_REQUIRE 9
e1a10f35 1003#define PERLSI_MULTICALL 10
e336de0d
GS
1004
1005struct stackinfo {
1006 AV * si_stack; /* stack for current runlevel */
1007 PERL_CONTEXT * si_cxstack; /* context stack for runlevel */
9bd87817
NC
1008 struct stackinfo * si_prev;
1009 struct stackinfo * si_next;
e336de0d
GS
1010 I32 si_cxix; /* current context index */
1011 I32 si_cxmax; /* maximum allocated index */
1012 I32 si_type; /* type of runlevel */
ce2f7c3b 1013 I32 si_markoff; /* offset where markstack begins for us.
e336de0d
GS
1014 * currently used only with DEBUGGING,
1015 * but not #ifdef-ed for bincompat */
1fced1a2 1016#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
87058c31
DM
1017/* high water mark: for checking if the stack was correctly extended /
1018 * tested for extension by each pp function */
1019 SSize_t si_stack_hwm;
1020#endif
1021
e336de0d
GS
1022};
1023
1024typedef struct stackinfo PERL_SI;
1025
3280af22
NIS
1026#define cxstack (PL_curstackinfo->si_cxstack)
1027#define cxstack_ix (PL_curstackinfo->si_cxix)
1028#define cxstack_max (PL_curstackinfo->si_cxmax)
e336de0d
GS
1029
1030#ifdef DEBUGGING
ce2f7c3b
GS
1031# define SET_MARK_OFFSET \
1032 PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack
e336de0d 1033#else
ce2f7c3b 1034# define SET_MARK_OFFSET NOOP
e336de0d
GS
1035#endif
1036
87058c31 1037#if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
45d6bfc0 1038# define PUSHSTACK_INIT_HWM(si) ((si)->si_stack_hwm = 0)
87058c31
DM
1039#else
1040# define PUSHSTACK_INIT_HWM(si) NOOP
1041#endif
1042
d3acc0f7 1043#define PUSHSTACKi(type) \
e336de0d 1044 STMT_START { \
3280af22 1045 PERL_SI *next = PL_curstackinfo->si_next; \
1c98cc53
DM
1046 DEBUG_l({ \
1047 int i = 0; PERL_SI *p = PL_curstackinfo; \
1048 while (p) { i++; p = p->si_prev; } \
1049 Perl_deb(aTHX_ "push STACKINFO %d at %s:%d\n", \
1050 i, __FILE__, __LINE__);}) \
e336de0d
GS
1051 if (!next) { \
1052 next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \
3280af22
NIS
1053 next->si_prev = PL_curstackinfo; \
1054 PL_curstackinfo->si_next = next; \
e336de0d
GS
1055 } \
1056 next->si_type = type; \
1057 next->si_cxix = -1; \
87058c31 1058 PUSHSTACK_INIT_HWM(next); \
e336de0d 1059 AvFILLp(next->si_stack) = 0; \
3280af22
NIS
1060 SWITCHSTACK(PL_curstack,next->si_stack); \
1061 PL_curstackinfo = next; \
ce2f7c3b 1062 SET_MARK_OFFSET; \
e336de0d
GS
1063 } STMT_END
1064
e788e7d3 1065#define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
d3acc0f7 1066
3095d977
GS
1067/* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
1068 * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
d3acc0f7 1069#define POPSTACK \
e336de0d 1070 STMT_START { \
39644a26 1071 dSP; \
c4420975 1072 PERL_SI * const prev = PL_curstackinfo->si_prev; \
1c98cc53
DM
1073 DEBUG_l({ \
1074 int i = -1; PERL_SI *p = PL_curstackinfo; \
1075 while (p) { i++; p = p->si_prev; } \
1076 Perl_deb(aTHX_ "pop STACKINFO %d at %s:%d\n", \
1077 i, __FILE__, __LINE__);}) \
e336de0d 1078 if (!prev) { \
3d04513d 1079 Perl_croak_popstack(); \
e336de0d 1080 } \
3280af22 1081 SWITCHSTACK(PL_curstack,prev->si_stack); \
e336de0d 1082 /* don't free prev here, free them all at the END{} */ \
3280af22 1083 PL_curstackinfo = prev; \
e336de0d
GS
1084 } STMT_END
1085
1086#define POPSTACK_TO(s) \
1087 STMT_START { \
3280af22 1088 while (PL_curstack != s) { \
bac4b2ad 1089 dounwind(-1); \
d3acc0f7 1090 POPSTACK; \
bac4b2ad 1091 } \
e336de0d 1092 } STMT_END
923e4eb5 1093
9d990559
DM
1094#define IN_PERL_COMPILETIME cBOOL(PL_curcop == &PL_compiling)
1095#define IN_PERL_RUNTIME cBOOL(PL_curcop != &PL_compiling)
923e4eb5 1096
ed8ff0f3
DM
1097
1098
1099
9850bf21
RH
1100/*
1101=head1 Multicall Functions
1102
1103=for apidoc Ams||dMULTICALL
72d33970 1104Declare local variables for a multicall. See L<perlcall/LIGHTWEIGHT CALLBACKS>.
9850bf21
RH
1105
1106=for apidoc Ams||PUSH_MULTICALL
1107Opening bracket for a lightweight callback.
0eda4409 1108See L<perlcall/LIGHTWEIGHT CALLBACKS>.
9850bf21
RH
1109
1110=for apidoc Ams||MULTICALL
72d33970 1111Make a lightweight callback. See L<perlcall/LIGHTWEIGHT CALLBACKS>.
9850bf21
RH
1112
1113=for apidoc Ams||POP_MULTICALL
1114Closing bracket for a lightweight callback.
0eda4409 1115See L<perlcall/LIGHTWEIGHT CALLBACKS>.
9850bf21
RH
1116
1117=cut
1118*/
1119
1120#define dMULTICALL \
79503099 1121 OP *multicall_cop; \
35095fd0 1122 bool multicall_oldcatch
9850bf21 1123
82f35e8b 1124#define PUSH_MULTICALL(the_cv) \
b0065247 1125 PUSH_MULTICALL_FLAGS(the_cv, 0)
81ed78b2 1126
b0065247
DM
1127/* Like PUSH_MULTICALL, but allows you to specify extra flags
1128 * for the CX stack entry (this isn't part of the public API) */
81ed78b2 1129
b0065247 1130#define PUSH_MULTICALL_FLAGS(the_cv, flags) \
9850bf21 1131 STMT_START { \
79503099 1132 PERL_CONTEXT *cx; \
0bcc34c2
AL
1133 CV * const _nOnclAshIngNamE_ = the_cv; \
1134 CV * const cv = _nOnclAshIngNamE_; \
b70d5558 1135 PADLIST * const padlist = CvPADLIST(cv); \
9850bf21 1136 multicall_oldcatch = CATCH_GET; \
9850bf21 1137 CATCH_SET(TRUE); \
e1a10f35 1138 PUSHSTACKi(PERLSI_MULTICALL); \
1c23e2bd 1139 cx = cx_pushblock((CXt_SUB|CXp_MULTICALL|flags), (U8)gimme, \
fd9b7244 1140 PL_stack_sp, PL_savestack_ix); \
79503099 1141 cx_pushsub(cx, cv, NULL, 0); \
49f582ca 1142 SAVEOP(); \
b0065247
DM
1143 if (!(flags & CXp_SUB_RE_FAKE)) \
1144 CvDEPTH(cv)++; \
d2af2719 1145 if (CvDEPTH(cv) >= 2) \
9850bf21 1146 Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \
9850bf21
RH
1147 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \
1148 multicall_cop = CvSTART(cv); \
1149 } STMT_END
1150
1151#define MULTICALL \
1152 STMT_START { \
1153 PL_op = multicall_cop; \
1154 CALLRUNOPS(aTHX); \
1155 } STMT_END
1156
1157#define POP_MULTICALL \
1158 STMT_START { \
79503099 1159 PERL_CONTEXT *cx; \
4ebe6e95 1160 cx = CX_CUR(); \
dfe0f39b 1161 CX_LEAVE_SCOPE(cx); \
a73d8813 1162 cx_popsub_common(cx); \
4df352a8 1163 gimme = cx->blk_gimme; \
7e27c4a5 1164 PERL_UNUSED_VAR(gimme); /* for API */ \
ed8ff0f3 1165 cx_popblock(cx); \
5da525e9 1166 CX_POP(cx); \
1bbbfc50 1167 POPSTACK; \
9850bf21 1168 CATCH_SET(multicall_oldcatch); \
1bbbfc50 1169 SPAGAIN; \
9850bf21 1170 } STMT_END
b3ca2e83 1171
81ed78b2
DM
1172/* Change the CV of an already-pushed MULTICALL CxSUB block.
1173 * (this isn't part of the public API) */
1174
b0065247 1175#define CHANGE_MULTICALL_FLAGS(the_cv, flags) \
81ed78b2
DM
1176 STMT_START { \
1177 CV * const _nOnclAshIngNamE_ = the_cv; \
1178 CV * const cv = _nOnclAshIngNamE_; \
b70d5558 1179 PADLIST * const padlist = CvPADLIST(cv); \
79503099 1180 PERL_CONTEXT *cx = CX_CUR(); \
79646418 1181 assert(CxMULTICALL(cx)); \
a73d8813 1182 cx_popsub_common(cx); \
b0065247 1183 cx->cx_type = (CXt_SUB|CXp_MULTICALL|flags); \
79503099 1184 cx_pushsub(cx, cv, NULL, 0); \
b0065247
DM
1185 if (!(flags & CXp_SUB_RE_FAKE)) \
1186 CvDEPTH(cv)++; \
d2af2719 1187 if (CvDEPTH(cv) >= 2) \
81ed78b2 1188 Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \
81ed78b2 1189 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \
81ed78b2
DM
1190 multicall_cop = CvSTART(cv); \
1191 } STMT_END
b3ca2e83 1192/*
14d04a33 1193 * ex: set ts=8 sts=4 sw=4 et:
b3ca2e83 1194 */