This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
S_padhv_rv2hv_common(): unroll hv_scalar() calls
[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 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
39     SSize_t             je_old_stack_hwm;
40 #endif
41 };
42
43 typedef struct jmpenv JMPENV;
44
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
52 #else
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
56 #endif
57
58 /*
59  * How to build the first jmpenv.
60  *
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 {}!
66  */
67
68 #define JMPENV_BOOTSTRAP \
69     STMT_START {                                \
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;                  \
77     } STMT_END
78
79 /*
80  *   PERL_FLEXIBLE_EXCEPTIONS
81  * 
82  * All the flexible exceptions code has been removed.
83  * See the following threads for details:
84  *
85  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-07/msg00378.html
86  * 
87  * Joshua's original patches (which weren't applied) and discussion:
88  * 
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
95  * 
96  * Chip's reworked patch and discussion:
97  * 
98  *   http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-03/msg00520.html
99  * 
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().
104  * 
105  * The original patches that introduces flexible exceptions were:
106  *
107  * http://perl5.git.perl.org/perl.git/commit/312caa8e97f1c7ee342a9895c2f0e749625b4929
108  * http://perl5.git.perl.org/perl.git/commit/14dd3ad8c9bf82cf09798a22cc89a9862dfd6d1a                                        
109  *  
110  */
111
112 #define dJMPENV         JMPENV cur_env
113
114 #define JMPENV_PUSH(v) \
115     STMT_START {                                                        \
116         DEBUG_l({                                                       \
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;                                           \
129     } STMT_END
130
131 #define JMPENV_POP \
132     STMT_START {                                                        \
133         DEBUG_l({                                                       \
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;                                   \
141     } STMT_END
142
143 #define JMPENV_JUMP(v) \
144     STMT_START {                                                \
145         DEBUG_l({                                               \
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));          \
152         if ((v) == 2)                                           \
153             PerlProc_exit(STATUS_EXIT);                         \
154         PerlIO_printf(PerlIO_stderr(), "panic: top_env, v=%d\n", (int)v); \
155         PerlProc_exit(1);                                       \
156     } STMT_END
157
158 #define CATCH_GET               (PL_top_env->je_mustcatch)
159 #define CATCH_SET(v) \
160     STMT_START {                                                        \
161         DEBUG_l(                                                        \
162             Perl_deb(aTHX_                                              \
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);                                 \
167     } STMT_END
168
169 /*
170 =head1 COP Hint Hashes
171 */
172
173 typedef struct refcounted_he COPHH;
174
175 #define COPHH_KEY_UTF8 REFCOUNTED_HE_KEY_UTF8
176
177 /*
178 =for apidoc Amx|SV *|cophh_fetch_pvn|const COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|U32 flags
179
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.
187
188 =cut
189 */
190
191 #define cophh_fetch_pvn(cophh, keypv, keylen, hash, flags) \
192     Perl_refcounted_he_fetch_pvn(aTHX_ cophh, keypv, keylen, hash, flags)
193
194 /*
195 =for apidoc Amx|SV *|cophh_fetch_pvs|const COPHH *cophh|const char *key|U32 flags
196
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.
199
200 =cut
201 */
202
203 #define cophh_fetch_pvs(cophh, key, flags) \
204     Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, flags)
205
206 /*
207 =for apidoc Amx|SV *|cophh_fetch_pv|const COPHH *cophh|const char *key|U32 hash|U32 flags
208
209 Like L</cophh_fetch_pvn>, but takes a nul-terminated string instead of
210 a string/length pair.
211
212 =cut
213 */
214
215 #define cophh_fetch_pv(cophh, key, hash, flags) \
216     Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, flags)
217
218 /*
219 =for apidoc Amx|SV *|cophh_fetch_sv|const COPHH *cophh|SV *key|U32 hash|U32 flags
220
221 Like L</cophh_fetch_pvn>, but takes a Perl scalar instead of a
222 string/length pair.
223
224 =cut
225 */
226
227 #define cophh_fetch_sv(cophh, key, hash, flags) \
228     Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, flags)
229
230 /*
231 =for apidoc Amx|HV *|cophh_2hv|const COPHH *cophh|U32 flags
232
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.
236
237 =cut
238 */
239
240 #define cophh_2hv(cophh, flags) \
241     Perl_refcounted_he_chain_2hv(aTHX_ cophh, flags)
242
243 /*
244 =for apidoc Amx|COPHH *|cophh_copy|COPHH *cophh
245
246 Make and return a complete copy of the cop hints hash C<cophh>.
247
248 =cut
249 */
250
251 #define cophh_copy(cophh) Perl_refcounted_he_inc(aTHX_ cophh)
252
253 /*
254 =for apidoc Amx|void|cophh_free|COPHH *cophh
255
256 Discard the cop hints hash C<cophh>, freeing all resources associated
257 with it.
258
259 =cut
260 */
261
262 #define cophh_free(cophh) Perl_refcounted_he_free(aTHX_ cophh)
263
264 /*
265 =for apidoc Amx|COPHH *|cophh_new_empty
266
267 Generate and return a fresh cop hints hash containing no entries.
268
269 =cut
270 */
271
272 #define cophh_new_empty() ((COPHH *)NULL)
273
274 /*
275 =for apidoc Amx|COPHH *|cophh_store_pvn|COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|SV *value|U32 flags
276
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.
282
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.
287
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.
293
294 =cut
295 */
296
297 #define cophh_store_pvn(cophh, keypv, keylen, hash, value, flags) \
298     Perl_refcounted_he_new_pvn(aTHX_ cophh, keypv, keylen, hash, value, flags)
299
300 /*
301 =for apidoc Amx|COPHH *|cophh_store_pvs|const COPHH *cophh|const char *key|SV *value|U32 flags
302
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.
305
306 =cut
307 */
308
309 #define cophh_store_pvs(cophh, key, value, flags) \
310     Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, value, flags)
311
312 /*
313 =for apidoc Amx|COPHH *|cophh_store_pv|const COPHH *cophh|const char *key|U32 hash|SV *value|U32 flags
314
315 Like L</cophh_store_pvn>, but takes a nul-terminated string instead of
316 a string/length pair.
317
318 =cut
319 */
320
321 #define cophh_store_pv(cophh, key, hash, value, flags) \
322     Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, value, flags)
323
324 /*
325 =for apidoc Amx|COPHH *|cophh_store_sv|const COPHH *cophh|SV *key|U32 hash|SV *value|U32 flags
326
327 Like L</cophh_store_pvn>, but takes a Perl scalar instead of a
328 string/length pair.
329
330 =cut
331 */
332
333 #define cophh_store_sv(cophh, key, hash, value, flags) \
334     Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, value, flags)
335
336 /*
337 =for apidoc Amx|COPHH *|cophh_delete_pvn|COPHH *cophh|const char *keypv|STRLEN keylen|U32 hash|U32 flags
338
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.
344
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.
349
350 =cut
351 */
352
353 #define cophh_delete_pvn(cophh, keypv, keylen, hash, flags) \
354     Perl_refcounted_he_new_pvn(aTHX_ cophh, keypv, keylen, hash, \
355         (SV *)NULL, flags)
356
357 /*
358 =for apidoc Amx|COPHH *|cophh_delete_pvs|const COPHH *cophh|const char *key|U32 flags
359
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.
362
363 =cut
364 */
365
366 #define cophh_delete_pvs(cophh, key, flags) \
367     Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \
368         (SV *)NULL, flags)
369
370 /*
371 =for apidoc Amx|COPHH *|cophh_delete_pv|const COPHH *cophh|const char *key|U32 hash|U32 flags
372
373 Like L</cophh_delete_pvn>, but takes a nul-terminated string instead of
374 a string/length pair.
375
376 =cut
377 */
378
379 #define cophh_delete_pv(cophh, key, hash, flags) \
380     Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, (SV *)NULL, flags)
381
382 /*
383 =for apidoc Amx|COPHH *|cophh_delete_sv|const COPHH *cophh|SV *key|U32 hash|U32 flags
384
385 Like L</cophh_delete_pvn>, but takes a Perl scalar instead of a
386 string/length pair.
387
388 =cut
389 */
390
391 #define cophh_delete_sv(cophh, key, hash, flags) \
392     Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, (SV *)NULL, flags)
393
394 #include "mydtrace.h"
395
396 struct cop {
397     BASEOP
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 */
402 #ifdef USE_ITHREADS
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 */
406 #else
407     HV *        cop_stash;      /* package line was compiled in */
408     GV *        cop_filegv;     /* file the following line # is from */
409 #endif
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;
417 };
418
419 #ifdef USE_ITHREADS
420 #  define CopFILE(c)            ((c)->cop_file)
421 #  define CopFILEGV(c)          (CopFILE(c) \
422                                  ? gv_fetchfile(CopFILE(c)) : NULL)
423                                  
424 #  ifdef NETWARE
425 #    define CopFILE_set(c,pv)   ((c)->cop_file = savepv(pv))
426 #    define CopFILE_setn(c,pv,l)  ((c)->cop_file = savepvn((pv),(l)))
427 #  else
428 #    define CopFILE_set(c,pv)   ((c)->cop_file = savesharedpv(pv))
429 #    define CopFILE_setn(c,pv,l)  ((c)->cop_file = savesharedpvn((pv),(l)))
430 #  endif
431
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))))
438
439 #  define CopSTASH(c)           PL_stashpad[(c)->cop_stashoff]
440 #  define CopSTASH_set(c,hv)    ((c)->cop_stashoff = (hv)               \
441                                     ? alloccopstash(hv)                 \
442                                     : 0)
443 #  ifdef NETWARE
444 #    define CopFILE_free(c) SAVECOPFILE_FREE(c)
445 #  else
446 #    define CopFILE_free(c)     (PerlMemShared_free(CopFILE(c)),(CopFILE(c) = NULL))
447 #  endif
448 #else
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)
455 #  ifdef DEBUGGING
456 #    define CopFILEAVx(c)       (assert(CopFILEGV(c)), GvAV(CopFILEGV(c)))
457 #  else
458 #    define CopFILEAVx(c)       (GvAV(CopFILEGV(c)))
459 # endif
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))
465
466 #endif /* USE_ITHREADS */
467
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))
472
473 #define CopHINTHASH_get(c)      ((COPHH*)((c)->cop_hints_hash))
474 #define CopHINTHASH_set(c,h)    ((c)->cop_hints_hash = (h))
475
476 /*
477 =head1 COP Hint Reading
478 */
479
480 /*
481 =for apidoc Am|SV *|cop_hints_fetch_pvn|const COP *cop|const char *keypv|STRLEN keylen|U32 hash|U32 flags
482
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.
490
491 =cut
492 */
493
494 #define cop_hints_fetch_pvn(cop, keypv, keylen, hash, flags) \
495     cophh_fetch_pvn(CopHINTHASH_get(cop), keypv, keylen, hash, flags)
496
497 /*
498 =for apidoc Am|SV *|cop_hints_fetch_pvs|const COP *cop|const char *key|U32 flags
499
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.
502
503 =cut
504 */
505
506 #define cop_hints_fetch_pvs(cop, key, flags) \
507     cophh_fetch_pvs(CopHINTHASH_get(cop), key, flags)
508
509 /*
510 =for apidoc Am|SV *|cop_hints_fetch_pv|const COP *cop|const char *key|U32 hash|U32 flags
511
512 Like L</cop_hints_fetch_pvn>, but takes a nul-terminated string instead
513 of a string/length pair.
514
515 =cut
516 */
517
518 #define cop_hints_fetch_pv(cop, key, hash, flags) \
519     cophh_fetch_pv(CopHINTHASH_get(cop), key, hash, flags)
520
521 /*
522 =for apidoc Am|SV *|cop_hints_fetch_sv|const COP *cop|SV *key|U32 hash|U32 flags
523
524 Like L</cop_hints_fetch_pvn>, but takes a Perl scalar instead of a
525 string/length pair.
526
527 =cut
528 */
529
530 #define cop_hints_fetch_sv(cop, key, hash, flags) \
531     cophh_fetch_sv(CopHINTHASH_get(cop), key, hash, flags)
532
533 /*
534 =for apidoc Am|HV *|cop_hints_2hv|const COP *cop|U32 flags
535
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
538 be zero.
539
540 =cut
541 */
542
543 #define cop_hints_2hv(cop, flags) \
544     cophh_2hv(CopHINTHASH_get(cop), flags)
545
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)
550
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))
556
557 /* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */
558 #define OutCopFILE(c) CopFILE(c)
559
560 #define CopHINTS_get(c)         ((c)->cop_hints + 0)
561 #define CopHINTS_set(c, h)      STMT_START {                            \
562                                     (c)->cop_hints = (h);               \
563                                 } STMT_END
564
565 /*
566  * Here we have some enormously heavy (or at least ponderous) wizardry.
567  */
568
569 /* subroutine context */
570 struct block_sub {
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 */
574     CV *        cv;
575     /* Above here is the same for sub and format.  */
576     I32         olddepth;
577     AV          *savearray;
578 };
579
580
581 /* format context */
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 */
586     CV *        cv;
587     /* Above here is the same for sub and format.  */
588     GV *        gv;
589     GV *        dfoutgv;
590 };
591
592 /* return a pointer to the current context */
593
594 #define CX_CUR() (&cxstack[cxstack_ix])
595
596 /* free all savestack items back to the watermark of the specified context */
597
598 #define CX_LEAVE_SCOPE(cx) LEAVE_SCOPE(cx->blk_oldsaveix)
599
600 #ifdef DEBUGGING
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);                                        \
606         cxstack_ix--;                                                  \
607         cx = NULL;
608 #else
609 #  define CX_POP(cx) cxstack_ix--;
610 #endif
611
612
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 */
616
617
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.        */     \
621         (                                                               \
622            (PL_op->op_flags & OPf_WANT)                                 \
623                ? OPpENTERSUB_LVAL_MASK                                  \
624                : !(PL_op->op_private & OPpENTERSUB_LVAL_MASK)           \
625                    ? 0 : (U8)func(aTHX)                                 \
626         )
627
628 /* Restore old @_ */
629 #define CX_POP_SAVEARRAY(cx)                                            \
630     STMT_START {                                                        \
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);                              \
635     } STMT_END
636
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) \
640     STMT_START {                                                        \
641         AvMAX(ary) += AvARRAY(ary) - AvALLOC(ary);                      \
642         AvARRAY(ary) = AvALLOC(ary);                                    \
643         AvFILLp(ary) = -1;                                              \
644     } STMT_END
645
646
647 /* eval context */
648 struct block_eval {
649     OP *        retop;  /* op to execute on exit from eval */
650     /* Above here is the same for sub, format and eval.  */
651     SV *        old_namesv;
652     OP *        old_eval_root;
653     SV *        cur_text;
654     CV *        cv;
655     JMPENV *    cur_top_env; /* value of PL_top_env when eval CX created */
656 };
657
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.
660 */
661
662 /* blk_u16 bit usage for eval contexts: */
663
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 */
667
668 /* loop context */
669 struct block_loop {
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 */
674     } itervar_u;
675     SV          *itersave; /* the original iteration var */
676     union {
677         struct { /* CXt_LOOP_ARY, C<for (@ary)>  */
678             AV *ary; /* array being iterated over */
679             IV  ix;   /* index relative to base of array */
680         } ary;
681         struct { /* CXt_LOOP_LIST, C<for (list)> */
682             I32 basesp; /* first element of list on stack */
683             IV  ix;      /* index relative to basesp */
684         } stack;
685         struct { /* CXt_LOOP_LAZYIV, C<for (1..9)> */
686             IV cur;
687             IV end;
688         } lazyiv;
689         struct { /* CXt_LOOP_LAZYSV C<for ('a'..'z')> */
690             SV * cur;
691             SV * end; /* maxiumum value (or minimum in reverse) */
692         } lazysv;
693     } state_u;
694 #ifdef USE_ITHREADS
695     PAD *oldcomppad; /* needed to map itervar_u.svp during thread clone */
696 #endif
697 };
698
699 #define CxITERVAR(c)                                    \
700         (CxPADLOOP(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)
705
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)
710
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.
721  */
722 #define CxLVAL(c)       (0 + ((c)->blk_u16 & 0xff))
723
724
725
726 /* given/when context */
727 struct block_givwhen {
728         OP *leave_op;
729         SV *defsv_save; /* the original $_ */
730 };
731
732
733
734 /* context common to subroutines, evals and loops */
735 struct block {
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 */
747
748     union {
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;
754     } blk_u;
755 };
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
770
771 #define CX_DEBUG(cx, action)                                            \
772     DEBUG_l(                                                            \
773         Perl_deb(aTHX_ "CX %ld %s %s (scope %ld,%ld) (save %ld,%ld) at %s:%d\n",\
774                     (long)cxstack_ix,                                   \
775                     action,                                             \
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__));
782
783
784
785 /* substitution context */
786 struct subst {
787     U8          sbu_type;       /* same as blku_type */
788     U8          sbu_rflags;
789     U16         sbu_rxtainted;
790     I32         sbu_oldsaveix; /* same as blku_oldsaveix */
791     /* all the fields above must be aligned with same-sized fields as blk_u */
792     SSize_t     sbu_iters;
793     SSize_t     sbu_maxiters;
794     char *      sbu_orig;
795     SV *        sbu_dstr;
796     SV *        sbu_targ;
797     char *      sbu_s;
798     char *      sbu_m;
799     char *      sbu_strend;
800     void *      sbu_rxres;
801     REGEXP *    sbu_rx;
802 };
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
815
816 #ifdef PERL_CORE
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,                                 \
826         cx->sb_s                = s,                                    \
827         cx->sb_m                = m,                                    \
828         cx->sb_strend           = strend,                               \
829         cx->sb_rxres            = NULL,                                 \
830         cx->sb_rx               = rx,                                   \
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)
835
836 #  define CX_POPSUBST(cx) \
837     STMT_START {                                                        \
838         REGEXP *re;                                                     \
839         assert(CxTYPE(cx) == CXt_SUBST);                                \
840         rxres_free(&cx->sb_rxres);                                      \
841         re = cx->sb_rx;                                                 \
842         cx->sb_rx = NULL;                                               \
843         ReREFCNT_dec(re);                                               \
844         SvREFCNT_dec_NN(cx->sb_targ);                                   \
845     } STMT_END
846 #endif
847
848 #define CxONCE(cx)              ((cx)->cx_type & CXp_ONCE)
849
850 struct context {
851     union {
852         struct block    cx_blk;
853         struct subst    cx_subst;
854     } cx_u;
855 };
856 #define cx_type cx_u.cx_subst.sbu_type
857
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 */
862 #define CXt_WHEN        1
863 #define CXt_BLOCK       2
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
867 */
868 #define CXt_GIVEN       3
869
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 { ...; } */
878 #define CXt_SUB         9
879 #define CXt_FORMAT     10
880 #define CXt_EVAL       11
881 #define CXt_SUBST      12
882 /* SUBST doesn't feature in all switch statements.  */
883
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 */
890
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 */
894
895 /* private flags for CXt_LOOP */
896
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 */
903
904 #define CxPADLOOP(c)    ((c)->cx_type & CXp_FOR_PAD)
905
906 /* private flags for CXt_SUBST */
907 #define CXp_ONCE        0x10    /* What was sbu_once in struct subst */
908
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)
919
920 #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
921
922 /* 
923 =head1 "Gimme" Values
924 */
925
926 /*
927 =for apidoc AmU||G_SCALAR
928 Used to indicate scalar context.  See C<L</GIMME_V>>, C<L</GIMME>>, and
929 L<perlcall>.
930
931 =for apidoc AmU||G_ARRAY
932 Used to indicate list context.  See C<L</GIMME_V>>, C<L</GIMME>> and
933 L<perlcall>.
934
935 =for apidoc AmU||G_VOID
936 Used to indicate void context.  See C<L</GIMME_V>> and L<perlcall>.
937
938 =for apidoc AmU||G_DISCARD
939 Indicates that arguments returned from a callback should be discarded.  See
940 L<perlcall>.
941
942 =for apidoc AmU||G_EVAL
943
944 Used to force a Perl C<eval> wrapper around a callback.  See
945 L<perlcall>.
946
947 =for apidoc AmU||G_NOARGS
948
949 Indicates that no arguments are being sent to a callback.  See
950 L<perlcall>.
951
952 =cut
953 */
954
955 #define G_SCALAR        2
956 #define G_ARRAY         3
957 #define G_VOID          1
958 #define G_WANT          3
959
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
970                                    fold_constants. */
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 ' */
978
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() */
987
988 /* Support for switching (stack and block) contexts.
989  * This ensures magic doesn't invalidate local stack and cx pointers.
990  */
991
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
1004
1005 struct stackinfo {
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;
1020 #endif
1021
1022 };
1023
1024 typedef struct stackinfo PERL_SI;
1025
1026 #define cxstack         (PL_curstackinfo->si_cxstack)
1027 #define cxstack_ix      (PL_curstackinfo->si_cxix)
1028 #define cxstack_max     (PL_curstackinfo->si_cxmax)
1029
1030 #ifdef DEBUGGING
1031 #  define       SET_MARK_OFFSET \
1032     PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack
1033 #else
1034 #  define       SET_MARK_OFFSET NOOP
1035 #endif
1036
1037 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
1038 #  define PUSHSTACK_INIT_HWM(si) ((si)->si_stack_hwm = 0)
1039 #else
1040 #  define PUSHSTACK_INIT_HWM(si) NOOP
1041 #endif
1042
1043 #define PUSHSTACKi(type) \
1044     STMT_START {                                                        \
1045         PERL_SI *next = PL_curstackinfo->si_next;                       \
1046         DEBUG_l({                                                       \
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__);})                      \
1051         if (!next) {                                                    \
1052             next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1);    \
1053             next->si_prev = PL_curstackinfo;                            \
1054             PL_curstackinfo->si_next = next;                            \
1055         }                                                               \
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;                                         \
1062         SET_MARK_OFFSET;                                                \
1063     } STMT_END
1064
1065 #define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
1066
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 */
1069 #define POPSTACK \
1070     STMT_START {                                                        \
1071         dSP;                                                            \
1072         PERL_SI * const prev = PL_curstackinfo->si_prev;                \
1073         DEBUG_l({                                                       \
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__);})                      \
1078         if (!prev) {                                                    \
1079             Perl_croak_popstack();                                      \
1080         }                                                               \
1081         SWITCHSTACK(PL_curstack,prev->si_stack);                        \
1082         /* don't free prev here, free them all at the END{} */          \
1083         PL_curstackinfo = prev;                                         \
1084     } STMT_END
1085
1086 #define POPSTACK_TO(s) \
1087     STMT_START {                                                        \
1088         while (PL_curstack != s) {                                      \
1089             dounwind(-1);                                               \
1090             POPSTACK;                                                   \
1091         }                                                               \
1092     } STMT_END
1093
1094 #define IN_PERL_COMPILETIME     cBOOL(PL_curcop == &PL_compiling)
1095 #define IN_PERL_RUNTIME         cBOOL(PL_curcop != &PL_compiling)
1096
1097
1098
1099
1100 /*
1101 =head1 Multicall Functions
1102
1103 =for apidoc Ams||dMULTICALL
1104 Declare local variables for a multicall.  See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1105
1106 =for apidoc Ams||PUSH_MULTICALL
1107 Opening bracket for a lightweight callback.
1108 See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1109
1110 =for apidoc Ams||MULTICALL
1111 Make a lightweight callback.  See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1112
1113 =for apidoc Ams||POP_MULTICALL
1114 Closing bracket for a lightweight callback.
1115 See L<perlcall/LIGHTWEIGHT CALLBACKS>.
1116
1117 =cut
1118 */
1119
1120 #define dMULTICALL \
1121     OP  *multicall_cop;                                                 \
1122     bool multicall_oldcatch
1123
1124 #define PUSH_MULTICALL(the_cv) \
1125     PUSH_MULTICALL_FLAGS(the_cv, 0)
1126
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) */
1129
1130 #define PUSH_MULTICALL_FLAGS(the_cv, flags) \
1131     STMT_START {                                                        \
1132         PERL_CONTEXT *cx;                                               \
1133         CV * const _nOnclAshIngNamE_ = the_cv;                          \
1134         CV * const cv = _nOnclAshIngNamE_;                              \
1135         PADLIST * const padlist = CvPADLIST(cv);                        \
1136         multicall_oldcatch = CATCH_GET;                                 \
1137         CATCH_SET(TRUE);                                                \
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);                                    \
1142         SAVEOP();                                                       \
1143         if (!(flags & CXp_SUB_RE_FAKE))                                 \
1144             CvDEPTH(cv)++;                                              \
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);                                    \
1149     } STMT_END
1150
1151 #define MULTICALL \
1152     STMT_START {                                                        \
1153         PL_op = multicall_cop;                                          \
1154         CALLRUNOPS(aTHX);                                               \
1155     } STMT_END
1156
1157 #define POP_MULTICALL \
1158     STMT_START {                                                        \
1159         PERL_CONTEXT *cx;                                               \
1160         cx = CX_CUR();                                                  \
1161         CX_LEAVE_SCOPE(cx);                                             \
1162         cx_popsub_common(cx);                                           \
1163         gimme = cx->blk_gimme;                                          \
1164         PERL_UNUSED_VAR(gimme); /* for API */                           \
1165         cx_popblock(cx);                                                \
1166         CX_POP(cx);                                                     \
1167         POPSTACK;                                                       \
1168         CATCH_SET(multicall_oldcatch);                                  \
1169         SPAGAIN;                                                        \
1170     } STMT_END
1171
1172 /* Change the CV of an already-pushed MULTICALL CxSUB block.
1173  * (this isn't part of the public API) */
1174
1175 #define CHANGE_MULTICALL_FLAGS(the_cv, flags) \
1176     STMT_START {                                                        \
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))                                 \
1186             CvDEPTH(cv)++;                                              \
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);                                    \
1191     } STMT_END
1192 /*
1193  * ex: set ts=8 sts=4 sw=4 et:
1194  */