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