3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
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.
9 * Control ops (cops) are one of the two ops OP_NEXTSTATE and OP_DBSTATE,
10 * that (loosely speaking) are statement separators.
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,
13 * and thus can be used to determine our current state.
16 /* A jmpenv packages the state required to perform a proper non-local jump.
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
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
23 * null to ensure this).
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.
33 struct jmpenv * je_prev;
34 Sigjmp_buf je_buf; /* uninit if je_prev is NULL */
35 int je_ret; /* last exception thrown */
36 bool je_mustcatch; /* need to call longjmp()? */
37 U16 je_old_delaymagic; /* saved PL_delaymagic */
38 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
39 SSize_t je_old_stack_hwm;
43 typedef struct jmpenv JMPENV;
45 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
46 # define JE_OLD_STACK_HWM_zero PL_start_env.je_old_stack_hwm = 0
47 # define JE_OLD_STACK_HWM_save(je) \
48 (je).je_old_stack_hwm = PL_curstackinfo->si_stack_hwm
49 # define JE_OLD_STACK_HWM_restore(je) \
50 if (PL_curstackinfo->si_stack_hwm < (je).je_old_stack_hwm) \
51 PL_curstackinfo->si_stack_hwm = (je).je_old_stack_hwm
53 # define JE_OLD_STACK_HWM_zero NOOP
54 # define JE_OLD_STACK_HWM_save(je) NOOP
55 # define JE_OLD_STACK_HWM_restore(je) NOOP
59 * How to build the first jmpenv.
61 * top_env needs to be non-zero. It points to an area
62 * in which longjmp() stuff is stored, as C callstack
63 * info there at least is thread specific this has to
64 * be per-thread. Otherwise a 'die' in a thread gives
65 * that thread the C stack of last thread to do an eval {}!
68 #define JMPENV_BOOTSTRAP \
70 PERL_POISON_EXPR(PoisonNew(&PL_start_env, 1, JMPENV));\
71 PL_top_env = &PL_start_env; \
72 PL_start_env.je_prev = NULL; \
73 PL_start_env.je_ret = -1; \
74 PL_start_env.je_mustcatch = TRUE; \
75 PL_start_env.je_old_delaymagic = 0; \
76 JE_OLD_STACK_HWM_zero; \
80 * PERL_FLEXIBLE_EXCEPTIONS
82 * All the flexible exceptions code has been removed.
83 * See the following threads for details:
85 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-07/msg00378.html
87 * Joshua's original patches (which weren't applied) and discussion:
89 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01396.html
90 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01489.html
91 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01491.html
92 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01608.html
93 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02144.html
94 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02998.html
96 * Chip's reworked patch and discussion:
98 * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-03/msg00520.html
100 * The flaw in these patches (which went unnoticed at the time) was
101 * that they moved some code that could potentially die() out of the
102 * region protected by the setjmp()s. This caused exceptions within
103 * END blocks and such to not be handled by the correct setjmp().
105 * The original patches that introduces flexible exceptions were:
107 * http://perl5.git.perl.org/perl.git/commit/312caa8e97f1c7ee342a9895c2f0e749625b4929
108 * http://perl5.git.perl.org/perl.git/commit/14dd3ad8c9bf82cf09798a22cc89a9862dfd6d1a
112 #define dJMPENV JMPENV cur_env
114 #define JMPENV_PUSH(v) \
117 int i = 0; JMPENV *p = PL_top_env; \
118 while (p) { i++; p = p->je_prev; } \
119 Perl_deb(aTHX_ "JUMPENV_PUSH level=%d at %s:%d\n", \
120 i, __FILE__, __LINE__);}) \
121 cur_env.je_prev = PL_top_env; \
122 JE_OLD_STACK_HWM_save(cur_env); \
123 cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK); \
124 JE_OLD_STACK_HWM_restore(cur_env); \
125 PL_top_env = &cur_env; \
126 cur_env.je_mustcatch = FALSE; \
127 cur_env.je_old_delaymagic = PL_delaymagic; \
128 (v) = cur_env.je_ret; \
134 int i = -1; JMPENV *p = PL_top_env; \
135 while (p) { i++; p = p->je_prev; } \
136 Perl_deb(aTHX_ "JUMPENV_POP level=%d at %s:%d\n", \
137 i, __FILE__, __LINE__);}) \
138 assert(PL_top_env == &cur_env); \
139 PL_delaymagic = cur_env.je_old_delaymagic; \
140 PL_top_env = cur_env.je_prev; \
143 #define JMPENV_JUMP(v) \
146 int i = -1; JMPENV *p = PL_top_env; \
147 while (p) { i++; p = p->je_prev; } \
148 Perl_deb(aTHX_ "JUMPENV_JUMP(%d) level=%d at %s:%d\n", \
149 (int)v, i, __FILE__, __LINE__);}) \
150 if (PL_top_env->je_prev) \
151 PerlProc_longjmp(PL_top_env->je_buf, (v)); \
153 PerlProc_exit(STATUS_EXIT); \
154 PerlIO_printf(PerlIO_stderr(), "panic: top_env, v=%d\n", (int)v); \
158 #define CATCH_GET (PL_top_env->je_mustcatch)
159 #define CATCH_SET(v) \
163 "JUMPLEVEL set catch %d => %d (for %p) at %s:%d\n", \
164 PL_top_env->je_mustcatch, v, (void*)PL_top_env, \
165 __FILE__, __LINE__);) \
166 PL_top_env->je_mustcatch = (v); \
170 =head1 COP Hint Hashes
173 typedef struct refcounted_he COPHH;
175 #define COPHH_KEY_UTF8 REFCOUNTED_HE_KEY_UTF8
178 =for apidoc Amx|SV *|cophh_fetch_pvn|const COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|U32 flags
180 Look up the entry in the cop hints hash C<cophh> with the key specified by
181 C<keypv> and C<keylen>. If C<flags> has the C<COPHH_KEY_UTF8> bit set,
182 the key octets are interpreted as UTF-8, otherwise they are interpreted
183 as Latin-1. C<hash> is a precomputed hash of the key string, or zero if
184 it has not been precomputed. Returns a mortal scalar copy of the value
185 associated with the key, or C<&PL_sv_placeholder> if there is no value
186 associated with the key.
191 #define cophh_fetch_pvn(cophh, keypv, keylen, hash, flags) \
192 Perl_refcounted_he_fetch_pvn(aTHX_ cophh, keypv, keylen, hash, flags)
195 =for apidoc Amx|SV *|cophh_fetch_pvs|const COPHH *cophh|const char *key|U32 flags
197 Like L</cophh_fetch_pvn>, but takes a C<NUL>-terminated literal string instead
198 of a string/length pair, and no precomputed hash.
203 #define cophh_fetch_pvs(cophh, key, flags) \
204 Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, flags)
207 =for apidoc Amx|SV *|cophh_fetch_pv|const COPHH *cophh|const char *key|U32 hash|U32 flags
209 Like L</cophh_fetch_pvn>, but takes a nul-terminated string instead of
210 a string/length pair.
215 #define cophh_fetch_pv(cophh, key, hash, flags) \
216 Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, flags)
219 =for apidoc Amx|SV *|cophh_fetch_sv|const COPHH *cophh|SV *key|U32 hash|U32 flags
221 Like L</cophh_fetch_pvn>, but takes a Perl scalar instead of a
227 #define cophh_fetch_sv(cophh, key, hash, flags) \
228 Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, flags)
231 =for apidoc Amx|HV *|cophh_2hv|const COPHH *cophh|U32 flags
233 Generates and returns a standard Perl hash representing the full set of
234 key/value pairs in the cop hints hash C<cophh>. C<flags> is currently
235 unused and must be zero.
240 #define cophh_2hv(cophh, flags) \
241 Perl_refcounted_he_chain_2hv(aTHX_ cophh, flags)
244 =for apidoc Amx|COPHH *|cophh_copy|COPHH *cophh
246 Make and return a complete copy of the cop hints hash C<cophh>.
251 #define cophh_copy(cophh) Perl_refcounted_he_inc(aTHX_ cophh)
254 =for apidoc Amx|void|cophh_free|COPHH *cophh
256 Discard the cop hints hash C<cophh>, freeing all resources associated
262 #define cophh_free(cophh) Perl_refcounted_he_free(aTHX_ cophh)
265 =for apidoc Amx|COPHH *|cophh_new_empty
267 Generate and return a fresh cop hints hash containing no entries.
272 #define cophh_new_empty() ((COPHH *)NULL)
275 =for apidoc Amx|COPHH *|cophh_store_pvn|COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|SV *value|U32 flags
277 Stores a value, associated with a key, in the cop hints hash C<cophh>,
278 and returns the modified hash. The returned hash pointer is in general
279 not the same as the hash pointer that was passed in. The input hash is
280 consumed by the function, and the pointer to it must not be subsequently
281 used. Use L</cophh_copy> if you need both hashes.
283 The key is specified by C<keypv> and C<keylen>. If C<flags> has the
284 C<COPHH_KEY_UTF8> bit set, the key octets are interpreted as UTF-8,
285 otherwise they are interpreted as Latin-1. C<hash> is a precomputed
286 hash of the key string, or zero if it has not been precomputed.
288 C<value> is the scalar value to store for this key. C<value> is copied
289 by this function, which thus does not take ownership of any reference
290 to it, and later changes to the scalar will not be reflected in the
291 value visible in the cop hints hash. Complex types of scalar will not
292 be stored with referential integrity, but will be coerced to strings.
297 #define cophh_store_pvn(cophh, keypv, keylen, hash, value, flags) \
298 Perl_refcounted_he_new_pvn(aTHX_ cophh, keypv, keylen, hash, value, flags)
301 =for apidoc Amx|COPHH *|cophh_store_pvs|const COPHH *cophh|const char *key|SV *value|U32 flags
303 Like L</cophh_store_pvn>, but takes a C<NUL>-terminated literal string instead
304 of a string/length pair, and no precomputed hash.
309 #define cophh_store_pvs(cophh, key, value, flags) \
310 Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, value, flags)
313 =for apidoc Amx|COPHH *|cophh_store_pv|const COPHH *cophh|const char *key|U32 hash|SV *value|U32 flags
315 Like L</cophh_store_pvn>, but takes a nul-terminated string instead of
316 a string/length pair.
321 #define cophh_store_pv(cophh, key, hash, value, flags) \
322 Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, value, flags)
325 =for apidoc Amx|COPHH *|cophh_store_sv|const COPHH *cophh|SV *key|U32 hash|SV *value|U32 flags
327 Like L</cophh_store_pvn>, but takes a Perl scalar instead of a
333 #define cophh_store_sv(cophh, key, hash, value, flags) \
334 Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, value, flags)
337 =for apidoc Amx|COPHH *|cophh_delete_pvn|COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|U32 flags
339 Delete a key and its associated value from the cop hints hash C<cophh>,
340 and returns the modified hash. The returned hash pointer is in general
341 not the same as the hash pointer that was passed in. The input hash is
342 consumed by the function, and the pointer to it must not be subsequently
343 used. Use L</cophh_copy> if you need both hashes.
345 The key is specified by C<keypv> and C<keylen>. If C<flags> has the
346 C<COPHH_KEY_UTF8> bit set, the key octets are interpreted as UTF-8,
347 otherwise they are interpreted as Latin-1. C<hash> is a precomputed
348 hash of the key string, or zero if it has not been precomputed.
353 #define cophh_delete_pvn(cophh, keypv, keylen, hash, flags) \
354 Perl_refcounted_he_new_pvn(aTHX_ cophh, keypv, keylen, hash, \
358 =for apidoc Amx|COPHH *|cophh_delete_pvs|const COPHH *cophh|const char *key|U32 flags
360 Like L</cophh_delete_pvn>, but takes a C<NUL>-terminated literal string instead
361 of a string/length pair, and no precomputed hash.
366 #define cophh_delete_pvs(cophh, key, flags) \
367 Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \
371 =for apidoc Amx|COPHH *|cophh_delete_pv|const COPHH *cophh|const char *key|U32 hash|U32 flags
373 Like L</cophh_delete_pvn>, but takes a nul-terminated string instead of
374 a string/length pair.
379 #define cophh_delete_pv(cophh, key, hash, flags) \
380 Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, (SV *)NULL, flags)
383 =for apidoc Amx|COPHH *|cophh_delete_sv|const COPHH *cophh|SV *key|U32 hash|U32 flags
385 Like L</cophh_delete_pvn>, but takes a Perl scalar instead of a
391 #define cophh_delete_sv(cophh, key, hash, flags) \
392 Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, (SV *)NULL, flags)
394 #include "mydtrace.h"
398 /* On LP64 putting this here takes advantage of the fact that BASEOP isn't
399 an exact multiple of 8 bytes to save structure padding. */
400 line_t cop_line; /* line # of this command */
401 /* label for this construct is now stored in cop_hints_hash */
403 PADOFFSET cop_stashoff; /* offset into PL_stashpad, for the
404 package the line was compiled in */
405 char * cop_file; /* file name the following line # is from */
407 HV * cop_stash; /* package line was compiled in */
408 GV * cop_filegv; /* file the following line # is from */
410 U32 cop_hints; /* hints bits from pragmata */
411 U32 cop_seq; /* parse sequence number */
412 /* Beware. mg.c and warnings.pl assume the type of this is STRLEN *: */
413 STRLEN * cop_warnings; /* lexical warnings bitmask */
414 /* compile time state of %^H. See the comment in op.c for how this is
415 used to recreate a hash to return from caller. */
416 COPHH * cop_hints_hash;
420 # define CopFILE(c) ((c)->cop_file)
421 # define CopFILEGV(c) (CopFILE(c) \
422 ? gv_fetchfile(CopFILE(c)) : NULL)
425 # define CopFILE_set(c,pv) ((c)->cop_file = savepv(pv))
426 # define CopFILE_setn(c,pv,l) ((c)->cop_file = savepvn((pv),(l)))
428 # define CopFILE_set(c,pv) ((c)->cop_file = savesharedpv(pv))
429 # define CopFILE_setn(c,pv,l) ((c)->cop_file = savesharedpvn((pv),(l)))
432 # define CopFILESV(c) (CopFILE(c) \
433 ? GvSV(gv_fetchfile(CopFILE(c))) : NULL)
434 # define CopFILEAV(c) (CopFILE(c) \
435 ? GvAV(gv_fetchfile(CopFILE(c))) : NULL)
436 # define CopFILEAVx(c) (assert_(CopFILE(c)) \
437 GvAV(gv_fetchfile(CopFILE(c))))
439 # define CopSTASH(c) PL_stashpad[(c)->cop_stashoff]
440 # define CopSTASH_set(c,hv) ((c)->cop_stashoff = (hv) \
441 ? alloccopstash(hv) \
444 # define CopFILE_free(c) SAVECOPFILE_FREE(c)
446 # define CopFILE_free(c) (PerlMemShared_free(CopFILE(c)),(CopFILE(c) = NULL))
449 # define CopFILEGV(c) ((c)->cop_filegv)
450 # define CopFILEGV_set(c,gv) ((c)->cop_filegv = (GV*)SvREFCNT_inc(gv))
451 # define CopFILE_set(c,pv) CopFILEGV_set((c), gv_fetchfile(pv))
452 # define CopFILE_setn(c,pv,l) CopFILEGV_set((c), gv_fetchfile_flags((pv),(l),0))
453 # define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : NULL)
454 # define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : NULL)
456 # define CopFILEAVx(c) (assert(CopFILEGV(c)), GvAV(CopFILEGV(c)))
458 # define CopFILEAVx(c) (GvAV(CopFILEGV(c)))
460 # define CopFILE(c) (CopFILEGV(c) \
461 ? GvNAME(CopFILEGV(c))+2 : NULL)
462 # define CopSTASH(c) ((c)->cop_stash)
463 # define CopSTASH_set(c,hv) ((c)->cop_stash = (hv))
464 # define CopFILE_free(c) (SvREFCNT_dec(CopFILEGV(c)),(CopFILEGV(c) = NULL))
466 #endif /* USE_ITHREADS */
468 #define CopSTASHPV(c) (CopSTASH(c) ? HvNAME_get(CopSTASH(c)) : NULL)
469 /* cop_stash is not refcounted */
470 #define CopSTASHPV_set(c,pv) CopSTASH_set((c), gv_stashpv(pv,GV_ADD))
471 #define CopSTASH_eq(c,hv) (CopSTASH(c) == (hv))
473 #define CopHINTHASH_get(c) ((COPHH*)((c)->cop_hints_hash))
474 #define CopHINTHASH_set(c,h) ((c)->cop_hints_hash = (h))
477 =head1 COP Hint Reading
481 =for apidoc Am|SV *|cop_hints_fetch_pvn|const COP *cop|const char *keypv|STRLEN keylen|U32 hash|U32 flags
483 Look up the hint entry in the cop C<cop> with the key specified by
484 C<keypv> and C<keylen>. If C<flags> has the C<COPHH_KEY_UTF8> bit set,
485 the key octets are interpreted as UTF-8, otherwise they are interpreted
486 as Latin-1. C<hash> is a precomputed hash of the key string, or zero if
487 it has not been precomputed. Returns a mortal scalar copy of the value
488 associated with the key, or C<&PL_sv_placeholder> if there is no value
489 associated with the key.
494 #define cop_hints_fetch_pvn(cop, keypv, keylen, hash, flags) \
495 cophh_fetch_pvn(CopHINTHASH_get(cop), keypv, keylen, hash, flags)
498 =for apidoc Am|SV *|cop_hints_fetch_pvs|const COP *cop|const char *key|U32 flags
500 Like L</cop_hints_fetch_pvn>, but takes a C<NUL>-terminated literal string
501 instead of a string/length pair, and no precomputed hash.
506 #define cop_hints_fetch_pvs(cop, key, flags) \
507 cophh_fetch_pvs(CopHINTHASH_get(cop), key, flags)
510 =for apidoc Am|SV *|cop_hints_fetch_pv|const COP *cop|const char *key|U32 hash|U32 flags
512 Like L</cop_hints_fetch_pvn>, but takes a nul-terminated string instead
513 of a string/length pair.
518 #define cop_hints_fetch_pv(cop, key, hash, flags) \
519 cophh_fetch_pv(CopHINTHASH_get(cop), key, hash, flags)
522 =for apidoc Am|SV *|cop_hints_fetch_sv|const COP *cop|SV *key|U32 hash|U32 flags
524 Like L</cop_hints_fetch_pvn>, but takes a Perl scalar instead of a
530 #define cop_hints_fetch_sv(cop, key, hash, flags) \
531 cophh_fetch_sv(CopHINTHASH_get(cop), key, hash, flags)
534 =for apidoc Am|HV *|cop_hints_2hv|const COP *cop|U32 flags
536 Generates and returns a standard Perl hash representing the full set of
537 hint entries in the cop C<cop>. C<flags> is currently unused and must
543 #define cop_hints_2hv(cop, flags) \
544 cophh_2hv(CopHINTHASH_get(cop), flags)
546 #define CopLABEL(c) Perl_cop_fetch_label(aTHX_ (c), NULL, NULL)
547 #define CopLABEL_len(c,len) Perl_cop_fetch_label(aTHX_ (c), len, NULL)
548 #define CopLABEL_len_flags(c,len,flags) Perl_cop_fetch_label(aTHX_ (c), len, flags)
549 #define CopLABEL_alloc(pv) ((pv)?savepv(pv):NULL)
551 #define CopSTASH_ne(c,hv) (!CopSTASH_eq(c,hv))
552 #define CopLINE(c) ((c)->cop_line)
553 #define CopLINE_inc(c) (++CopLINE(c))
554 #define CopLINE_dec(c) (--CopLINE(c))
555 #define CopLINE_set(c,l) (CopLINE(c) = (l))
557 /* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */
558 #define OutCopFILE(c) CopFILE(c)
560 #define CopHINTS_get(c) ((c)->cop_hints + 0)
561 #define CopHINTS_set(c, h) STMT_START { \
562 (c)->cop_hints = (h); \
566 * Here we have some enormously heavy (or at least ponderous) wizardry.
569 /* subroutine context */
571 OP * retop; /* op to execute on exit from sub */
572 /* Above here is the same for sub, format and eval. */
573 PAD *prevcomppad; /* the caller's PL_comppad */
575 /* Above here is the same for sub and format. */
582 struct block_format {
583 OP * retop; /* op to execute on exit from sub */
584 /* Above here is the same for sub, format and eval. */
585 PAD *prevcomppad; /* the caller's PL_comppad */
587 /* Above here is the same for sub and format. */
592 /* return a pointer to the current context */
594 #define CX_CUR() (&cxstack[cxstack_ix])
596 /* free all savestack items back to the watermark of the specified context */
598 #define CX_LEAVE_SCOPE(cx) LEAVE_SCOPE(cx->blk_oldsaveix)
601 /* on debugging builds, poison cx afterwards so we know no code
602 * uses it - because after doing cxstack_ix--, any ties, exceptions etc
603 * may overwrite the current stack frame */
604 # define CX_POP(cx) \
605 assert(CX_CUR() == cx); \
609 # define CX_POP(cx) cxstack_ix--;
613 /* base for the next two macros. Don't use directly.
614 * The context frame holds a reference to the CV so that it can't be
615 * freed while we're executing it */
618 #define CX_PUSHSUB_GET_LVALUE_MASK(func) \
619 /* If the context is indeterminate, then only the lvalue */ \
620 /* flags that the caller also has are applicable. */ \
622 (PL_op->op_flags & OPf_WANT) \
623 ? OPpENTERSUB_LVAL_MASK \
624 : !(PL_op->op_private & OPpENTERSUB_LVAL_MASK) \
625 ? 0 : (U8)func(aTHX) \
629 #define CX_POP_SAVEARRAY(cx) \
631 AV *cx_pop_savearray_av = GvAV(PL_defgv); \
632 GvAV(PL_defgv) = cx->blk_sub.savearray; \
633 cx->blk_sub.savearray = NULL; \
634 SvREFCNT_dec(cx_pop_savearray_av); \
637 /* junk in @_ spells trouble when cloning CVs and in pp_caller(), so don't
638 * leave any (a fast av_clear(ary), basically) */
639 #define CLEAR_ARGARRAY(ary) \
641 AvMAX(ary) += AvARRAY(ary) - AvALLOC(ary); \
642 AvARRAY(ary) = AvALLOC(ary); \
649 OP * retop; /* op to execute on exit from eval */
650 /* Above here is the same for sub, format and eval. */
655 JMPENV * cur_top_env; /* value of PL_top_env when eval CX created */
658 /* If we ever need more than 512 op types, change the shift from 7.
659 blku_gimme is actually also only 2 bits, so could be merged with something.
662 /* blk_u16 bit usage for eval contexts: */
664 #define CxOLD_IN_EVAL(cx) (((cx)->blk_u16) & 0x3F) /* saved PL in_eval */
665 #define CxEVAL_TXT_REFCNTED(cx) (((cx)->blk_u16) & 0x40) /* cur_text rc++ */
666 #define CxOLD_OP_TYPE(cx) (((cx)->blk_u16) >> 7) /* type of eval op */
670 LOOP * my_op; /* My op, that contains redo, next and last ops. */
671 union { /* different ways of locating the iteration variable */
672 SV **svp; /* for lexicals: address of pad slot */
673 GV *gv; /* for package vars */
675 SV *itersave; /* the original iteration var */
677 struct { /* CXt_LOOP_ARY, C<for (@ary)> */
678 AV *ary; /* array being iterated over */
679 IV ix; /* index relative to base of array */
681 struct { /* CXt_LOOP_LIST, C<for (list)> */
682 I32 basesp; /* first element of list on stack */
683 IV ix; /* index relative to basesp */
685 struct { /* CXt_LOOP_LAZYIV, C<for (1..9)> */
689 struct { /* CXt_LOOP_LAZYSV C<for ('a'..'z')> */
691 SV * end; /* maxiumum value (or minimum in reverse) */
695 PAD *oldcomppad; /* needed to map itervar_u.svp during thread clone */
699 #define CxITERVAR(c) \
701 ? (c)->blk_loop.itervar_u.svp \
702 : ((c)->cx_type & CXp_FOR_GV) \
703 ? &GvSV((c)->blk_loop.itervar_u.gv) \
704 : (SV **)&(c)->blk_loop.itervar_u.gv)
706 #define CxLABEL(c) (0 + CopLABEL((c)->blk_oldcop))
707 #define CxLABEL_len(c,len) (0 + CopLABEL_len((c)->blk_oldcop, len))
708 #define CxLABEL_len_flags(c,len,flags) (0 + CopLABEL_len_flags((c)->blk_oldcop, len, flags))
709 #define CxHASARGS(c) (((c)->cx_type & CXp_HASARGS) == CXp_HASARGS)
711 /* CxLVAL(): the lval flags of the call site: the relevant flag bits from
712 * the op_private field of the calling pp_entersub (or its caller's caller
713 * if the caller's lvalue context isn't known):
714 * OPpLVAL_INTRO: sub used in lvalue context, e.g. f() = 1;
715 * OPpENTERSUB_INARGS (in conjunction with OPpLVAL_INTRO): the
716 * function is being used as a sub arg or as a referent, e.g.
717 * g(...,f(),...) or $r = \f()
718 * OPpDEREF: 2-bit mask indicating e.g. f()->[0].
719 * Note the contrast with CvLVALUE(), which is a property of the sub
720 * rather than the call site.
722 #define CxLVAL(c) (0 + ((c)->blk_u16 & 0xff))
726 /* given/when context */
727 struct block_givwhen {
729 SV *defsv_save; /* the original $_ */
734 /* context common to subroutines, evals and loops */
736 U8 blku_type; /* what kind of context this is */
737 U8 blku_gimme; /* is this block running in list context? */
738 U16 blku_u16; /* used by block_sub and block_eval (so far) */
739 I32 blku_oldsaveix; /* saved PL_savestack_ix */
740 /* all the fields above must be aligned with same-sized fields as sbu */
741 I32 blku_oldsp; /* current sp floor: where nextstate pops to */
742 I32 blku_oldmarksp; /* mark stack index */
743 COP * blku_oldcop; /* old curcop pointer */
744 PMOP * blku_oldpm; /* values of pattern match vars */
745 SSize_t blku_old_tmpsfloor; /* saved PL_tmps_floor */
746 I32 blku_oldscopesp; /* scope stack index */
749 struct block_sub blku_sub;
750 struct block_format blku_format;
751 struct block_eval blku_eval;
752 struct block_loop blku_loop;
753 struct block_givwhen blku_givwhen;
756 #define blk_oldsp cx_u.cx_blk.blku_oldsp
757 #define blk_oldcop cx_u.cx_blk.blku_oldcop
758 #define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
759 #define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
760 #define blk_oldpm cx_u.cx_blk.blku_oldpm
761 #define blk_gimme cx_u.cx_blk.blku_gimme
762 #define blk_u16 cx_u.cx_blk.blku_u16
763 #define blk_oldsaveix cx_u.cx_blk.blku_oldsaveix
764 #define blk_old_tmpsfloor cx_u.cx_blk.blku_old_tmpsfloor
765 #define blk_sub cx_u.cx_blk.blk_u.blku_sub
766 #define blk_format cx_u.cx_blk.blk_u.blku_format
767 #define blk_eval cx_u.cx_blk.blk_u.blku_eval
768 #define blk_loop cx_u.cx_blk.blk_u.blku_loop
769 #define blk_givwhen cx_u.cx_blk.blk_u.blku_givwhen
771 #define CX_DEBUG(cx, action) \
773 Perl_deb(aTHX_ "CX %ld %s %s (scope %ld,%ld) (save %ld,%ld) at %s:%d\n",\
776 PL_block_type[CxTYPE(cx)], \
777 (long)PL_scopestack_ix, \
778 (long)(cx->blk_oldscopesp), \
779 (long)PL_savestack_ix, \
780 (long)(cx->blk_oldsaveix), \
781 __FILE__, __LINE__));
785 /* substitution context */
787 U8 sbu_type; /* same as blku_type */
790 I32 sbu_oldsaveix; /* same as blku_oldsaveix */
791 /* all the fields above must be aligned with same-sized fields as blk_u */
793 SSize_t sbu_maxiters;
803 #define sb_iters cx_u.cx_subst.sbu_iters
804 #define sb_maxiters cx_u.cx_subst.sbu_maxiters
805 #define sb_rflags cx_u.cx_subst.sbu_rflags
806 #define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
807 #define sb_orig cx_u.cx_subst.sbu_orig
808 #define sb_dstr cx_u.cx_subst.sbu_dstr
809 #define sb_targ cx_u.cx_subst.sbu_targ
810 #define sb_s cx_u.cx_subst.sbu_s
811 #define sb_m cx_u.cx_subst.sbu_m
812 #define sb_strend cx_u.cx_subst.sbu_strend
813 #define sb_rxres cx_u.cx_subst.sbu_rxres
814 #define sb_rx cx_u.cx_subst.sbu_rx
817 # define CX_PUSHSUBST(cx) CXINC, cx = CX_CUR(), \
818 cx->blk_oldsaveix = oldsave, \
819 cx->sb_iters = iters, \
820 cx->sb_maxiters = maxiters, \
821 cx->sb_rflags = r_flags, \
822 cx->sb_rxtainted = rxtainted, \
823 cx->sb_orig = orig, \
824 cx->sb_dstr = dstr, \
825 cx->sb_targ = targ, \
828 cx->sb_strend = strend, \
829 cx->sb_rxres = NULL, \
831 cx->cx_type = CXt_SUBST | (once ? CXp_ONCE : 0); \
832 rxres_save(&cx->sb_rxres, rx); \
833 (void)ReREFCNT_inc(rx); \
834 SvREFCNT_inc_void_NN(targ)
836 # define CX_POPSUBST(cx) \
839 assert(CxTYPE(cx) == CXt_SUBST); \
840 rxres_free(&cx->sb_rxres); \
844 SvREFCNT_dec_NN(cx->sb_targ); \
848 #define CxONCE(cx) ((cx)->cx_type & CXp_ONCE)
853 struct subst cx_subst;
856 #define cx_type cx_u.cx_subst.sbu_type
858 /* If you re-order these, there is also an array of uppercase names in perl.h
859 and a static array of context names in pp_ctl.c */
860 #define CXTYPEMASK 0xf
861 #define CXt_NULL 0 /* currently only used for sort BLOCK */
864 /* When micro-optimising :-) keep GIVEN next to the LOOPs, as these 5 share a
865 jump table in pp_ctl.c
866 The first 4 don't have a 'case' in at least one switch statement in pp_ctl.c
870 /* be careful of the ordering of these five. Macros like CxTYPE_is_LOOP,
871 * CxFOREACH compare ranges */
872 #define CXt_LOOP_ARY 4 /* for (@ary) { ...; } */
873 #define CXt_LOOP_LAZYSV 5 /* for ('a'..'z') { ...; } */
874 #define CXt_LOOP_LAZYIV 6 /* for (1..9) { ...; } */
875 #define CXt_LOOP_LIST 7 /* for (1,2,3) { ...; } */
876 #define CXt_LOOP_PLAIN 8 /* while (...) { ...; }
877 or plain block { ...; } */
879 #define CXt_FORMAT 10
882 /* SUBST doesn't feature in all switch statements. */
884 /* private flags for CXt_SUB and CXt_FORMAT */
885 #define CXp_MULTICALL 0x10 /* part of a multicall (so don't tear down
886 context on exit). (not CXt_FORMAT) */
887 #define CXp_HASARGS 0x20
888 #define CXp_SUB_RE 0x40 /* code called within regex, i.e. (?{}) */
889 #define CXp_SUB_RE_FAKE 0x80 /* fake sub CX for (?{}) in current scope */
891 /* private flags for CXt_EVAL */
892 #define CXp_REAL 0x20 /* truly eval'', not a lookalike */
893 #define CXp_TRYBLOCK 0x40 /* eval{}, not eval'' or similar */
895 /* private flags for CXt_LOOP */
897 /* this is only set in conjunction with CXp_FOR_GV */
898 #define CXp_FOR_DEF 0x10 /* foreach using $_ */
899 /* these 3 are mutually exclusive */
900 #define CXp_FOR_LVREF 0x20 /* foreach using \$var */
901 #define CXp_FOR_GV 0x40 /* foreach using package var */
902 #define CXp_FOR_PAD 0x80 /* foreach using lexical var */
904 #define CxPADLOOP(c) ((c)->cx_type & CXp_FOR_PAD)
906 /* private flags for CXt_SUBST */
907 #define CXp_ONCE 0x10 /* What was sbu_once in struct subst */
909 #define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
910 #define CxTYPE_is_LOOP(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \
911 && CxTYPE(cx) <= CXt_LOOP_PLAIN)
912 #define CxMULTICALL(c) ((c)->cx_type & CXp_MULTICALL)
913 #define CxREALEVAL(c) (((c)->cx_type & (CXTYPEMASK|CXp_REAL)) \
914 == (CXt_EVAL|CXp_REAL))
915 #define CxTRYBLOCK(c) (((c)->cx_type & (CXTYPEMASK|CXp_TRYBLOCK)) \
916 == (CXt_EVAL|CXp_TRYBLOCK))
917 #define CxFOREACH(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \
918 && CxTYPE(cx) <= CXt_LOOP_LIST)
920 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
923 =head1 "Gimme" Values
927 =for apidoc AmU||G_SCALAR
928 Used to indicate scalar context. See C<L</GIMME_V>>, C<L</GIMME>>, and
931 =for apidoc AmU||G_ARRAY
932 Used to indicate list context. See C<L</GIMME_V>>, C<L</GIMME>> and
935 =for apidoc AmU||G_VOID
936 Used to indicate void context. See C<L</GIMME_V>> and L<perlcall>.
938 =for apidoc AmU||G_DISCARD
939 Indicates that arguments returned from a callback should be discarded. See
942 =for apidoc AmU||G_EVAL
944 Used to force a Perl C<eval> wrapper around a callback. See
947 =for apidoc AmU||G_NOARGS
949 Indicates that no arguments are being sent to a callback. See
960 /* extra flags for Perl_call_* routines */
961 #define G_DISCARD 4 /* Call FREETMPS.
962 Don't change this without consulting the
963 hash actions codes defined in hv.h */
964 #define G_EVAL 8 /* Assume eval {} around subroutine call. */
965 #define G_NOARGS 16 /* Don't construct a @_ array. */
966 #define G_KEEPERR 32 /* Warn for errors, don't overwrite $@ */
967 #define G_NODEBUG 64 /* Disable debugging at toplevel. */
968 #define G_METHOD 128 /* Calling method. */
969 #define G_FAKINGEVAL 256 /* Faking an eval context for call_sv or
971 #define G_UNDEF_FILL 512 /* Fill the stack with &PL_sv_undef
972 A special case for UNSHIFT in
973 Perl_magic_methcall(). */
974 #define G_WRITING_TO_STDERR 1024 /* Perl_write_to_stderr() is calling
975 Perl_magic_methcall(). */
976 #define G_RE_REPARSING 0x800 /* compiling a run-time /(?{..})/ */
977 #define G_METHOD_NAMED 4096 /* calling named method, eg without :: or ' */
979 /* flag bits for PL_in_eval */
980 #define EVAL_NULL 0 /* not in an eval */
981 #define EVAL_INEVAL 1 /* some enclosing scope is an eval */
982 #define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */
983 #define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */
984 #define EVAL_INREQUIRE 8 /* The code is being required. */
985 #define EVAL_RE_REPARSING 0x10 /* eval_sv() called with G_RE_REPARSING */
986 /* if adding extra bits, make sure they can fit in CxOLD_OP_TYPE() */
988 /* Support for switching (stack and block) contexts.
989 * This ensures magic doesn't invalidate local stack and cx pointers.
992 #define PERLSI_UNKNOWN -1
993 #define PERLSI_UNDEF 0
994 #define PERLSI_MAIN 1
995 #define PERLSI_MAGIC 2
996 #define PERLSI_SORT 3
997 #define PERLSI_SIGNAL 4
998 #define PERLSI_OVERLOAD 5
999 #define PERLSI_DESTROY 6
1000 #define PERLSI_WARNHOOK 7
1001 #define PERLSI_DIEHOOK 8
1002 #define PERLSI_REQUIRE 9
1003 #define PERLSI_MULTICALL 10
1006 AV * si_stack; /* stack for current runlevel */
1007 PERL_CONTEXT * si_cxstack; /* context stack for runlevel */
1008 struct stackinfo * si_prev;
1009 struct stackinfo * si_next;
1010 I32 si_cxix; /* current context index */
1011 I32 si_cxmax; /* maximum allocated index */
1012 I32 si_type; /* type of runlevel */
1013 I32 si_markoff; /* offset where markstack begins for us.
1014 * currently used only with DEBUGGING,
1015 * but not #ifdef-ed for bincompat */
1016 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
1017 /* high water mark: for checking if the stack was correctly extended /
1018 * tested for extension by each pp function */
1019 SSize_t si_stack_hwm;
1024 typedef struct stackinfo PERL_SI;
1026 #define cxstack (PL_curstackinfo->si_cxstack)
1027 #define cxstack_ix (PL_curstackinfo->si_cxix)
1028 #define cxstack_max (PL_curstackinfo->si_cxmax)
1031 # define SET_MARK_OFFSET \
1032 PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack
1034 # define SET_MARK_OFFSET NOOP
1037 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
1038 # define PUSHSTACK_INIT_HWM(si) ((si)->si_stack_hwm = 0)
1040 # define PUSHSTACK_INIT_HWM(si) NOOP
1043 #define PUSHSTACKi(type) \
1045 PERL_SI *next = PL_curstackinfo->si_next; \
1047 int i = 0; PERL_SI *p = PL_curstackinfo; \
1048 while (p) { i++; p = p->si_prev; } \
1049 Perl_deb(aTHX_ "push STACKINFO %d at %s:%d\n", \
1050 i, __FILE__, __LINE__);}) \
1052 next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \
1053 next->si_prev = PL_curstackinfo; \
1054 PL_curstackinfo->si_next = next; \
1056 next->si_type = type; \
1057 next->si_cxix = -1; \
1058 PUSHSTACK_INIT_HWM(next); \
1059 AvFILLp(next->si_stack) = 0; \
1060 SWITCHSTACK(PL_curstack,next->si_stack); \
1061 PL_curstackinfo = next; \
1065 #define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
1067 /* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
1068 * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
1072 PERL_SI * const prev = PL_curstackinfo->si_prev; \
1074 int i = -1; PERL_SI *p = PL_curstackinfo; \
1075 while (p) { i++; p = p->si_prev; } \
1076 Perl_deb(aTHX_ "pop STACKINFO %d at %s:%d\n", \
1077 i, __FILE__, __LINE__);}) \
1079 Perl_croak_popstack(); \
1081 SWITCHSTACK(PL_curstack,prev->si_stack); \
1082 /* don't free prev here, free them all at the END{} */ \
1083 PL_curstackinfo = prev; \
1086 #define POPSTACK_TO(s) \
1088 while (PL_curstack != s) { \
1094 #define IN_PERL_COMPILETIME cBOOL(PL_curcop == &PL_compiling)
1095 #define IN_PERL_RUNTIME cBOOL(PL_curcop != &PL_compiling)
1101 =head1 Multicall Functions
1103 =for apidoc Ams||dMULTICALL
1104 Declare local variables for a multicall. See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1106 =for apidoc Ams||PUSH_MULTICALL
1107 Opening bracket for a lightweight callback.
1108 See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1110 =for apidoc Ams||MULTICALL
1111 Make a lightweight callback. See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1113 =for apidoc Ams||POP_MULTICALL
1114 Closing bracket for a lightweight callback.
1115 See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1120 #define dMULTICALL \
1121 OP *multicall_cop; \
1122 bool multicall_oldcatch
1124 #define PUSH_MULTICALL(the_cv) \
1125 PUSH_MULTICALL_FLAGS(the_cv, 0)
1127 /* Like PUSH_MULTICALL, but allows you to specify extra flags
1128 * for the CX stack entry (this isn't part of the public API) */
1130 #define PUSH_MULTICALL_FLAGS(the_cv, flags) \
1133 CV * const _nOnclAshIngNamE_ = the_cv; \
1134 CV * const cv = _nOnclAshIngNamE_; \
1135 PADLIST * const padlist = CvPADLIST(cv); \
1136 multicall_oldcatch = CATCH_GET; \
1138 PUSHSTACKi(PERLSI_MULTICALL); \
1139 cx = cx_pushblock((CXt_SUB|CXp_MULTICALL|flags), (U8)gimme, \
1140 PL_stack_sp, PL_savestack_ix); \
1141 cx_pushsub(cx, cv, NULL, 0); \
1143 if (!(flags & CXp_SUB_RE_FAKE)) \
1145 if (CvDEPTH(cv) >= 2) \
1146 Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \
1147 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \
1148 multicall_cop = CvSTART(cv); \
1153 PL_op = multicall_cop; \
1157 #define POP_MULTICALL \
1161 CX_LEAVE_SCOPE(cx); \
1162 cx_popsub_common(cx); \
1163 gimme = cx->blk_gimme; \
1164 PERL_UNUSED_VAR(gimme); /* for API */ \
1168 CATCH_SET(multicall_oldcatch); \
1172 /* Change the CV of an already-pushed MULTICALL CxSUB block.
1173 * (this isn't part of the public API) */
1175 #define CHANGE_MULTICALL_FLAGS(the_cv, flags) \
1177 CV * const _nOnclAshIngNamE_ = the_cv; \
1178 CV * const cv = _nOnclAshIngNamE_; \
1179 PADLIST * const padlist = CvPADLIST(cv); \
1180 PERL_CONTEXT *cx = CX_CUR(); \
1181 assert(CxMULTICALL(cx)); \
1182 cx_popsub_common(cx); \
1183 cx->cx_type = (CXt_SUB|CXp_MULTICALL|flags); \
1184 cx_pushsub(cx, cv, NULL, 0); \
1185 if (!(flags & CXp_SUB_RE_FAKE)) \
1187 if (CvDEPTH(cv) >= 2) \
1188 Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \
1189 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \
1190 multicall_cop = CvSTART(cv); \
1193 * ex: set ts=8 sts=4 sw=4 et: