Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* cop.h |
79072805 | 2 | * |
1129b882 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
8622e0e2 | 4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
8622e0e2 | 9 | * Control ops (cops) are one of the two ops OP_NEXTSTATE and OP_DBSTATE, |
775e5718 | 10 | * that (loosely speaking) are statement separators. |
3ffa1527 JH |
11 | * They hold information important for lexical state and error reporting. |
12 | * At run time, PL_curcop is set to point to the most recently executed cop, | |
a3985cdc | 13 | * and thus can be used to determine our current state. |
79072805 LW |
14 | */ |
15 | ||
0d2925a6 | 16 | /* A jmpenv packages the state required to perform a proper non-local jump. |
1c98cc53 DM |
17 | * Note that there is a PL_start_env initialized when perl starts, and |
18 | * PL_top_env points to this initially, so PL_top_env should always be | |
19 | * non-null. | |
0d2925a6 | 20 | * |
1c98cc53 DM |
21 | * Existence of a non-null PL_top_env->je_prev implies it is valid to call |
22 | * longjmp() at that runlevel (we make sure PL_start_env.je_prev is always | |
0d2925a6 DM |
23 | * null to ensure this). |
24 | * | |
25 | * je_mustcatch, when set at any runlevel to TRUE, means eval ops must | |
26 | * establish a local jmpenv to handle exception traps. Care must be taken | |
27 | * to restore the previous value of je_mustcatch before exiting the | |
28 | * stack frame iff JMPENV_PUSH was not called in that stack frame. | |
29 | * GSAR 97-03-27 | |
30 | */ | |
31 | ||
32 | struct jmpenv { | |
33 | struct jmpenv * je_prev; | |
caa674f3 | 34 | Sigjmp_buf je_buf; /* uninit if je_prev is NULL */ |
0d2925a6 | 35 | int je_ret; /* last exception thrown */ |
5bfa3d72 | 36 | bool je_mustcatch; /* longjmp()s must be caught locally */ |
a68090fe | 37 | U16 je_old_delaymagic; /* saved PL_delaymagic */ |
9449f0d6 | 38 | SSize_t je_old_stack_hwm; |
0d2925a6 DM |
39 | }; |
40 | ||
41 | typedef struct jmpenv JMPENV; | |
42 | ||
9449f0d6 DM |
43 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
44 | # define JE_OLD_STACK_HWM_zero PL_start_env.je_old_stack_hwm = 0 | |
45 | # define JE_OLD_STACK_HWM_save(je) \ | |
46 | (je).je_old_stack_hwm = PL_curstackinfo->si_stack_hwm | |
47 | # define JE_OLD_STACK_HWM_restore(je) \ | |
978b1859 DM |
48 | if (PL_curstackinfo->si_stack_hwm < (je).je_old_stack_hwm) \ |
49 | PL_curstackinfo->si_stack_hwm = (je).je_old_stack_hwm | |
9449f0d6 DM |
50 | #else |
51 | # define JE_OLD_STACK_HWM_zero NOOP | |
52 | # define JE_OLD_STACK_HWM_save(je) NOOP | |
53 | # define JE_OLD_STACK_HWM_restore(je) NOOP | |
54 | #endif | |
55 | ||
0d2925a6 DM |
56 | /* |
57 | * How to build the first jmpenv. | |
58 | * | |
59 | * top_env needs to be non-zero. It points to an area | |
60 | * in which longjmp() stuff is stored, as C callstack | |
61 | * info there at least is thread specific this has to | |
62 | * be per-thread. Otherwise a 'die' in a thread gives | |
63 | * that thread the C stack of last thread to do an eval {}! | |
64 | */ | |
65 | ||
66 | #define JMPENV_BOOTSTRAP \ | |
67 | STMT_START { \ | |
1f4fbd3b MS |
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; \ | |
9449f0d6 | 74 | JE_OLD_STACK_HWM_zero; \ |
0d2925a6 DM |
75 | } STMT_END |
76 | ||
77 | /* | |
78 | * PERL_FLEXIBLE_EXCEPTIONS | |
0bbec57d | 79 | * |
0d2925a6 DM |
80 | * All the flexible exceptions code has been removed. |
81 | * See the following threads for details: | |
82 | * | |
0bbec57d MM |
83 | * Message-Id: 20040713143217.GB1424@plum.flirble.org |
84 | * https://www.nntp.perl.org/group/perl.perl5.porters/2004/07/msg93041.html | |
85 | * | |
0d2925a6 | 86 | * Joshua's original patches (which weren't applied) and discussion: |
0bbec57d | 87 | * |
0d2925a6 DM |
88 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01396.html |
89 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01489.html | |
90 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01491.html | |
91 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01608.html | |
92 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02144.html | |
93 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02998.html | |
0bbec57d | 94 | * |
0d2925a6 | 95 | * Chip's reworked patch and discussion: |
0bbec57d | 96 | * |
0d2925a6 | 97 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-03/msg00520.html |
0bbec57d | 98 | * |
0d2925a6 DM |
99 | * The flaw in these patches (which went unnoticed at the time) was |
100 | * that they moved some code that could potentially die() out of the | |
101 | * region protected by the setjmp()s. This caused exceptions within | |
102 | * END blocks and such to not be handled by the correct setjmp(). | |
c77726bb | 103 | * |
0d2925a6 DM |
104 | * The original patches that introduces flexible exceptions were: |
105 | * | |
47ef154c DB |
106 | * https://github.com/Perl/perl5/commit/312caa8e97f1c7ee342a9895c2f0e749625b4929 |
107 | * https://github.com/Perl/perl5/commit/14dd3ad8c9bf82cf09798a22cc89a9862dfd6d1a | |
c77726bb | 108 | * |
0d2925a6 DM |
109 | */ |
110 | ||
111 | #define dJMPENV JMPENV cur_env | |
112 | ||
112f4f94 | 113 | #define JMPENV_PUSH(v) \ |
0d2925a6 | 114 | STMT_START { \ |
112f4f94 | 115 | DEBUG_l({ \ |
aeff225d | 116 | int i = 0; \ |
112f4f94 | 117 | JMPENV *p = PL_top_env; \ |
1f4fbd3b | 118 | while (p) { i++; p = p->je_prev; } \ |
f31da5f4 YO |
119 | Perl_deb(aTHX_ "JMPENV_PUSH pre level=%d in %s at %s:%d\n", \ |
120 | i, SAFE_FUNCTION__, __FILE__, __LINE__); \ | |
112f4f94 | 121 | }); \ |
1f4fbd3b | 122 | cur_env.je_prev = PL_top_env; \ |
9449f0d6 | 123 | JE_OLD_STACK_HWM_save(cur_env); \ |
f9e2b18f TC |
124 | /* setjmp() is callable in limited contexts which does not */ \ |
125 | /* include assignment, so switch() instead */ \ | |
126 | switch (PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK)) { \ | |
127 | case 0: cur_env.je_ret = 0; break; \ | |
128 | case 1: cur_env.je_ret = 1; break; \ | |
129 | case 2: cur_env.je_ret = 2; break; \ | |
130 | case 3: cur_env.je_ret = 3; break; \ | |
131 | default: Perl_croak(aTHX_ "panic: unexpected setjmp() result\n"); \ | |
132 | } \ | |
9449f0d6 | 133 | JE_OLD_STACK_HWM_restore(cur_env); \ |
1f4fbd3b MS |
134 | PL_top_env = &cur_env; \ |
135 | cur_env.je_mustcatch = FALSE; \ | |
136 | cur_env.je_old_delaymagic = PL_delaymagic; \ | |
112f4f94 | 137 | DEBUG_l({ \ |
aeff225d YO |
138 | int i = 0; \ |
139 | JMPENV *p = PL_top_env; \ | |
140 | while (p) { i++; p = p->je_prev; } \ | |
f31da5f4 YO |
141 | Perl_deb(aTHX_ "JMPENV_PUSH level=%d ret=%d in %s at %s:%d\n", \ |
142 | i, cur_env.je_ret, SAFE_FUNCTION__, __FILE__, __LINE__); \ | |
112f4f94 | 143 | }); \ |
aeff225d | 144 | (v) = cur_env.je_ret; \ |
0d2925a6 DM |
145 | } STMT_END |
146 | ||
147 | #define JMPENV_POP \ | |
148 | STMT_START { \ | |
112f4f94 | 149 | DEBUG_l({ \ |
1f4fbd3b MS |
150 | int i = -1; JMPENV *p = PL_top_env; \ |
151 | while (p) { i++; p = p->je_prev; } \ | |
f31da5f4 YO |
152 | Perl_deb(aTHX_ "JMPENV_POP level=%d in %s at %s:%d\n", \ |
153 | i, SAFE_FUNCTION__, __FILE__, __LINE__);}) \ | |
1f4fbd3b MS |
154 | assert(PL_top_env == &cur_env); \ |
155 | PL_delaymagic = cur_env.je_old_delaymagic; \ | |
156 | PL_top_env = cur_env.je_prev; \ | |
0d2925a6 DM |
157 | } STMT_END |
158 | ||
159 | #define JMPENV_JUMP(v) \ | |
160 | STMT_START { \ | |
112f4f94 | 161 | DEBUG_l({ \ |
1f4fbd3b MS |
162 | int i = -1; JMPENV *p = PL_top_env; \ |
163 | while (p) { i++; p = p->je_prev; } \ | |
f31da5f4 YO |
164 | Perl_deb(aTHX_ "JMPENV_JUMP(%d) level=%d in %s at %s:%d\n", \ |
165 | (int)(v), i, SAFE_FUNCTION__, __FILE__, __LINE__);}) \ | |
f9e2b18f TC |
166 | if (PL_top_env->je_prev) { \ |
167 | assert((v) >= 0 && (v) <= 3); \ | |
1f4fbd3b | 168 | PerlProc_longjmp(PL_top_env->je_buf, (v)); \ |
f9e2b18f | 169 | } \ |
1f4fbd3b MS |
170 | if ((v) == 2) \ |
171 | PerlProc_exit(STATUS_EXIT); \ | |
112f4f94 | 172 | PerlIO_printf(PerlIO_stderr(), "panic: top_env, v=%d\n", (int)(v)); \ |
1f4fbd3b | 173 | PerlProc_exit(1); \ |
0d2925a6 DM |
174 | } STMT_END |
175 | ||
176 | #define CATCH_GET (PL_top_env->je_mustcatch) | |
1c98cc53 DM |
177 | #define CATCH_SET(v) \ |
178 | STMT_START { \ | |
112f4f94 | 179 | DEBUG_l( \ |
1f4fbd3b | 180 | Perl_deb(aTHX_ \ |
f31da5f4 | 181 | "JUMPLEVEL set catch %d => %d (for %p) in %s at %s:%d\n", \ |
112f4f94 | 182 | PL_top_env->je_mustcatch, (v), (void*)PL_top_env, \ |
f31da5f4 | 183 | SAFE_FUNCTION__, __FILE__, __LINE__);) \ |
1f4fbd3b | 184 | PL_top_env->je_mustcatch = (v); \ |
1c98cc53 | 185 | } STMT_END |
0d2925a6 | 186 | |
20439bc7 | 187 | /* |
3f620621 | 188 | =for apidoc_section $COP |
20439bc7 Z |
189 | */ |
190 | ||
191 | typedef struct refcounted_he COPHH; | |
192 | ||
193 | #define COPHH_KEY_UTF8 REFCOUNTED_HE_KEY_UTF8 | |
d36f8b33 | 194 | #define COPHH_EXISTS REFCOUNTED_HE_EXISTS |
20439bc7 Z |
195 | |
196 | /* | |
1607e393 KW |
197 | =for apidoc Amx|SV *|cophh_fetch_pv |const COPHH *cophh|const char *key |U32 hash|U32 flags |
198 | =for apidoc_item|SV *|cophh_fetch_pvn|const COPHH *cophh|const char *key|STRLEN keylen|U32 hash|U32 flags | |
699ef60a KW |
199 | =for apidoc_item|SV *|cophh_fetch_pvs|const COPHH *cophh| "key" |U32 flags |
200 | =for apidoc_item|SV *|cophh_fetch_sv |const COPHH *cophh| SV *key |U32 hash|U32 flags | |
201 | ||
202 | These look up the entry in the cop hints hash C<cophh> with the key specified by | |
203 | C<key> (and C<keylen> in the C<pvn> form), returning that value as a mortal | |
204 | scalar copy, or C<&PL_sv_placeholder> if there is no value associated with the | |
205 | key. | |
206 | ||
207 | The forms differ in how the key is specified. | |
208 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
209 | In the C<pvs> form, the key is a C language string literal. | |
210 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
211 | the string, which hence, may contain embedded-NUL characters. | |
212 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
213 | using C<L</SvPV_const>>. | |
214 | ||
215 | C<hash> is a precomputed hash of the key string, or zero if it has not been | |
216 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
217 | automatically at compile time. | |
218 | ||
219 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. | |
220 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
221 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
222 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
223 | the octets. | |
20439bc7 | 224 | |
5af38e47 KW |
225 | =for apidoc Amnh||COPHH_KEY_UTF8 |
226 | ||
20439bc7 | 227 | =cut |
9d0ade3b | 228 | |
20439bc7 Z |
229 | */ |
230 | ||
9d0ade3b KW |
231 | #define cophh_fetch_pvn(cophh, key, keylen, hash, flags) \ |
232 | Perl_refcounted_he_fetch_pvn(aTHX_ cophh, key, keylen, hash, \ | |
233 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 | 234 | |
9d0ade3b KW |
235 | #define cophh_fetch_pvs(cophh, key, flags) \ |
236 | Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \ | |
237 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 | 238 | |
9d0ade3b KW |
239 | #define cophh_fetch_pv(cophh, key, hash, flags) \ |
240 | Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, \ | |
241 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 | 242 | |
9d0ade3b KW |
243 | #define cophh_fetch_sv(cophh, key, hash, flags) \ |
244 | Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, \ | |
245 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 Z |
246 | |
247 | /* | |
c4f35413 | 248 | =for apidoc Amx|bool|cophh_exists_pvn|const COPHH *cophh|const char *key|STRLEN keylen|U32 hash|U32 flags |
d36f8b33 | 249 | |
c4f35413 KW |
250 | These look up the hint entry in the cop C<cop> with the key specified by |
251 | C<key> (and C<keylen> in the C<pvn> form), returning true if a value exists, | |
252 | and false otherwise. | |
d36f8b33 | 253 | |
c4f35413 KW |
254 | The forms differ in how the key is specified. |
255 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
256 | In the C<pvs> form, the key is a C language string literal. | |
257 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
258 | the string, which hence, may contain embedded-NUL characters. | |
259 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
260 | using C<L</SvPV_const>>. | |
d36f8b33 | 261 | |
c4f35413 KW |
262 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
263 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
264 | automatically at compile time. | |
d36f8b33 | 265 | |
c4f35413 KW |
266 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
267 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
268 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
269 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
270 | the octets. | |
d36f8b33 LT |
271 | |
272 | =cut | |
273 | */ | |
274 | ||
c4f35413 KW |
275 | #define cophh_exists_pvn(cophh, key, keylen, hash, flags) \ |
276 | cBOOL(Perl_refcounted_he_fetch_pvn(aTHX_ cophh, key, keylen, hash, flags | COPHH_EXISTS)) | |
277 | ||
d36f8b33 LT |
278 | #define cophh_exists_pvs(cophh, key, flags) \ |
279 | cBOOL(Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, flags | COPHH_EXISTS)) | |
280 | ||
d36f8b33 LT |
281 | #define cophh_exists_pv(cophh, key, hash, flags) \ |
282 | cBOOL(Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, flags | COPHH_EXISTS)) | |
283 | ||
d36f8b33 LT |
284 | #define cophh_exists_sv(cophh, key, hash, flags) \ |
285 | cBOOL(Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, flags | COPHH_EXISTS)) | |
286 | ||
287 | /* | |
ffd08bbf | 288 | =for apidoc Amx|HV *|cophh_2hv|const COPHH *cophh|U32 flags |
20439bc7 Z |
289 | |
290 | Generates and returns a standard Perl hash representing the full set of | |
2d7f6611 | 291 | key/value pairs in the cop hints hash C<cophh>. C<flags> is currently |
20439bc7 Z |
292 | unused and must be zero. |
293 | ||
294 | =cut | |
295 | */ | |
296 | ||
297 | #define cophh_2hv(cophh, flags) \ | |
298 | Perl_refcounted_he_chain_2hv(aTHX_ cophh, flags) | |
299 | ||
300 | /* | |
ffd08bbf | 301 | =for apidoc Amx|COPHH *|cophh_copy|COPHH *cophh |
20439bc7 | 302 | |
2d7f6611 | 303 | Make and return a complete copy of the cop hints hash C<cophh>. |
20439bc7 Z |
304 | |
305 | =cut | |
306 | */ | |
307 | ||
308 | #define cophh_copy(cophh) Perl_refcounted_he_inc(aTHX_ cophh) | |
309 | ||
310 | /* | |
ffd08bbf | 311 | =for apidoc Amx|void|cophh_free|COPHH *cophh |
20439bc7 | 312 | |
2d7f6611 | 313 | Discard the cop hints hash C<cophh>, freeing all resources associated |
20439bc7 Z |
314 | with it. |
315 | ||
316 | =cut | |
317 | */ | |
318 | ||
319 | #define cophh_free(cophh) Perl_refcounted_he_free(aTHX_ cophh) | |
320 | ||
321 | /* | |
ffd08bbf | 322 | =for apidoc Amx|COPHH *|cophh_new_empty |
20439bc7 Z |
323 | |
324 | Generate and return a fresh cop hints hash containing no entries. | |
325 | ||
326 | =cut | |
327 | */ | |
328 | ||
329 | #define cophh_new_empty() ((COPHH *)NULL) | |
330 | ||
331 | /* | |
1607e393 KW |
332 | =for apidoc Amx|COPHH *|cophh_store_pv |COPHH *cophh|const char *key |U32 hash|SV *value|U32 flags |
333 | =for apidoc_item|COPHH *|cophh_store_pvn|COPHH *cophh|const char *key|STRLEN keylen|U32 hash|SV *value|U32 flags | |
51505c5b KW |
334 | =for apidoc_item|COPHH *|cophh_store_pvs|COPHH *cophh| "key" |SV *value|U32 flags |
335 | =for apidoc_item|COPHH *|cophh_store_sv |COPHH *cophh| SV *key |U32 hash|SV *value|U32 flags | |
20439bc7 | 336 | |
51505c5b KW |
337 | These store a value, associated with a key, in the cop hints hash C<cophh>, |
338 | and return the modified hash. The returned hash pointer is in general | |
20439bc7 Z |
339 | not the same as the hash pointer that was passed in. The input hash is |
340 | consumed by the function, and the pointer to it must not be subsequently | |
341 | used. Use L</cophh_copy> if you need both hashes. | |
342 | ||
2d7f6611 | 343 | C<value> is the scalar value to store for this key. C<value> is copied |
51505c5b KW |
344 | by these functions, which thus do not take ownership of any reference |
345 | to it, and hence later changes to the scalar will not be reflected in the value | |
346 | visible in the cop hints hash. Complex types of scalar will not be stored with | |
347 | referential integrity, but will be coerced to strings. | |
348 | ||
349 | The forms differ in how the key is specified. In all forms, the key is pointed | |
350 | to by C<key>. | |
351 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
352 | In the C<pvs> form, the key is a C language string literal. | |
353 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
354 | the string, which hence, may contain embedded-NUL characters. | |
355 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
356 | using C<L</SvPV_const>>. | |
357 | ||
358 | C<hash> is a precomputed hash of the key string, or zero if it has not been | |
359 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
360 | automatically at compile time. | |
361 | ||
362 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. | |
363 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
364 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
365 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
366 | the octets. | |
20439bc7 Z |
367 | |
368 | =cut | |
369 | */ | |
370 | ||
51505c5b KW |
371 | #define cophh_store_pvn(cophh, key, keylen, hash, value, flags) \ |
372 | Perl_refcounted_he_new_pvn(aTHX_ cophh, key, keylen, hash, value, flags) | |
20439bc7 Z |
373 | |
374 | #define cophh_store_pvs(cophh, key, value, flags) \ | |
375 | Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, value, flags) | |
376 | ||
20439bc7 Z |
377 | #define cophh_store_pv(cophh, key, hash, value, flags) \ |
378 | Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, value, flags) | |
379 | ||
20439bc7 Z |
380 | #define cophh_store_sv(cophh, key, hash, value, flags) \ |
381 | Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, value, flags) | |
382 | ||
383 | /* | |
1607e393 KW |
384 | =for apidoc Amx|COPHH *|cophh_delete_pv |COPHH *cophh|const char *key |U32 hash|U32 flags |
385 | =for apidoc_item|COPHH *|cophh_delete_pvn|COPHH *cophh|const char *key|STRLEN keylen|U32 hash|U32 flags | |
226b86f1 KW |
386 | =for apidoc_item|COPHH *|cophh_delete_pvs|COPHH *cophh| "key" |U32 flags |
387 | =for apidoc_item|COPHH *|cophh_delete_sv |COPHH *cophh| SV *key |U32 hash|U32 flags | |
20439bc7 | 388 | |
226b86f1 KW |
389 | These delete a key and its associated value from the cop hints hash C<cophh>, |
390 | and return the modified hash. The returned hash pointer is in general | |
20439bc7 Z |
391 | not the same as the hash pointer that was passed in. The input hash is |
392 | consumed by the function, and the pointer to it must not be subsequently | |
393 | used. Use L</cophh_copy> if you need both hashes. | |
394 | ||
226b86f1 KW |
395 | The forms differ in how the key is specified. In all forms, the key is pointed |
396 | to by C<key>. | |
397 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
398 | In the C<pvs> form, the key is a C language string literal. | |
399 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
400 | the string, which hence, may contain embedded-NUL characters. | |
401 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
402 | using C<L</SvPV_const>>. | |
20439bc7 | 403 | |
226b86f1 KW |
404 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
405 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
406 | automatically at compile time. | |
20439bc7 | 407 | |
226b86f1 KW |
408 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
409 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
410 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
411 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
412 | the octets. | |
20439bc7 Z |
413 | |
414 | =cut | |
415 | */ | |
416 | ||
226b86f1 KW |
417 | #define cophh_delete_pvn(cophh, key, keylen, hash, flags) \ |
418 | Perl_refcounted_he_new_pvn(aTHX_ cophh, key, keylen, hash, \ | |
419 | (SV *)NULL, flags) | |
420 | ||
20439bc7 Z |
421 | #define cophh_delete_pvs(cophh, key, flags) \ |
422 | Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \ | |
1f4fbd3b | 423 | (SV *)NULL, flags) |
20439bc7 | 424 | |
20439bc7 Z |
425 | #define cophh_delete_pv(cophh, key, hash, flags) \ |
426 | Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, (SV *)NULL, flags) | |
427 | ||
20439bc7 Z |
428 | #define cophh_delete_sv(cophh, key, hash, flags) \ |
429 | Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, (SV *)NULL, flags) | |
0d2925a6 | 430 | |
5ac1e9b2 | 431 | #include "mydtrace.h" |
0d2925a6 | 432 | |
88df5f01 FC |
433 | struct cop { |
434 | BASEOP | |
435 | /* On LP64 putting this here takes advantage of the fact that BASEOP isn't | |
436 | an exact multiple of 8 bytes to save structure padding. */ | |
437 | line_t cop_line; /* line # of this command */ | |
438 | /* label for this construct is now stored in cop_hints_hash */ | |
57843af0 | 439 | #ifdef USE_ITHREADS |
88df5f01 | 440 | PADOFFSET cop_stashoff; /* offset into PL_stashpad, for the |
1f4fbd3b | 441 | package the line was compiled in */ |
6760f691 | 442 | char * cop_file; /* rcpv containing name of file this command is from */ |
57843af0 | 443 | #else |
88df5f01 | 444 | HV * cop_stash; /* package line was compiled in */ |
6600fe70 | 445 | GV * cop_filegv; /* name of GV file this command is from */ |
57843af0 | 446 | #endif |
88df5f01 FC |
447 | U32 cop_hints; /* hints bits from pragmata */ |
448 | U32 cop_seq; /* parse sequence number */ | |
f8552c1a YO |
449 | char * cop_warnings; /* Lexical warnings bitmask vector. |
450 | Refcounted shared copy of ${^WARNING_BITS}. | |
451 | This pointer either points at one of the | |
452 | magic values for warnings, or it points | |
453 | at a buffer constructed with rcpv_new(). | |
454 | Use the RCPV_LEN() macro to get its length. | |
455 | */ | |
88df5f01 FC |
456 | /* compile time state of %^H. See the comment in op.c for how this is |
457 | used to recreate a hash to return from caller. */ | |
20439bc7 | 458 | COPHH * cop_hints_hash; |
9f601cf3 TC |
459 | /* for now just a bitmask stored here. |
460 | If we get sufficient features this may become a pointer. | |
461 | How these flags are stored is subject to change without | |
462 | notice. Use the macros to test for features. | |
463 | */ | |
464 | U32 cop_features; | |
79072805 LW |
465 | }; |
466 | ||
add0fa58 KW |
467 | /* |
468 | =for apidoc Am|const char *|CopFILE|const COP * c | |
469 | Returns the name of the file associated with the C<COP> C<c> | |
470 | ||
6760f691 YO |
471 | =for apidoc Am|const char *|CopFILE_LEN|const COP * c |
472 | Returns the length of the file associated with the C<COP> C<c> | |
473 | ||
6abc6edf | 474 | =for apidoc Am|line_t|CopLINE|const COP * c |
add0fa58 KW |
475 | Returns the line number in the source code associated with the C<COP> C<c> |
476 | ||
477 | =for apidoc Am|AV *|CopFILEAV|const COP * c | |
9c913148 TC |
478 | Returns the AV associated with the C<COP> C<c>, creating it if necessary. |
479 | ||
480 | =for apidoc Am|AV *|CopFILEAVn|const COP * c | |
481 | Returns the AV associated with the C<COP> C<c>, returning NULL if it | |
482 | doesn't already exist. | |
add0fa58 KW |
483 | |
484 | =for apidoc Am|SV *|CopFILESV|const COP * c | |
485 | Returns the SV associated with the C<COP> C<c> | |
486 | ||
487 | =for apidoc Am|void|CopFILE_set|COP * c|const char * pv | |
488 | Makes C<pv> the name of the file associated with the C<COP> C<c> | |
489 | ||
6760f691 YO |
490 | =for apidoc Am|void|CopFILE_setn|COP * c|const char * pv|STRLEN len |
491 | Makes C<pv> the name of the file associated with the C<COP> C<c> | |
492 | ||
493 | =for apidoc Am|void|CopFILE_copy|COP * dst|COP * src | |
494 | Efficiently copies the cop file name from one COP to another. Wraps | |
495 | the required logic to do a refcounted copy under threads or not. | |
496 | ||
497 | =for apidoc Am|void|CopFILE_free|COP * c | |
498 | Frees the file data in a cop. Under the hood this is a refcounting | |
499 | operation. | |
500 | ||
add0fa58 KW |
501 | =for apidoc Am|GV *|CopFILEGV|const COP * c |
502 | Returns the GV associated with the C<COP> C<c> | |
503 | ||
504 | =for apidoc CopFILEGV_set | |
505 | Available only on unthreaded perls. Makes C<pv> the name of the file | |
506 | associated with the C<COP> C<c> | |
507 | ||
7f02c139 KW |
508 | =for apidoc Am|HV *|CopSTASH|const COP * c |
509 | Returns the stash associated with C<c>. | |
510 | ||
511 | =for apidoc Am|bool|CopSTASH_eq|const COP * c|const HV * hv | |
512 | Returns a boolean as to whether or not C<hv> is the stash associated with C<c>. | |
513 | ||
514 | =for apidoc Am|bool|CopSTASH_set|COP * c|HV * hv | |
515 | Set the stash associated with C<c> to C<hv>. | |
516 | ||
517 | =for apidoc Am|char *|CopSTASHPV|const COP * c | |
518 | Returns the package name of the stash associated with C<c>, or C<NULL> if no | |
519 | associated stash | |
520 | ||
521 | =for apidoc Am|void|CopSTASHPV_set|COP * c|const char * pv | |
522 | Set the package name of the stash associated with C<c>, to the NUL-terminated C | |
523 | string C<p>, creating the package if necessary. | |
524 | ||
add0fa58 KW |
525 | =cut |
526 | */ | |
527 | ||
6760f691 YO |
528 | /* |
529 | =for apidoc Am|RCPV *|RCPVx|char *pv | |
530 | Returns the RCPV structure (struct rcpv) for a refcounted | |
531 | string pv created with C<rcpv_new()>. | |
532 | ||
533 | =for apidoc Am|RCPV *|RCPV_REFCOUNT|char *pv | |
534 | Returns the refcount for a pv created with C<rcpv_new()>. | |
535 | ||
536 | =for apidoc Am|RCPV *|RCPV_LEN|char *pv | |
a511d7dc YO |
537 | Returns the length of a pv created with C<rcpv_new()>. |
538 | Note that this reflects the length of the string from the callers | |
539 | point of view, it does not include the mandatory null which is | |
540 | always injected at the end of the string by rcpv_new(). | |
6760f691 YO |
541 | |
542 | =cut | |
543 | */ | |
544 | ||
545 | struct rcpv { | |
546 | STRLEN refcount; /* UV would mean a 64 refcnt on | |
547 | 32 bit builds with -Duse64bitint */ | |
a511d7dc YO |
548 | STRLEN len; /* length of string including mandatory |
549 | null byte at end */ | |
6760f691 YO |
550 | char pv[1]; |
551 | }; | |
552 | typedef struct rcpv RCPV; | |
553 | ||
a511d7dc YO |
554 | #define RCPVf_USE_STRLEN (1 << 0) |
555 | #define RCPVf_NO_COPY (1 << 1) | |
556 | #define RCPVf_ALLOW_EMPTY (1 << 2) | |
557 | ||
6760f691 YO |
558 | #define RCPVx(pv_arg) ((RCPV *)((pv_arg) - STRUCT_OFFSET(struct rcpv, pv))) |
559 | #define RCPV_REFCOUNT(pv) (RCPVx(pv)->refcount) | |
a511d7dc | 560 | #define RCPV_LEN(pv) (RCPVx(pv)->len-1) /* len always includes space for a null */ |
6760f691 | 561 | |
57843af0 | 562 | #ifdef USE_ITHREADS |
a8899380 | 563 | |
6760f691 YO |
564 | # define CopFILE(c) ((c)->cop_file) |
565 | # define CopFILE_LEN(c) (CopFILE(c) ? RCPV_LEN(CopFILE(c)) : 0) | |
1dc74fdb | 566 | # define CopFILEGV(c) (CopFILE(c) \ |
1f4fbd3b | 567 | ? gv_fetchfile(CopFILE(c)) : NULL) |
0bbec57d | 568 | |
6760f691 YO |
569 | # define CopFILE_set_x(c,pv) ((c)->cop_file = rcpv_new((pv),0,RCPVf_USE_STRLEN)) |
570 | # define CopFILE_setn_x(c,pv,l) ((c)->cop_file = rcpv_new((pv),(l),0)) | |
571 | # define CopFILE_free_x(c) ((c)->cop_file = rcpv_free((c)->cop_file)) | |
572 | # define CopFILE_copy_x(dst,src) ((dst)->cop_file = rcpv_copy((src)->cop_file)) | |
573 | ||
574 | /* change condition to 1 && to enable this debugging */ | |
575 | # define CopFILE_debug(c,t,rk) \ | |
576 | if (0 && (c)->cop_file) \ | |
577 | PerlIO_printf(Perl_debug_log, \ | |
578 | "%-14s THX:%p OP:%p PV:%p rc: " \ | |
579 | "%6zu fn: '%.*s' at %s line %d\n", \ | |
580 | (t), aTHX, (c), (c)->cop_file, \ | |
581 | RCPV_REFCOUNT((c)->cop_file)-rk, \ | |
582 | (int)RCPV_LEN((c)->cop_file), \ | |
583 | (c)->cop_file,__FILE__,__LINE__) \ | |
584 | ||
585 | ||
586 | # define CopFILE_set(c,pv) \ | |
587 | STMT_START { \ | |
588 | CopFILE_set_x(c,pv); \ | |
589 | CopFILE_debug(c,"CopFILE_set", 0); \ | |
590 | } STMT_END | |
591 | ||
592 | # define CopFILE_setn(c,pv,l) \ | |
593 | STMT_START { \ | |
594 | CopFILE_setn_x(c,pv,l); \ | |
595 | CopFILE_debug(c,"CopFILE_setn", 0); \ | |
596 | } STMT_END | |
597 | ||
598 | # define CopFILE_copy(dst,src) \ | |
599 | STMT_START { \ | |
600 | CopFILE_copy_x((dst),(src)); \ | |
601 | CopFILE_debug((dst),"CopFILE_copy", 0); \ | |
602 | } STMT_END | |
603 | ||
604 | # define CopFILE_free(c) \ | |
605 | STMT_START { \ | |
606 | CopFILE_debug((c),"CopFILE_free", 1); \ | |
607 | CopFILE_free_x(c); \ | |
608 | } STMT_END | |
609 | ||
1dc74fdb FC |
610 | |
611 | # define CopFILESV(c) (CopFILE(c) \ | |
1f4fbd3b | 612 | ? GvSV(gv_fetchfile(CopFILE(c))) : NULL) |
1dc74fdb | 613 | # define CopFILEAV(c) (CopFILE(c) \ |
1f4fbd3b | 614 | ? GvAV(gv_fetchfile(CopFILE(c))) : NULL) |
1dc74fdb | 615 | # define CopFILEAVx(c) (assert_(CopFILE(c)) \ |
1f4fbd3b | 616 | GvAV(gv_fetchfile(CopFILE(c)))) |
9c913148 | 617 | # define CopFILEAVn(c) (cop_file_avn(c)) |
d4d03940 FC |
618 | # define CopSTASH(c) PL_stashpad[(c)->cop_stashoff] |
619 | # define CopSTASH_set(c,hv) ((c)->cop_stashoff = (hv) \ | |
1f4fbd3b MS |
620 | ? alloccopstash(hv) \ |
621 | : 0) | |
a8899380 TC |
622 | |
623 | #else /* Above: yes threads; Below no threads */ | |
624 | ||
57843af0 | 625 | # define CopFILEGV(c) ((c)->cop_filegv) |
f4dd75d9 | 626 | # define CopFILEGV_set(c,gv) ((c)->cop_filegv = (GV*)SvREFCNT_inc(gv)) |
1dc74fdb | 627 | # define CopFILE_set(c,pv) CopFILEGV_set((c), gv_fetchfile(pv)) |
6760f691 | 628 | # define CopFILE_copy(dst,src) CopFILEGV_set((dst),CopFILEGV(src)) |
1dc74fdb FC |
629 | # define CopFILE_setn(c,pv,l) CopFILEGV_set((c), gv_fetchfile_flags((pv),(l),0)) |
630 | # define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : NULL) | |
631 | # define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : NULL) | |
632 | # ifdef DEBUGGING | |
633 | # define CopFILEAVx(c) (assert(CopFILEGV(c)), GvAV(CopFILEGV(c))) | |
634 | # else | |
635 | # define CopFILEAVx(c) (GvAV(CopFILEGV(c))) | |
636 | # endif | |
9c913148 | 637 | # define CopFILEAVn(c) (CopFILEGV(c) ? GvAVn(CopFILEGV(c)) : NULL) |
6600fe70 | 638 | # define CopFILE(c) (CopFILEGV(c) /* +2 for '_<' */ \ |
1f4fbd3b | 639 | ? GvNAME(CopFILEGV(c))+2 : NULL) |
6760f691 YO |
640 | # define CopFILE_LEN(c) (CopFILEGV(c) /* -2 for '_<' */ \ |
641 | ? GvNAMELEN(CopFILEGV(c))-2 : 0) | |
57843af0 | 642 | # define CopSTASH(c) ((c)->cop_stash) |
f4dd75d9 | 643 | # define CopSTASH_set(c,hv) ((c)->cop_stash = (hv)) |
a0714e2c | 644 | # define CopFILE_free(c) (SvREFCNT_dec(CopFILEGV(c)),(CopFILEGV(c) = NULL)) |
05ec9bb3 | 645 | |
57843af0 | 646 | #endif /* USE_ITHREADS */ |
20439bc7 | 647 | |
d4d03940 FC |
648 | #define CopSTASHPV(c) (CopSTASH(c) ? HvNAME_get(CopSTASH(c)) : NULL) |
649 | /* cop_stash is not refcounted */ | |
650 | #define CopSTASHPV_set(c,pv) CopSTASH_set((c), gv_stashpv(pv,GV_ADD)) | |
651 | #define CopSTASH_eq(c,hv) (CopSTASH(c) == (hv)) | |
d4d03940 | 652 | |
20439bc7 Z |
653 | #define CopHINTHASH_get(c) ((COPHH*)((c)->cop_hints_hash)) |
654 | #define CopHINTHASH_set(c,h) ((c)->cop_hints_hash = (h)) | |
655 | ||
a73e2f42 TC |
656 | #define CopFEATURES_setfrom(dst, src) ((dst)->cop_features = (src)->cop_features) |
657 | ||
20439bc7 | 658 | /* |
1607e393 KW |
659 | =for apidoc Am|SV *|cop_hints_fetch_pv |const COP *cop|const char *key |U32 hash|U32 flags |
660 | =for apidoc_item|SV *|cop_hints_fetch_pvn|const COP *cop|const char *key|STRLEN keylen|U32 hash|U32 flags | |
661 | =for apidoc_item|SV *|cop_hints_fetch_pvs|const COP *cop| "key" |U32 flags | |
c7c0c00c | 662 | =for apidoc_item|SV *|cop_hints_fetch_sv |const COP *cop| SV *key |U32 hash|U32 flags |
20439bc7 | 663 | |
c7c0c00c KW |
664 | These look up the hint entry in the cop C<cop> with the key specified by |
665 | C<key> (and C<keylen> in the C<pvn> form), returning that value as a mortal | |
666 | scalar copy, or C<&PL_sv_placeholder> if there is no value associated with the | |
667 | key. | |
20439bc7 | 668 | |
c7c0c00c KW |
669 | The forms differ in how the key is specified. |
670 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
671 | In the C<pvs> form, the key is a C language string literal. | |
672 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
673 | the string, which hence, may contain embedded-NUL characters. | |
674 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
675 | using C<L</SvPV_const>>. | |
20439bc7 | 676 | |
c7c0c00c KW |
677 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
678 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
679 | automatically at compile time. | |
20439bc7 | 680 | |
c7c0c00c KW |
681 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
682 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
683 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
684 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
685 | the octets. | |
20439bc7 Z |
686 | |
687 | =cut | |
688 | */ | |
689 | ||
c7c0c00c KW |
690 | #define cop_hints_fetch_pvn(cop, key, keylen, hash, flags) \ |
691 | cophh_fetch_pvn(CopHINTHASH_get(cop), key, keylen, hash, flags) | |
692 | ||
20439bc7 Z |
693 | #define cop_hints_fetch_pvs(cop, key, flags) \ |
694 | cophh_fetch_pvs(CopHINTHASH_get(cop), key, flags) | |
695 | ||
20439bc7 Z |
696 | #define cop_hints_fetch_pv(cop, key, hash, flags) \ |
697 | cophh_fetch_pv(CopHINTHASH_get(cop), key, hash, flags) | |
698 | ||
20439bc7 Z |
699 | #define cop_hints_fetch_sv(cop, key, hash, flags) \ |
700 | cophh_fetch_sv(CopHINTHASH_get(cop), key, hash, flags) | |
701 | ||
702 | /* | |
1607e393 KW |
703 | =for apidoc Am|bool|cop_hints_exists_pv |const COP *cop|const char *key|U32 hash |U32 flags |
704 | =for apidoc_item|bool|cop_hints_exists_pvn|const COP *cop|const char *key|STRLEN keylen|U32 hash|U32 flags | |
d809c335 KW |
705 | =for apidoc_item|bool|cop_hints_exists_pvs|const COP *cop| "key" |U32 flags |
706 | =for apidoc_item|bool|cop_hints_exists_sv |const COP *cop| SV *key |U32 hash|U32 flags | |
bbb58664 | 707 | |
d809c335 KW |
708 | These look up the hint entry in the cop C<cop> with the key specified by |
709 | C<key> (and C<keylen> in the C<pvn> form), returning true if a value exists, | |
710 | and false otherwise. | |
bbb58664 | 711 | |
d809c335 KW |
712 | The forms differ in how the key is specified. In all forms, the key is pointed |
713 | to by C<key>. | |
714 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
715 | In the C<pvs> form, the key is a C language string literal. | |
716 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
717 | the string, which hence, may contain embedded-NUL characters. | |
718 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
719 | using C<L</SvPV_const>>. | |
bbb58664 | 720 | |
d809c335 KW |
721 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
722 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
723 | automatically at compile time. | |
bbb58664 | 724 | |
d809c335 KW |
725 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
726 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
727 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
728 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
729 | the octets. | |
bbb58664 LT |
730 | |
731 | =cut | |
732 | */ | |
733 | ||
d809c335 KW |
734 | #define cop_hints_exists_pvn(cop, key, keylen, hash, flags) \ |
735 | cophh_exists_pvn(CopHINTHASH_get(cop), key, keylen, hash, flags) | |
736 | ||
bbb58664 LT |
737 | #define cop_hints_exists_pvs(cop, key, flags) \ |
738 | cophh_exists_pvs(CopHINTHASH_get(cop), key, flags) | |
739 | ||
bbb58664 LT |
740 | #define cop_hints_exists_pv(cop, key, hash, flags) \ |
741 | cophh_exists_pv(CopHINTHASH_get(cop), key, hash, flags) | |
742 | ||
bbb58664 LT |
743 | #define cop_hints_exists_sv(cop, key, hash, flags) \ |
744 | cophh_exists_sv(CopHINTHASH_get(cop), key, hash, flags) | |
745 | ||
746 | /* | |
20439bc7 Z |
747 | =for apidoc Am|HV *|cop_hints_2hv|const COP *cop|U32 flags |
748 | ||
749 | Generates and returns a standard Perl hash representing the full set of | |
2d7f6611 | 750 | hint entries in the cop C<cop>. C<flags> is currently unused and must |
20439bc7 Z |
751 | be zero. |
752 | ||
753 | =cut | |
754 | */ | |
755 | ||
756 | #define cop_hints_2hv(cop, flags) \ | |
757 | cophh_2hv(CopHINTHASH_get(cop), flags) | |
758 | ||
33d29c13 | 759 | /* |
71b454dc KW |
760 | =for apidoc Am|const char *|CopLABEL |COP *const cop |
761 | =for apidoc_item|const char *|CopLABEL_len |COP *const cop|STRLEN *len | |
762 | =for apidoc_item|const char *|CopLABEL_len_flags|COP *const cop|STRLEN *len|U32 *flags | |
33d29c13 | 763 | |
71b454dc | 764 | These return the label attached to a cop. |
33d29c13 | 765 | |
71b454dc KW |
766 | C<CopLABEL_len> and C<CopLABEL_len_flags> additionally store the number of |
767 | bytes comprising the returned label into C<*len>. | |
33d29c13 | 768 | |
71b454dc KW |
769 | C<CopLABEL_len_flags> additionally returns the UTF-8ness of the returned label, |
770 | by setting C<*flags> to 0 or C<SVf_UTF8>. | |
33d29c13 KW |
771 | |
772 | =cut | |
773 | */ | |
774 | ||
aebc0cbe | 775 | #define CopLABEL(c) Perl_cop_fetch_label(aTHX_ (c), NULL, NULL) |
5db1eb8d BF |
776 | #define CopLABEL_len(c,len) Perl_cop_fetch_label(aTHX_ (c), len, NULL) |
777 | #define CopLABEL_len_flags(c,len,flags) Perl_cop_fetch_label(aTHX_ (c), len, flags) | |
dca6062a | 778 | #define CopLABEL_alloc(pv) ((pv)?savepv(pv):NULL) |
57843af0 | 779 | |
ed094faf | 780 | #define CopSTASH_ne(c,hv) (!CopSTASH_eq(c,hv)) |
cc49e20b | 781 | #define CopLINE(c) ((c)->cop_line) |
57843af0 GS |
782 | #define CopLINE_inc(c) (++CopLINE(c)) |
783 | #define CopLINE_dec(c) (--CopLINE(c)) | |
784 | #define CopLINE_set(c,l) (CopLINE(c) = (l)) | |
cc49e20b | 785 | |
248c2a4d | 786 | /* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */ |
e37778c2 | 787 | #define OutCopFILE(c) CopFILE(c) |
248c2a4d | 788 | |
d5ec2987 | 789 | #define CopHINTS_get(c) ((c)->cop_hints + 0) |
623e6609 | 790 | #define CopHINTS_set(c, h) STMT_START { \ |
1f4fbd3b MS |
791 | (c)->cop_hints = (h); \ |
792 | } STMT_END | |
623e6609 | 793 | |
79072805 LW |
794 | /* |
795 | * Here we have some enormously heavy (or at least ponderous) wizardry. | |
796 | */ | |
797 | ||
798 | /* subroutine context */ | |
799 | struct block_sub { | |
f9c764c5 | 800 | OP * retop; /* op to execute on exit from sub */ |
5b6f7443 | 801 | I32 old_cxsubix; /* previous value of si_cxsubix */ |
adcbf118 | 802 | /* Above here is the same for sub, format and eval. */ |
9aa350eb | 803 | PAD *prevcomppad; /* the caller's PL_comppad */ |
8e663997 | 804 | CV * cv; |
f9c764c5 | 805 | /* Above here is the same for sub and format. */ |
bb172083 | 806 | I32 olddepth; |
3073d04f | 807 | AV *savearray; |
f9c764c5 NC |
808 | }; |
809 | ||
810 | ||
811 | /* format context */ | |
812 | struct block_format { | |
f39bc417 | 813 | OP * retop; /* op to execute on exit from sub */ |
5b6f7443 | 814 | I32 old_cxsubix; /* previous value of si_cxsubix */ |
adcbf118 | 815 | /* Above here is the same for sub, format and eval. */ |
9aa350eb | 816 | PAD *prevcomppad; /* the caller's PL_comppad */ |
8e663997 | 817 | CV * cv; |
f9c764c5 NC |
818 | /* Above here is the same for sub and format. */ |
819 | GV * gv; | |
820 | GV * dfoutgv; | |
79072805 LW |
821 | }; |
822 | ||
4ebe6e95 DM |
823 | /* return a pointer to the current context */ |
824 | ||
825 | #define CX_CUR() (&cxstack[cxstack_ix]) | |
826 | ||
dfe0f39b DM |
827 | /* free all savestack items back to the watermark of the specified context */ |
828 | ||
e992140c | 829 | #define CX_LEAVE_SCOPE(cx) LEAVE_SCOPE(cx->blk_oldsaveix) |
dfe0f39b | 830 | |
5da525e9 DM |
831 | #ifdef DEBUGGING |
832 | /* on debugging builds, poison cx afterwards so we know no code | |
833 | * uses it - because after doing cxstack_ix--, any ties, exceptions etc | |
834 | * may overwrite the current stack frame */ | |
835 | # define CX_POP(cx) \ | |
4ebe6e95 | 836 | assert(CX_CUR() == cx); \ |
5da525e9 DM |
837 | cxstack_ix--; \ |
838 | cx = NULL; | |
839 | #else | |
840 | # define CX_POP(cx) cxstack_ix--; | |
841 | #endif | |
842 | ||
490576d1 | 843 | #define CX_PUSHSUB_GET_LVALUE_MASK(func) \ |
1f4fbd3b MS |
844 | /* If the context is indeterminate, then only the lvalue */ \ |
845 | /* flags that the caller also has are applicable. */ \ | |
846 | ( \ | |
847 | (PL_op->op_flags & OPf_WANT) \ | |
848 | ? OPpENTERSUB_LVAL_MASK \ | |
849 | : !(PL_op->op_private & OPpENTERSUB_LVAL_MASK) \ | |
850 | ? 0 : (U8)func(aTHX) \ | |
851 | ) | |
4587c532 | 852 | |
fad386cb | 853 | /* Restore old @_ */ |
b405d38b | 854 | #define CX_POP_SAVEARRAY(cx) \ |
6d4ff0d2 | 855 | STMT_START { \ |
f2b9631d | 856 | AV *cx_pop_savearray_av = GvAV(PL_defgv); \ |
1f4fbd3b | 857 | GvAV(PL_defgv) = cx->blk_sub.savearray; \ |
656457d0 | 858 | cx->blk_sub.savearray = NULL; \ |
f2b9631d | 859 | SvREFCNT_dec(cx_pop_savearray_av); \ |
6d4ff0d2 | 860 | } STMT_END |
6d4ff0d2 | 861 | |
ecf8e9dd | 862 | /* junk in @_ spells trouble when cloning CVs and in pp_caller(), so don't |
8e09340b GS |
863 | * leave any (a fast av_clear(ary), basically) */ |
864 | #define CLEAR_ARGARRAY(ary) \ | |
865 | STMT_START { \ | |
1f4fbd3b MS |
866 | AvMAX(ary) += AvARRAY(ary) - AvALLOC(ary); \ |
867 | AvARRAY(ary) = AvALLOC(ary); \ | |
868 | AvFILLp(ary) = -1; \ | |
8e09340b | 869 | } STMT_END |
7766f137 | 870 | |
7e27c4a5 | 871 | |
79072805 LW |
872 | /* eval context */ |
873 | struct block_eval { | |
8e663997 | 874 | OP * retop; /* op to execute on exit from eval */ |
5b6f7443 | 875 | I32 old_cxsubix; /* previous value of si_cxsubix */ |
8e663997 | 876 | /* Above here is the same for sub, format and eval. */ |
0f79a09d | 877 | SV * old_namesv; |
79072805 | 878 | OP * old_eval_root; |
748a9306 | 879 | SV * cur_text; |
2090ab20 | 880 | CV * cv; |
abd70938 | 881 | JMPENV * cur_top_env; /* value of PL_top_env when eval CX created */ |
79072805 LW |
882 | }; |
883 | ||
17347a51 NC |
884 | /* If we ever need more than 512 op types, change the shift from 7. |
885 | blku_gimme is actually also only 2 bits, so could be merged with something. | |
886 | */ | |
887 | ||
4c57ced5 DM |
888 | /* blk_u16 bit usage for eval contexts: */ |
889 | ||
890 | #define CxOLD_IN_EVAL(cx) (((cx)->blk_u16) & 0x3F) /* saved PL in_eval */ | |
891 | #define CxEVAL_TXT_REFCNTED(cx) (((cx)->blk_u16) & 0x40) /* cur_text rc++ */ | |
892 | #define CxOLD_OP_TYPE(cx) (((cx)->blk_u16) >> 7) /* type of eval op */ | |
85a64632 | 893 | |
79072805 LW |
894 | /* loop context */ |
895 | struct block_loop { | |
022eaa24 | 896 | LOOP * my_op; /* My op, that contains redo, next and last ops. */ |
df530c37 | 897 | union { /* different ways of locating the iteration variable */ |
1f4fbd3b MS |
898 | SV **svp; /* for lexicals: address of pad slot */ |
899 | GV *gv; /* for package vars */ | |
df530c37 | 900 | } itervar_u; |
f0bb9bf7 | 901 | SV *itersave; /* the original iteration var */ |
493b0a3c | 902 | union { |
1f4fbd3b MS |
903 | struct { /* CXt_LOOP_ARY, C<for (@ary)> */ |
904 | AV *ary; /* array being iterated over */ | |
905 | IV ix; /* index relative to base of array */ | |
906 | } ary; | |
907 | struct { /* CXt_LOOP_LIST, C<for (list)> */ | |
908 | I32 basesp; /* first element of list on stack */ | |
909 | IV ix; /* index relative to basesp */ | |
910 | } stack; | |
911 | struct { /* CXt_LOOP_LAZYIV, C<for (1..9)> */ | |
912 | IV cur; | |
913 | IV end; | |
914 | } lazyiv; | |
915 | struct { /* CXt_LOOP_LAZYSV C<for ('a'..'z')> */ | |
916 | SV * cur; | |
917 | SV * end; /* maxiumum value (or minimum in reverse) */ | |
918 | } lazysv; | |
d01136d6 | 919 | } state_u; |
7766f137 | 920 | #ifdef USE_ITHREADS |
b52de964 | 921 | PAD *oldcomppad; /* needed to map itervar_u.svp during thread clone */ |
f83b46a0 | 922 | #endif |
b52de964 | 923 | }; |
f83b46a0 | 924 | |
9af0df0b DM |
925 | #define CxITERVAR(c) \ |
926 | (CxPADLOOP(c) \ | |
927 | ? (c)->blk_loop.itervar_u.svp \ | |
928 | : ((c)->cx_type & CXp_FOR_GV) \ | |
929 | ? &GvSV((c)->blk_loop.itervar_u.gv) \ | |
930 | : (SV **)&(c)->blk_loop.itervar_u.gv) | |
f83b46a0 | 931 | |
a8b89476 TC |
932 | #define CxLABEL(c) (CopLABEL((c)->blk_oldcop)) |
933 | #define CxLABEL_len(c,len) (CopLABEL_len((c)->blk_oldcop, len)) | |
934 | #define CxLABEL_len_flags(c,len,flags) ((const char *)CopLABEL_len_flags((c)->blk_oldcop, len, flags)) | |
ae423868 | 935 | #define CxHASARGS(c) (((c)->cx_type & CXp_HASARGS) == CXp_HASARGS) |
d9203e03 DM |
936 | |
937 | /* CxLVAL(): the lval flags of the call site: the relevant flag bits from | |
938 | * the op_private field of the calling pp_entersub (or its caller's caller | |
939 | * if the caller's lvalue context isn't known): | |
940 | * OPpLVAL_INTRO: sub used in lvalue context, e.g. f() = 1; | |
941 | * OPpENTERSUB_INARGS (in conjunction with OPpLVAL_INTRO): the | |
942 | * function is being used as a sub arg or as a referent, e.g. | |
943 | * g(...,f(),...) or $r = \f() | |
944 | * OPpDEREF: 2-bit mask indicating e.g. f()->[0]. | |
945 | * Note the contrast with CvLVALUE(), which is a property of the sub | |
946 | * rather than the call site. | |
947 | */ | |
fb1a3d54 | 948 | #define CxLVAL(c) (0 + ((U8)((c)->blk_u16))) |
1956db7e | 949 | |
7766f137 | 950 | |
79072805 | 951 | |
7896dde7 Z |
952 | /* given/when context */ |
953 | struct block_givwhen { | |
1f4fbd3b | 954 | OP *leave_op; |
7896dde7 | 955 | SV *defsv_save; /* the original $_ */ |
0d863452 RH |
956 | }; |
957 | ||
0d863452 | 958 | |
c5369ccc | 959 | |
79072805 LW |
960 | /* context common to subroutines, evals and loops */ |
961 | struct block { | |
446d0787 | 962 | U8 blku_type; /* what kind of context this is */ |
2e0df0e8 | 963 | U8 blku_gimme; /* is this block running in list context? */ |
17347a51 | 964 | U16 blku_u16; /* used by block_sub and block_eval (so far) */ |
e992140c DM |
965 | I32 blku_oldsaveix; /* saved PL_savestack_ix */ |
966 | /* all the fields above must be aligned with same-sized fields as sbu */ | |
1bef65a2 | 967 | I32 blku_oldsp; /* current sp floor: where nextstate pops to */ |
79072805 | 968 | I32 blku_oldmarksp; /* mark stack index */ |
f284fecd | 969 | COP * blku_oldcop; /* old curcop pointer */ |
79072805 | 970 | PMOP * blku_oldpm; /* values of pattern match vars */ |
bb3300d1 | 971 | SSize_t blku_old_tmpsfloor; /* saved PL_tmps_floor */ |
f284fecd | 972 | I32 blku_oldscopesp; /* scope stack index */ |
79072805 LW |
973 | |
974 | union { | |
1f4fbd3b MS |
975 | struct block_sub blku_sub; |
976 | struct block_format blku_format; | |
977 | struct block_eval blku_eval; | |
978 | struct block_loop blku_loop; | |
979 | struct block_givwhen blku_givwhen; | |
79072805 LW |
980 | } blk_u; |
981 | }; | |
982 | #define blk_oldsp cx_u.cx_blk.blku_oldsp | |
983 | #define blk_oldcop cx_u.cx_blk.blku_oldcop | |
79072805 LW |
984 | #define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp |
985 | #define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp | |
986 | #define blk_oldpm cx_u.cx_blk.blku_oldpm | |
987 | #define blk_gimme cx_u.cx_blk.blku_gimme | |
446d0787 | 988 | #define blk_u16 cx_u.cx_blk.blku_u16 |
e992140c | 989 | #define blk_oldsaveix cx_u.cx_blk.blku_oldsaveix |
ce8bb8d8 | 990 | #define blk_old_tmpsfloor cx_u.cx_blk.blku_old_tmpsfloor |
79072805 | 991 | #define blk_sub cx_u.cx_blk.blk_u.blku_sub |
f9c764c5 | 992 | #define blk_format cx_u.cx_blk.blk_u.blku_format |
79072805 LW |
993 | #define blk_eval cx_u.cx_blk.blk_u.blku_eval |
994 | #define blk_loop cx_u.cx_blk.blk_u.blku_loop | |
7896dde7 | 995 | #define blk_givwhen cx_u.cx_blk.blk_u.blku_givwhen |
79072805 | 996 | |
5e691bc6 | 997 | #define CX_DEBUG(cx, action) \ |
d9f81b50 | 998 | DEBUG_l( \ |
f31da5f4 | 999 | Perl_deb(aTHX_ "CX %ld %s %s (scope %ld,%ld) (save %ld,%ld) in %s at %s:%d\n",\ |
1f4fbd3b MS |
1000 | (long)cxstack_ix, \ |
1001 | action, \ | |
1002 | PL_block_type[CxTYPE(cx)], \ | |
1003 | (long)PL_scopestack_ix, \ | |
1004 | (long)(cx->blk_oldscopesp), \ | |
1005 | (long)PL_savestack_ix, \ | |
1006 | (long)(cx->blk_oldsaveix), \ | |
f31da5f4 | 1007 | SAFE_FUNCTION__, __FILE__, __LINE__)); |
1c98cc53 | 1008 | |
ed8ff0f3 | 1009 | |
79072805 LW |
1010 | |
1011 | /* substitution context */ | |
1012 | struct subst { | |
e992140c | 1013 | U8 sbu_type; /* same as blku_type */ |
1ed74d04 | 1014 | U8 sbu_rflags; |
e992140c DM |
1015 | U16 sbu_rxtainted; |
1016 | I32 sbu_oldsaveix; /* same as blku_oldsaveix */ | |
a7f94955 | 1017 | /* all the fields above must be aligned with same-sized fields as blk_u */ |
3c6ef0a5 FC |
1018 | SSize_t sbu_iters; |
1019 | SSize_t sbu_maxiters; | |
79072805 LW |
1020 | char * sbu_orig; |
1021 | SV * sbu_dstr; | |
1022 | SV * sbu_targ; | |
1023 | char * sbu_s; | |
1024 | char * sbu_m; | |
1025 | char * sbu_strend; | |
c90c0ff4 | 1026 | void * sbu_rxres; |
c07a80fd | 1027 | REGEXP * sbu_rx; |
79072805 | 1028 | }; |
a0412c00 KW |
1029 | |
1030 | #ifdef PERL_CORE | |
1031 | ||
79072805 LW |
1032 | #define sb_iters cx_u.cx_subst.sbu_iters |
1033 | #define sb_maxiters cx_u.cx_subst.sbu_maxiters | |
22e551b9 | 1034 | #define sb_rflags cx_u.cx_subst.sbu_rflags |
71be2cbc | 1035 | #define sb_rxtainted cx_u.cx_subst.sbu_rxtainted |
79072805 LW |
1036 | #define sb_orig cx_u.cx_subst.sbu_orig |
1037 | #define sb_dstr cx_u.cx_subst.sbu_dstr | |
1038 | #define sb_targ cx_u.cx_subst.sbu_targ | |
1039 | #define sb_s cx_u.cx_subst.sbu_s | |
1040 | #define sb_m cx_u.cx_subst.sbu_m | |
1041 | #define sb_strend cx_u.cx_subst.sbu_strend | |
c90c0ff4 | 1042 | #define sb_rxres cx_u.cx_subst.sbu_rxres |
c07a80fd | 1043 | #define sb_rx cx_u.cx_subst.sbu_rx |
79072805 | 1044 | |
490576d1 | 1045 | # define CX_PUSHSUBST(cx) CXINC, cx = CX_CUR(), \ |
1f4fbd3b MS |
1046 | cx->blk_oldsaveix = oldsave, \ |
1047 | cx->sb_iters = iters, \ | |
1048 | cx->sb_maxiters = maxiters, \ | |
1049 | cx->sb_rflags = r_flags, \ | |
1050 | cx->sb_rxtainted = rxtainted, \ | |
1051 | cx->sb_orig = orig, \ | |
1052 | cx->sb_dstr = dstr, \ | |
1053 | cx->sb_targ = targ, \ | |
1054 | cx->sb_s = s, \ | |
1055 | cx->sb_m = m, \ | |
1056 | cx->sb_strend = strend, \ | |
1057 | cx->sb_rxres = NULL, \ | |
1058 | cx->sb_rx = rx, \ | |
1059 | cx->cx_type = CXt_SUBST | (once ? CXp_ONCE : 0); \ | |
1060 | rxres_save(&cx->sb_rxres, rx); \ | |
1061 | (void)ReREFCNT_inc(rx); \ | |
cf69025f | 1062 | SvREFCNT_inc_void_NN(targ) |
79072805 | 1063 | |
b405d38b | 1064 | # define CX_POPSUBST(cx) \ |
77189b8c DM |
1065 | STMT_START { \ |
1066 | REGEXP *re; \ | |
e4592eb4 | 1067 | assert(CxTYPE(cx) == CXt_SUBST); \ |
1f4fbd3b MS |
1068 | rxres_free(&cx->sb_rxres); \ |
1069 | re = cx->sb_rx; \ | |
1070 | cx->sb_rx = NULL; \ | |
1071 | ReREFCNT_dec(re); \ | |
77189b8c DM |
1072 | SvREFCNT_dec_NN(cx->sb_targ); \ |
1073 | } STMT_END | |
9c105995 NC |
1074 | #endif |
1075 | ||
1076 | #define CxONCE(cx) ((cx)->cx_type & CXp_ONCE) | |
79072805 LW |
1077 | |
1078 | struct context { | |
79072805 | 1079 | union { |
1f4fbd3b MS |
1080 | struct block cx_blk; |
1081 | struct subst cx_subst; | |
79072805 LW |
1082 | } cx_u; |
1083 | }; | |
2e0df0e8 | 1084 | #define cx_type cx_u.cx_subst.sbu_type |
6b35e009 | 1085 | |
76753e7f NC |
1086 | /* If you re-order these, there is also an array of uppercase names in perl.h |
1087 | and a static array of context names in pp_ctl.c */ | |
3701055e | 1088 | #define CXTYPEMASK 0xf |
79646418 | 1089 | #define CXt_NULL 0 /* currently only used for sort BLOCK */ |
7896dde7 | 1090 | #define CXt_WHEN 1 |
76753e7f | 1091 | #define CXt_BLOCK 2 |
7896dde7 Z |
1092 | /* When micro-optimising :-) keep GIVEN next to the LOOPs, as these 5 share a |
1093 | jump table in pp_ctl.c | |
1094 | The first 4 don't have a 'case' in at least one switch statement in pp_ctl.c | |
1095 | */ | |
1096 | #define CXt_GIVEN 3 | |
1097 | ||
1098 | /* be careful of the ordering of these five. Macros like CxTYPE_is_LOOP, | |
93661e56 | 1099 | * CxFOREACH compare ranges */ |
c349b9a0 DM |
1100 | #define CXt_LOOP_ARY 4 /* for (@ary) { ...; } */ |
1101 | #define CXt_LOOP_LAZYSV 5 /* for ('a'..'z') { ...; } */ | |
1102 | #define CXt_LOOP_LAZYIV 6 /* for (1..9) { ...; } */ | |
1103 | #define CXt_LOOP_LIST 7 /* for (1,2,3) { ...; } */ | |
1104 | #define CXt_LOOP_PLAIN 8 /* while (...) { ...; } | |
1105 | or plain block { ...; } */ | |
93661e56 DM |
1106 | #define CXt_SUB 9 |
1107 | #define CXt_FORMAT 10 | |
a1325b90 | 1108 | #define CXt_EVAL 11 /* eval'', eval{}, try{} */ |
93661e56 | 1109 | #define CXt_SUBST 12 |
f79e2ff9 | 1110 | #define CXt_DEFER 13 |
76753e7f | 1111 | /* SUBST doesn't feature in all switch statements. */ |
79072805 | 1112 | |
ae423868 | 1113 | /* private flags for CXt_SUB and CXt_FORMAT */ |
79646418 DM |
1114 | #define CXp_MULTICALL 0x10 /* part of a multicall (so don't tear down |
1115 | context on exit). (not CXt_FORMAT) */ | |
1f27d788 | 1116 | #define CXp_HASARGS 0x20 |
b0065247 DM |
1117 | #define CXp_SUB_RE 0x40 /* code called within regex, i.e. (?{}) */ |
1118 | #define CXp_SUB_RE_FAKE 0x80 /* fake sub CX for (?{}) in current scope */ | |
ae423868 | 1119 | |
6b35e009 | 1120 | /* private flags for CXt_EVAL */ |
1f27d788 | 1121 | #define CXp_REAL 0x20 /* truly eval'', not a lookalike */ |
99dbf645 | 1122 | #define CXp_EVALBLOCK 0x40 /* eval{}, not eval'' or similar */ |
a1325b90 | 1123 | #define CXp_TRY 0x80 /* try {} block */ |
6b35e009 | 1124 | |
7766f137 | 1125 | /* private flags for CXt_LOOP */ |
28e0cf42 | 1126 | |
7896dde7 Z |
1127 | /* this is only set in conjunction with CXp_FOR_GV */ |
1128 | #define CXp_FOR_DEF 0x10 /* foreach using $_ */ | |
28e0cf42 | 1129 | /* these 3 are mutually exclusive */ |
d39c26a6 | 1130 | #define CXp_FOR_LVREF 0x20 /* foreach using \$var */ |
9af0df0b DM |
1131 | #define CXp_FOR_GV 0x40 /* foreach using package var */ |
1132 | #define CXp_FOR_PAD 0x80 /* foreach using lexical var */ | |
28e0cf42 | 1133 | |
9af0df0b | 1134 | #define CxPADLOOP(c) ((c)->cx_type & CXp_FOR_PAD) |
e846cb92 | 1135 | |
1ed74d04 NC |
1136 | /* private flags for CXt_SUBST */ |
1137 | #define CXp_ONCE 0x10 /* What was sbu_once in struct subst */ | |
7766f137 | 1138 | |
6b35e009 | 1139 | #define CxTYPE(c) ((c)->cx_type & CXTYPEMASK) |
7896dde7 | 1140 | #define CxTYPE_is_LOOP(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \ |
113ebcf5 | 1141 | && CxTYPE(cx) <= CXt_LOOP_PLAIN) |
79646418 | 1142 | #define CxMULTICALL(c) ((c)->cx_type & CXp_MULTICALL) |
0588a150 | 1143 | #define CxREALEVAL(c) (((c)->cx_type & (CXTYPEMASK|CXp_REAL)) \ |
1f4fbd3b | 1144 | == (CXt_EVAL|CXp_REAL)) |
99dbf645 | 1145 | #define CxEVALBLOCK(c) (((c)->cx_type & (CXTYPEMASK|CXp_EVALBLOCK)) \ |
1f4fbd3b | 1146 | == (CXt_EVAL|CXp_EVALBLOCK)) |
a1325b90 PE |
1147 | #define CxTRY(c) (((c)->cx_type & (CXTYPEMASK|CXp_TRY)) \ |
1148 | == (CXt_EVAL|CXp_TRY)) | |
113ebcf5 DM |
1149 | #define CxFOREACH(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \ |
1150 | && CxTYPE(cx) <= CXt_LOOP_LIST) | |
6b35e009 | 1151 | |
e5e291f5 PE |
1152 | /* private flags for CXt_DEFER */ |
1153 | #define CXp_FINALLY 0x20 /* `finally` block; semantically identical | |
1154 | * but matters for diagnostic messages */ | |
1155 | ||
99dbf645 PE |
1156 | /* deprecated old name before real try/catch was added */ |
1157 | #define CXp_TRYBLOCK CXp_EVALBLOCK | |
1158 | #define CxTRYBLOCK(c) CxEVALBLOCK(c) | |
1159 | ||
109bf713 | 1160 | #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc())) |
79072805 | 1161 | |
eb7e169e PE |
1162 | #define G_SCALAR 2 |
1163 | #define G_LIST 3 | |
1164 | #define G_VOID 1 | |
1165 | #define G_WANT 3 | |
1166 | ||
1167 | #ifndef PERL_CORE | |
1168 | /* name prior to 5.31.1 */ | |
1169 | # define G_ARRAY G_LIST | |
1170 | #endif | |
79072805 | 1171 | |
864dbfa3 | 1172 | /* extra flags for Perl_call_* routines */ |
66ff4fb5 | 1173 | #define G_DISCARD 0x4 /* Call FREETMPS. |
1f4fbd3b MS |
1174 | Don't change this without consulting the |
1175 | hash actions codes defined in hv.h */ | |
66ff4fb5 TC |
1176 | #define G_EVAL 0x8 /* Assume eval {} around subroutine call. */ |
1177 | #define G_NOARGS 0x10 /* Don't construct a @_ array. */ | |
1178 | #define G_KEEPERR 0x20 /* Warn for errors, don't overwrite $@ */ | |
1179 | #define G_NODEBUG 0x40 /* Disable debugging at toplevel. */ | |
1180 | #define G_METHOD 0x80 /* Calling method. */ | |
1181 | #define G_FAKINGEVAL 0x100 /* Faking an eval context for call_sv or | |
1f4fbd3b | 1182 | fold_constants. */ |
66ff4fb5 | 1183 | #define G_UNDEF_FILL 0x200 /* Fill the stack with &PL_sv_undef |
1f4fbd3b MS |
1184 | A special case for UNSHIFT in |
1185 | Perl_magic_methcall(). */ | |
66ff4fb5 | 1186 | #define G_WRITING_TO_STDERR 0x400 /* Perl_write_to_stderr() is calling |
1f4fbd3b | 1187 | Perl_magic_methcall(). */ |
66ff4fb5 TC |
1188 | #define G_RE_REPARSING 0x800 /* compiling a run-time /(?{..})/ */ |
1189 | #define G_METHOD_NAMED 0x1000 /* calling named method, eg without :: or ' */ | |
1190 | #define G_RETHROW 0x2000 /* eval_sv(): re-throw any error */ | |
e336de0d | 1191 | |
faef0170 HS |
1192 | /* flag bits for PL_in_eval */ |
1193 | #define EVAL_NULL 0 /* not in an eval */ | |
1194 | #define EVAL_INEVAL 1 /* some enclosing scope is an eval */ | |
1195 | #define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */ | |
864dbfa3 | 1196 | #define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */ |
6dc8a9e4 | 1197 | #define EVAL_INREQUIRE 8 /* The code is being required. */ |
a1941760 | 1198 | #define EVAL_RE_REPARSING 0x10 /* eval_sv() called with G_RE_REPARSING */ |
4c57ced5 | 1199 | /* if adding extra bits, make sure they can fit in CxOLD_OP_TYPE() */ |
faef0170 | 1200 | |
e336de0d GS |
1201 | /* Support for switching (stack and block) contexts. |
1202 | * This ensures magic doesn't invalidate local stack and cx pointers. | |
ec834f46 KW |
1203 | * Which one to use (or add) is mostly, but not completely arbitrary: See |
1204 | * http://nntp.perl.org/group/perl.perl5.porters/257169 | |
e336de0d GS |
1205 | */ |
1206 | ||
e788e7d3 GS |
1207 | #define PERLSI_UNKNOWN -1 |
1208 | #define PERLSI_UNDEF 0 | |
1209 | #define PERLSI_MAIN 1 | |
1210 | #define PERLSI_MAGIC 2 | |
1211 | #define PERLSI_SORT 3 | |
1212 | #define PERLSI_SIGNAL 4 | |
1213 | #define PERLSI_OVERLOAD 5 | |
1214 | #define PERLSI_DESTROY 6 | |
1215 | #define PERLSI_WARNHOOK 7 | |
1216 | #define PERLSI_DIEHOOK 8 | |
1217 | #define PERLSI_REQUIRE 9 | |
e1a10f35 | 1218 | #define PERLSI_MULTICALL 10 |
b6d9446c | 1219 | #define PERLSI_REGCOMP 11 |
e336de0d GS |
1220 | |
1221 | struct stackinfo { | |
1222 | AV * si_stack; /* stack for current runlevel */ | |
1223 | PERL_CONTEXT * si_cxstack; /* context stack for runlevel */ | |
9bd87817 NC |
1224 | struct stackinfo * si_prev; |
1225 | struct stackinfo * si_next; | |
e336de0d GS |
1226 | I32 si_cxix; /* current context index */ |
1227 | I32 si_cxmax; /* maximum allocated index */ | |
5b6f7443 | 1228 | I32 si_cxsubix; /* topmost sub/eval/format */ |
e336de0d | 1229 | I32 si_type; /* type of runlevel */ |
ce2f7c3b | 1230 | I32 si_markoff; /* offset where markstack begins for us. |
1f4fbd3b MS |
1231 | * currently used only with DEBUGGING, |
1232 | * but not #ifdef-ed for bincompat */ | |
1fced1a2 | 1233 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
87058c31 DM |
1234 | /* high water mark: for checking if the stack was correctly extended / |
1235 | * tested for extension by each pp function */ | |
1236 | SSize_t si_stack_hwm; | |
1237 | #endif | |
1238 | ||
e336de0d GS |
1239 | }; |
1240 | ||
55d39ade KW |
1241 | /* |
1242 | =for apidoc Ay||PERL_SI | |
1243 | Use this typedef to declare variables that are to hold C<struct stackinfo>. | |
1244 | ||
1245 | =cut | |
1246 | */ | |
e336de0d GS |
1247 | typedef struct stackinfo PERL_SI; |
1248 | ||
3280af22 NIS |
1249 | #define cxstack (PL_curstackinfo->si_cxstack) |
1250 | #define cxstack_ix (PL_curstackinfo->si_cxix) | |
1251 | #define cxstack_max (PL_curstackinfo->si_cxmax) | |
e336de0d GS |
1252 | |
1253 | #ifdef DEBUGGING | |
ce2f7c3b GS |
1254 | # define SET_MARK_OFFSET \ |
1255 | PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack | |
e336de0d | 1256 | #else |
ce2f7c3b | 1257 | # define SET_MARK_OFFSET NOOP |
e336de0d GS |
1258 | #endif |
1259 | ||
87058c31 | 1260 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
45d6bfc0 | 1261 | # define PUSHSTACK_INIT_HWM(si) ((si)->si_stack_hwm = 0) |
87058c31 DM |
1262 | #else |
1263 | # define PUSHSTACK_INIT_HWM(si) NOOP | |
1264 | #endif | |
1265 | ||
d3acc0f7 | 1266 | #define PUSHSTACKi(type) \ |
e336de0d | 1267 | STMT_START { \ |
1f4fbd3b MS |
1268 | PERL_SI *next = PL_curstackinfo->si_next; \ |
1269 | DEBUG_l({ \ | |
1270 | int i = 0; PERL_SI *p = PL_curstackinfo; \ | |
1271 | while (p) { i++; p = p->si_prev; } \ | |
f31da5f4 YO |
1272 | Perl_deb(aTHX_ "push STACKINFO %d in %s at %s:%d\n", \ |
1273 | i, SAFE_FUNCTION__, __FILE__, __LINE__);}) \ | |
1f4fbd3b MS |
1274 | if (!next) { \ |
1275 | next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \ | |
1276 | next->si_prev = PL_curstackinfo; \ | |
1277 | PL_curstackinfo->si_next = next; \ | |
1278 | } \ | |
1279 | next->si_type = type; \ | |
1280 | next->si_cxix = -1; \ | |
1281 | next->si_cxsubix = -1; \ | |
87058c31 | 1282 | PUSHSTACK_INIT_HWM(next); \ |
1f4fbd3b MS |
1283 | AvFILLp(next->si_stack) = 0; \ |
1284 | SWITCHSTACK(PL_curstack,next->si_stack); \ | |
1285 | PL_curstackinfo = next; \ | |
1286 | SET_MARK_OFFSET; \ | |
e336de0d GS |
1287 | } STMT_END |
1288 | ||
e788e7d3 | 1289 | #define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN) |
d3acc0f7 | 1290 | |
3095d977 GS |
1291 | /* POPSTACK works with PL_stack_sp, so it may need to be bracketed by |
1292 | * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */ | |
d3acc0f7 | 1293 | #define POPSTACK \ |
e336de0d | 1294 | STMT_START { \ |
1f4fbd3b MS |
1295 | dSP; \ |
1296 | PERL_SI * const prev = PL_curstackinfo->si_prev; \ | |
1297 | DEBUG_l({ \ | |
1298 | int i = -1; PERL_SI *p = PL_curstackinfo; \ | |
1299 | while (p) { i++; p = p->si_prev; } \ | |
f31da5f4 YO |
1300 | Perl_deb(aTHX_ "pop STACKINFO %d in %s at %s:%d\n", \ |
1301 | i, SAFE_FUNCTION__, __FILE__, __LINE__);}) \ | |
1f4fbd3b MS |
1302 | if (!prev) { \ |
1303 | Perl_croak_popstack(); \ | |
1304 | } \ | |
1305 | SWITCHSTACK(PL_curstack,prev->si_stack); \ | |
1306 | /* don't free prev here, free them all at the END{} */ \ | |
1307 | PL_curstackinfo = prev; \ | |
e336de0d GS |
1308 | } STMT_END |
1309 | ||
1310 | #define POPSTACK_TO(s) \ | |
1311 | STMT_START { \ | |
1f4fbd3b MS |
1312 | while (PL_curstack != s) { \ |
1313 | dounwind(-1); \ | |
1314 | POPSTACK; \ | |
1315 | } \ | |
e336de0d | 1316 | } STMT_END |
923e4eb5 | 1317 | |
d57dfc83 | 1318 | /* |
3f620621 | 1319 | =for apidoc_section $utility |
d57dfc83 KW |
1320 | =for apidoc Amn|bool|IN_PERL_COMPILETIME |
1321 | Returns 1 if this macro is being called during the compilation phase of the | |
1322 | program; otherwise 0; | |
ed8ff0f3 | 1323 | |
d57dfc83 KW |
1324 | =for apidoc Amn|bool|IN_PERL_RUNTIME |
1325 | Returns 1 if this macro is being called during the execution phase of the | |
1326 | program; otherwise 0; | |
ed8ff0f3 | 1327 | |
d57dfc83 KW |
1328 | =cut |
1329 | */ | |
1330 | #define IN_PERL_COMPILETIME cBOOL(PL_curcop == &PL_compiling) | |
1331 | #define IN_PERL_RUNTIME cBOOL(PL_curcop != &PL_compiling) | |
ed8ff0f3 | 1332 | |
9850bf21 | 1333 | /* |
3f620621 | 1334 | =for apidoc_section $multicall |
9850bf21 | 1335 | |
3734d2f5 | 1336 | =for apidoc Amn;||dMULTICALL |
72d33970 | 1337 | Declare local variables for a multicall. See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 | 1338 | |
3734d2f5 | 1339 | =for apidoc Am;||PUSH_MULTICALL|CV* the_cv |
9850bf21 | 1340 | Opening bracket for a lightweight callback. |
0eda4409 | 1341 | See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 | 1342 | |
3734d2f5 | 1343 | =for apidoc Amn;||MULTICALL |
72d33970 | 1344 | Make a lightweight callback. See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 | 1345 | |
3734d2f5 | 1346 | =for apidoc Amn;||POP_MULTICALL |
9850bf21 | 1347 | Closing bracket for a lightweight callback. |
0eda4409 | 1348 | See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 RH |
1349 | |
1350 | =cut | |
1351 | */ | |
1352 | ||
1353 | #define dMULTICALL \ | |
79503099 | 1354 | OP *multicall_cop; \ |
35095fd0 | 1355 | bool multicall_oldcatch |
9850bf21 | 1356 | |
82f35e8b | 1357 | #define PUSH_MULTICALL(the_cv) \ |
b0065247 | 1358 | PUSH_MULTICALL_FLAGS(the_cv, 0) |
81ed78b2 | 1359 | |
b0065247 DM |
1360 | /* Like PUSH_MULTICALL, but allows you to specify extra flags |
1361 | * for the CX stack entry (this isn't part of the public API) */ | |
81ed78b2 | 1362 | |
b0065247 | 1363 | #define PUSH_MULTICALL_FLAGS(the_cv, flags) \ |
9850bf21 | 1364 | STMT_START { \ |
79503099 | 1365 | PERL_CONTEXT *cx; \ |
1f4fbd3b MS |
1366 | CV * const _nOnclAshIngNamE_ = the_cv; \ |
1367 | CV * const cv = _nOnclAshIngNamE_; \ | |
1368 | PADLIST * const padlist = CvPADLIST(cv); \ | |
1369 | multicall_oldcatch = CATCH_GET; \ | |
1370 | CATCH_SET(TRUE); \ | |
1371 | PUSHSTACKi(PERLSI_MULTICALL); \ | |
1372 | cx = cx_pushblock((CXt_SUB|CXp_MULTICALL|flags), (U8)gimme, \ | |
fd9b7244 | 1373 | PL_stack_sp, PL_savestack_ix); \ |
79503099 | 1374 | cx_pushsub(cx, cv, NULL, 0); \ |
1f4fbd3b | 1375 | SAVEOP(); \ |
b0065247 DM |
1376 | if (!(flags & CXp_SUB_RE_FAKE)) \ |
1377 | CvDEPTH(cv)++; \ | |
1f4fbd3b MS |
1378 | if (CvDEPTH(cv) >= 2) \ |
1379 | Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \ | |
1380 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \ | |
1381 | multicall_cop = CvSTART(cv); \ | |
9850bf21 RH |
1382 | } STMT_END |
1383 | ||
1384 | #define MULTICALL \ | |
1385 | STMT_START { \ | |
1f4fbd3b MS |
1386 | PL_op = multicall_cop; \ |
1387 | CALLRUNOPS(aTHX); \ | |
9850bf21 RH |
1388 | } STMT_END |
1389 | ||
1390 | #define POP_MULTICALL \ | |
1391 | STMT_START { \ | |
79503099 | 1392 | PERL_CONTEXT *cx; \ |
1f4fbd3b MS |
1393 | cx = CX_CUR(); \ |
1394 | CX_LEAVE_SCOPE(cx); \ | |
a73d8813 | 1395 | cx_popsub_common(cx); \ |
4df352a8 | 1396 | gimme = cx->blk_gimme; \ |
7e27c4a5 | 1397 | PERL_UNUSED_VAR(gimme); /* for API */ \ |
1f4fbd3b MS |
1398 | cx_popblock(cx); \ |
1399 | CX_POP(cx); \ | |
1400 | POPSTACK; \ | |
1401 | CATCH_SET(multicall_oldcatch); \ | |
1402 | SPAGAIN; \ | |
9850bf21 | 1403 | } STMT_END |
b3ca2e83 | 1404 | |
81ed78b2 DM |
1405 | /* Change the CV of an already-pushed MULTICALL CxSUB block. |
1406 | * (this isn't part of the public API) */ | |
1407 | ||
b0065247 | 1408 | #define CHANGE_MULTICALL_FLAGS(the_cv, flags) \ |
81ed78b2 | 1409 | STMT_START { \ |
1f4fbd3b MS |
1410 | CV * const _nOnclAshIngNamE_ = the_cv; \ |
1411 | CV * const cv = _nOnclAshIngNamE_; \ | |
1412 | PADLIST * const padlist = CvPADLIST(cv); \ | |
79503099 | 1413 | PERL_CONTEXT *cx = CX_CUR(); \ |
1f4fbd3b | 1414 | assert(CxMULTICALL(cx)); \ |
a73d8813 | 1415 | cx_popsub_common(cx); \ |
1f4fbd3b | 1416 | cx->cx_type = (CXt_SUB|CXp_MULTICALL|flags); \ |
79503099 | 1417 | cx_pushsub(cx, cv, NULL, 0); \ |
b0065247 DM |
1418 | if (!(flags & CXp_SUB_RE_FAKE)) \ |
1419 | CvDEPTH(cv)++; \ | |
1f4fbd3b MS |
1420 | if (CvDEPTH(cv) >= 2) \ |
1421 | Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \ | |
1422 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \ | |
1423 | multicall_cop = CvSTART(cv); \ | |
81ed78b2 | 1424 | } STMT_END |
b3ca2e83 | 1425 | /* |
14d04a33 | 1426 | * ex: set ts=8 sts=4 sw=4 et: |
b3ca2e83 | 1427 | */ |