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