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