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