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