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