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