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