1 #line 2 "op.c" 2 /* op.c 3 * 4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others 6 * 7 * You may distribute under the terms of either the GNU General Public 8 * License or the Artistic License, as specified in the README file. 9 * 10 */ 11 12 /* 13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was 14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the 15 * youngest of the Old Took's daughters); and Mr. Drogo was his second 16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed 17 * either way, as the saying is, if you follow me.' --the Gaffer 18 * 19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] 20 */ 21 22 /* This file contains the functions that create, manipulate and optimize 23 * the OP structures that hold a compiled perl program. 24 * 25 * Note that during the build of miniperl, a temporary copy of this file 26 * is made, called opmini.c. 27 * 28 * A Perl program is compiled into a tree of OP nodes. Each op contains: 29 * * structural OP pointers to its children and siblings (op_sibling, 30 * op_first etc) that define the tree structure; 31 * * execution order OP pointers (op_next, plus sometimes op_other, 32 * op_lastop etc) that define the execution sequence plus variants; 33 * * a pointer to the C "pp" function that would execute the op; 34 * * any data specific to that op. 35 * For example, an OP_CONST op points to the pp_const() function and to an 36 * SV containing the constant value. When pp_const() is executed, its job 37 * is to push that SV onto the stack. 38 * 39 * OPs are mainly created by the newFOO() functions, which are mainly 40 * called from the parser (in perly.y) as the code is parsed. For example 41 * the Perl code $a + $b * $c would cause the equivalent of the following 42 * to be called (oversimplifying a bit): 43 * 44 * newBINOP(OP_ADD, flags, 45 * newSVREF($a), 46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c)) 47 * ) 48 * 49 * As the parser reduces low-level rules, it creates little op subtrees; 50 * as higher-level rules are resolved, these subtrees get joined together 51 * as branches on a bigger subtree, until eventually a top-level rule like 52 * a subroutine definition is reduced, at which point there is one large 53 * parse tree left. 54 * 55 * The execution order pointers (op_next) are generated as the subtrees 56 * are joined together. Consider this sub-expression: A*B + C/D: at the 57 * point when it's just been parsed, the op tree looks like: 58 * 59 * [+] 60 * | 61 * [*]------[/] 62 * | | 63 * A---B C---D 64 * 65 * with the intended execution order being: 66 * 67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT] 68 * 69 * At this point all the nodes' op_next pointers will have been set, 70 * except that: 71 * * we don't know what the [NEXT] node will be yet; 72 * * we don't know what the [PREV] node will be yet, but when it gets 73 * created and needs its op_next set, it needs to be set to point to 74 * A, which is non-obvious. 75 * To handle both those cases, we temporarily set the top node's 76 * op_next to point to the first node to be executed in this subtree (A in 77 * this case). This means that initially a subtree's op_next chain, 78 * starting from the top node, will visit each node in execution sequence 79 * then point back at the top node. 80 * When we embed this subtree in a larger tree, its top op_next is used 81 * to get the start node, then is set to point to its new neighbour. 82 * For example the two separate [*],A,B and [/],C,D subtrees would 83 * initially have had: 84 * [*] => A; A => B; B => [*] 85 * and 86 * [/] => C; C => D; D => [/] 87 * When these two subtrees were joined together to make the [+] subtree, 88 * [+]'s op_next was set to [*]'s op_next, i.e. A; then [*]'s op_next was 89 * set to point to [/]'s op_next, i.e. C. 90 * 91 * This op_next linking is done by the LINKLIST() macro and its underlying 92 * op_linklist() function. Given a top-level op, if its op_next is 93 * non-null, it's already been linked, so leave it. Otherwise link it with 94 * its children as described above, possibly recursively if any of the 95 * children have a null op_next. 96 * 97 * In summary: given a subtree, its top-level node's op_next will either 98 * be: 99 * NULL: the subtree hasn't been LINKLIST()ed yet; 100 * fake: points to the start op for this subtree; 101 * real: once the subtree has been embedded into a larger tree 102 */ 103 104 /* 105 106 Here's an older description from Larry. 107 108 Perl's compiler is essentially a 3-pass compiler with interleaved phases: 109 110 A bottom-up pass 111 A top-down pass 112 An execution-order pass 113 114 The bottom-up pass is represented by all the "newOP" routines and 115 the ck_ routines. The bottom-upness is actually driven by yacc. 116 So at the point that a ck_ routine fires, we have no idea what the 117 context is, either upward in the syntax tree, or either forward or 118 backward in the execution order. (The bottom-up parser builds that 119 part of the execution order it knows about, but if you follow the "next" 120 links around, you'll find it's actually a closed loop through the 121 top level node.) 122 123 Whenever the bottom-up parser gets to a node that supplies context to 124 its components, it invokes that portion of the top-down pass that applies 125 to that part of the subtree (and marks the top node as processed, so 126 if a node further up supplies context, it doesn't have to take the 127 plunge again). As a particular subcase of this, as the new node is 128 built, it takes all the closed execution loops of its subcomponents 129 and links them into a new closed loop for the higher level node. But 130 it's still not the real execution order. 131 132 The actual execution order is not known till we get a grammar reduction 133 to a top-level unit like a subroutine or file that will be called by 134 "name" rather than via a "next" pointer. At that point, we can call 135 into peep() to do that code's portion of the 3rd pass. It has to be 136 recursive, but it's recursive on basic blocks, not on tree nodes. 137 */ 138 139 /* To implement user lexical pragmas, there needs to be a way at run time to 140 get the compile time state of %^H for that block. Storing %^H in every 141 block (or even COP) would be very expensive, so a different approach is 142 taken. The (running) state of %^H is serialised into a tree of HE-like 143 structs. Stores into %^H are chained onto the current leaf as a struct 144 refcounted_he * with the key and the value. Deletes from %^H are saved 145 with a value of PL_sv_placeholder. The state of %^H at any point can be 146 turned back into a regular HV by walking back up the tree from that point's 147 leaf, ignoring any key you've already seen (placeholder or not), storing 148 the rest into the HV structure, then removing the placeholders. Hence 149 memory is only used to store the %^H deltas from the enclosing COP, rather 150 than the entire %^H on each COP. 151 152 To cause actions on %^H to write out the serialisation records, it has 153 magic type 'H'. This magic (itself) does nothing, but its presence causes 154 the values to gain magic type 'h', which has entries for set and clear. 155 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store 156 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS> 157 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that 158 it will be correctly restored when any inner compiling scope is exited. 159 */ 160 161 #include "EXTERN.h" 162 #define PERL_IN_OP_C 163 #include "perl.h" 164 #include "keywords.h" 165 #include "feature.h" 166 #include "regcomp.h" 167 #include "invlist_inline.h" 168 169 #define CALL_PEEP(o) PL_peepp(aTHX_ o) 170 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o) 171 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o) 172 173 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar"; 174 175 /* remove any leading "empty" ops from the op_next chain whose first 176 * node's address is stored in op_p. Store the updated address of the 177 * first node in op_p. 178 */ 179 180 STATIC void 181 S_prune_chain_head(OP** op_p) 182 { 183 while (*op_p 184 && ( (*op_p)->op_type == OP_NULL 185 || (*op_p)->op_type == OP_SCOPE 186 || (*op_p)->op_type == OP_SCALAR 187 || (*op_p)->op_type == OP_LINESEQ) 188 ) 189 *op_p = (*op_p)->op_next; 190 } 191 192 193 /* See the explanatory comments above struct opslab in op.h. */ 194 195 #ifdef PERL_DEBUG_READONLY_OPS 196 # define PERL_SLAB_SIZE 128 197 # define PERL_MAX_SLAB_SIZE 4096 198 # include <sys/mman.h> 199 #endif 200 201 #ifndef PERL_SLAB_SIZE 202 # define PERL_SLAB_SIZE 64 203 #endif 204 #ifndef PERL_MAX_SLAB_SIZE 205 # define PERL_MAX_SLAB_SIZE 2048 206 #endif 207 208 /* rounds up to nearest pointer */ 209 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *)) 210 211 #define DIFF(o,p) \ 212 (assert(((char *)(p) - (char *)(o)) % sizeof(I32**) == 0), \ 213 ((size_t)((I32 **)(p) - (I32**)(o)))) 214 215 /* requires double parens and aTHX_ */ 216 #define DEBUG_S_warn(args) \ 217 DEBUG_S( \ 218 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \ 219 ) 220 221 /* opslot_size includes the size of the slot header, and an op can't be smaller than BASEOP */ 222 #define OPSLOT_SIZE_BASE (SIZE_TO_PSIZE(sizeof(OPSLOT))) 223 224 /* the number of bytes to allocate for a slab with sz * sizeof(I32 **) space for op */ 225 #define OpSLABSizeBytes(sz) \ 226 ((sz) * sizeof(I32 *) + STRUCT_OFFSET(OPSLAB, opslab_slots)) 227 228 /* malloc a new op slab (suitable for attaching to PL_compcv). 229 * sz is in units of pointers from the beginning of opslab_opslots */ 230 231 static OPSLAB * 232 S_new_slab(pTHX_ OPSLAB *head, size_t sz) 233 { 234 OPSLAB *slab; 235 size_t sz_bytes = OpSLABSizeBytes(sz); 236 237 /* opslot_offset is only U16 */ 238 assert(sz < U16_MAX); 239 /* room for at least one op */ 240 assert(sz >= OPSLOT_SIZE_BASE); 241 242 #ifdef PERL_DEBUG_READONLY_OPS 243 slab = (OPSLAB *) mmap(0, sz_bytes, 244 PROT_READ|PROT_WRITE, 245 MAP_ANON|MAP_PRIVATE, -1, 0); 246 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n", 247 (unsigned long) sz, slab)); 248 if (slab == MAP_FAILED) { 249 perror("mmap failed"); 250 abort(); 251 } 252 #else 253 slab = (OPSLAB *)PerlMemShared_malloc(sz_bytes); 254 Zero(slab, sz_bytes, char); 255 #endif 256 slab->opslab_size = (U16)sz; 257 258 #ifndef WIN32 259 /* The context is unused in non-Windows */ 260 PERL_UNUSED_CONTEXT; 261 #endif 262 slab->opslab_free_space = sz; 263 slab->opslab_head = head ? head : slab; 264 DEBUG_S_warn((aTHX_ "allocated new op slab sz 0x%x, %p, head slab %p", 265 (unsigned int)slab->opslab_size, (void*)slab, 266 (void*)(slab->opslab_head))); 267 return slab; 268 } 269 270 #define OPSLOT_SIZE_TO_INDEX(sz) ((sz) - OPSLOT_SIZE_BASE) 271 272 #define link_freed_op(slab, o) S_link_freed_op(aTHX_ slab, o) 273 static void 274 S_link_freed_op(pTHX_ OPSLAB *slab, OP *o) { 275 U16 sz = OpSLOT(o)->opslot_size; 276 U16 index = OPSLOT_SIZE_TO_INDEX(sz); 277 278 assert(sz >= OPSLOT_SIZE_BASE); 279 /* make sure the array is large enough to include ops this large */ 280 if (!slab->opslab_freed) { 281 /* we don't have a free list array yet, make a new one */ 282 slab->opslab_freed_size = index+1; 283 slab->opslab_freed = (OP**)PerlMemShared_calloc((slab->opslab_freed_size), sizeof(OP*)); 284 285 if (!slab->opslab_freed) 286 croak_no_mem(); 287 } 288 else if (index >= slab->opslab_freed_size) { 289 /* It's probably not worth doing exponential expansion here, the number of op sizes 290 is small. 291 */ 292 /* We already have a list that isn't large enough, expand it */ 293 size_t newsize = index+1; 294 OP **p = (OP **)PerlMemShared_realloc(slab->opslab_freed, newsize * sizeof(OP*)); 295 296 if (!p) 297 croak_no_mem(); 298 299 Zero(p+slab->opslab_freed_size, newsize - slab->opslab_freed_size, OP *); 300 301 slab->opslab_freed = p; 302 slab->opslab_freed_size = newsize; 303 } 304 305 o->op_next = slab->opslab_freed[index]; 306 slab->opslab_freed[index] = o; 307 } 308 309 /* Returns a sz-sized block of memory (suitable for holding an op) from 310 * a free slot in the chain of op slabs attached to PL_compcv. 311 * Allocates a new slab if necessary. 312 * if PL_compcv isn't compiling, malloc() instead. 313 */ 314 315 void * 316 Perl_Slab_Alloc(pTHX_ size_t sz) 317 { 318 OPSLAB *head_slab; /* first slab in the chain */ 319 OPSLAB *slab2; 320 OPSLOT *slot; 321 OP *o; 322 size_t sz_in_p; /* size in pointer units, including the OPSLOT header */ 323 324 /* We only allocate ops from the slab during subroutine compilation. 325 We find the slab via PL_compcv, hence that must be non-NULL. It could 326 also be pointing to a subroutine which is now fully set up (CvROOT() 327 pointing to the top of the optree for that sub), or a subroutine 328 which isn't using the slab allocator. If our sanity checks aren't met, 329 don't use a slab, but allocate the OP directly from the heap. */ 330 if (!PL_compcv || CvROOT(PL_compcv) 331 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv))) 332 { 333 o = (OP*)PerlMemShared_calloc(1, sz); 334 goto gotit; 335 } 336 337 /* While the subroutine is under construction, the slabs are accessed via 338 CvSTART(), to avoid needing to expand PVCV by one pointer for something 339 unneeded at runtime. Once a subroutine is constructed, the slabs are 340 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been 341 allocated yet. See the commit message for 8be227ab5eaa23f2 for more 342 details. */ 343 if (!CvSTART(PL_compcv)) { 344 CvSTART(PL_compcv) = 345 (OP *)(head_slab = S_new_slab(aTHX_ NULL, PERL_SLAB_SIZE)); 346 CvSLABBED_on(PL_compcv); 347 head_slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */ 348 } 349 else ++(head_slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt; 350 351 sz_in_p = SIZE_TO_PSIZE(sz + OPSLOT_HEADER); 352 353 /* The head slab for each CV maintains a free list of OPs. In particular, constant folding 354 will free up OPs, so it makes sense to re-use them where possible. A 355 freed up slot is used in preference to a new allocation. */ 356 if (head_slab->opslab_freed && 357 OPSLOT_SIZE_TO_INDEX(sz_in_p) < head_slab->opslab_freed_size) { 358 U16 base_index; 359 360 /* look for a large enough size with any freed ops */ 361 for (base_index = OPSLOT_SIZE_TO_INDEX(sz_in_p); 362 base_index < head_slab->opslab_freed_size && !head_slab->opslab_freed[base_index]; 363 ++base_index) { 364 } 365 366 if (base_index < head_slab->opslab_freed_size) { 367 /* found a freed op */ 368 o = head_slab->opslab_freed[base_index]; 369 370 DEBUG_S_warn((aTHX_ "realloced op at %p, slab %p, head slab %p", 371 (void *)o, (void *)OpMySLAB(o), (void *)head_slab)); 372 head_slab->opslab_freed[base_index] = o->op_next; 373 Zero(o, sz, char); 374 o->op_slabbed = 1; 375 goto gotit; 376 } 377 } 378 379 #define INIT_OPSLOT(s) \ 380 slot->opslot_offset = DIFF(&slab2->opslab_slots, slot) ; \ 381 slot->opslot_size = s; \ 382 slab2->opslab_free_space -= s; \ 383 o = &slot->opslot_op; \ 384 o->op_slabbed = 1 385 386 /* The partially-filled slab is next in the chain. */ 387 slab2 = head_slab->opslab_next ? head_slab->opslab_next : head_slab; 388 if (slab2->opslab_free_space < sz_in_p) { 389 /* Remaining space is too small. */ 390 /* If we can fit a BASEOP, add it to the free chain, so as not 391 to waste it. */ 392 if (slab2->opslab_free_space >= OPSLOT_SIZE_BASE) { 393 slot = &slab2->opslab_slots; 394 INIT_OPSLOT(slab2->opslab_free_space); 395 o->op_type = OP_FREED; 396 DEBUG_S_warn((aTHX_ "linked unused op space at %p, slab %p, head slab %p", 397 (void *)o, (void *)slab2, (void *)head_slab)); 398 link_freed_op(head_slab, o); 399 } 400 401 /* Create a new slab. Make this one twice as big. */ 402 slab2 = S_new_slab(aTHX_ head_slab, 403 slab2->opslab_size > PERL_MAX_SLAB_SIZE / 2 404 ? PERL_MAX_SLAB_SIZE 405 : slab2->opslab_size * 2); 406 slab2->opslab_next = head_slab->opslab_next; 407 head_slab->opslab_next = slab2; 408 } 409 assert(slab2->opslab_size >= sz_in_p); 410 411 /* Create a new op slot */ 412 slot = OpSLOToff(slab2, slab2->opslab_free_space - sz_in_p); 413 assert(slot >= &slab2->opslab_slots); 414 INIT_OPSLOT(sz_in_p); 415 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p, head slab %p", 416 (void*)o, (void*)slab2, (void*)head_slab)); 417 418 gotit: 419 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */ 420 assert(!o->op_moresib); 421 assert(!o->op_sibparent); 422 423 return (void *)o; 424 } 425 426 #undef INIT_OPSLOT 427 428 #ifdef PERL_DEBUG_READONLY_OPS 429 void 430 Perl_Slab_to_ro(pTHX_ OPSLAB *slab) 431 { 432 PERL_ARGS_ASSERT_SLAB_TO_RO; 433 434 if (slab->opslab_readonly) return; 435 slab->opslab_readonly = 1; 436 for (; slab; slab = slab->opslab_next) { 437 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n", 438 (unsigned long) slab->opslab_size, (void *)slab));*/ 439 if (mprotect(slab, OpSLABSizeBytes(slab->opslab_size), PROT_READ)) 440 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", (void *)slab, 441 (unsigned long)slab->opslab_size, errno); 442 } 443 } 444 445 void 446 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab) 447 { 448 OPSLAB *slab2; 449 450 PERL_ARGS_ASSERT_SLAB_TO_RW; 451 452 if (!slab->opslab_readonly) return; 453 slab2 = slab; 454 for (; slab2; slab2 = slab2->opslab_next) { 455 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n", 456 (unsigned long) size, (void *)slab2));*/ 457 if (mprotect((void *)slab2, OpSLABSizeBytes(slab2->opslab_size), 458 PROT_READ|PROT_WRITE)) { 459 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", (void *)slab, 460 (unsigned long)slab2->opslab_size, errno); 461 } 462 } 463 slab->opslab_readonly = 0; 464 } 465 466 #else 467 # define Slab_to_rw(op) NOOP 468 #endif 469 470 /* make freed ops die if they're inadvertently executed */ 471 #ifdef DEBUGGING 472 static OP * 473 S_pp_freed(pTHX) 474 { 475 DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op); 476 } 477 #endif 478 479 480 /* Return the block of memory used by an op to the free list of 481 * the OP slab associated with that op. 482 */ 483 484 void 485 Perl_Slab_Free(pTHX_ void *op) 486 { 487 OP * const o = (OP *)op; 488 OPSLAB *slab; 489 490 PERL_ARGS_ASSERT_SLAB_FREE; 491 492 #ifdef DEBUGGING 493 o->op_ppaddr = S_pp_freed; 494 #endif 495 496 if (!o->op_slabbed) { 497 if (!o->op_static) 498 PerlMemShared_free(op); 499 return; 500 } 501 502 slab = OpSLAB(o); 503 /* If this op is already freed, our refcount will get screwy. */ 504 assert(o->op_type != OP_FREED); 505 o->op_type = OP_FREED; 506 link_freed_op(slab, o); 507 DEBUG_S_warn((aTHX_ "freeing op at %p, slab %p, head slab %p", 508 (void*)o, (void *)OpMySLAB(o), (void*)slab)); 509 OpslabREFCNT_dec_padok(slab); 510 } 511 512 void 513 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab) 514 { 515 const bool havepad = !!PL_comppad; 516 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD; 517 if (havepad) { 518 ENTER; 519 PAD_SAVE_SETNULLPAD(); 520 } 521 opslab_free(slab); 522 if (havepad) LEAVE; 523 } 524 525 /* Free a chain of OP slabs. Should only be called after all ops contained 526 * in it have been freed. At this point, its reference count should be 1, 527 * because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1, 528 * and just directly calls opslab_free(). 529 * (Note that the reference count which PL_compcv held on the slab should 530 * have been removed once compilation of the sub was complete). 531 * 532 * 533 */ 534 535 void 536 Perl_opslab_free(pTHX_ OPSLAB *slab) 537 { 538 OPSLAB *slab2; 539 PERL_ARGS_ASSERT_OPSLAB_FREE; 540 PERL_UNUSED_CONTEXT; 541 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab)); 542 assert(slab->opslab_refcnt == 1); 543 PerlMemShared_free(slab->opslab_freed); 544 do { 545 slab2 = slab->opslab_next; 546 #ifdef DEBUGGING 547 slab->opslab_refcnt = ~(size_t)0; 548 #endif 549 #ifdef PERL_DEBUG_READONLY_OPS 550 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n", 551 (void*)slab)); 552 if (munmap(slab, OpSLABSizeBytes(slab->opslab_size))) { 553 perror("munmap failed"); 554 abort(); 555 } 556 #else 557 PerlMemShared_free(slab); 558 #endif 559 slab = slab2; 560 } while (slab); 561 } 562 563 /* like opslab_free(), but first calls op_free() on any ops in the slab 564 * not marked as OP_FREED 565 */ 566 567 void 568 Perl_opslab_force_free(pTHX_ OPSLAB *slab) 569 { 570 OPSLAB *slab2; 571 #ifdef DEBUGGING 572 size_t savestack_count = 0; 573 #endif 574 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE; 575 slab2 = slab; 576 do { 577 OPSLOT *slot = OpSLOToff(slab2, slab2->opslab_free_space); 578 OPSLOT *end = OpSLOToff(slab2, slab2->opslab_size); 579 for (; slot < end; 580 slot = (OPSLOT*) ((I32**)slot + slot->opslot_size) ) 581 { 582 if (slot->opslot_op.op_type != OP_FREED 583 && !(slot->opslot_op.op_savefree 584 #ifdef DEBUGGING 585 && ++savestack_count 586 #endif 587 ) 588 ) { 589 assert(slot->opslot_op.op_slabbed); 590 op_free(&slot->opslot_op); 591 if (slab->opslab_refcnt == 1) goto free; 592 } 593 } 594 } while ((slab2 = slab2->opslab_next)); 595 /* > 1 because the CV still holds a reference count. */ 596 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */ 597 #ifdef DEBUGGING 598 assert(savestack_count == slab->opslab_refcnt-1); 599 #endif 600 /* Remove the CV’s reference count. */ 601 slab->opslab_refcnt--; 602 return; 603 } 604 free: 605 opslab_free(slab); 606 } 607 608 #ifdef PERL_DEBUG_READONLY_OPS 609 OP * 610 Perl_op_refcnt_inc(pTHX_ OP *o) 611 { 612 if(o) { 613 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL; 614 if (slab && slab->opslab_readonly) { 615 Slab_to_rw(slab); 616 ++o->op_targ; 617 Slab_to_ro(slab); 618 } else { 619 ++o->op_targ; 620 } 621 } 622 return o; 623 624 } 625 626 PADOFFSET 627 Perl_op_refcnt_dec(pTHX_ OP *o) 628 { 629 PADOFFSET result; 630 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL; 631 632 PERL_ARGS_ASSERT_OP_REFCNT_DEC; 633 634 if (slab && slab->opslab_readonly) { 635 Slab_to_rw(slab); 636 result = --o->op_targ; 637 Slab_to_ro(slab); 638 } else { 639 result = --o->op_targ; 640 } 641 return result; 642 } 643 #endif 644 /* 645 * In the following definition, the ", (OP*)0" is just to make the compiler 646 * think the expression is of the right type: croak actually does a Siglongjmp. 647 */ 648 #define CHECKOP(type,o) \ 649 ((PL_op_mask && PL_op_mask[type]) \ 650 ? ( op_free((OP*)o), \ 651 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \ 652 (OP*)0 ) \ 653 : PL_check[type](aTHX_ (OP*)o)) 654 655 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2) 656 657 #define OpTYPE_set(o,type) \ 658 STMT_START { \ 659 o->op_type = (OPCODE)type; \ 660 o->op_ppaddr = PL_ppaddr[type]; \ 661 } STMT_END 662 663 STATIC OP * 664 S_no_fh_allowed(pTHX_ OP *o) 665 { 666 PERL_ARGS_ASSERT_NO_FH_ALLOWED; 667 668 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function", 669 OP_DESC(o))); 670 return o; 671 } 672 673 STATIC OP * 674 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags) 675 { 676 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV; 677 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags); 678 return o; 679 } 680 681 STATIC OP * 682 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags) 683 { 684 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV; 685 686 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags); 687 return o; 688 } 689 690 STATIC void 691 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid) 692 { 693 PERL_ARGS_ASSERT_BAD_TYPE_PV; 694 695 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)", 696 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0); 697 } 698 699 STATIC void 700 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t) 701 { 702 SV * const namesv = cv_name((CV *)gv, NULL, 0); 703 PERL_ARGS_ASSERT_BAD_TYPE_GV; 704 705 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)", 706 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv)); 707 } 708 709 STATIC void 710 S_no_bareword_allowed(pTHX_ OP *o) 711 { 712 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED; 713 714 qerror(Perl_mess(aTHX_ 715 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use", 716 SVfARG(cSVOPo_sv))); 717 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */ 718 } 719 720 void 721 Perl_no_bareword_filehandle(pTHX_ const char *fhname) { 722 PERL_ARGS_ASSERT_NO_BAREWORD_FILEHANDLE; 723 724 if (strNE(fhname, "STDERR") 725 && strNE(fhname, "STDOUT") 726 && strNE(fhname, "STDIN") 727 && strNE(fhname, "_") 728 && strNE(fhname, "ARGV") 729 && strNE(fhname, "ARGVOUT") 730 && strNE(fhname, "DATA")) { 731 qerror(Perl_mess(aTHX_ "Bareword filehandle \"%s\" not allowed under 'no feature \"bareword_filehandles\"'", fhname)); 732 } 733 } 734 735 /* "register" allocation */ 736 737 PADOFFSET 738 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags) 739 { 740 PADOFFSET off; 741 bool is_idfirst, is_default; 742 const bool is_our = (PL_parser->in_my == KEY_our); 743 744 PERL_ARGS_ASSERT_ALLOCMY; 745 746 if (flags & ~SVf_UTF8) 747 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf, 748 (UV)flags); 749 750 is_idfirst = flags & SVf_UTF8 751 ? isIDFIRST_utf8_safe((U8*)name + 1, name + len) 752 : isIDFIRST_A(name[1]); 753 754 /* $_, @_, etc. */ 755 is_default = len == 2 && name[1] == '_'; 756 757 /* complain about "my $<special_var>" etc etc */ 758 if (!is_our && (!is_idfirst || is_default)) { 759 const char * const type = 760 PL_parser->in_my == KEY_sigvar ? "subroutine signature" : 761 PL_parser->in_my == KEY_state ? "\"state\"" : "\"my\""; 762 763 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1])) 764 && isASCII(name[1]) 765 && (!isPRINT(name[1]) || memCHRs("\t\n\r\f", name[1]))) { 766 /* diag_listed_as: Can't use global %s in %s */ 767 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in %s", 768 name[0], toCTRL(name[1]), 769 (int)(len - 2), name + 2, 770 type)); 771 } else { 772 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in %s", 773 (int) len, name, 774 type), flags & SVf_UTF8); 775 } 776 } 777 778 /* allocate a spare slot and store the name in that slot */ 779 780 off = pad_add_name_pvn(name, len, 781 (is_our ? padadd_OUR : 782 PL_parser->in_my == KEY_state ? padadd_STATE : 0), 783 PL_parser->in_my_stash, 784 (is_our 785 /* $_ is always in main::, even with our */ 786 ? (PL_curstash && !memEQs(name,len,"$_") 787 ? PL_curstash 788 : PL_defstash) 789 : NULL 790 ) 791 ); 792 /* anon sub prototypes contains state vars should always be cloned, 793 * otherwise the state var would be shared between anon subs */ 794 795 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv)) 796 CvCLONE_on(PL_compcv); 797 798 return off; 799 } 800 801 /* 802 =for apidoc_section $optree_manipulation 803 804 =for apidoc alloccopstash 805 806 Available only under threaded builds, this function allocates an entry in 807 C<PL_stashpad> for the stash passed to it. 808 809 =cut 810 */ 811 812 #ifdef USE_ITHREADS 813 PADOFFSET 814 Perl_alloccopstash(pTHX_ HV *hv) 815 { 816 PADOFFSET off = 0, o = 1; 817 bool found_slot = FALSE; 818 819 PERL_ARGS_ASSERT_ALLOCCOPSTASH; 820 821 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix; 822 823 for (; o < PL_stashpadmax; ++o) { 824 if (PL_stashpad[o] == hv) return PL_stashpadix = o; 825 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV) 826 found_slot = TRUE, off = o; 827 } 828 if (!found_slot) { 829 Renew(PL_stashpad, PL_stashpadmax + 10, HV *); 830 Zero(PL_stashpad + PL_stashpadmax, 10, HV *); 831 off = PL_stashpadmax; 832 PL_stashpadmax += 10; 833 } 834 835 PL_stashpad[PL_stashpadix = off] = hv; 836 return off; 837 } 838 #endif 839 840 /* free the body of an op without examining its contents. 841 * Always use this rather than FreeOp directly */ 842 843 static void 844 S_op_destroy(pTHX_ OP *o) 845 { 846 FreeOp(o); 847 } 848 849 /* Destructor */ 850 851 /* 852 =for apidoc op_free 853 854 Free an op and its children. Only use this when an op is no longer linked 855 to from any optree. 856 857 =cut 858 */ 859 860 void 861 Perl_op_free(pTHX_ OP *o) 862 { 863 OPCODE type; 864 OP *top_op = o; 865 OP *next_op = o; 866 bool went_up = FALSE; /* whether we reached the current node by 867 following the parent pointer from a child, and 868 so have already seen this node */ 869 870 if (!o || o->op_type == OP_FREED) 871 return; 872 873 if (o->op_private & OPpREFCOUNTED) { 874 /* if base of tree is refcounted, just decrement */ 875 switch (o->op_type) { 876 case OP_LEAVESUB: 877 case OP_LEAVESUBLV: 878 case OP_LEAVEEVAL: 879 case OP_LEAVE: 880 case OP_SCOPE: 881 case OP_LEAVEWRITE: 882 { 883 PADOFFSET refcnt; 884 OP_REFCNT_LOCK; 885 refcnt = OpREFCNT_dec(o); 886 OP_REFCNT_UNLOCK; 887 if (refcnt) { 888 /* Need to find and remove any pattern match ops from 889 * the list we maintain for reset(). */ 890 find_and_forget_pmops(o); 891 return; 892 } 893 } 894 break; 895 default: 896 break; 897 } 898 } 899 900 while (next_op) { 901 o = next_op; 902 903 /* free child ops before ourself, (then free ourself "on the 904 * way back up") */ 905 906 if (!went_up && o->op_flags & OPf_KIDS) { 907 next_op = cUNOPo->op_first; 908 continue; 909 } 910 911 /* find the next node to visit, *then* free the current node 912 * (can't rely on o->op_* fields being valid after o has been 913 * freed) */ 914 915 /* The next node to visit will be either the sibling, or the 916 * parent if no siblings left, or NULL if we've worked our way 917 * back up to the top node in the tree */ 918 next_op = (o == top_op) ? NULL : o->op_sibparent; 919 went_up = cBOOL(!OpHAS_SIBLING(o)); /* parents are already visited */ 920 921 /* Now process the current node */ 922 923 /* Though ops may be freed twice, freeing the op after its slab is a 924 big no-no. */ 925 assert(!o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0); 926 /* During the forced freeing of ops after compilation failure, kidops 927 may be freed before their parents. */ 928 if (!o || o->op_type == OP_FREED) 929 continue; 930 931 type = o->op_type; 932 933 /* an op should only ever acquire op_private flags that we know about. 934 * If this fails, you may need to fix something in regen/op_private. 935 * Don't bother testing if: 936 * * the op_ppaddr doesn't match the op; someone may have 937 * overridden the op and be doing strange things with it; 938 * * we've errored, as op flags are often left in an 939 * inconsistent state then. Note that an error when 940 * compiling the main program leaves PL_parser NULL, so 941 * we can't spot faults in the main code, only 942 * evaled/required code; 943 * * it's a banned op - we may be croaking before the op is 944 * fully formed. - see CHECKOP. */ 945 #ifdef DEBUGGING 946 if ( o->op_ppaddr == PL_ppaddr[type] 947 && PL_parser 948 && !PL_parser->error_count 949 && !(PL_op_mask && PL_op_mask[type]) 950 ) 951 { 952 assert(!(o->op_private & ~PL_op_private_valid[type])); 953 } 954 #endif 955 956 957 /* Call the op_free hook if it has been set. Do it now so that it's called 958 * at the right time for refcounted ops, but still before all of the kids 959 * are freed. */ 960 CALL_OPFREEHOOK(o); 961 962 if (type == OP_NULL) 963 type = (OPCODE)o->op_targ; 964 965 if (o->op_slabbed) 966 Slab_to_rw(OpSLAB(o)); 967 968 /* COP* is not cleared by op_clear() so that we may track line 969 * numbers etc even after null() */ 970 if (type == OP_NEXTSTATE || type == OP_DBSTATE) { 971 cop_free((COP*)o); 972 } 973 974 op_clear(o); 975 FreeOp(o); 976 if (PL_op == o) 977 PL_op = NULL; 978 } 979 } 980 981 982 /* S_op_clear_gv(): free a GV attached to an OP */ 983 984 STATIC 985 #ifdef USE_ITHREADS 986 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp) 987 #else 988 void S_op_clear_gv(pTHX_ OP *o, SV**svp) 989 #endif 990 { 991 992 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV 993 || o->op_type == OP_MULTIDEREF) 994 #ifdef USE_ITHREADS 995 && PL_curpad 996 ? ((GV*)PAD_SVl(*ixp)) : NULL; 997 #else 998 ? (GV*)(*svp) : NULL; 999 #endif 1000 /* It's possible during global destruction that the GV is freed 1001 before the optree. Whilst the SvREFCNT_inc is happy to bump from 1002 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0 1003 will trigger an assertion failure, because the entry to sv_clear 1004 checks that the scalar is not already freed. A check of for 1005 !SvIS_FREED(gv) turns out to be invalid, because during global 1006 destruction the reference count can be forced down to zero 1007 (with SVf_BREAK set). In which case raising to 1 and then 1008 dropping to 0 triggers cleanup before it should happen. I 1009 *think* that this might actually be a general, systematic, 1010 weakness of the whole idea of SVf_BREAK, in that code *is* 1011 allowed to raise and lower references during global destruction, 1012 so any *valid* code that happens to do this during global 1013 destruction might well trigger premature cleanup. */ 1014 bool still_valid = gv && SvREFCNT(gv); 1015 1016 if (still_valid) 1017 SvREFCNT_inc_simple_void(gv); 1018 #ifdef USE_ITHREADS 1019 if (*ixp > 0) { 1020 pad_swipe(*ixp, TRUE); 1021 *ixp = 0; 1022 } 1023 #else 1024 SvREFCNT_dec(*svp); 1025 *svp = NULL; 1026 #endif 1027 if (still_valid) { 1028 int try_downgrade = SvREFCNT(gv) == 2; 1029 SvREFCNT_dec_NN(gv); 1030 if (try_downgrade) 1031 gv_try_downgrade(gv); 1032 } 1033 } 1034 1035 1036 void 1037 Perl_op_clear(pTHX_ OP *o) 1038 { 1039 1040 1041 PERL_ARGS_ASSERT_OP_CLEAR; 1042 1043 switch (o->op_type) { 1044 case OP_NULL: /* Was holding old type, if any. */ 1045 /* FALLTHROUGH */ 1046 case OP_ENTERTRY: 1047 case OP_ENTEREVAL: /* Was holding hints. */ 1048 case OP_ARGDEFELEM: /* Was holding signature index. */ 1049 o->op_targ = 0; 1050 break; 1051 default: 1052 if (!(o->op_flags & OPf_REF) || !OP_IS_STAT(o->op_type)) 1053 break; 1054 /* FALLTHROUGH */ 1055 case OP_GVSV: 1056 case OP_GV: 1057 case OP_AELEMFAST: 1058 #ifdef USE_ITHREADS 1059 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix)); 1060 #else 1061 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv)); 1062 #endif 1063 break; 1064 case OP_METHOD_REDIR: 1065 case OP_METHOD_REDIR_SUPER: 1066 #ifdef USE_ITHREADS 1067 if (cMETHOPx(o)->op_rclass_targ) { 1068 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1); 1069 cMETHOPx(o)->op_rclass_targ = 0; 1070 } 1071 #else 1072 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv); 1073 cMETHOPx(o)->op_rclass_sv = NULL; 1074 #endif 1075 /* FALLTHROUGH */ 1076 case OP_METHOD_NAMED: 1077 case OP_METHOD_SUPER: 1078 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv); 1079 cMETHOPx(o)->op_u.op_meth_sv = NULL; 1080 #ifdef USE_ITHREADS 1081 if (o->op_targ) { 1082 pad_swipe(o->op_targ, 1); 1083 o->op_targ = 0; 1084 } 1085 #endif 1086 break; 1087 case OP_CONST: 1088 case OP_HINTSEVAL: 1089 SvREFCNT_dec(cSVOPo->op_sv); 1090 cSVOPo->op_sv = NULL; 1091 #ifdef USE_ITHREADS 1092 /** Bug #15654 1093 Even if op_clear does a pad_free for the target of the op, 1094 pad_free doesn't actually remove the sv that exists in the pad; 1095 instead it lives on. This results in that it could be reused as 1096 a target later on when the pad was reallocated. 1097 **/ 1098 if(o->op_targ) { 1099 pad_swipe(o->op_targ,1); 1100 o->op_targ = 0; 1101 } 1102 #endif 1103 break; 1104 case OP_DUMP: 1105 case OP_GOTO: 1106 case OP_NEXT: 1107 case OP_LAST: 1108 case OP_REDO: 1109 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) 1110 break; 1111 /* FALLTHROUGH */ 1112 case OP_TRANS: 1113 case OP_TRANSR: 1114 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR) 1115 && (o->op_private & OPpTRANS_USE_SVOP)) 1116 { 1117 #ifdef USE_ITHREADS 1118 if (cPADOPo->op_padix > 0) { 1119 pad_swipe(cPADOPo->op_padix, TRUE); 1120 cPADOPo->op_padix = 0; 1121 } 1122 #else 1123 SvREFCNT_dec(cSVOPo->op_sv); 1124 cSVOPo->op_sv = NULL; 1125 #endif 1126 } 1127 else { 1128 PerlMemShared_free(cPVOPo->op_pv); 1129 cPVOPo->op_pv = NULL; 1130 } 1131 break; 1132 case OP_SUBST: 1133 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot); 1134 goto clear_pmop; 1135 1136 case OP_SPLIT: 1137 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */ 1138 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */ 1139 { 1140 if (o->op_private & OPpSPLIT_LEX) 1141 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff); 1142 else 1143 #ifdef USE_ITHREADS 1144 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE); 1145 #else 1146 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv)); 1147 #endif 1148 } 1149 /* FALLTHROUGH */ 1150 case OP_MATCH: 1151 case OP_QR: 1152 clear_pmop: 1153 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE)) 1154 op_free(cPMOPo->op_code_list); 1155 cPMOPo->op_code_list = NULL; 1156 forget_pmop(cPMOPo); 1157 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL; 1158 /* we use the same protection as the "SAFE" version of the PM_ macros 1159 * here since sv_clean_all might release some PMOPs 1160 * after PL_regex_padav has been cleared 1161 * and the clearing of PL_regex_padav needs to 1162 * happen before sv_clean_all 1163 */ 1164 #ifdef USE_ITHREADS 1165 if(PL_regex_pad) { /* We could be in destruction */ 1166 const IV offset = (cPMOPo)->op_pmoffset; 1167 ReREFCNT_dec(PM_GETRE(cPMOPo)); 1168 PL_regex_pad[offset] = &PL_sv_undef; 1169 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset, 1170 sizeof(offset)); 1171 } 1172 #else 1173 ReREFCNT_dec(PM_GETRE(cPMOPo)); 1174 PM_SETRE(cPMOPo, NULL); 1175 #endif 1176 1177 break; 1178 1179 case OP_ARGCHECK: 1180 PerlMemShared_free(cUNOP_AUXo->op_aux); 1181 break; 1182 1183 case OP_MULTICONCAT: 1184 { 1185 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux; 1186 /* aux[PERL_MULTICONCAT_IX_PLAIN_PV] and/or 1187 * aux[PERL_MULTICONCAT_IX_UTF8_PV] point to plain and/or 1188 * utf8 shared strings */ 1189 char *p1 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv; 1190 char *p2 = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv; 1191 if (p1) 1192 PerlMemShared_free(p1); 1193 if (p2 && p1 != p2) 1194 PerlMemShared_free(p2); 1195 PerlMemShared_free(aux); 1196 } 1197 break; 1198 1199 case OP_MULTIDEREF: 1200 { 1201 UNOP_AUX_item *items = cUNOP_AUXo->op_aux; 1202 UV actions = items->uv; 1203 bool last = 0; 1204 bool is_hash = FALSE; 1205 1206 while (!last) { 1207 switch (actions & MDEREF_ACTION_MASK) { 1208 1209 case MDEREF_reload: 1210 actions = (++items)->uv; 1211 continue; 1212 1213 case MDEREF_HV_padhv_helem: 1214 is_hash = TRUE; 1215 /* FALLTHROUGH */ 1216 case MDEREF_AV_padav_aelem: 1217 pad_free((++items)->pad_offset); 1218 goto do_elem; 1219 1220 case MDEREF_HV_gvhv_helem: 1221 is_hash = TRUE; 1222 /* FALLTHROUGH */ 1223 case MDEREF_AV_gvav_aelem: 1224 #ifdef USE_ITHREADS 1225 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); 1226 #else 1227 S_op_clear_gv(aTHX_ o, &((++items)->sv)); 1228 #endif 1229 goto do_elem; 1230 1231 case MDEREF_HV_gvsv_vivify_rv2hv_helem: 1232 is_hash = TRUE; 1233 /* FALLTHROUGH */ 1234 case MDEREF_AV_gvsv_vivify_rv2av_aelem: 1235 #ifdef USE_ITHREADS 1236 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); 1237 #else 1238 S_op_clear_gv(aTHX_ o, &((++items)->sv)); 1239 #endif 1240 goto do_vivify_rv2xv_elem; 1241 1242 case MDEREF_HV_padsv_vivify_rv2hv_helem: 1243 is_hash = TRUE; 1244 /* FALLTHROUGH */ 1245 case MDEREF_AV_padsv_vivify_rv2av_aelem: 1246 pad_free((++items)->pad_offset); 1247 goto do_vivify_rv2xv_elem; 1248 1249 case MDEREF_HV_pop_rv2hv_helem: 1250 case MDEREF_HV_vivify_rv2hv_helem: 1251 is_hash = TRUE; 1252 /* FALLTHROUGH */ 1253 do_vivify_rv2xv_elem: 1254 case MDEREF_AV_pop_rv2av_aelem: 1255 case MDEREF_AV_vivify_rv2av_aelem: 1256 do_elem: 1257 switch (actions & MDEREF_INDEX_MASK) { 1258 case MDEREF_INDEX_none: 1259 last = 1; 1260 break; 1261 case MDEREF_INDEX_const: 1262 if (is_hash) { 1263 #ifdef USE_ITHREADS 1264 /* see RT #15654 */ 1265 pad_swipe((++items)->pad_offset, 1); 1266 #else 1267 SvREFCNT_dec((++items)->sv); 1268 #endif 1269 } 1270 else 1271 items++; 1272 break; 1273 case MDEREF_INDEX_padsv: 1274 pad_free((++items)->pad_offset); 1275 break; 1276 case MDEREF_INDEX_gvsv: 1277 #ifdef USE_ITHREADS 1278 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); 1279 #else 1280 S_op_clear_gv(aTHX_ o, &((++items)->sv)); 1281 #endif 1282 break; 1283 } 1284 1285 if (actions & MDEREF_FLAG_last) 1286 last = 1; 1287 is_hash = FALSE; 1288 1289 break; 1290 1291 default: 1292 assert(0); 1293 last = 1; 1294 break; 1295 1296 } /* switch */ 1297 1298 actions >>= MDEREF_SHIFT; 1299 } /* while */ 1300 1301 /* start of malloc is at op_aux[-1], where the length is 1302 * stored */ 1303 PerlMemShared_free(cUNOP_AUXo->op_aux - 1); 1304 } 1305 break; 1306 } 1307 1308 if (o->op_targ > 0) { 1309 pad_free(o->op_targ); 1310 o->op_targ = 0; 1311 } 1312 } 1313 1314 STATIC void 1315 S_cop_free(pTHX_ COP* cop) 1316 { 1317 PERL_ARGS_ASSERT_COP_FREE; 1318 1319 /* If called during global destruction PL_defstash might be NULL and there 1320 shouldn't be any code running that will trip over the bad cop address. 1321 This also avoids uselessly creating the AV after it's been destroyed. 1322 */ 1323 if (cop->op_type == OP_DBSTATE && PL_phase != PERL_PHASE_DESTRUCT) { 1324 /* Remove the now invalid op from the line number information. 1325 This could cause a freed memory overwrite if the debugger tried to 1326 set a breakpoint on this line. 1327 */ 1328 AV *av = CopFILEAVn(cop); 1329 if (av) { 1330 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE); 1331 if (svp && *svp != &PL_sv_undef && SvIVX(*svp) == PTR2IV(cop) ) { 1332 (void)SvIOK_off(*svp); 1333 SvIV_set(*svp, 0); 1334 } 1335 } 1336 } 1337 CopFILE_free(cop); 1338 if (! specialWARN(cop->cop_warnings)) 1339 PerlMemShared_free(cop->cop_warnings); 1340 cophh_free(CopHINTHASH_get(cop)); 1341 if (PL_curcop == cop) 1342 PL_curcop = NULL; 1343 } 1344 1345 STATIC void 1346 S_forget_pmop(pTHX_ PMOP *const o) 1347 { 1348 HV * const pmstash = PmopSTASH(o); 1349 1350 PERL_ARGS_ASSERT_FORGET_PMOP; 1351 1352 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) { 1353 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab); 1354 if (mg) { 1355 PMOP **const array = (PMOP**) mg->mg_ptr; 1356 U32 count = mg->mg_len / sizeof(PMOP**); 1357 U32 i = count; 1358 1359 while (i--) { 1360 if (array[i] == o) { 1361 /* Found it. Move the entry at the end to overwrite it. */ 1362 array[i] = array[--count]; 1363 mg->mg_len = count * sizeof(PMOP**); 1364 /* Could realloc smaller at this point always, but probably 1365 not worth it. Probably worth free()ing if we're the 1366 last. */ 1367 if(!count) { 1368 Safefree(mg->mg_ptr); 1369 mg->mg_ptr = NULL; 1370 } 1371 break; 1372 } 1373 } 1374 } 1375 } 1376 if (PL_curpm == o) 1377 PL_curpm = NULL; 1378 } 1379 1380 1381 STATIC void 1382 S_find_and_forget_pmops(pTHX_ OP *o) 1383 { 1384 OP* top_op = o; 1385 1386 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS; 1387 1388 while (1) { 1389 switch (o->op_type) { 1390 case OP_SUBST: 1391 case OP_SPLIT: 1392 case OP_MATCH: 1393 case OP_QR: 1394 forget_pmop((PMOP*)o); 1395 } 1396 1397 if (o->op_flags & OPf_KIDS) { 1398 o = cUNOPo->op_first; 1399 continue; 1400 } 1401 1402 while (1) { 1403 if (o == top_op) 1404 return; /* at top; no parents/siblings to try */ 1405 if (OpHAS_SIBLING(o)) { 1406 o = o->op_sibparent; /* process next sibling */ 1407 break; 1408 } 1409 o = o->op_sibparent; /*try parent's next sibling */ 1410 } 1411 } 1412 } 1413 1414 1415 /* 1416 =for apidoc op_null 1417 1418 Neutralizes an op when it is no longer needed, but is still linked to from 1419 other ops. 1420 1421 =cut 1422 */ 1423 1424 void 1425 Perl_op_null(pTHX_ OP *o) 1426 { 1427 1428 PERL_ARGS_ASSERT_OP_NULL; 1429 1430 if (o->op_type == OP_NULL) 1431 return; 1432 op_clear(o); 1433 o->op_targ = o->op_type; 1434 OpTYPE_set(o, OP_NULL); 1435 } 1436 1437 /* 1438 =for apidoc op_refcnt_lock 1439 1440 Implements the C<OP_REFCNT_LOCK> macro which you should use instead. 1441 1442 =cut 1443 */ 1444 1445 void 1446 Perl_op_refcnt_lock(pTHX) 1447 PERL_TSA_ACQUIRE(PL_op_mutex) 1448 { 1449 PERL_UNUSED_CONTEXT; 1450 OP_REFCNT_LOCK; 1451 } 1452 1453 /* 1454 =for apidoc op_refcnt_unlock 1455 1456 Implements the C<OP_REFCNT_UNLOCK> macro which you should use instead. 1457 1458 =cut 1459 */ 1460 1461 void 1462 Perl_op_refcnt_unlock(pTHX) 1463 PERL_TSA_RELEASE(PL_op_mutex) 1464 { 1465 PERL_UNUSED_CONTEXT; 1466 OP_REFCNT_UNLOCK; 1467 } 1468 1469 1470 /* 1471 =for apidoc op_sibling_splice 1472 1473 A general function for editing the structure of an existing chain of 1474 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows 1475 you to delete zero or more sequential nodes, replacing them with zero or 1476 more different nodes. Performs the necessary op_first/op_last 1477 housekeeping on the parent node and op_sibling manipulation on the 1478 children. The last deleted node will be marked as the last node by 1479 updating the op_sibling/op_sibparent or op_moresib field as appropriate. 1480 1481 Note that op_next is not manipulated, and nodes are not freed; that is the 1482 responsibility of the caller. It also won't create a new list op for an 1483 empty list etc; use higher-level functions like op_append_elem() for that. 1484 1485 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if 1486 the splicing doesn't affect the first or last op in the chain. 1487 1488 C<start> is the node preceding the first node to be spliced. Node(s) 1489 following it will be deleted, and ops will be inserted after it. If it is 1490 C<NULL>, the first node onwards is deleted, and nodes are inserted at the 1491 beginning. 1492 1493 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted. 1494 If -1 or greater than or equal to the number of remaining kids, all 1495 remaining kids are deleted. 1496 1497 C<insert> is the first of a chain of nodes to be inserted in place of the nodes. 1498 If C<NULL>, no nodes are inserted. 1499 1500 The head of the chain of deleted ops is returned, or C<NULL> if no ops were 1501 deleted. 1502 1503 For example: 1504 1505 action before after returns 1506 ------ ----- ----- ------- 1507 1508 P P 1509 splice(P, A, 2, X-Y-Z) | | B-C 1510 A-B-C-D A-X-Y-Z-D 1511 1512 P P 1513 splice(P, NULL, 1, X-Y) | | A 1514 A-B-C-D X-Y-B-C-D 1515 1516 P P 1517 splice(P, NULL, 3, NULL) | | A-B-C 1518 A-B-C-D D 1519 1520 P P 1521 splice(P, B, 0, X-Y) | | NULL 1522 A-B-C-D A-B-X-Y-C-D 1523 1524 1525 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>, 1526 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>. 1527 1528 =cut 1529 */ 1530 1531 OP * 1532 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert) 1533 { 1534 OP *first; 1535 OP *rest; 1536 OP *last_del = NULL; 1537 OP *last_ins = NULL; 1538 1539 if (start) 1540 first = OpSIBLING(start); 1541 else if (!parent) 1542 goto no_parent; 1543 else 1544 first = cLISTOPx(parent)->op_first; 1545 1546 assert(del_count >= -1); 1547 1548 if (del_count && first) { 1549 last_del = first; 1550 while (--del_count && OpHAS_SIBLING(last_del)) 1551 last_del = OpSIBLING(last_del); 1552 rest = OpSIBLING(last_del); 1553 OpLASTSIB_set(last_del, NULL); 1554 } 1555 else 1556 rest = first; 1557 1558 if (insert) { 1559 last_ins = insert; 1560 while (OpHAS_SIBLING(last_ins)) 1561 last_ins = OpSIBLING(last_ins); 1562 OpMAYBESIB_set(last_ins, rest, NULL); 1563 } 1564 else 1565 insert = rest; 1566 1567 if (start) { 1568 OpMAYBESIB_set(start, insert, NULL); 1569 } 1570 else { 1571 assert(parent); 1572 cLISTOPx(parent)->op_first = insert; 1573 if (insert) 1574 parent->op_flags |= OPf_KIDS; 1575 else 1576 parent->op_flags &= ~OPf_KIDS; 1577 } 1578 1579 if (!rest) { 1580 /* update op_last etc */ 1581 U32 type; 1582 OP *lastop; 1583 1584 if (!parent) 1585 goto no_parent; 1586 1587 /* ought to use OP_CLASS(parent) here, but that can't handle 1588 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't 1589 * either */ 1590 type = parent->op_type; 1591 if (type == OP_CUSTOM) { 1592 dTHX; 1593 type = XopENTRYCUSTOM(parent, xop_class); 1594 } 1595 else { 1596 if (type == OP_NULL) 1597 type = parent->op_targ; 1598 type = PL_opargs[type] & OA_CLASS_MASK; 1599 } 1600 1601 lastop = last_ins ? last_ins : start ? start : NULL; 1602 if ( type == OA_BINOP 1603 || type == OA_LISTOP 1604 || type == OA_PMOP 1605 || type == OA_LOOP 1606 ) 1607 cLISTOPx(parent)->op_last = lastop; 1608 1609 if (lastop) 1610 OpLASTSIB_set(lastop, parent); 1611 } 1612 return last_del ? first : NULL; 1613 1614 no_parent: 1615 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent"); 1616 } 1617 1618 /* 1619 =for apidoc op_parent 1620 1621 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise. 1622 1623 =cut 1624 */ 1625 1626 OP * 1627 Perl_op_parent(OP *o) 1628 { 1629 PERL_ARGS_ASSERT_OP_PARENT; 1630 while (OpHAS_SIBLING(o)) 1631 o = OpSIBLING(o); 1632 return o->op_sibparent; 1633 } 1634 1635 /* replace the sibling following start with a new UNOP, which becomes 1636 * the parent of the original sibling; e.g. 1637 * 1638 * op_sibling_newUNOP(P, A, unop-args...) 1639 * 1640 * P P 1641 * | becomes | 1642 * A-B-C A-U-C 1643 * | 1644 * B 1645 * 1646 * where U is the new UNOP. 1647 * 1648 * parent and start args are the same as for op_sibling_splice(); 1649 * type and flags args are as newUNOP(). 1650 * 1651 * Returns the new UNOP. 1652 */ 1653 1654 STATIC OP * 1655 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags) 1656 { 1657 OP *kid, *newop; 1658 1659 kid = op_sibling_splice(parent, start, 1, NULL); 1660 newop = newUNOP(type, flags, kid); 1661 op_sibling_splice(parent, start, 0, newop); 1662 return newop; 1663 } 1664 1665 1666 /* lowest-level newLOGOP-style function - just allocates and populates 1667 * the struct. Higher-level stuff should be done by S_new_logop() / 1668 * newLOGOP(). This function exists mainly to avoid op_first assignment 1669 * being spread throughout this file. 1670 */ 1671 1672 LOGOP * 1673 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other) 1674 { 1675 LOGOP *logop; 1676 OP *kid = first; 1677 NewOp(1101, logop, 1, LOGOP); 1678 OpTYPE_set(logop, type); 1679 logop->op_first = first; 1680 logop->op_other = other; 1681 if (first) 1682 logop->op_flags = OPf_KIDS; 1683 while (kid && OpHAS_SIBLING(kid)) 1684 kid = OpSIBLING(kid); 1685 if (kid) 1686 OpLASTSIB_set(kid, (OP*)logop); 1687 return logop; 1688 } 1689 1690 1691 /* Contextualizers */ 1692 1693 /* 1694 =for apidoc op_contextualize 1695 1696 Applies a syntactic context to an op tree representing an expression. 1697 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_LIST>, 1698 or C<G_VOID> to specify the context to apply. The modified op tree 1699 is returned. 1700 1701 =cut 1702 */ 1703 1704 OP * 1705 Perl_op_contextualize(pTHX_ OP *o, I32 context) 1706 { 1707 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE; 1708 switch (context) { 1709 case G_SCALAR: return scalar(o); 1710 case G_LIST: return list(o); 1711 case G_VOID: return scalarvoid(o); 1712 default: 1713 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld", 1714 (long) context); 1715 } 1716 } 1717 1718 /* 1719 1720 =for apidoc op_linklist 1721 This function is the implementation of the L</LINKLIST> macro. It should 1722 not be called directly. 1723 1724 =cut 1725 */ 1726 1727 1728 OP * 1729 Perl_op_linklist(pTHX_ OP *o) 1730 { 1731 1732 OP **prevp; 1733 OP *kid; 1734 OP * top_op = o; 1735 1736 PERL_ARGS_ASSERT_OP_LINKLIST; 1737 1738 while (1) { 1739 /* Descend down the tree looking for any unprocessed subtrees to 1740 * do first */ 1741 if (!o->op_next) { 1742 if (o->op_flags & OPf_KIDS) { 1743 o = cUNOPo->op_first; 1744 continue; 1745 } 1746 o->op_next = o; /* leaf node; link to self initially */ 1747 } 1748 1749 /* if we're at the top level, there either weren't any children 1750 * to process, or we've worked our way back to the top. */ 1751 if (o == top_op) 1752 return o->op_next; 1753 1754 /* o is now processed. Next, process any sibling subtrees */ 1755 1756 if (OpHAS_SIBLING(o)) { 1757 o = OpSIBLING(o); 1758 continue; 1759 } 1760 1761 /* Done all the subtrees at this level. Go back up a level and 1762 * link the parent in with all its (processed) children. 1763 */ 1764 1765 o = o->op_sibparent; 1766 assert(!o->op_next); 1767 prevp = &(o->op_next); 1768 kid = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL; 1769 while (kid) { 1770 *prevp = kid->op_next; 1771 prevp = &(kid->op_next); 1772 kid = OpSIBLING(kid); 1773 } 1774 *prevp = o; 1775 } 1776 } 1777 1778 1779 static OP * 1780 S_scalarkids(pTHX_ OP *o) 1781 { 1782 if (o && o->op_flags & OPf_KIDS) { 1783 OP *kid; 1784 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 1785 scalar(kid); 1786 } 1787 return o; 1788 } 1789 1790 STATIC OP * 1791 S_scalarboolean(pTHX_ OP *o) 1792 { 1793 PERL_ARGS_ASSERT_SCALARBOOLEAN; 1794 1795 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST && 1796 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) || 1797 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN && 1798 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST && 1799 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) { 1800 if (ckWARN(WARN_SYNTAX)) { 1801 const line_t oldline = CopLINE(PL_curcop); 1802 1803 if (PL_parser && PL_parser->copline != NOLINE) { 1804 /* This ensures that warnings are reported at the first line 1805 of the conditional, not the last. */ 1806 CopLINE_set(PL_curcop, PL_parser->copline); 1807 } 1808 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be =="); 1809 CopLINE_set(PL_curcop, oldline); 1810 } 1811 } 1812 return scalar(o); 1813 } 1814 1815 static SV * 1816 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type) 1817 { 1818 assert(o); 1819 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV || 1820 o->op_type == OP_PADHV || o->op_type == OP_RV2HV); 1821 { 1822 const char funny = o->op_type == OP_PADAV 1823 || o->op_type == OP_RV2AV ? '@' : '%'; 1824 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) { 1825 GV *gv; 1826 if (cUNOPo->op_first->op_type != OP_GV 1827 || !(gv = cGVOPx_gv(cUNOPo->op_first))) 1828 return NULL; 1829 return varname(gv, funny, 0, NULL, 0, subscript_type); 1830 } 1831 return 1832 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type); 1833 } 1834 } 1835 1836 static SV * 1837 S_op_varname(pTHX_ const OP *o) 1838 { 1839 return S_op_varname_subscript(aTHX_ o, 1); 1840 } 1841 1842 static void 1843 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv) 1844 { /* or not so pretty :-) */ 1845 if (o->op_type == OP_CONST) { 1846 *retsv = cSVOPo_sv; 1847 if (SvPOK(*retsv)) { 1848 SV *sv = *retsv; 1849 *retsv = sv_newmortal(); 1850 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL, 1851 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT); 1852 } 1853 else if (!SvOK(*retsv)) 1854 *retpv = "undef"; 1855 } 1856 else *retpv = "..."; 1857 } 1858 1859 static void 1860 S_scalar_slice_warning(pTHX_ const OP *o) 1861 { 1862 OP *kid; 1863 const bool h = o->op_type == OP_HSLICE 1864 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE); 1865 const char lbrack = 1866 h ? '{' : '['; 1867 const char rbrack = 1868 h ? '}' : ']'; 1869 SV *name; 1870 SV *keysv = NULL; /* just to silence compiler warnings */ 1871 const char *key = NULL; 1872 1873 if (!(o->op_private & OPpSLICEWARNING)) 1874 return; 1875 if (PL_parser && PL_parser->error_count) 1876 /* This warning can be nonsensical when there is a syntax error. */ 1877 return; 1878 1879 kid = cLISTOPo->op_first; 1880 kid = OpSIBLING(kid); /* get past pushmark */ 1881 /* weed out false positives: any ops that can return lists */ 1882 switch (kid->op_type) { 1883 case OP_BACKTICK: 1884 case OP_GLOB: 1885 case OP_READLINE: 1886 case OP_MATCH: 1887 case OP_RV2AV: 1888 case OP_EACH: 1889 case OP_VALUES: 1890 case OP_KEYS: 1891 case OP_SPLIT: 1892 case OP_LIST: 1893 case OP_SORT: 1894 case OP_REVERSE: 1895 case OP_ENTERSUB: 1896 case OP_CALLER: 1897 case OP_LSTAT: 1898 case OP_STAT: 1899 case OP_READDIR: 1900 case OP_SYSTEM: 1901 case OP_TMS: 1902 case OP_LOCALTIME: 1903 case OP_GMTIME: 1904 case OP_ENTEREVAL: 1905 return; 1906 } 1907 1908 /* Don't warn if we have a nulled list either. */ 1909 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST) 1910 return; 1911 1912 assert(OpSIBLING(kid)); 1913 name = S_op_varname(aTHX_ OpSIBLING(kid)); 1914 if (!name) /* XS module fiddling with the op tree */ 1915 return; 1916 S_op_pretty(aTHX_ kid, &keysv, &key); 1917 assert(SvPOK(name)); 1918 sv_chop(name,SvPVX(name)+1); 1919 if (key) 1920 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */ 1921 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 1922 "Scalar value @%" SVf "%c%s%c better written as $%" SVf 1923 "%c%s%c", 1924 SVfARG(name), lbrack, key, rbrack, SVfARG(name), 1925 lbrack, key, rbrack); 1926 else 1927 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */ 1928 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 1929 "Scalar value @%" SVf "%c%" SVf "%c better written as $%" 1930 SVf "%c%" SVf "%c", 1931 SVfARG(name), lbrack, SVfARG(keysv), rbrack, 1932 SVfARG(name), lbrack, SVfARG(keysv), rbrack); 1933 } 1934 1935 1936 1937 /* apply scalar context to the o subtree */ 1938 1939 OP * 1940 Perl_scalar(pTHX_ OP *o) 1941 { 1942 OP * top_op = o; 1943 1944 while (1) { 1945 OP *next_kid = NULL; /* what op (if any) to process next */ 1946 OP *kid; 1947 1948 /* assumes no premature commitment */ 1949 if (!o || (PL_parser && PL_parser->error_count) 1950 || (o->op_flags & OPf_WANT) 1951 || o->op_type == OP_RETURN) 1952 { 1953 goto do_next; 1954 } 1955 1956 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; 1957 1958 switch (o->op_type) { 1959 case OP_REPEAT: 1960 scalar(cBINOPo->op_first); 1961 /* convert what initially looked like a list repeat into a 1962 * scalar repeat, e.g. $s = (1) x $n 1963 */ 1964 if (o->op_private & OPpREPEAT_DOLIST) { 1965 kid = cLISTOPx(cUNOPo->op_first)->op_first; 1966 assert(kid->op_type == OP_PUSHMARK); 1967 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) { 1968 op_null(cLISTOPx(cUNOPo->op_first)->op_first); 1969 o->op_private &=~ OPpREPEAT_DOLIST; 1970 } 1971 } 1972 break; 1973 1974 case OP_OR: 1975 case OP_AND: 1976 case OP_COND_EXPR: 1977 /* impose scalar context on everything except the condition */ 1978 next_kid = OpSIBLING(cUNOPo->op_first); 1979 break; 1980 1981 default: 1982 if (o->op_flags & OPf_KIDS) 1983 next_kid = cUNOPo->op_first; /* do all kids */ 1984 break; 1985 1986 /* the children of these ops are usually a list of statements, 1987 * except the leaves, whose first child is a corresponding enter 1988 */ 1989 case OP_SCOPE: 1990 case OP_LINESEQ: 1991 case OP_LIST: 1992 kid = cLISTOPo->op_first; 1993 goto do_kids; 1994 case OP_LEAVE: 1995 case OP_LEAVETRY: 1996 kid = cLISTOPo->op_first; 1997 scalar(kid); 1998 kid = OpSIBLING(kid); 1999 do_kids: 2000 while (kid) { 2001 OP *sib = OpSIBLING(kid); 2002 /* Apply void context to all kids except the last, which 2003 * is scalar (ignoring a trailing ex-nextstate in determining 2004 * if it's the last kid). E.g. 2005 * $scalar = do { void; void; scalar } 2006 * Except that 'when's are always scalar, e.g. 2007 * $scalar = do { given(..) { 2008 * when (..) { scalar } 2009 * when (..) { scalar } 2010 * ... 2011 * }} 2012 */ 2013 if (!sib 2014 || ( !OpHAS_SIBLING(sib) 2015 && sib->op_type == OP_NULL 2016 && ( sib->op_targ == OP_NEXTSTATE 2017 || sib->op_targ == OP_DBSTATE ) 2018 ) 2019 ) 2020 { 2021 /* tail call optimise calling scalar() on the last kid */ 2022 next_kid = kid; 2023 goto do_next; 2024 } 2025 else if (kid->op_type == OP_LEAVEWHEN) 2026 scalar(kid); 2027 else 2028 scalarvoid(kid); 2029 kid = sib; 2030 } 2031 NOT_REACHED; /* NOTREACHED */ 2032 break; 2033 2034 case OP_SORT: 2035 Perl_ck_warner(aTHX_ packWARN(WARN_SCALAR), "Useless use of %s in scalar context", "sort"); 2036 break; 2037 2038 case OP_KVHSLICE: 2039 case OP_KVASLICE: 2040 { 2041 /* Warn about scalar context */ 2042 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '['; 2043 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']'; 2044 SV *name; 2045 SV *keysv; 2046 const char *key = NULL; 2047 2048 /* This warning can be nonsensical when there is a syntax error. */ 2049 if (PL_parser && PL_parser->error_count) 2050 break; 2051 2052 if (!ckWARN(WARN_SYNTAX)) break; 2053 2054 kid = cLISTOPo->op_first; 2055 kid = OpSIBLING(kid); /* get past pushmark */ 2056 assert(OpSIBLING(kid)); 2057 name = S_op_varname(aTHX_ OpSIBLING(kid)); 2058 if (!name) /* XS module fiddling with the op tree */ 2059 break; 2060 S_op_pretty(aTHX_ kid, &keysv, &key); 2061 assert(SvPOK(name)); 2062 sv_chop(name,SvPVX(name)+1); 2063 if (key) 2064 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */ 2065 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 2066 "%%%" SVf "%c%s%c in scalar context better written " 2067 "as $%" SVf "%c%s%c", 2068 SVfARG(name), lbrack, key, rbrack, SVfARG(name), 2069 lbrack, key, rbrack); 2070 else 2071 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */ 2072 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 2073 "%%%" SVf "%c%" SVf "%c in scalar context better " 2074 "written as $%" SVf "%c%" SVf "%c", 2075 SVfARG(name), lbrack, SVfARG(keysv), rbrack, 2076 SVfARG(name), lbrack, SVfARG(keysv), rbrack); 2077 } 2078 } /* switch */ 2079 2080 /* If next_kid is set, someone in the code above wanted us to process 2081 * that kid and all its remaining siblings. Otherwise, work our way 2082 * back up the tree */ 2083 do_next: 2084 while (!next_kid) { 2085 if (o == top_op) 2086 return top_op; /* at top; no parents/siblings to try */ 2087 if (OpHAS_SIBLING(o)) 2088 next_kid = o->op_sibparent; 2089 else { 2090 o = o->op_sibparent; /*try parent's next sibling */ 2091 switch (o->op_type) { 2092 case OP_SCOPE: 2093 case OP_LINESEQ: 2094 case OP_LIST: 2095 case OP_LEAVE: 2096 case OP_LEAVETRY: 2097 /* should really restore PL_curcop to its old value, but 2098 * setting it to PL_compiling is better than do nothing */ 2099 PL_curcop = &PL_compiling; 2100 } 2101 } 2102 } 2103 o = next_kid; 2104 } /* while */ 2105 } 2106 2107 2108 /* apply void context to the optree arg */ 2109 2110 OP * 2111 Perl_scalarvoid(pTHX_ OP *arg) 2112 { 2113 OP *kid; 2114 SV* sv; 2115 OP *o = arg; 2116 2117 PERL_ARGS_ASSERT_SCALARVOID; 2118 2119 while (1) { 2120 U8 want; 2121 SV *useless_sv = NULL; 2122 const char* useless = NULL; 2123 OP * next_kid = NULL; 2124 2125 if (o->op_type == OP_NEXTSTATE 2126 || o->op_type == OP_DBSTATE 2127 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE 2128 || o->op_targ == OP_DBSTATE))) 2129 PL_curcop = (COP*)o; /* for warning below */ 2130 2131 /* assumes no premature commitment */ 2132 want = o->op_flags & OPf_WANT; 2133 if ((want && want != OPf_WANT_SCALAR) 2134 || (PL_parser && PL_parser->error_count) 2135 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN) 2136 { 2137 goto get_next_op; 2138 } 2139 2140 if ((o->op_private & OPpTARGET_MY) 2141 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ 2142 { 2143 /* newASSIGNOP has already applied scalar context, which we 2144 leave, as if this op is inside SASSIGN. */ 2145 goto get_next_op; 2146 } 2147 2148 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; 2149 2150 switch (o->op_type) { 2151 default: 2152 if (!(PL_opargs[o->op_type] & OA_FOLDCONST)) 2153 break; 2154 /* FALLTHROUGH */ 2155 case OP_REPEAT: 2156 if (o->op_flags & OPf_STACKED) 2157 break; 2158 if (o->op_type == OP_REPEAT) 2159 scalar(cBINOPo->op_first); 2160 goto func_ops; 2161 case OP_CONCAT: 2162 if ((o->op_flags & OPf_STACKED) && 2163 !(o->op_private & OPpCONCAT_NESTED)) 2164 break; 2165 goto func_ops; 2166 case OP_SUBSTR: 2167 if (o->op_private == 4) 2168 break; 2169 /* FALLTHROUGH */ 2170 case OP_WANTARRAY: 2171 case OP_GV: 2172 case OP_SMARTMATCH: 2173 case OP_AV2ARYLEN: 2174 case OP_REF: 2175 case OP_REFGEN: 2176 case OP_SREFGEN: 2177 case OP_DEFINED: 2178 case OP_HEX: 2179 case OP_OCT: 2180 case OP_LENGTH: 2181 case OP_VEC: 2182 case OP_INDEX: 2183 case OP_RINDEX: 2184 case OP_SPRINTF: 2185 case OP_KVASLICE: 2186 case OP_KVHSLICE: 2187 case OP_UNPACK: 2188 case OP_PACK: 2189 case OP_JOIN: 2190 case OP_LSLICE: 2191 case OP_ANONLIST: 2192 case OP_ANONHASH: 2193 case OP_SORT: 2194 case OP_REVERSE: 2195 case OP_RANGE: 2196 case OP_FLIP: 2197 case OP_FLOP: 2198 case OP_CALLER: 2199 case OP_FILENO: 2200 case OP_EOF: 2201 case OP_TELL: 2202 case OP_GETSOCKNAME: 2203 case OP_GETPEERNAME: 2204 case OP_READLINK: 2205 case OP_TELLDIR: 2206 case OP_GETPPID: 2207 case OP_GETPGRP: 2208 case OP_GETPRIORITY: 2209 case OP_TIME: 2210 case OP_TMS: 2211 case OP_LOCALTIME: 2212 case OP_GMTIME: 2213 case OP_GHBYNAME: 2214 case OP_GHBYADDR: 2215 case OP_GHOSTENT: 2216 case OP_GNBYNAME: 2217 case OP_GNBYADDR: 2218 case OP_GNETENT: 2219 case OP_GPBYNAME: 2220 case OP_GPBYNUMBER: 2221 case OP_GPROTOENT: 2222 case OP_GSBYNAME: 2223 case OP_GSBYPORT: 2224 case OP_GSERVENT: 2225 case OP_GPWNAM: 2226 case OP_GPWUID: 2227 case OP_GGRNAM: 2228 case OP_GGRGID: 2229 case OP_GETLOGIN: 2230 case OP_PROTOTYPE: 2231 case OP_RUNCV: 2232 func_ops: 2233 useless = OP_DESC(o); 2234 break; 2235 2236 case OP_GVSV: 2237 case OP_PADSV: 2238 case OP_PADAV: 2239 case OP_PADHV: 2240 case OP_PADANY: 2241 case OP_AELEM: 2242 case OP_AELEMFAST: 2243 case OP_AELEMFAST_LEX: 2244 case OP_ASLICE: 2245 case OP_HELEM: 2246 case OP_HSLICE: 2247 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))) 2248 /* Otherwise it's "Useless use of grep iterator" */ 2249 useless = OP_DESC(o); 2250 break; 2251 2252 case OP_SPLIT: 2253 if (!(o->op_private & OPpSPLIT_ASSIGN)) 2254 useless = OP_DESC(o); 2255 break; 2256 2257 case OP_NOT: 2258 kid = cUNOPo->op_first; 2259 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST && 2260 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) { 2261 goto func_ops; 2262 } 2263 useless = "negative pattern binding (!~)"; 2264 break; 2265 2266 case OP_SUBST: 2267 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT) 2268 useless = "non-destructive substitution (s///r)"; 2269 break; 2270 2271 case OP_TRANSR: 2272 useless = "non-destructive transliteration (tr///r)"; 2273 break; 2274 2275 case OP_RV2GV: 2276 case OP_RV2SV: 2277 case OP_RV2AV: 2278 case OP_RV2HV: 2279 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) && 2280 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE)) 2281 useless = "a variable"; 2282 break; 2283 2284 case OP_CONST: 2285 sv = cSVOPo_sv; 2286 if (cSVOPo->op_private & OPpCONST_STRICT) 2287 no_bareword_allowed(o); 2288 else { 2289 if (ckWARN(WARN_VOID)) { 2290 NV nv; 2291 /* don't warn on optimised away booleans, eg 2292 * use constant Foo, 5; Foo || print; */ 2293 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT) 2294 useless = NULL; 2295 /* the constants 0 and 1 are permitted as they are 2296 conventionally used as dummies in constructs like 2297 1 while some_condition_with_side_effects; */ 2298 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0)) 2299 useless = NULL; 2300 else if (SvPOK(sv)) { 2301 SV * const dsv = newSVpvs(""); 2302 useless_sv 2303 = Perl_newSVpvf(aTHX_ 2304 "a constant (%s)", 2305 pv_pretty(dsv, SvPVX_const(sv), 2306 SvCUR(sv), 32, NULL, NULL, 2307 PERL_PV_PRETTY_DUMP 2308 | PERL_PV_ESCAPE_NOCLEAR 2309 | PERL_PV_ESCAPE_UNI_DETECT)); 2310 SvREFCNT_dec_NN(dsv); 2311 } 2312 else if (SvOK(sv)) { 2313 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv)); 2314 } 2315 else 2316 useless = "a constant (undef)"; 2317 } 2318 } 2319 op_null(o); /* don't execute or even remember it */ 2320 break; 2321 2322 case OP_POSTINC: 2323 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */ 2324 break; 2325 2326 case OP_POSTDEC: 2327 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */ 2328 break; 2329 2330 case OP_I_POSTINC: 2331 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */ 2332 break; 2333 2334 case OP_I_POSTDEC: 2335 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */ 2336 break; 2337 2338 case OP_SASSIGN: { 2339 OP *rv2gv; 2340 UNOP *refgen, *rv2cv; 2341 LISTOP *exlist; 2342 2343 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2) 2344 break; 2345 2346 rv2gv = ((BINOP *)o)->op_last; 2347 if (!rv2gv || rv2gv->op_type != OP_RV2GV) 2348 break; 2349 2350 refgen = (UNOP *)((BINOP *)o)->op_first; 2351 2352 if (!refgen || (refgen->op_type != OP_REFGEN 2353 && refgen->op_type != OP_SREFGEN)) 2354 break; 2355 2356 exlist = (LISTOP *)refgen->op_first; 2357 if (!exlist || exlist->op_type != OP_NULL 2358 || exlist->op_targ != OP_LIST) 2359 break; 2360 2361 if (exlist->op_first->op_type != OP_PUSHMARK 2362 && exlist->op_first != exlist->op_last) 2363 break; 2364 2365 rv2cv = (UNOP*)exlist->op_last; 2366 2367 if (rv2cv->op_type != OP_RV2CV) 2368 break; 2369 2370 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0); 2371 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0); 2372 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0); 2373 2374 o->op_private |= OPpASSIGN_CV_TO_GV; 2375 rv2gv->op_private |= OPpDONT_INIT_GV; 2376 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT; 2377 2378 break; 2379 } 2380 2381 case OP_AASSIGN: { 2382 inplace_aassign(o); 2383 break; 2384 } 2385 2386 case OP_OR: 2387 case OP_AND: 2388 kid = cLOGOPo->op_first; 2389 if (kid->op_type == OP_NOT 2390 && (kid->op_flags & OPf_KIDS)) { 2391 if (o->op_type == OP_AND) { 2392 OpTYPE_set(o, OP_OR); 2393 } else { 2394 OpTYPE_set(o, OP_AND); 2395 } 2396 op_null(kid); 2397 } 2398 /* FALLTHROUGH */ 2399 2400 case OP_DOR: 2401 case OP_COND_EXPR: 2402 case OP_ENTERGIVEN: 2403 case OP_ENTERWHEN: 2404 next_kid = OpSIBLING(cUNOPo->op_first); 2405 break; 2406 2407 case OP_NULL: 2408 if (o->op_flags & OPf_STACKED) 2409 break; 2410 /* FALLTHROUGH */ 2411 case OP_NEXTSTATE: 2412 case OP_DBSTATE: 2413 case OP_ENTERTRY: 2414 case OP_ENTER: 2415 if (!(o->op_flags & OPf_KIDS)) 2416 break; 2417 /* FALLTHROUGH */ 2418 case OP_SCOPE: 2419 case OP_LEAVE: 2420 case OP_LEAVETRY: 2421 case OP_LEAVELOOP: 2422 case OP_LINESEQ: 2423 case OP_LEAVEGIVEN: 2424 case OP_LEAVEWHEN: 2425 kids: 2426 next_kid = cLISTOPo->op_first; 2427 break; 2428 case OP_LIST: 2429 /* If the first kid after pushmark is something that the padrange 2430 optimisation would reject, then null the list and the pushmark. 2431 */ 2432 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK 2433 && ( !(kid = OpSIBLING(kid)) 2434 || ( kid->op_type != OP_PADSV 2435 && kid->op_type != OP_PADAV 2436 && kid->op_type != OP_PADHV) 2437 || kid->op_private & ~OPpLVAL_INTRO 2438 || !(kid = OpSIBLING(kid)) 2439 || ( kid->op_type != OP_PADSV 2440 && kid->op_type != OP_PADAV 2441 && kid->op_type != OP_PADHV) 2442 || kid->op_private & ~OPpLVAL_INTRO) 2443 ) { 2444 op_null(cUNOPo->op_first); /* NULL the pushmark */ 2445 op_null(o); /* NULL the list */ 2446 } 2447 goto kids; 2448 case OP_ENTEREVAL: 2449 scalarkids(o); 2450 break; 2451 case OP_SCALAR: 2452 scalar(o); 2453 break; 2454 } 2455 2456 if (useless_sv) { 2457 /* mortalise it, in case warnings are fatal. */ 2458 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 2459 "Useless use of %" SVf " in void context", 2460 SVfARG(sv_2mortal(useless_sv))); 2461 } 2462 else if (useless) { 2463 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 2464 "Useless use of %s in void context", 2465 useless); 2466 } 2467 2468 get_next_op: 2469 /* if a kid hasn't been nominated to process, continue with the 2470 * next sibling, or if no siblings left, go back to the parent's 2471 * siblings and so on 2472 */ 2473 while (!next_kid) { 2474 if (o == arg) 2475 return arg; /* at top; no parents/siblings to try */ 2476 if (OpHAS_SIBLING(o)) 2477 next_kid = o->op_sibparent; 2478 else 2479 o = o->op_sibparent; /*try parent's next sibling */ 2480 } 2481 o = next_kid; 2482 } 2483 2484 return arg; 2485 } 2486 2487 2488 static OP * 2489 S_listkids(pTHX_ OP *o) 2490 { 2491 if (o && o->op_flags & OPf_KIDS) { 2492 OP *kid; 2493 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 2494 list(kid); 2495 } 2496 return o; 2497 } 2498 2499 2500 /* apply list context to the o subtree */ 2501 2502 OP * 2503 Perl_list(pTHX_ OP *o) 2504 { 2505 OP * top_op = o; 2506 2507 while (1) { 2508 OP *next_kid = NULL; /* what op (if any) to process next */ 2509 2510 OP *kid; 2511 2512 /* assumes no premature commitment */ 2513 if (!o || (o->op_flags & OPf_WANT) 2514 || (PL_parser && PL_parser->error_count) 2515 || o->op_type == OP_RETURN) 2516 { 2517 goto do_next; 2518 } 2519 2520 if ((o->op_private & OPpTARGET_MY) 2521 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ 2522 { 2523 goto do_next; /* As if inside SASSIGN */ 2524 } 2525 2526 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; 2527 2528 switch (o->op_type) { 2529 case OP_REPEAT: 2530 if (o->op_private & OPpREPEAT_DOLIST 2531 && !(o->op_flags & OPf_STACKED)) 2532 { 2533 list(cBINOPo->op_first); 2534 kid = cBINOPo->op_last; 2535 /* optimise away (.....) x 1 */ 2536 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv) 2537 && SvIVX(kSVOP_sv) == 1) 2538 { 2539 op_null(o); /* repeat */ 2540 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */ 2541 /* const (rhs): */ 2542 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL)); 2543 } 2544 } 2545 break; 2546 2547 case OP_OR: 2548 case OP_AND: 2549 case OP_COND_EXPR: 2550 /* impose list context on everything except the condition */ 2551 next_kid = OpSIBLING(cUNOPo->op_first); 2552 break; 2553 2554 default: 2555 if (!(o->op_flags & OPf_KIDS)) 2556 break; 2557 /* possibly flatten 1..10 into a constant array */ 2558 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { 2559 list(cBINOPo->op_first); 2560 gen_constant_list(o); 2561 goto do_next; 2562 } 2563 next_kid = cUNOPo->op_first; /* do all kids */ 2564 break; 2565 2566 case OP_LIST: 2567 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) { 2568 op_null(cUNOPo->op_first); /* NULL the pushmark */ 2569 op_null(o); /* NULL the list */ 2570 } 2571 if (o->op_flags & OPf_KIDS) 2572 next_kid = cUNOPo->op_first; /* do all kids */ 2573 break; 2574 2575 /* the children of these ops are usually a list of statements, 2576 * except the leaves, whose first child is a corresponding enter 2577 */ 2578 case OP_SCOPE: 2579 case OP_LINESEQ: 2580 kid = cLISTOPo->op_first; 2581 goto do_kids; 2582 case OP_LEAVE: 2583 case OP_LEAVETRY: 2584 kid = cLISTOPo->op_first; 2585 list(kid); 2586 kid = OpSIBLING(kid); 2587 do_kids: 2588 while (kid) { 2589 OP *sib = OpSIBLING(kid); 2590 /* Apply void context to all kids except the last, which 2591 * is list. E.g. 2592 * @a = do { void; void; list } 2593 * Except that 'when's are always list context, e.g. 2594 * @a = do { given(..) { 2595 * when (..) { list } 2596 * when (..) { list } 2597 * ... 2598 * }} 2599 */ 2600 if (!sib) { 2601 /* tail call optimise calling list() on the last kid */ 2602 next_kid = kid; 2603 goto do_next; 2604 } 2605 else if (kid->op_type == OP_LEAVEWHEN) 2606 list(kid); 2607 else 2608 scalarvoid(kid); 2609 kid = sib; 2610 } 2611 NOT_REACHED; /* NOTREACHED */ 2612 break; 2613 2614 } 2615 2616 /* If next_kid is set, someone in the code above wanted us to process 2617 * that kid and all its remaining siblings. Otherwise, work our way 2618 * back up the tree */ 2619 do_next: 2620 while (!next_kid) { 2621 if (o == top_op) 2622 return top_op; /* at top; no parents/siblings to try */ 2623 if (OpHAS_SIBLING(o)) 2624 next_kid = o->op_sibparent; 2625 else { 2626 o = o->op_sibparent; /*try parent's next sibling */ 2627 switch (o->op_type) { 2628 case OP_SCOPE: 2629 case OP_LINESEQ: 2630 case OP_LIST: 2631 case OP_LEAVE: 2632 case OP_LEAVETRY: 2633 /* should really restore PL_curcop to its old value, but 2634 * setting it to PL_compiling is better than do nothing */ 2635 PL_curcop = &PL_compiling; 2636 } 2637 } 2638 2639 2640 } 2641 o = next_kid; 2642 } /* while */ 2643 } 2644 2645 /* apply void context to non-final ops of a sequence */ 2646 2647 static OP * 2648 S_voidnonfinal(pTHX_ OP *o) 2649 { 2650 if (o) { 2651 const OPCODE type = o->op_type; 2652 2653 if (type == OP_LINESEQ || type == OP_SCOPE || 2654 type == OP_LEAVE || type == OP_LEAVETRY) 2655 { 2656 OP *kid = cLISTOPo->op_first, *sib; 2657 if(type == OP_LEAVE) { 2658 /* Don't put the OP_ENTER in void context */ 2659 assert(kid->op_type == OP_ENTER); 2660 kid = OpSIBLING(kid); 2661 } 2662 for (; kid; kid = sib) { 2663 if ((sib = OpSIBLING(kid)) 2664 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL 2665 || ( sib->op_targ != OP_NEXTSTATE 2666 && sib->op_targ != OP_DBSTATE ))) 2667 { 2668 scalarvoid(kid); 2669 } 2670 } 2671 PL_curcop = &PL_compiling; 2672 } 2673 o->op_flags &= ~OPf_PARENS; 2674 if (PL_hints & HINT_BLOCK_SCOPE) 2675 o->op_flags |= OPf_PARENS; 2676 } 2677 else 2678 o = newOP(OP_STUB, 0); 2679 return o; 2680 } 2681 2682 STATIC OP * 2683 S_modkids(pTHX_ OP *o, I32 type) 2684 { 2685 if (o && o->op_flags & OPf_KIDS) { 2686 OP *kid; 2687 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 2688 op_lvalue(kid, type); 2689 } 2690 return o; 2691 } 2692 2693 2694 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid 2695 * const fields. Also, convert CONST keys to HEK-in-SVs. 2696 * rop is the op that retrieves the hash; 2697 * key_op is the first key 2698 * real if false, only check (and possibly croak); don't update op 2699 */ 2700 2701 STATIC void 2702 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real) 2703 { 2704 PADNAME *lexname; 2705 GV **fields; 2706 bool check_fields; 2707 2708 /* find the padsv corresponding to $lex->{} or @{$lex}{} */ 2709 if (rop) { 2710 if (rop->op_first->op_type == OP_PADSV) 2711 /* @$hash{qw(keys here)} */ 2712 rop = (UNOP*)rop->op_first; 2713 else { 2714 /* @{$hash}{qw(keys here)} */ 2715 if (rop->op_first->op_type == OP_SCOPE 2716 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV) 2717 { 2718 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last; 2719 } 2720 else 2721 rop = NULL; 2722 } 2723 } 2724 2725 lexname = NULL; /* just to silence compiler warnings */ 2726 fields = NULL; /* just to silence compiler warnings */ 2727 2728 check_fields = 2729 rop 2730 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ), 2731 SvPAD_TYPED(lexname)) 2732 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE)) 2733 && isGV(*fields) && GvHV(*fields); 2734 2735 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) { 2736 SV **svp, *sv; 2737 if (key_op->op_type != OP_CONST) 2738 continue; 2739 svp = cSVOPx_svp(key_op); 2740 2741 /* make sure it's not a bareword under strict subs */ 2742 if (key_op->op_private & OPpCONST_BARE && 2743 key_op->op_private & OPpCONST_STRICT) 2744 { 2745 no_bareword_allowed((OP*)key_op); 2746 } 2747 2748 /* Make the CONST have a shared SV */ 2749 if ( !SvIsCOW_shared_hash(sv = *svp) 2750 && SvTYPE(sv) < SVt_PVMG 2751 && SvOK(sv) 2752 && !SvROK(sv) 2753 && real) 2754 { 2755 SSize_t keylen; 2756 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen); 2757 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0); 2758 SvREFCNT_dec_NN(sv); 2759 *svp = nsv; 2760 } 2761 2762 if ( check_fields 2763 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) 2764 { 2765 Perl_croak(aTHX_ "No such class field \"%" SVf "\" " 2766 "in variable %" PNf " of type %" HEKf, 2767 SVfARG(*svp), PNfARG(lexname), 2768 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname)))); 2769 } 2770 } 2771 } 2772 2773 /* info returned by S_sprintf_is_multiconcatable() */ 2774 2775 struct sprintf_ismc_info { 2776 SSize_t nargs; /* num of args to sprintf (not including the format) */ 2777 char *start; /* start of raw format string */ 2778 char *end; /* bytes after end of raw format string */ 2779 STRLEN total_len; /* total length (in bytes) of format string, not 2780 including '%s' and half of '%%' */ 2781 STRLEN variant; /* number of bytes by which total_len_p would grow 2782 if upgraded to utf8 */ 2783 bool utf8; /* whether the format is utf8 */ 2784 }; 2785 2786 2787 /* is the OP_SPRINTF o suitable for converting into a multiconcat op? 2788 * i.e. its format argument is a const string with only '%s' and '%%' 2789 * formats, and the number of args is known, e.g. 2790 * sprintf "a=%s f=%s", $a[0], scalar(f()); 2791 * but not 2792 * sprintf "i=%d a=%s f=%s", $i, @a, f(); 2793 * 2794 * If successful, the sprintf_ismc_info struct pointed to by info will be 2795 * populated. 2796 */ 2797 2798 STATIC bool 2799 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info) 2800 { 2801 OP *pm, *constop, *kid; 2802 SV *sv; 2803 char *s, *e, *p; 2804 SSize_t nargs, nformats; 2805 STRLEN cur, total_len, variant; 2806 bool utf8; 2807 2808 /* if sprintf's behaviour changes, die here so that someone 2809 * can decide whether to enhance this function or skip optimising 2810 * under those new circumstances */ 2811 assert(!(o->op_flags & OPf_STACKED)); 2812 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX)); 2813 assert(!(o->op_private & ~OPpARG4_MASK)); 2814 2815 pm = cUNOPo->op_first; 2816 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */ 2817 return FALSE; 2818 constop = OpSIBLING(pm); 2819 if (!constop || constop->op_type != OP_CONST) 2820 return FALSE; 2821 sv = cSVOPx_sv(constop); 2822 if (SvMAGICAL(sv) || !SvPOK(sv)) 2823 return FALSE; 2824 2825 s = SvPV(sv, cur); 2826 e = s + cur; 2827 2828 /* Scan format for %% and %s and work out how many %s there are. 2829 * Abandon if other format types are found. 2830 */ 2831 2832 nformats = 0; 2833 total_len = 0; 2834 variant = 0; 2835 2836 for (p = s; p < e; p++) { 2837 if (*p != '%') { 2838 total_len++; 2839 if (!UTF8_IS_INVARIANT(*p)) 2840 variant++; 2841 continue; 2842 } 2843 p++; 2844 if (p >= e) 2845 return FALSE; /* lone % at end gives "Invalid conversion" */ 2846 if (*p == '%') 2847 total_len++; 2848 else if (*p == 's') 2849 nformats++; 2850 else 2851 return FALSE; 2852 } 2853 2854 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG) 2855 return FALSE; 2856 2857 utf8 = cBOOL(SvUTF8(sv)); 2858 if (utf8) 2859 variant = 0; 2860 2861 /* scan args; they must all be in scalar cxt */ 2862 2863 nargs = 0; 2864 kid = OpSIBLING(constop); 2865 2866 while (kid) { 2867 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR) 2868 return FALSE; 2869 nargs++; 2870 kid = OpSIBLING(kid); 2871 } 2872 2873 if (nargs != nformats) 2874 return FALSE; /* e.g. sprintf("%s%s", $a); */ 2875 2876 2877 info->nargs = nargs; 2878 info->start = s; 2879 info->end = e; 2880 info->total_len = total_len; 2881 info->variant = variant; 2882 info->utf8 = utf8; 2883 2884 return TRUE; 2885 } 2886 2887 2888 2889 /* S_maybe_multiconcat(): 2890 * 2891 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly 2892 * convert it (and its children) into an OP_MULTICONCAT. See the code 2893 * comments just before pp_multiconcat() for the full details of what 2894 * OP_MULTICONCAT supports. 2895 * 2896 * Basically we're looking for an optree with a chain of OP_CONCATS down 2897 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or 2898 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g. 2899 * 2900 * $x = "$a$b-$c" 2901 * 2902 * looks like 2903 * 2904 * SASSIGN 2905 * | 2906 * STRINGIFY -- PADSV[$x] 2907 * | 2908 * | 2909 * ex-PUSHMARK -- CONCAT/S 2910 * | 2911 * CONCAT/S -- PADSV[$d] 2912 * | 2913 * CONCAT -- CONST["-"] 2914 * | 2915 * PADSV[$a] -- PADSV[$b] 2916 * 2917 * Note that at this stage the OP_SASSIGN may have already been optimised 2918 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT. 2919 */ 2920 2921 STATIC void 2922 S_maybe_multiconcat(pTHX_ OP *o) 2923 { 2924 OP *lastkidop; /* the right-most of any kids unshifted onto o */ 2925 OP *topop; /* the top-most op in the concat tree (often equals o, 2926 unless there are assign/stringify ops above it */ 2927 OP *parentop; /* the parent op of topop (or itself if no parent) */ 2928 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */ 2929 OP *targetop; /* the op corresponding to target=... or target.=... */ 2930 OP *stringop; /* the OP_STRINGIFY op, if any */ 2931 OP *nextop; /* used for recreating the op_next chain without consts */ 2932 OP *kid; /* general-purpose op pointer */ 2933 UNOP_AUX_item *aux; 2934 UNOP_AUX_item *lenp; 2935 char *const_str, *p; 2936 struct sprintf_ismc_info sprintf_info; 2937 2938 /* store info about each arg in args[]; 2939 * toparg is the highest used slot; argp is a general 2940 * pointer to args[] slots */ 2941 struct { 2942 void *p; /* initially points to const sv (or null for op); 2943 later, set to SvPV(constsv), with ... */ 2944 STRLEN len; /* ... len set to SvPV(..., len) */ 2945 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1]; 2946 2947 SSize_t nargs = 0; 2948 SSize_t nconst = 0; 2949 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */ 2950 STRLEN variant; 2951 bool utf8 = FALSE; 2952 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op; 2953 the last-processed arg will the LHS of one, 2954 as args are processed in reverse order */ 2955 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */ 2956 STRLEN total_len = 0; /* sum of the lengths of the const segments */ 2957 U8 flags = 0; /* what will become the op_flags and ... */ 2958 U8 private_flags = 0; /* ... op_private of the multiconcat op */ 2959 bool is_sprintf = FALSE; /* we're optimising an sprintf */ 2960 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */ 2961 bool prev_was_const = FALSE; /* previous arg was a const */ 2962 2963 /* ----------------------------------------------------------------- 2964 * Phase 1: 2965 * 2966 * Examine the optree non-destructively to determine whether it's 2967 * suitable to be converted into an OP_MULTICONCAT. Accumulate 2968 * information about the optree in args[]. 2969 */ 2970 2971 argp = args; 2972 targmyop = NULL; 2973 targetop = NULL; 2974 stringop = NULL; 2975 topop = o; 2976 parentop = o; 2977 2978 assert( o->op_type == OP_SASSIGN 2979 || o->op_type == OP_CONCAT 2980 || o->op_type == OP_SPRINTF 2981 || o->op_type == OP_STRINGIFY); 2982 2983 Zero(&sprintf_info, 1, struct sprintf_ismc_info); 2984 2985 /* first see if, at the top of the tree, there is an assign, 2986 * append and/or stringify */ 2987 2988 if (topop->op_type == OP_SASSIGN) { 2989 /* expr = ..... */ 2990 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN]) 2991 return; 2992 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV)) 2993 return; 2994 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */ 2995 2996 parentop = topop; 2997 topop = cBINOPo->op_first; 2998 targetop = OpSIBLING(topop); 2999 if (!targetop) /* probably some sort of syntax error */ 3000 return; 3001 3002 /* don't optimise away assign in 'local $foo = ....' */ 3003 if ( (targetop->op_private & OPpLVAL_INTRO) 3004 /* these are the common ops which do 'local', but 3005 * not all */ 3006 && ( targetop->op_type == OP_GVSV 3007 || targetop->op_type == OP_RV2SV 3008 || targetop->op_type == OP_AELEM 3009 || targetop->op_type == OP_HELEM 3010 ) 3011 ) 3012 return; 3013 } 3014 else if ( topop->op_type == OP_CONCAT 3015 && (topop->op_flags & OPf_STACKED) 3016 && (!(topop->op_private & OPpCONCAT_NESTED)) 3017 ) 3018 { 3019 /* expr .= ..... */ 3020 3021 /* OPpTARGET_MY shouldn't be able to be set here. If it is, 3022 * decide what to do about it */ 3023 assert(!(o->op_private & OPpTARGET_MY)); 3024 3025 /* barf on unknown flags */ 3026 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY))); 3027 private_flags |= OPpMULTICONCAT_APPEND; 3028 targetop = cBINOPo->op_first; 3029 parentop = topop; 3030 topop = OpSIBLING(targetop); 3031 3032 /* $x .= <FOO> gets optimised to rcatline instead */ 3033 if (topop->op_type == OP_READLINE) 3034 return; 3035 } 3036 3037 if (targetop) { 3038 /* Can targetop (the LHS) if it's a padsv, be optimised 3039 * away and use OPpTARGET_MY instead? 3040 */ 3041 if ( (targetop->op_type == OP_PADSV) 3042 && !(targetop->op_private & OPpDEREF) 3043 && !(targetop->op_private & OPpPAD_STATE) 3044 /* we don't support 'my $x .= ...' */ 3045 && ( o->op_type == OP_SASSIGN 3046 || !(targetop->op_private & OPpLVAL_INTRO)) 3047 ) 3048 is_targable = TRUE; 3049 } 3050 3051 if (topop->op_type == OP_STRINGIFY) { 3052 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY]) 3053 return; 3054 stringop = topop; 3055 3056 /* barf on unknown flags */ 3057 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY))); 3058 3059 if ((topop->op_private & OPpTARGET_MY)) { 3060 if (o->op_type == OP_SASSIGN) 3061 return; /* can't have two assigns */ 3062 targmyop = topop; 3063 } 3064 3065 private_flags |= OPpMULTICONCAT_STRINGIFY; 3066 parentop = topop; 3067 topop = cBINOPx(topop)->op_first; 3068 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK)); 3069 topop = OpSIBLING(topop); 3070 } 3071 3072 if (topop->op_type == OP_SPRINTF) { 3073 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF]) 3074 return; 3075 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) { 3076 nargs = sprintf_info.nargs; 3077 total_len = sprintf_info.total_len; 3078 variant = sprintf_info.variant; 3079 utf8 = sprintf_info.utf8; 3080 is_sprintf = TRUE; 3081 private_flags |= OPpMULTICONCAT_FAKE; 3082 toparg = argp; 3083 /* we have an sprintf op rather than a concat optree. 3084 * Skip most of the code below which is associated with 3085 * processing that optree. We also skip phase 2, determining 3086 * whether its cost effective to optimise, since for sprintf, 3087 * multiconcat is *always* faster */ 3088 goto create_aux; 3089 } 3090 /* note that even if the sprintf itself isn't multiconcatable, 3091 * the expression as a whole may be, e.g. in 3092 * $x .= sprintf("%d",...) 3093 * the sprintf op will be left as-is, but the concat/S op may 3094 * be upgraded to multiconcat 3095 */ 3096 } 3097 else if (topop->op_type == OP_CONCAT) { 3098 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT]) 3099 return; 3100 3101 if ((topop->op_private & OPpTARGET_MY)) { 3102 if (o->op_type == OP_SASSIGN || targmyop) 3103 return; /* can't have two assigns */ 3104 targmyop = topop; 3105 } 3106 } 3107 3108 /* Is it safe to convert a sassign/stringify/concat op into 3109 * a multiconcat? */ 3110 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP); 3111 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP); 3112 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP); 3113 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP); 3114 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last) 3115 == STRUCT_OFFSET(UNOP_AUX, op_aux)); 3116 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last) 3117 == STRUCT_OFFSET(UNOP_AUX, op_aux)); 3118 3119 /* Now scan the down the tree looking for a series of 3120 * CONCAT/OPf_STACKED ops on the LHS (with the last one not 3121 * stacked). For example this tree: 3122 * 3123 * | 3124 * CONCAT/STACKED 3125 * | 3126 * CONCAT/STACKED -- EXPR5 3127 * | 3128 * CONCAT/STACKED -- EXPR4 3129 * | 3130 * CONCAT -- EXPR3 3131 * | 3132 * EXPR1 -- EXPR2 3133 * 3134 * corresponds to an expression like 3135 * 3136 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5) 3137 * 3138 * Record info about each EXPR in args[]: in particular, whether it is 3139 * a stringifiable OP_CONST and if so what the const sv is. 3140 * 3141 * The reason why the last concat can't be STACKED is the difference 3142 * between 3143 * 3144 * ((($a .= $a) .= $a) .= $a) .= $a 3145 * 3146 * and 3147 * $a . $a . $a . $a . $a 3148 * 3149 * The main difference between the optrees for those two constructs 3150 * is the presence of the last STACKED. As well as modifying $a, 3151 * the former sees the changed $a between each concat, so if $s is 3152 * initially 'a', the first returns 'a' x 16, while the latter returns 3153 * 'a' x 5. And pp_multiconcat can't handle that kind of thing. 3154 */ 3155 3156 kid = topop; 3157 3158 for (;;) { 3159 OP *argop; 3160 SV *sv; 3161 bool last = FALSE; 3162 3163 if ( kid->op_type == OP_CONCAT 3164 && !kid_is_last 3165 ) { 3166 OP *k1, *k2; 3167 k1 = cUNOPx(kid)->op_first; 3168 k2 = OpSIBLING(k1); 3169 /* shouldn't happen except maybe after compile err? */ 3170 if (!k2) 3171 return; 3172 3173 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */ 3174 if (kid->op_private & OPpTARGET_MY) 3175 kid_is_last = TRUE; 3176 3177 stacked_last = (kid->op_flags & OPf_STACKED); 3178 if (!stacked_last) 3179 kid_is_last = TRUE; 3180 3181 kid = k1; 3182 argop = k2; 3183 } 3184 else { 3185 argop = kid; 3186 last = TRUE; 3187 } 3188 3189 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2 3190 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2) 3191 { 3192 /* At least two spare slots are needed to decompose both 3193 * concat args. If there are no slots left, continue to 3194 * examine the rest of the optree, but don't push new values 3195 * on args[]. If the optree as a whole is legal for conversion 3196 * (in particular that the last concat isn't STACKED), then 3197 * the first PERL_MULTICONCAT_MAXARG elements of the optree 3198 * can be converted into an OP_MULTICONCAT now, with the first 3199 * child of that op being the remainder of the optree - 3200 * which may itself later be converted to a multiconcat op 3201 * too. 3202 */ 3203 if (last) { 3204 /* the last arg is the rest of the optree */ 3205 argp++->p = NULL; 3206 nargs++; 3207 } 3208 } 3209 else if ( argop->op_type == OP_CONST 3210 && ((sv = cSVOPx_sv(argop))) 3211 /* defer stringification until runtime of 'constant' 3212 * things that might stringify variantly, e.g. the radix 3213 * point of NVs, or overloaded RVs */ 3214 && (SvPOK(sv) || SvIOK(sv)) 3215 && (!SvGMAGICAL(sv)) 3216 ) { 3217 if (argop->op_private & OPpCONST_STRICT) 3218 no_bareword_allowed(argop); 3219 argp++->p = sv; 3220 utf8 |= cBOOL(SvUTF8(sv)); 3221 nconst++; 3222 if (prev_was_const) 3223 /* this const may be demoted back to a plain arg later; 3224 * make sure we have enough arg slots left */ 3225 nadjconst++; 3226 prev_was_const = !prev_was_const; 3227 } 3228 else { 3229 argp++->p = NULL; 3230 nargs++; 3231 prev_was_const = FALSE; 3232 } 3233 3234 if (last) 3235 break; 3236 } 3237 3238 toparg = argp - 1; 3239 3240 if (stacked_last) 3241 return; /* we don't support ((A.=B).=C)...) */ 3242 3243 /* look for two adjacent consts and don't fold them together: 3244 * $o . "a" . "b" 3245 * should do 3246 * $o->concat("a")->concat("b") 3247 * rather than 3248 * $o->concat("ab") 3249 * (but $o .= "a" . "b" should still fold) 3250 */ 3251 { 3252 bool seen_nonconst = FALSE; 3253 for (argp = toparg; argp >= args; argp--) { 3254 if (argp->p == NULL) { 3255 seen_nonconst = TRUE; 3256 continue; 3257 } 3258 if (!seen_nonconst) 3259 continue; 3260 if (argp[1].p) { 3261 /* both previous and current arg were constants; 3262 * leave the current OP_CONST as-is */ 3263 argp->p = NULL; 3264 nconst--; 3265 nargs++; 3266 } 3267 } 3268 } 3269 3270 /* ----------------------------------------------------------------- 3271 * Phase 2: 3272 * 3273 * At this point we have determined that the optree *can* be converted 3274 * into a multiconcat. Having gathered all the evidence, we now decide 3275 * whether it *should*. 3276 */ 3277 3278 3279 /* we need at least one concat action, e.g.: 3280 * 3281 * Y . Z 3282 * X = Y . Z 3283 * X .= Y 3284 * 3285 * otherwise we could be doing something like $x = "foo", which 3286 * if treated as a concat, would fail to COW. 3287 */ 3288 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2) 3289 return; 3290 3291 /* Benchmarking seems to indicate that we gain if: 3292 * * we optimise at least two actions into a single multiconcat 3293 * (e.g concat+concat, sassign+concat); 3294 * * or if we can eliminate at least 1 OP_CONST; 3295 * * or if we can eliminate a padsv via OPpTARGET_MY 3296 */ 3297 3298 if ( 3299 /* eliminated at least one OP_CONST */ 3300 nconst >= 1 3301 /* eliminated an OP_SASSIGN */ 3302 || o->op_type == OP_SASSIGN 3303 /* eliminated an OP_PADSV */ 3304 || (!targmyop && is_targable) 3305 ) 3306 /* definitely a net gain to optimise */ 3307 goto optimise; 3308 3309 /* ... if not, what else? */ 3310 3311 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1): 3312 * multiconcat is faster (due to not creating a temporary copy of 3313 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is 3314 * faster. 3315 */ 3316 if ( nconst == 0 3317 && nargs == 2 3318 && targmyop 3319 && topop->op_type == OP_CONCAT 3320 ) { 3321 PADOFFSET t = targmyop->op_targ; 3322 OP *k1 = cBINOPx(topop)->op_first; 3323 OP *k2 = cBINOPx(topop)->op_last; 3324 if ( k2->op_type == OP_PADSV 3325 && k2->op_targ == t 3326 && ( k1->op_type != OP_PADSV 3327 || k1->op_targ != t) 3328 ) 3329 goto optimise; 3330 } 3331 3332 /* need at least two concats */ 3333 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3) 3334 return; 3335 3336 3337 3338 /* ----------------------------------------------------------------- 3339 * Phase 3: 3340 * 3341 * At this point the optree has been verified as ok to be optimised 3342 * into an OP_MULTICONCAT. Now start changing things. 3343 */ 3344 3345 optimise: 3346 3347 /* stringify all const args and determine utf8ness */ 3348 3349 variant = 0; 3350 for (argp = args; argp <= toparg; argp++) { 3351 SV *sv = (SV*)argp->p; 3352 if (!sv) 3353 continue; /* not a const op */ 3354 if (utf8 && !SvUTF8(sv)) 3355 sv_utf8_upgrade_nomg(sv); 3356 argp->p = SvPV_nomg(sv, argp->len); 3357 total_len += argp->len; 3358 3359 /* see if any strings would grow if converted to utf8 */ 3360 if (!utf8) { 3361 variant += variant_under_utf8_count((U8 *) argp->p, 3362 (U8 *) argp->p + argp->len); 3363 } 3364 } 3365 3366 /* create and populate aux struct */ 3367 3368 create_aux: 3369 3370 aux = (UNOP_AUX_item*)PerlMemShared_malloc( 3371 sizeof(UNOP_AUX_item) 3372 * ( 3373 PERL_MULTICONCAT_HEADER_SIZE 3374 + ((nargs + 1) * (variant ? 2 : 1)) 3375 ) 3376 ); 3377 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1); 3378 3379 /* Extract all the non-const expressions from the concat tree then 3380 * dispose of the old tree, e.g. convert the tree from this: 3381 * 3382 * o => SASSIGN 3383 * | 3384 * STRINGIFY -- TARGET 3385 * | 3386 * ex-PUSHMARK -- CONCAT 3387 * | 3388 * CONCAT -- EXPR5 3389 * | 3390 * CONCAT -- EXPR4 3391 * | 3392 * CONCAT -- EXPR3 3393 * | 3394 * EXPR1 -- EXPR2 3395 * 3396 * 3397 * to: 3398 * 3399 * o => MULTICONCAT 3400 * | 3401 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET 3402 * 3403 * except that if EXPRi is an OP_CONST, it's discarded. 3404 * 3405 * During the conversion process, EXPR ops are stripped from the tree 3406 * and unshifted onto o. Finally, any of o's remaining original 3407 * childen are discarded and o is converted into an OP_MULTICONCAT. 3408 * 3409 * In this middle of this, o may contain both: unshifted args on the 3410 * left, and some remaining original args on the right. lastkidop 3411 * is set to point to the right-most unshifted arg to delineate 3412 * between the two sets. 3413 */ 3414 3415 3416 if (is_sprintf) { 3417 /* create a copy of the format with the %'s removed, and record 3418 * the sizes of the const string segments in the aux struct */ 3419 char *q, *oldq; 3420 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS; 3421 3422 p = sprintf_info.start; 3423 q = const_str; 3424 oldq = q; 3425 for (; p < sprintf_info.end; p++) { 3426 if (*p == '%') { 3427 p++; 3428 if (*p != '%') { 3429 (lenp++)->ssize = q - oldq; 3430 oldq = q; 3431 continue; 3432 } 3433 } 3434 *q++ = *p; 3435 } 3436 lenp->ssize = q - oldq; 3437 assert((STRLEN)(q - const_str) == total_len); 3438 3439 /* Attach all the args (i.e. the kids of the sprintf) to o (which 3440 * may or may not be topop) The pushmark and const ops need to be 3441 * kept in case they're an op_next entry point. 3442 */ 3443 lastkidop = cLISTOPx(topop)->op_last; 3444 kid = cUNOPx(topop)->op_first; /* pushmark */ 3445 op_null(kid); 3446 op_null(OpSIBLING(kid)); /* const */ 3447 if (o != topop) { 3448 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */ 3449 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */ 3450 lastkidop->op_next = o; 3451 } 3452 } 3453 else { 3454 p = const_str; 3455 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS; 3456 3457 lenp->ssize = -1; 3458 3459 /* Concatenate all const strings into const_str. 3460 * Note that args[] contains the RHS args in reverse order, so 3461 * we scan args[] from top to bottom to get constant strings 3462 * in L-R order 3463 */ 3464 for (argp = toparg; argp >= args; argp--) { 3465 if (!argp->p) 3466 /* not a const op */ 3467 (++lenp)->ssize = -1; 3468 else { 3469 STRLEN l = argp->len; 3470 Copy(argp->p, p, l, char); 3471 p += l; 3472 if (lenp->ssize == -1) 3473 lenp->ssize = l; 3474 else 3475 lenp->ssize += l; 3476 } 3477 } 3478 3479 kid = topop; 3480 nextop = o; 3481 lastkidop = NULL; 3482 3483 for (argp = args; argp <= toparg; argp++) { 3484 /* only keep non-const args, except keep the first-in-next-chain 3485 * arg no matter what it is (but nulled if OP_CONST), because it 3486 * may be the entry point to this subtree from the previous 3487 * op_next. 3488 */ 3489 bool last = (argp == toparg); 3490 OP *prev; 3491 3492 /* set prev to the sibling *before* the arg to be cut out, 3493 * e.g. when cutting EXPR: 3494 * 3495 * | 3496 * kid= CONCAT 3497 * | 3498 * prev= CONCAT -- EXPR 3499 * | 3500 */ 3501 if (argp == args && kid->op_type != OP_CONCAT) { 3502 /* in e.g. '$x .= f(1)' there's no RHS concat tree 3503 * so the expression to be cut isn't kid->op_last but 3504 * kid itself */ 3505 OP *o1, *o2; 3506 /* find the op before kid */ 3507 o1 = NULL; 3508 o2 = cUNOPx(parentop)->op_first; 3509 while (o2 && o2 != kid) { 3510 o1 = o2; 3511 o2 = OpSIBLING(o2); 3512 } 3513 assert(o2 == kid); 3514 prev = o1; 3515 kid = parentop; 3516 } 3517 else if (kid == o && lastkidop) 3518 prev = last ? lastkidop : OpSIBLING(lastkidop); 3519 else 3520 prev = last ? NULL : cUNOPx(kid)->op_first; 3521 3522 if (!argp->p || last) { 3523 /* cut RH op */ 3524 OP *aop = op_sibling_splice(kid, prev, 1, NULL); 3525 /* and unshift to front of o */ 3526 op_sibling_splice(o, NULL, 0, aop); 3527 /* record the right-most op added to o: later we will 3528 * free anything to the right of it */ 3529 if (!lastkidop) 3530 lastkidop = aop; 3531 aop->op_next = nextop; 3532 if (last) { 3533 if (argp->p) 3534 /* null the const at start of op_next chain */ 3535 op_null(aop); 3536 } 3537 else if (prev) 3538 nextop = prev->op_next; 3539 } 3540 3541 /* the last two arguments are both attached to the same concat op */ 3542 if (argp < toparg - 1) 3543 kid = prev; 3544 } 3545 } 3546 3547 /* Populate the aux struct */ 3548 3549 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs; 3550 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str; 3551 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len; 3552 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str; 3553 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len; 3554 3555 /* if variant > 0, calculate a variant const string and lengths where 3556 * the utf8 version of the string will take 'variant' more bytes than 3557 * the plain one. */ 3558 3559 if (variant) { 3560 char *p = const_str; 3561 STRLEN ulen = total_len + variant; 3562 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS; 3563 UNOP_AUX_item *ulens = lens + (nargs + 1); 3564 char *up = (char*)PerlMemShared_malloc(ulen); 3565 SSize_t n; 3566 3567 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up; 3568 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen; 3569 3570 for (n = 0; n < (nargs + 1); n++) { 3571 SSize_t i; 3572 char * orig_up = up; 3573 for (i = (lens++)->ssize; i > 0; i--) { 3574 U8 c = *p++; 3575 append_utf8_from_native_byte(c, (U8**)&up); 3576 } 3577 (ulens++)->ssize = (i < 0) ? i : up - orig_up; 3578 } 3579 } 3580 3581 if (stringop) { 3582 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep 3583 * that op's first child - an ex-PUSHMARK - because the op_next of 3584 * the previous op may point to it (i.e. it's the entry point for 3585 * the o optree) 3586 */ 3587 OP *pmop = 3588 (stringop == o) 3589 ? op_sibling_splice(o, lastkidop, 1, NULL) 3590 : op_sibling_splice(stringop, NULL, 1, NULL); 3591 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK)); 3592 op_sibling_splice(o, NULL, 0, pmop); 3593 if (!lastkidop) 3594 lastkidop = pmop; 3595 } 3596 3597 /* Optimise 3598 * target = A.B.C... 3599 * target .= A.B.C... 3600 */ 3601 3602 if (targetop) { 3603 assert(!targmyop); 3604 3605 if (o->op_type == OP_SASSIGN) { 3606 /* Move the target subtree from being the last of o's children 3607 * to being the last of o's preserved children. 3608 * Note the difference between 'target = ...' and 'target .= ...': 3609 * for the former, target is executed last; for the latter, 3610 * first. 3611 */ 3612 kid = OpSIBLING(lastkidop); 3613 op_sibling_splice(o, kid, 1, NULL); /* cut target op */ 3614 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */ 3615 lastkidop->op_next = kid->op_next; 3616 lastkidop = targetop; 3617 } 3618 else { 3619 /* Move the target subtree from being the first of o's 3620 * original children to being the first of *all* o's children. 3621 */ 3622 if (lastkidop) { 3623 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */ 3624 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/ 3625 } 3626 else { 3627 /* if the RHS of .= doesn't contain a concat (e.g. 3628 * $x .= "foo"), it gets missed by the "strip ops from the 3629 * tree and add to o" loop earlier */ 3630 assert(topop->op_type != OP_CONCAT); 3631 if (stringop) { 3632 /* in e.g. $x .= "$y", move the $y expression 3633 * from being a child of OP_STRINGIFY to being the 3634 * second child of the OP_CONCAT 3635 */ 3636 assert(cUNOPx(stringop)->op_first == topop); 3637 op_sibling_splice(stringop, NULL, 1, NULL); 3638 op_sibling_splice(o, cUNOPo->op_first, 0, topop); 3639 } 3640 assert(topop == OpSIBLING(cBINOPo->op_first)); 3641 if (toparg->p) 3642 op_null(topop); 3643 lastkidop = topop; 3644 } 3645 } 3646 3647 if (is_targable) { 3648 /* optimise 3649 * my $lex = A.B.C... 3650 * $lex = A.B.C... 3651 * $lex .= A.B.C... 3652 * The original padsv op is kept but nulled in case it's the 3653 * entry point for the optree (which it will be for 3654 * '$lex .= ... ' 3655 */ 3656 private_flags |= OPpTARGET_MY; 3657 private_flags |= (targetop->op_private & OPpLVAL_INTRO); 3658 o->op_targ = targetop->op_targ; 3659 targetop->op_targ = 0; 3660 op_null(targetop); 3661 } 3662 else 3663 flags |= OPf_STACKED; 3664 } 3665 else if (targmyop) { 3666 private_flags |= OPpTARGET_MY; 3667 if (o != targmyop) { 3668 o->op_targ = targmyop->op_targ; 3669 targmyop->op_targ = 0; 3670 } 3671 } 3672 3673 /* detach the emaciated husk of the sprintf/concat optree and free it */ 3674 for (;;) { 3675 kid = op_sibling_splice(o, lastkidop, 1, NULL); 3676 if (!kid) 3677 break; 3678 op_free(kid); 3679 } 3680 3681 /* and convert o into a multiconcat */ 3682 3683 o->op_flags = (flags|OPf_KIDS|stacked_last 3684 |(o->op_flags & (OPf_WANT|OPf_PARENS))); 3685 o->op_private = private_flags; 3686 o->op_type = OP_MULTICONCAT; 3687 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT]; 3688 cUNOP_AUXo->op_aux = aux; 3689 } 3690 3691 3692 /* do all the final processing on an optree (e.g. running the peephole 3693 * optimiser on it), then attach it to cv (if cv is non-null) 3694 */ 3695 3696 static void 3697 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start) 3698 { 3699 OP **startp; 3700 3701 /* XXX for some reason, evals, require and main optrees are 3702 * never attached to their CV; instead they just hang off 3703 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start 3704 * and get manually freed when appropriate */ 3705 if (cv) 3706 startp = &CvSTART(cv); 3707 else 3708 startp = PL_in_eval? &PL_eval_start : &PL_main_start; 3709 3710 *startp = start; 3711 optree->op_private |= OPpREFCOUNTED; 3712 OpREFCNT_set(optree, 1); 3713 optimize_optree(optree); 3714 CALL_PEEP(*startp); 3715 finalize_optree(optree); 3716 S_prune_chain_head(startp); 3717 3718 if (cv) { 3719 /* now that optimizer has done its work, adjust pad values */ 3720 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT 3721 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB); 3722 } 3723 } 3724 3725 3726 /* 3727 =for apidoc optimize_optree 3728 3729 This function applies some optimisations to the optree in top-down order. 3730 It is called before the peephole optimizer, which processes ops in 3731 execution order. Note that finalize_optree() also does a top-down scan, 3732 but is called *after* the peephole optimizer. 3733 3734 =cut 3735 */ 3736 3737 void 3738 Perl_optimize_optree(pTHX_ OP* o) 3739 { 3740 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE; 3741 3742 ENTER; 3743 SAVEVPTR(PL_curcop); 3744 3745 optimize_op(o); 3746 3747 LEAVE; 3748 } 3749 3750 3751 #define warn_implicit_snail_cvsig(o) S_warn_implicit_snail_cvsig(aTHX_ o) 3752 static void 3753 S_warn_implicit_snail_cvsig(pTHX_ OP *o) 3754 { 3755 CV *cv = PL_compcv; 3756 while(cv && CvEVAL(cv)) 3757 cv = CvOUTSIDE(cv); 3758 3759 if(cv && CvSIGNATURE(cv)) 3760 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__ARGS_ARRAY_WITH_SIGNATURES), 3761 "Implicit use of @_ in %s with signatured subroutine is experimental", OP_DESC(o)); 3762 } 3763 3764 #define OP_ZOOM(o) (OP_TYPE_IS(o, OP_NULL) ? cUNOPx(o)->op_first : (o)) 3765 3766 /* helper for optimize_optree() which optimises one op then recurses 3767 * to optimise any children. 3768 */ 3769 3770 STATIC void 3771 S_optimize_op(pTHX_ OP* o) 3772 { 3773 OP *top_op = o; 3774 3775 PERL_ARGS_ASSERT_OPTIMIZE_OP; 3776 3777 while (1) { 3778 OP * next_kid = NULL; 3779 3780 assert(o->op_type != OP_FREED); 3781 3782 switch (o->op_type) { 3783 case OP_NEXTSTATE: 3784 case OP_DBSTATE: 3785 PL_curcop = ((COP*)o); /* for warnings */ 3786 break; 3787 3788 3789 case OP_CONCAT: 3790 case OP_SASSIGN: 3791 case OP_STRINGIFY: 3792 case OP_SPRINTF: 3793 S_maybe_multiconcat(aTHX_ o); 3794 break; 3795 3796 case OP_SUBST: 3797 if (cPMOPo->op_pmreplrootu.op_pmreplroot) { 3798 /* we can't assume that op_pmreplroot->op_sibparent == o 3799 * and that it is thus possible to walk back up the tree 3800 * past op_pmreplroot. So, although we try to avoid 3801 * recursing through op trees, do it here. After all, 3802 * there are unlikely to be many nested s///e's within 3803 * the replacement part of a s///e. 3804 */ 3805 optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot); 3806 } 3807 break; 3808 3809 case OP_RV2AV: 3810 { 3811 OP *first = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL; 3812 CV *cv = PL_compcv; 3813 while(cv && CvEVAL(cv)) 3814 cv = CvOUTSIDE(cv); 3815 3816 if(cv && CvSIGNATURE(cv) && 3817 OP_TYPE_IS(first, OP_GV) && cGVOPx_gv(first) == PL_defgv) { 3818 OP *parent = op_parent(o); 3819 while(OP_TYPE_IS(parent, OP_NULL)) 3820 parent = op_parent(parent); 3821 3822 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__ARGS_ARRAY_WITH_SIGNATURES), 3823 "Use of @_ in %s with signatured subroutine is experimental", OP_DESC(parent)); 3824 } 3825 break; 3826 } 3827 3828 case OP_SHIFT: 3829 case OP_POP: 3830 if(!CvUNIQUE(PL_compcv) && !(o->op_flags & OPf_KIDS)) 3831 warn_implicit_snail_cvsig(o); 3832 break; 3833 3834 case OP_ENTERSUB: 3835 if(!(o->op_flags & OPf_STACKED)) 3836 warn_implicit_snail_cvsig(o); 3837 break; 3838 3839 case OP_GOTO: 3840 { 3841 OP *first = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL; 3842 OP *ffirst; 3843 if(OP_TYPE_IS(first, OP_SREFGEN) && 3844 (ffirst = OP_ZOOM(cUNOPx(first)->op_first)) && 3845 OP_TYPE_IS(ffirst, OP_RV2CV)) 3846 warn_implicit_snail_cvsig(o); 3847 break; 3848 } 3849 3850 default: 3851 break; 3852 } 3853 3854 if (o->op_flags & OPf_KIDS) 3855 next_kid = cUNOPo->op_first; 3856 3857 /* if a kid hasn't been nominated to process, continue with the 3858 * next sibling, or if no siblings left, go back to the parent's 3859 * siblings and so on 3860 */ 3861 while (!next_kid) { 3862 if (o == top_op) 3863 return; /* at top; no parents/siblings to try */ 3864 if (OpHAS_SIBLING(o)) 3865 next_kid = o->op_sibparent; 3866 else 3867 o = o->op_sibparent; /*try parent's next sibling */ 3868 } 3869 3870 /* this label not yet used. Goto here if any code above sets 3871 * next-kid 3872 get_next_op: 3873 */ 3874 o = next_kid; 3875 } 3876 } 3877 3878 3879 /* 3880 =for apidoc finalize_optree 3881 3882 This function finalizes the optree. Should be called directly after 3883 the complete optree is built. It does some additional 3884 checking which can't be done in the normal C<ck_>xxx functions and makes 3885 the tree thread-safe. 3886 3887 =cut 3888 */ 3889 void 3890 Perl_finalize_optree(pTHX_ OP* o) 3891 { 3892 PERL_ARGS_ASSERT_FINALIZE_OPTREE; 3893 3894 ENTER; 3895 SAVEVPTR(PL_curcop); 3896 3897 finalize_op(o); 3898 3899 LEAVE; 3900 } 3901 3902 #ifdef USE_ITHREADS 3903 /* Relocate sv to the pad for thread safety. 3904 * Despite being a "constant", the SV is written to, 3905 * for reference counts, sv_upgrade() etc. */ 3906 PERL_STATIC_INLINE void 3907 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp) 3908 { 3909 PADOFFSET ix; 3910 PERL_ARGS_ASSERT_OP_RELOCATE_SV; 3911 if (!*svp) return; 3912 ix = pad_alloc(OP_CONST, SVf_READONLY); 3913 SvREFCNT_dec(PAD_SVl(ix)); 3914 PAD_SETSV(ix, *svp); 3915 /* XXX I don't know how this isn't readonly already. */ 3916 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix)); 3917 *svp = NULL; 3918 *targp = ix; 3919 } 3920 #endif 3921 3922 /* 3923 =for apidoc traverse_op_tree 3924 3925 Return the next op in a depth-first traversal of the op tree, 3926 returning NULL when the traversal is complete. 3927 3928 The initial call must supply the root of the tree as both top and o. 3929 3930 For now it's static, but it may be exposed to the API in the future. 3931 3932 =cut 3933 */ 3934 3935 STATIC OP* 3936 S_traverse_op_tree(pTHX_ OP *top, OP *o) { 3937 OP *sib; 3938 3939 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE; 3940 3941 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) { 3942 return cUNOPo->op_first; 3943 } 3944 else if ((sib = OpSIBLING(o))) { 3945 return sib; 3946 } 3947 else { 3948 OP *parent = o->op_sibparent; 3949 assert(!(o->op_moresib)); 3950 while (parent && parent != top) { 3951 OP *sib = OpSIBLING(parent); 3952 if (sib) 3953 return sib; 3954 parent = parent->op_sibparent; 3955 } 3956 3957 return NULL; 3958 } 3959 } 3960 3961 STATIC void 3962 S_finalize_op(pTHX_ OP* o) 3963 { 3964 OP * const top = o; 3965 PERL_ARGS_ASSERT_FINALIZE_OP; 3966 3967 do { 3968 assert(o->op_type != OP_FREED); 3969 3970 switch (o->op_type) { 3971 case OP_NEXTSTATE: 3972 case OP_DBSTATE: 3973 PL_curcop = ((COP*)o); /* for warnings */ 3974 break; 3975 case OP_EXEC: 3976 if (OpHAS_SIBLING(o)) { 3977 OP *sib = OpSIBLING(o); 3978 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE) 3979 && ckWARN(WARN_EXEC) 3980 && OpHAS_SIBLING(sib)) 3981 { 3982 const OPCODE type = OpSIBLING(sib)->op_type; 3983 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) { 3984 const line_t oldline = CopLINE(PL_curcop); 3985 CopLINE_set(PL_curcop, CopLINE((COP*)sib)); 3986 Perl_warner(aTHX_ packWARN(WARN_EXEC), 3987 "Statement unlikely to be reached"); 3988 Perl_warner(aTHX_ packWARN(WARN_EXEC), 3989 "\t(Maybe you meant system() when you said exec()?)\n"); 3990 CopLINE_set(PL_curcop, oldline); 3991 } 3992 } 3993 } 3994 break; 3995 3996 case OP_GV: 3997 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) { 3998 GV * const gv = cGVOPo_gv; 3999 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) { 4000 /* XXX could check prototype here instead of just carping */ 4001 SV * const sv = sv_newmortal(); 4002 gv_efullname3(sv, gv, NULL); 4003 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), 4004 "%" SVf "() called too early to check prototype", 4005 SVfARG(sv)); 4006 } 4007 } 4008 break; 4009 4010 case OP_CONST: 4011 if (cSVOPo->op_private & OPpCONST_STRICT) 4012 no_bareword_allowed(o); 4013 #ifdef USE_ITHREADS 4014 /* FALLTHROUGH */ 4015 case OP_HINTSEVAL: 4016 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ); 4017 #endif 4018 break; 4019 4020 #ifdef USE_ITHREADS 4021 /* Relocate all the METHOP's SVs to the pad for thread safety. */ 4022 case OP_METHOD_NAMED: 4023 case OP_METHOD_SUPER: 4024 case OP_METHOD_REDIR: 4025 case OP_METHOD_REDIR_SUPER: 4026 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ); 4027 break; 4028 #endif 4029 4030 case OP_HELEM: { 4031 UNOP *rop; 4032 SVOP *key_op; 4033 OP *kid; 4034 4035 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST) 4036 break; 4037 4038 rop = (UNOP*)((BINOP*)o)->op_first; 4039 4040 goto check_keys; 4041 4042 case OP_HSLICE: 4043 S_scalar_slice_warning(aTHX_ o); 4044 /* FALLTHROUGH */ 4045 4046 case OP_KVHSLICE: 4047 kid = OpSIBLING(cLISTOPo->op_first); 4048 if (/* I bet there's always a pushmark... */ 4049 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST) 4050 && OP_TYPE_ISNT_NN(kid, OP_CONST)) 4051 { 4052 break; 4053 } 4054 4055 key_op = (SVOP*)(kid->op_type == OP_CONST 4056 ? kid 4057 : OpSIBLING(kLISTOP->op_first)); 4058 4059 rop = (UNOP*)((LISTOP*)o)->op_last; 4060 4061 check_keys: 4062 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV) 4063 rop = NULL; 4064 S_check_hash_fields_and_hekify(aTHX_ rop, key_op, 1); 4065 break; 4066 } 4067 case OP_NULL: 4068 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE) 4069 break; 4070 /* FALLTHROUGH */ 4071 case OP_ASLICE: 4072 S_scalar_slice_warning(aTHX_ o); 4073 break; 4074 4075 case OP_SUBST: { 4076 if (cPMOPo->op_pmreplrootu.op_pmreplroot) 4077 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot); 4078 break; 4079 } 4080 default: 4081 break; 4082 } 4083 4084 #ifdef DEBUGGING 4085 if (o->op_flags & OPf_KIDS) { 4086 OP *kid; 4087 4088 /* check that op_last points to the last sibling, and that 4089 * the last op_sibling/op_sibparent field points back to the 4090 * parent, and that the only ops with KIDS are those which are 4091 * entitled to them */ 4092 U32 type = o->op_type; 4093 U32 family; 4094 bool has_last; 4095 4096 if (type == OP_NULL) { 4097 type = o->op_targ; 4098 /* ck_glob creates a null UNOP with ex-type GLOB 4099 * (which is a list op. So pretend it wasn't a listop */ 4100 if (type == OP_GLOB) 4101 type = OP_NULL; 4102 } 4103 family = PL_opargs[type] & OA_CLASS_MASK; 4104 4105 has_last = ( family == OA_BINOP 4106 || family == OA_LISTOP 4107 || family == OA_PMOP 4108 || family == OA_LOOP 4109 ); 4110 assert( has_last /* has op_first and op_last, or ... 4111 ... has (or may have) op_first: */ 4112 || family == OA_UNOP 4113 || family == OA_UNOP_AUX 4114 || family == OA_LOGOP 4115 || family == OA_BASEOP_OR_UNOP 4116 || family == OA_FILESTATOP 4117 || family == OA_LOOPEXOP 4118 || family == OA_METHOP 4119 || type == OP_CUSTOM 4120 || type == OP_NULL /* new_logop does this */ 4121 ); 4122 4123 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { 4124 if (!OpHAS_SIBLING(kid)) { 4125 if (has_last) 4126 assert(kid == cLISTOPo->op_last); 4127 assert(kid->op_sibparent == o); 4128 } 4129 } 4130 } 4131 #endif 4132 } while (( o = traverse_op_tree(top, o)) != NULL); 4133 } 4134 4135 static void 4136 S_mark_padname_lvalue(pTHX_ PADNAME *pn) 4137 { 4138 CV *cv = PL_compcv; 4139 PadnameLVALUE_on(pn); 4140 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) { 4141 cv = CvOUTSIDE(cv); 4142 /* RT #127786: cv can be NULL due to an eval within the DB package 4143 * called from an anon sub - anon subs don't have CvOUTSIDE() set 4144 * unless they contain an eval, but calling eval within DB 4145 * pretends the eval was done in the caller's scope. 4146 */ 4147 if (!cv) 4148 break; 4149 assert(CvPADLIST(cv)); 4150 pn = 4151 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)]; 4152 assert(PadnameLEN(pn)); 4153 PadnameLVALUE_on(pn); 4154 } 4155 } 4156 4157 static bool 4158 S_vivifies(const OPCODE type) 4159 { 4160 switch(type) { 4161 case OP_RV2AV: case OP_ASLICE: 4162 case OP_RV2HV: case OP_KVASLICE: 4163 case OP_RV2SV: case OP_HSLICE: 4164 case OP_AELEMFAST: case OP_KVHSLICE: 4165 case OP_HELEM: 4166 case OP_AELEM: 4167 return 1; 4168 } 4169 return 0; 4170 } 4171 4172 4173 /* apply lvalue reference (aliasing) context to the optree o. 4174 * E.g. in 4175 * \($x,$y) = (...) 4176 * o would be the list ($x,$y) and type would be OP_AASSIGN. 4177 * It may descend and apply this to children too, for example in 4178 * \( $cond ? $x, $y) = (...) 4179 */ 4180 4181 static void 4182 S_lvref(pTHX_ OP *o, I32 type) 4183 { 4184 OP *kid; 4185 OP * top_op = o; 4186 4187 while (1) { 4188 switch (o->op_type) { 4189 case OP_COND_EXPR: 4190 o = OpSIBLING(cUNOPo->op_first); 4191 continue; 4192 4193 case OP_PUSHMARK: 4194 goto do_next; 4195 4196 case OP_RV2AV: 4197 if (cUNOPo->op_first->op_type != OP_GV) goto badref; 4198 o->op_flags |= OPf_STACKED; 4199 if (o->op_flags & OPf_PARENS) { 4200 if (o->op_private & OPpLVAL_INTRO) { 4201 yyerror(Perl_form(aTHX_ "Can't modify reference to " 4202 "localized parenthesized array in list assignment")); 4203 goto do_next; 4204 } 4205 slurpy: 4206 OpTYPE_set(o, OP_LVAVREF); 4207 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE; 4208 o->op_flags |= OPf_MOD|OPf_REF; 4209 goto do_next; 4210 } 4211 o->op_private |= OPpLVREF_AV; 4212 goto checkgv; 4213 4214 case OP_RV2CV: 4215 kid = cUNOPo->op_first; 4216 if (kid->op_type == OP_NULL) 4217 kid = cUNOPx(OpSIBLING(kUNOP->op_first)) 4218 ->op_first; 4219 o->op_private = OPpLVREF_CV; 4220 if (kid->op_type == OP_GV) 4221 o->op_flags |= OPf_STACKED; 4222 else if (kid->op_type == OP_PADCV) { 4223 o->op_targ = kid->op_targ; 4224 kid->op_targ = 0; 4225 op_free(cUNOPo->op_first); 4226 cUNOPo->op_first = NULL; 4227 o->op_flags &=~ OPf_KIDS; 4228 } 4229 else goto badref; 4230 break; 4231 4232 case OP_RV2HV: 4233 if (o->op_flags & OPf_PARENS) { 4234 parenhash: 4235 yyerror(Perl_form(aTHX_ "Can't modify reference to " 4236 "parenthesized hash in list assignment")); 4237 goto do_next; 4238 } 4239 o->op_private |= OPpLVREF_HV; 4240 /* FALLTHROUGH */ 4241 case OP_RV2SV: 4242 checkgv: 4243 if (cUNOPo->op_first->op_type != OP_GV) goto badref; 4244 o->op_flags |= OPf_STACKED; 4245 break; 4246 4247 case OP_PADHV: 4248 if (o->op_flags & OPf_PARENS) goto parenhash; 4249 o->op_private |= OPpLVREF_HV; 4250 /* FALLTHROUGH */ 4251 case OP_PADSV: 4252 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); 4253 break; 4254 4255 case OP_PADAV: 4256 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); 4257 if (o->op_flags & OPf_PARENS) goto slurpy; 4258 o->op_private |= OPpLVREF_AV; 4259 break; 4260 4261 case OP_AELEM: 4262 case OP_HELEM: 4263 o->op_private |= OPpLVREF_ELEM; 4264 o->op_flags |= OPf_STACKED; 4265 break; 4266 4267 case OP_ASLICE: 4268 case OP_HSLICE: 4269 OpTYPE_set(o, OP_LVREFSLICE); 4270 o->op_private &= OPpLVAL_INTRO; 4271 goto do_next; 4272 4273 case OP_NULL: 4274 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ 4275 goto badref; 4276 else if (!(o->op_flags & OPf_KIDS)) 4277 goto do_next; 4278 4279 /* the code formerly only recursed into the first child of 4280 * a non ex-list OP_NULL. if we ever encounter such a null op with 4281 * more than one child, need to decide whether its ok to process 4282 * *all* its kids or not */ 4283 assert(o->op_targ == OP_LIST 4284 || !(OpHAS_SIBLING(cBINOPo->op_first))); 4285 /* FALLTHROUGH */ 4286 case OP_LIST: 4287 o = cLISTOPo->op_first; 4288 continue; 4289 4290 case OP_STUB: 4291 if (o->op_flags & OPf_PARENS) 4292 goto do_next; 4293 /* FALLTHROUGH */ 4294 default: 4295 badref: 4296 /* diag_listed_as: Can't modify reference to %s in %s assignment */ 4297 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s", 4298 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL 4299 ? "do block" 4300 : OP_DESC(o), 4301 PL_op_desc[type])); 4302 goto do_next; 4303 } 4304 4305 OpTYPE_set(o, OP_LVREF); 4306 o->op_private &= 4307 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE; 4308 if (type == OP_ENTERLOOP) 4309 o->op_private |= OPpLVREF_ITER; 4310 4311 do_next: 4312 while (1) { 4313 if (o == top_op) 4314 return; /* at top; no parents/siblings to try */ 4315 if (OpHAS_SIBLING(o)) { 4316 o = o->op_sibparent; 4317 break; 4318 } 4319 o = o->op_sibparent; /*try parent's next sibling */ 4320 } 4321 } /* while */ 4322 } 4323 4324 4325 PERL_STATIC_INLINE bool 4326 S_potential_mod_type(I32 type) 4327 { 4328 /* Types that only potentially result in modification. */ 4329 return type == OP_GREPSTART || type == OP_ENTERSUB 4330 || type == OP_REFGEN || type == OP_LEAVESUBLV; 4331 } 4332 4333 4334 /* 4335 =for apidoc op_lvalue 4336 4337 Propagate lvalue ("modifiable") context to an op and its children. 4338 C<type> represents the context type, roughly based on the type of op that 4339 would do the modifying, although C<local()> is represented by C<OP_NULL>, 4340 because it has no op type of its own (it is signalled by a flag on 4341 the lvalue op). 4342 4343 This function detects things that can't be modified, such as C<$x+1>, and 4344 generates errors for them. For example, C<$x+1 = 2> would cause it to be 4345 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>. 4346 4347 It also flags things that need to behave specially in an lvalue context, 4348 such as C<$$x = 5> which might have to vivify a reference in C<$x>. 4349 4350 =cut 4351 4352 Perl_op_lvalue_flags() is a non-API lower-level interface to 4353 op_lvalue(). The flags param has these bits: 4354 OP_LVALUE_NO_CROAK: return rather than croaking on error 4355 4356 */ 4357 4358 OP * 4359 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags) 4360 { 4361 OP *top_op = o; 4362 4363 if (!o || (PL_parser && PL_parser->error_count)) 4364 return o; 4365 4366 while (1) { 4367 OP *kid; 4368 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */ 4369 int localize = -1; 4370 OP *next_kid = NULL; 4371 4372 if ((o->op_private & OPpTARGET_MY) 4373 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ 4374 { 4375 goto do_next; 4376 } 4377 4378 /* elements of a list might be in void context because the list is 4379 in scalar context or because they are attribute sub calls */ 4380 if ((o->op_flags & OPf_WANT) == OPf_WANT_VOID) 4381 goto do_next; 4382 4383 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB; 4384 4385 switch (o->op_type) { 4386 case OP_UNDEF: 4387 if (type == OP_SASSIGN) 4388 goto nomod; 4389 PL_modcount++; 4390 goto do_next; 4391 4392 case OP_STUB: 4393 if ((o->op_flags & OPf_PARENS)) 4394 break; 4395 goto nomod; 4396 4397 case OP_ENTERSUB: 4398 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) && 4399 !(o->op_flags & OPf_STACKED)) { 4400 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */ 4401 assert(cUNOPo->op_first->op_type == OP_NULL); 4402 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ 4403 break; 4404 } 4405 else { /* lvalue subroutine call */ 4406 o->op_private |= OPpLVAL_INTRO; 4407 PL_modcount = RETURN_UNLIMITED_NUMBER; 4408 if (S_potential_mod_type(type)) { 4409 o->op_private |= OPpENTERSUB_INARGS; 4410 break; 4411 } 4412 else { /* Compile-time error message: */ 4413 OP *kid = cUNOPo->op_first; 4414 CV *cv; 4415 GV *gv; 4416 SV *namesv; 4417 4418 if (kid->op_type != OP_PUSHMARK) { 4419 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST) 4420 Perl_croak(aTHX_ 4421 "panic: unexpected lvalue entersub " 4422 "args: type/targ %ld:%" UVuf, 4423 (long)kid->op_type, (UV)kid->op_targ); 4424 kid = kLISTOP->op_first; 4425 } 4426 while (OpHAS_SIBLING(kid)) 4427 kid = OpSIBLING(kid); 4428 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) { 4429 break; /* Postpone until runtime */ 4430 } 4431 4432 kid = kUNOP->op_first; 4433 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV) 4434 kid = kUNOP->op_first; 4435 if (kid->op_type == OP_NULL) 4436 Perl_croak(aTHX_ 4437 "panic: unexpected constant lvalue entersub " 4438 "entry via type/targ %ld:%" UVuf, 4439 (long)kid->op_type, (UV)kid->op_targ); 4440 if (kid->op_type != OP_GV) { 4441 break; 4442 } 4443 4444 gv = kGVOP_gv; 4445 cv = isGV(gv) 4446 ? GvCV(gv) 4447 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV 4448 ? MUTABLE_CV(SvRV(gv)) 4449 : NULL; 4450 if (!cv) 4451 break; 4452 if (CvLVALUE(cv)) 4453 break; 4454 if (flags & OP_LVALUE_NO_CROAK) 4455 return NULL; 4456 4457 namesv = cv_name(cv, NULL, 0); 4458 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue " 4459 "subroutine call of &%" SVf " in %s", 4460 SVfARG(namesv), PL_op_desc[type]), 4461 SvUTF8(namesv)); 4462 goto do_next; 4463 } 4464 } 4465 /* FALLTHROUGH */ 4466 default: 4467 nomod: 4468 if (flags & OP_LVALUE_NO_CROAK) return NULL; 4469 /* grep, foreach, subcalls, refgen */ 4470 if (S_potential_mod_type(type)) 4471 break; 4472 yyerror(Perl_form(aTHX_ "Can't modify %s in %s", 4473 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL) 4474 ? "do block" 4475 : OP_DESC(o)), 4476 type ? PL_op_desc[type] : "local")); 4477 goto do_next; 4478 4479 case OP_PREINC: 4480 case OP_PREDEC: 4481 case OP_POW: 4482 case OP_MULTIPLY: 4483 case OP_DIVIDE: 4484 case OP_MODULO: 4485 case OP_ADD: 4486 case OP_SUBTRACT: 4487 case OP_CONCAT: 4488 case OP_LEFT_SHIFT: 4489 case OP_RIGHT_SHIFT: 4490 case OP_BIT_AND: 4491 case OP_BIT_XOR: 4492 case OP_BIT_OR: 4493 case OP_I_MULTIPLY: 4494 case OP_I_DIVIDE: 4495 case OP_I_MODULO: 4496 case OP_I_ADD: 4497 case OP_I_SUBTRACT: 4498 if (!(o->op_flags & OPf_STACKED)) 4499 goto nomod; 4500 PL_modcount++; 4501 break; 4502 4503 case OP_REPEAT: 4504 if (o->op_flags & OPf_STACKED) { 4505 PL_modcount++; 4506 break; 4507 } 4508 if (!(o->op_private & OPpREPEAT_DOLIST)) 4509 goto nomod; 4510 else { 4511 const I32 mods = PL_modcount; 4512 /* we recurse rather than iterate here because we need to 4513 * calculate and use the delta applied to PL_modcount by the 4514 * first child. So in something like 4515 * ($x, ($y) x 3) = split; 4516 * split knows that 4 elements are wanted 4517 */ 4518 modkids(cBINOPo->op_first, type); 4519 if (type != OP_AASSIGN) 4520 goto nomod; 4521 kid = cBINOPo->op_last; 4522 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) { 4523 const IV iv = SvIV(kSVOP_sv); 4524 if (PL_modcount != RETURN_UNLIMITED_NUMBER) 4525 PL_modcount = 4526 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv); 4527 } 4528 else 4529 PL_modcount = RETURN_UNLIMITED_NUMBER; 4530 } 4531 break; 4532 4533 case OP_COND_EXPR: 4534 localize = 1; 4535 next_kid = OpSIBLING(cUNOPo->op_first); 4536 break; 4537 4538 case OP_RV2AV: 4539 case OP_RV2HV: 4540 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { 4541 PL_modcount = RETURN_UNLIMITED_NUMBER; 4542 /* Treat \(@foo) like ordinary list, but still mark it as modi- 4543 fiable since some contexts need to know. */ 4544 o->op_flags |= OPf_MOD; 4545 goto do_next; 4546 } 4547 /* FALLTHROUGH */ 4548 case OP_RV2GV: 4549 if (scalar_mod_type(o, type)) 4550 goto nomod; 4551 ref(cUNOPo->op_first, o->op_type); 4552 /* FALLTHROUGH */ 4553 case OP_ASLICE: 4554 case OP_HSLICE: 4555 localize = 1; 4556 /* FALLTHROUGH */ 4557 case OP_AASSIGN: 4558 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */ 4559 if (type == OP_LEAVESUBLV && ( 4560 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV) 4561 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR 4562 )) 4563 o->op_private |= OPpMAYBE_LVSUB; 4564 /* FALLTHROUGH */ 4565 case OP_NEXTSTATE: 4566 case OP_DBSTATE: 4567 PL_modcount = RETURN_UNLIMITED_NUMBER; 4568 break; 4569 4570 case OP_KVHSLICE: 4571 case OP_KVASLICE: 4572 case OP_AKEYS: 4573 if (type == OP_LEAVESUBLV) 4574 o->op_private |= OPpMAYBE_LVSUB; 4575 goto nomod; 4576 4577 case OP_AVHVSWITCH: 4578 if (type == OP_LEAVESUBLV 4579 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS) 4580 o->op_private |= OPpMAYBE_LVSUB; 4581 goto nomod; 4582 4583 case OP_AV2ARYLEN: 4584 PL_hints |= HINT_BLOCK_SCOPE; 4585 if (type == OP_LEAVESUBLV) 4586 o->op_private |= OPpMAYBE_LVSUB; 4587 PL_modcount++; 4588 break; 4589 4590 case OP_RV2SV: 4591 ref(cUNOPo->op_first, o->op_type); 4592 localize = 1; 4593 /* FALLTHROUGH */ 4594 case OP_GV: 4595 PL_hints |= HINT_BLOCK_SCOPE; 4596 /* FALLTHROUGH */ 4597 case OP_SASSIGN: 4598 case OP_ANDASSIGN: 4599 case OP_ORASSIGN: 4600 case OP_DORASSIGN: 4601 PL_modcount++; 4602 break; 4603 4604 case OP_AELEMFAST: 4605 case OP_AELEMFAST_LEX: 4606 localize = -1; 4607 PL_modcount++; 4608 break; 4609 4610 case OP_PADAV: 4611 case OP_PADHV: 4612 PL_modcount = RETURN_UNLIMITED_NUMBER; 4613 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) 4614 { 4615 /* Treat \(@foo) like ordinary list, but still mark it as modi- 4616 fiable since some contexts need to know. */ 4617 o->op_flags |= OPf_MOD; 4618 goto do_next; 4619 } 4620 if (scalar_mod_type(o, type)) 4621 goto nomod; 4622 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR 4623 && type == OP_LEAVESUBLV) 4624 o->op_private |= OPpMAYBE_LVSUB; 4625 /* FALLTHROUGH */ 4626 case OP_PADSV: 4627 PL_modcount++; 4628 if (!type) /* local() */ 4629 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf, 4630 PNfARG(PAD_COMPNAME(o->op_targ))); 4631 if (!(o->op_private & OPpLVAL_INTRO) 4632 || ( type != OP_SASSIGN && type != OP_AASSIGN 4633 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) )) 4634 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ)); 4635 break; 4636 4637 case OP_PUSHMARK: 4638 localize = 0; 4639 break; 4640 4641 case OP_KEYS: 4642 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type)) 4643 goto nomod; 4644 goto lvalue_func; 4645 case OP_SUBSTR: 4646 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */ 4647 goto nomod; 4648 /* FALLTHROUGH */ 4649 case OP_POS: 4650 case OP_VEC: 4651 lvalue_func: 4652 if (type == OP_LEAVESUBLV) 4653 o->op_private |= OPpMAYBE_LVSUB; 4654 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) { 4655 /* we recurse rather than iterate here because the child 4656 * needs to be processed with a different 'type' parameter */ 4657 4658 /* substr and vec */ 4659 /* If this op is in merely potential (non-fatal) modifiable 4660 context, then apply OP_ENTERSUB context to 4661 the kid op (to avoid croaking). Other- 4662 wise pass this op’s own type so the correct op is mentioned 4663 in error messages. */ 4664 op_lvalue(OpSIBLING(cBINOPo->op_first), 4665 S_potential_mod_type(type) 4666 ? (I32)OP_ENTERSUB 4667 : o->op_type); 4668 } 4669 break; 4670 4671 case OP_AELEM: 4672 case OP_HELEM: 4673 ref(cBINOPo->op_first, o->op_type); 4674 if (type == OP_ENTERSUB && 4675 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) 4676 o->op_private |= OPpLVAL_DEFER; 4677 if (type == OP_LEAVESUBLV) 4678 o->op_private |= OPpMAYBE_LVSUB; 4679 localize = 1; 4680 PL_modcount++; 4681 break; 4682 4683 case OP_LEAVE: 4684 case OP_LEAVELOOP: 4685 o->op_private |= OPpLVALUE; 4686 /* FALLTHROUGH */ 4687 case OP_SCOPE: 4688 case OP_ENTER: 4689 case OP_LINESEQ: 4690 localize = 0; 4691 if (o->op_flags & OPf_KIDS) 4692 next_kid = cLISTOPo->op_last; 4693 break; 4694 4695 case OP_NULL: 4696 localize = 0; 4697 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ 4698 goto nomod; 4699 else if (!(o->op_flags & OPf_KIDS)) 4700 break; 4701 4702 if (o->op_targ != OP_LIST) { 4703 OP *sib = OpSIBLING(cLISTOPo->op_first); 4704 /* OP_TRANS and OP_TRANSR with argument have a weird optree 4705 * that looks like 4706 * 4707 * null 4708 * arg 4709 * trans 4710 * 4711 * compared with things like OP_MATCH which have the argument 4712 * as a child: 4713 * 4714 * match 4715 * arg 4716 * 4717 * so handle specially to correctly get "Can't modify" croaks etc 4718 */ 4719 4720 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR)) 4721 { 4722 /* this should trigger a "Can't modify transliteration" err */ 4723 op_lvalue(sib, type); 4724 } 4725 next_kid = cBINOPo->op_first; 4726 /* we assume OP_NULLs which aren't ex-list have no more than 2 4727 * children. If this assumption is wrong, increase the scan 4728 * limit below */ 4729 assert( !OpHAS_SIBLING(next_kid) 4730 || !OpHAS_SIBLING(OpSIBLING(next_kid))); 4731 break; 4732 } 4733 /* FALLTHROUGH */ 4734 case OP_LIST: 4735 localize = 0; 4736 next_kid = cLISTOPo->op_first; 4737 break; 4738 4739 case OP_COREARGS: 4740 goto do_next; 4741 4742 case OP_AND: 4743 case OP_OR: 4744 if (type == OP_LEAVESUBLV 4745 || !S_vivifies(cLOGOPo->op_first->op_type)) 4746 next_kid = cLOGOPo->op_first; 4747 else if (type == OP_LEAVESUBLV 4748 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type)) 4749 next_kid = OpSIBLING(cLOGOPo->op_first); 4750 goto nomod; 4751 4752 case OP_SREFGEN: 4753 if (type == OP_NULL) { /* local */ 4754 local_refgen: 4755 if (!FEATURE_MYREF_IS_ENABLED) 4756 Perl_croak(aTHX_ "The experimental declared_refs " 4757 "feature is not enabled"); 4758 Perl_ck_warner_d(aTHX_ 4759 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS), 4760 "Declaring references is experimental"); 4761 next_kid = cUNOPo->op_first; 4762 goto do_next; 4763 } 4764 if (type != OP_AASSIGN && type != OP_SASSIGN 4765 && type != OP_ENTERLOOP) 4766 goto nomod; 4767 /* Don’t bother applying lvalue context to the ex-list. */ 4768 kid = cUNOPx(cUNOPo->op_first)->op_first; 4769 assert (!OpHAS_SIBLING(kid)); 4770 goto kid_2lvref; 4771 case OP_REFGEN: 4772 if (type == OP_NULL) /* local */ 4773 goto local_refgen; 4774 if (type != OP_AASSIGN) goto nomod; 4775 kid = cUNOPo->op_first; 4776 kid_2lvref: 4777 { 4778 const U8 ec = PL_parser ? PL_parser->error_count : 0; 4779 S_lvref(aTHX_ kid, type); 4780 if (!PL_parser || PL_parser->error_count == ec) { 4781 if (!FEATURE_REFALIASING_IS_ENABLED) 4782 Perl_croak(aTHX_ 4783 "Experimental aliasing via reference not enabled"); 4784 Perl_ck_warner_d(aTHX_ 4785 packWARN(WARN_EXPERIMENTAL__REFALIASING), 4786 "Aliasing via reference is experimental"); 4787 } 4788 } 4789 if (o->op_type == OP_REFGEN) 4790 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */ 4791 op_null(o); 4792 goto do_next; 4793 4794 case OP_SPLIT: 4795 if ((o->op_private & OPpSPLIT_ASSIGN)) { 4796 /* This is actually @array = split. */ 4797 PL_modcount = RETURN_UNLIMITED_NUMBER; 4798 break; 4799 } 4800 goto nomod; 4801 4802 case OP_SCALAR: 4803 op_lvalue(cUNOPo->op_first, OP_ENTERSUB); 4804 goto nomod; 4805 } 4806 4807 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that 4808 their argument is a filehandle; thus \stat(".") should not set 4809 it. AMS 20011102 */ 4810 if (type == OP_REFGEN && OP_IS_STAT(o->op_type)) 4811 goto do_next; 4812 4813 if (type != OP_LEAVESUBLV) 4814 o->op_flags |= OPf_MOD; 4815 4816 if (type == OP_AASSIGN || type == OP_SASSIGN) 4817 o->op_flags |= OPf_SPECIAL 4818 |(o->op_type == OP_ENTERSUB ? 0 : OPf_REF); 4819 else if (!type) { /* local() */ 4820 switch (localize) { 4821 case 1: 4822 o->op_private |= OPpLVAL_INTRO; 4823 o->op_flags &= ~OPf_SPECIAL; 4824 PL_hints |= HINT_BLOCK_SCOPE; 4825 break; 4826 case 0: 4827 break; 4828 case -1: 4829 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), 4830 "Useless localization of %s", OP_DESC(o)); 4831 } 4832 } 4833 else if (type != OP_GREPSTART && type != OP_ENTERSUB 4834 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB) 4835 o->op_flags |= OPf_REF; 4836 4837 do_next: 4838 while (!next_kid) { 4839 if (o == top_op) 4840 return top_op; /* at top; no parents/siblings to try */ 4841 if (OpHAS_SIBLING(o)) { 4842 next_kid = o->op_sibparent; 4843 if (!OpHAS_SIBLING(next_kid)) { 4844 /* a few node types don't recurse into their second child */ 4845 OP *parent = next_kid->op_sibparent; 4846 I32 ptype = parent->op_type; 4847 if ( (ptype == OP_NULL && parent->op_targ != OP_LIST) 4848 || ( (ptype == OP_AND || ptype == OP_OR) 4849 && (type != OP_LEAVESUBLV 4850 && S_vivifies(next_kid->op_type)) 4851 ) 4852 ) { 4853 /*try parent's next sibling */ 4854 o = parent; 4855 next_kid = NULL; 4856 } 4857 } 4858 } 4859 else 4860 o = o->op_sibparent; /*try parent's next sibling */ 4861 4862 } 4863 o = next_kid; 4864 4865 } /* while */ 4866 4867 } 4868 4869 4870 STATIC bool 4871 S_scalar_mod_type(const OP *o, I32 type) 4872 { 4873 switch (type) { 4874 case OP_POS: 4875 case OP_SASSIGN: 4876 if (o && o->op_type == OP_RV2GV) 4877 return FALSE; 4878 /* FALLTHROUGH */ 4879 case OP_PREINC: 4880 case OP_PREDEC: 4881 case OP_POSTINC: 4882 case OP_POSTDEC: 4883 case OP_I_PREINC: 4884 case OP_I_PREDEC: 4885 case OP_I_POSTINC: 4886 case OP_I_POSTDEC: 4887 case OP_POW: 4888 case OP_MULTIPLY: 4889 case OP_DIVIDE: 4890 case OP_MODULO: 4891 case OP_REPEAT: 4892 case OP_ADD: 4893 case OP_SUBTRACT: 4894 case OP_I_MULTIPLY: 4895 case OP_I_DIVIDE: 4896 case OP_I_MODULO: 4897 case OP_I_ADD: 4898 case OP_I_SUBTRACT: 4899 case OP_LEFT_SHIFT: 4900 case OP_RIGHT_SHIFT: 4901 case OP_BIT_AND: 4902 case OP_BIT_XOR: 4903 case OP_BIT_OR: 4904 case OP_NBIT_AND: 4905 case OP_NBIT_XOR: 4906 case OP_NBIT_OR: 4907 case OP_SBIT_AND: 4908 case OP_SBIT_XOR: 4909 case OP_SBIT_OR: 4910 case OP_CONCAT: 4911 case OP_SUBST: 4912 case OP_TRANS: 4913 case OP_TRANSR: 4914 case OP_READ: 4915 case OP_SYSREAD: 4916 case OP_RECV: 4917 case OP_ANDASSIGN: 4918 case OP_ORASSIGN: 4919 case OP_DORASSIGN: 4920 case OP_VEC: 4921 case OP_SUBSTR: 4922 return TRUE; 4923 default: 4924 return FALSE; 4925 } 4926 } 4927 4928 STATIC bool 4929 S_is_handle_constructor(const OP *o, I32 numargs) 4930 { 4931 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR; 4932 4933 switch (o->op_type) { 4934 case OP_PIPE_OP: 4935 case OP_SOCKPAIR: 4936 if (numargs == 2) 4937 return TRUE; 4938 /* FALLTHROUGH */ 4939 case OP_SYSOPEN: 4940 case OP_OPEN: 4941 case OP_SELECT: /* XXX c.f. SelectSaver.pm */ 4942 case OP_SOCKET: 4943 case OP_OPEN_DIR: 4944 case OP_ACCEPT: 4945 if (numargs == 1) 4946 return TRUE; 4947 /* FALLTHROUGH */ 4948 default: 4949 return FALSE; 4950 } 4951 } 4952 4953 static OP * 4954 S_refkids(pTHX_ OP *o, I32 type) 4955 { 4956 if (o && o->op_flags & OPf_KIDS) { 4957 OP *kid; 4958 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 4959 ref(kid, type); 4960 } 4961 return o; 4962 } 4963 4964 4965 /* Apply reference (autovivification) context to the subtree at o. 4966 * For example in 4967 * push @{expression}, ....; 4968 * o will be the head of 'expression' and type will be OP_RV2AV. 4969 * It marks the op o (or a suitable child) as autovivifying, e.g. by 4970 * setting OPf_MOD. 4971 * For OP_RV2AV/OP_PADAV and OP_RV2HV/OP_PADHV sets OPf_REF too if 4972 * set_op_ref is true. 4973 * 4974 * Also calls scalar(o). 4975 */ 4976 4977 OP * 4978 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref) 4979 { 4980 OP * top_op = o; 4981 4982 PERL_ARGS_ASSERT_DOREF; 4983 4984 if (PL_parser && PL_parser->error_count) 4985 return o; 4986 4987 while (1) { 4988 switch (o->op_type) { 4989 case OP_ENTERSUB: 4990 if ((type == OP_EXISTS || type == OP_DEFINED) && 4991 !(o->op_flags & OPf_STACKED)) { 4992 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */ 4993 assert(cUNOPo->op_first->op_type == OP_NULL); 4994 /* disable pushmark */ 4995 op_null(((LISTOP*)cUNOPo->op_first)->op_first); 4996 o->op_flags |= OPf_SPECIAL; 4997 } 4998 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){ 4999 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV 5000 : type == OP_RV2HV ? OPpDEREF_HV 5001 : OPpDEREF_SV); 5002 o->op_flags |= OPf_MOD; 5003 } 5004 5005 break; 5006 5007 case OP_COND_EXPR: 5008 o = OpSIBLING(cUNOPo->op_first); 5009 continue; 5010 5011 case OP_RV2SV: 5012 if (type == OP_DEFINED) 5013 o->op_flags |= OPf_SPECIAL; /* don't create GV */ 5014 /* FALLTHROUGH */ 5015 case OP_PADSV: 5016 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { 5017 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV 5018 : type == OP_RV2HV ? OPpDEREF_HV 5019 : OPpDEREF_SV); 5020 o->op_flags |= OPf_MOD; 5021 } 5022 if (o->op_flags & OPf_KIDS) { 5023 type = o->op_type; 5024 o = cUNOPo->op_first; 5025 continue; 5026 } 5027 break; 5028 5029 case OP_RV2AV: 5030 case OP_RV2HV: 5031 if (set_op_ref) 5032 o->op_flags |= OPf_REF; 5033 /* FALLTHROUGH */ 5034 case OP_RV2GV: 5035 if (type == OP_DEFINED) 5036 o->op_flags |= OPf_SPECIAL; /* don't create GV */ 5037 type = o->op_type; 5038 o = cUNOPo->op_first; 5039 continue; 5040 5041 case OP_PADAV: 5042 case OP_PADHV: 5043 if (set_op_ref) 5044 o->op_flags |= OPf_REF; 5045 break; 5046 5047 case OP_SCALAR: 5048 case OP_NULL: 5049 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED) 5050 break; 5051 o = cBINOPo->op_first; 5052 continue; 5053 5054 case OP_AELEM: 5055 case OP_HELEM: 5056 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { 5057 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV 5058 : type == OP_RV2HV ? OPpDEREF_HV 5059 : OPpDEREF_SV); 5060 o->op_flags |= OPf_MOD; 5061 } 5062 type = o->op_type; 5063 o = cBINOPo->op_first; 5064 continue;; 5065 5066 case OP_SCOPE: 5067 case OP_LEAVE: 5068 set_op_ref = FALSE; 5069 /* FALLTHROUGH */ 5070 case OP_ENTER: 5071 case OP_LIST: 5072 if (!(o->op_flags & OPf_KIDS)) 5073 break; 5074 o = cLISTOPo->op_last; 5075 continue; 5076 5077 default: 5078 break; 5079 } /* switch */ 5080 5081 while (1) { 5082 if (o == top_op) 5083 return scalar(top_op); /* at top; no parents/siblings to try */ 5084 if (OpHAS_SIBLING(o)) { 5085 o = o->op_sibparent; 5086 /* Normally skip all siblings and go straight to the parent; 5087 * the only op that requires two children to be processed 5088 * is OP_COND_EXPR */ 5089 if (!OpHAS_SIBLING(o) 5090 && o->op_sibparent->op_type == OP_COND_EXPR) 5091 break; 5092 continue; 5093 } 5094 o = o->op_sibparent; /*try parent's next sibling */ 5095 } 5096 } /* while */ 5097 } 5098 5099 5100 STATIC OP * 5101 S_dup_attrlist(pTHX_ OP *o) 5102 { 5103 OP *rop; 5104 5105 PERL_ARGS_ASSERT_DUP_ATTRLIST; 5106 5107 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids, 5108 * where the first kid is OP_PUSHMARK and the remaining ones 5109 * are OP_CONST. We need to push the OP_CONST values. 5110 */ 5111 if (o->op_type == OP_CONST) 5112 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv)); 5113 else { 5114 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS)); 5115 rop = NULL; 5116 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) { 5117 if (o->op_type == OP_CONST) 5118 rop = op_append_elem(OP_LIST, rop, 5119 newSVOP(OP_CONST, o->op_flags, 5120 SvREFCNT_inc_NN(cSVOPo->op_sv))); 5121 } 5122 } 5123 return rop; 5124 } 5125 5126 STATIC void 5127 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs) 5128 { 5129 PERL_ARGS_ASSERT_APPLY_ATTRS; 5130 { 5131 SV * const stashsv = newSVhek(HvNAME_HEK(stash)); 5132 5133 /* fake up C<use attributes $pkg,$rv,@attrs> */ 5134 5135 #define ATTRSMODULE "attributes" 5136 #define ATTRSMODULE_PM "attributes.pm" 5137 5138 Perl_load_module( 5139 aTHX_ PERL_LOADMOD_IMPORT_OPS, 5140 newSVpvs(ATTRSMODULE), 5141 NULL, 5142 op_prepend_elem(OP_LIST, 5143 newSVOP(OP_CONST, 0, stashsv), 5144 op_prepend_elem(OP_LIST, 5145 newSVOP(OP_CONST, 0, 5146 newRV(target)), 5147 dup_attrlist(attrs)))); 5148 } 5149 } 5150 5151 STATIC void 5152 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp) 5153 { 5154 OP *pack, *imop, *arg; 5155 SV *meth, *stashsv, **svp; 5156 5157 PERL_ARGS_ASSERT_APPLY_ATTRS_MY; 5158 5159 if (!attrs) 5160 return; 5161 5162 assert(target->op_type == OP_PADSV || 5163 target->op_type == OP_PADHV || 5164 target->op_type == OP_PADAV); 5165 5166 /* Ensure that attributes.pm is loaded. */ 5167 /* Don't force the C<use> if we don't need it. */ 5168 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE); 5169 if (svp && *svp != &PL_sv_undef) 5170 NOOP; /* already in %INC */ 5171 else 5172 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, 5173 newSVpvs(ATTRSMODULE), NULL); 5174 5175 /* Need package name for method call. */ 5176 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE)); 5177 5178 /* Build up the real arg-list. */ 5179 stashsv = newSVhek(HvNAME_HEK(stash)); 5180 5181 arg = newOP(OP_PADSV, 0); 5182 arg->op_targ = target->op_targ; 5183 arg = op_prepend_elem(OP_LIST, 5184 newSVOP(OP_CONST, 0, stashsv), 5185 op_prepend_elem(OP_LIST, 5186 newUNOP(OP_REFGEN, 0, 5187 arg), 5188 dup_attrlist(attrs))); 5189 5190 /* Fake up a method call to import */ 5191 meth = newSVpvs_share("import"); 5192 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID, 5193 op_append_elem(OP_LIST, 5194 op_prepend_elem(OP_LIST, pack, arg), 5195 newMETHOP_named(OP_METHOD_NAMED, 0, meth))); 5196 5197 /* Combine the ops. */ 5198 *imopsp = op_append_elem(OP_LIST, *imopsp, imop); 5199 } 5200 5201 /* 5202 =notfor apidoc apply_attrs_string 5203 5204 Attempts to apply a list of attributes specified by the C<attrstr> and 5205 C<len> arguments to the subroutine identified by the C<cv> argument which 5206 is expected to be associated with the package identified by the C<stashpv> 5207 argument (see L<attributes>). It gets this wrong, though, in that it 5208 does not correctly identify the boundaries of the individual attribute 5209 specifications within C<attrstr>. This is not really intended for the 5210 public API, but has to be listed here for systems such as AIX which 5211 need an explicit export list for symbols. (It's called from XS code 5212 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it 5213 to respect attribute syntax properly would be welcome. 5214 5215 =cut 5216 */ 5217 5218 void 5219 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv, 5220 const char *attrstr, STRLEN len) 5221 { 5222 OP *attrs = NULL; 5223 5224 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING; 5225 5226 if (!len) { 5227 len = strlen(attrstr); 5228 } 5229 5230 while (len) { 5231 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ; 5232 if (len) { 5233 const char * const sstr = attrstr; 5234 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ; 5235 attrs = op_append_elem(OP_LIST, attrs, 5236 newSVOP(OP_CONST, 0, 5237 newSVpvn(sstr, attrstr-sstr))); 5238 } 5239 } 5240 5241 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, 5242 newSVpvs(ATTRSMODULE), 5243 NULL, op_prepend_elem(OP_LIST, 5244 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)), 5245 op_prepend_elem(OP_LIST, 5246 newSVOP(OP_CONST, 0, 5247 newRV(MUTABLE_SV(cv))), 5248 attrs))); 5249 } 5250 5251 STATIC void 5252 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name, 5253 bool curstash) 5254 { 5255 OP *new_proto = NULL; 5256 STRLEN pvlen; 5257 char *pv; 5258 OP *o; 5259 5260 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR; 5261 5262 if (!*attrs) 5263 return; 5264 5265 o = *attrs; 5266 if (o->op_type == OP_CONST) { 5267 pv = SvPV(cSVOPo_sv, pvlen); 5268 if (memBEGINs(pv, pvlen, "prototype(")) { 5269 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv)); 5270 SV ** const tmpo = cSVOPx_svp(o); 5271 SvREFCNT_dec(cSVOPo_sv); 5272 *tmpo = tmpsv; 5273 new_proto = o; 5274 *attrs = NULL; 5275 } 5276 } else if (o->op_type == OP_LIST) { 5277 OP * lasto; 5278 assert(o->op_flags & OPf_KIDS); 5279 lasto = cLISTOPo->op_first; 5280 assert(lasto->op_type == OP_PUSHMARK); 5281 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) { 5282 if (o->op_type == OP_CONST) { 5283 pv = SvPV(cSVOPo_sv, pvlen); 5284 if (memBEGINs(pv, pvlen, "prototype(")) { 5285 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv)); 5286 SV ** const tmpo = cSVOPx_svp(o); 5287 SvREFCNT_dec(cSVOPo_sv); 5288 *tmpo = tmpsv; 5289 if (new_proto && ckWARN(WARN_MISC)) { 5290 STRLEN new_len; 5291 const char * newp = SvPV(cSVOPo_sv, new_len); 5292 Perl_warner(aTHX_ packWARN(WARN_MISC), 5293 "Attribute prototype(%" UTF8f ") discards earlier prototype attribute in same sub", 5294 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp)); 5295 op_free(new_proto); 5296 } 5297 else if (new_proto) 5298 op_free(new_proto); 5299 new_proto = o; 5300 /* excise new_proto from the list */ 5301 op_sibling_splice(*attrs, lasto, 1, NULL); 5302 o = lasto; 5303 continue; 5304 } 5305 } 5306 lasto = o; 5307 } 5308 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs 5309 would get pulled in with no real need */ 5310 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) { 5311 op_free(*attrs); 5312 *attrs = NULL; 5313 } 5314 } 5315 5316 if (new_proto) { 5317 SV *svname; 5318 if (isGV(name)) { 5319 svname = sv_newmortal(); 5320 gv_efullname3(svname, name, NULL); 5321 } 5322 else if (SvPOK(name) && *SvPVX((SV *)name) == '&') 5323 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP); 5324 else 5325 svname = (SV *)name; 5326 if (ckWARN(WARN_ILLEGALPROTO)) 5327 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE, 5328 curstash); 5329 if (*proto && ckWARN(WARN_PROTOTYPE)) { 5330 STRLEN old_len, new_len; 5331 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len); 5332 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len); 5333 5334 if (curstash && svname == (SV *)name 5335 && !memchr(SvPVX(svname), ':', SvCUR(svname))) { 5336 svname = sv_2mortal(newSVsv(PL_curstname)); 5337 sv_catpvs(svname, "::"); 5338 sv_catsv(svname, (SV *)name); 5339 } 5340 5341 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), 5342 "Prototype '%" UTF8f "' overridden by attribute 'prototype(%" UTF8f ")'" 5343 " in %" SVf, 5344 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp), 5345 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp), 5346 SVfARG(svname)); 5347 } 5348 if (*proto) 5349 op_free(*proto); 5350 *proto = new_proto; 5351 } 5352 } 5353 5354 static void 5355 S_cant_declare(pTHX_ OP *o) 5356 { 5357 if (o->op_type == OP_NULL 5358 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS) 5359 o = cUNOPo->op_first; 5360 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"", 5361 o->op_type == OP_NULL 5362 && o->op_flags & OPf_SPECIAL 5363 ? "do block" 5364 : OP_DESC(o), 5365 PL_parser->in_my == KEY_our ? "our" : 5366 PL_parser->in_my == KEY_state ? "state" : 5367 "my")); 5368 } 5369 5370 STATIC OP * 5371 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp) 5372 { 5373 I32 type; 5374 const bool stately = PL_parser && PL_parser->in_my == KEY_state; 5375 5376 PERL_ARGS_ASSERT_MY_KID; 5377 5378 if (!o || (PL_parser && PL_parser->error_count)) 5379 return o; 5380 5381 type = o->op_type; 5382 5383 if (OP_TYPE_IS_OR_WAS(o, OP_LIST)) { 5384 OP *kid; 5385 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 5386 my_kid(kid, attrs, imopsp); 5387 return o; 5388 } else if (type == OP_UNDEF || type == OP_STUB) { 5389 return o; 5390 } else if (type == OP_RV2SV || /* "our" declaration */ 5391 type == OP_RV2AV || 5392 type == OP_RV2HV) { 5393 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */ 5394 S_cant_declare(aTHX_ o); 5395 } else if (attrs) { 5396 GV * const gv = cGVOPx_gv(cUNOPo->op_first); 5397 assert(PL_parser); 5398 PL_parser->in_my = FALSE; 5399 PL_parser->in_my_stash = NULL; 5400 apply_attrs(GvSTASH(gv), 5401 (type == OP_RV2SV ? GvSVn(gv) : 5402 type == OP_RV2AV ? MUTABLE_SV(GvAVn(gv)) : 5403 type == OP_RV2HV ? MUTABLE_SV(GvHVn(gv)) : MUTABLE_SV(gv)), 5404 attrs); 5405 } 5406 o->op_private |= OPpOUR_INTRO; 5407 return o; 5408 } 5409 else if (type == OP_REFGEN || type == OP_SREFGEN) { 5410 if (!FEATURE_MYREF_IS_ENABLED) 5411 Perl_croak(aTHX_ "The experimental declared_refs " 5412 "feature is not enabled"); 5413 Perl_ck_warner_d(aTHX_ 5414 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS), 5415 "Declaring references is experimental"); 5416 /* Kid is a nulled OP_LIST, handled above. */ 5417 my_kid(cUNOPo->op_first, attrs, imopsp); 5418 return o; 5419 } 5420 else if (type != OP_PADSV && 5421 type != OP_PADAV && 5422 type != OP_PADHV && 5423 type != OP_PUSHMARK) 5424 { 5425 S_cant_declare(aTHX_ o); 5426 return o; 5427 } 5428 else if (attrs && type != OP_PUSHMARK) { 5429 HV *stash; 5430 5431 assert(PL_parser); 5432 PL_parser->in_my = FALSE; 5433 PL_parser->in_my_stash = NULL; 5434 5435 /* check for C<my Dog $spot> when deciding package */ 5436 stash = PAD_COMPNAME_TYPE(o->op_targ); 5437 if (!stash) 5438 stash = PL_curstash; 5439 apply_attrs_my(stash, o, attrs, imopsp); 5440 } 5441 o->op_flags |= OPf_MOD; 5442 o->op_private |= OPpLVAL_INTRO; 5443 if (stately) 5444 o->op_private |= OPpPAD_STATE; 5445 return o; 5446 } 5447 5448 OP * 5449 Perl_my_attrs(pTHX_ OP *o, OP *attrs) 5450 { 5451 OP *rops; 5452 int maybe_scalar = 0; 5453 5454 PERL_ARGS_ASSERT_MY_ATTRS; 5455 5456 /* [perl #17376]: this appears to be premature, and results in code such as 5457 C< our(%x); > executing in list mode rather than void mode */ 5458 #if 0 5459 if (o->op_flags & OPf_PARENS) 5460 list(o); 5461 else 5462 maybe_scalar = 1; 5463 #else 5464 maybe_scalar = 1; 5465 #endif 5466 if (attrs) 5467 SAVEFREEOP(attrs); 5468 rops = NULL; 5469 o = my_kid(o, attrs, &rops); 5470 if (rops) { 5471 if (maybe_scalar && o->op_type == OP_PADSV) { 5472 o = scalar(op_append_list(OP_LIST, rops, o)); 5473 o->op_private |= OPpLVAL_INTRO; 5474 } 5475 else { 5476 /* The listop in rops might have a pushmark at the beginning, 5477 which will mess up list assignment. */ 5478 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */ 5479 if (rops->op_type == OP_LIST && 5480 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK) 5481 { 5482 OP * const pushmark = lrops->op_first; 5483 /* excise pushmark */ 5484 op_sibling_splice(rops, NULL, 1, NULL); 5485 op_free(pushmark); 5486 } 5487 o = op_append_list(OP_LIST, o, rops); 5488 } 5489 } 5490 PL_parser->in_my = FALSE; 5491 PL_parser->in_my_stash = NULL; 5492 return o; 5493 } 5494 5495 OP * 5496 Perl_sawparens(pTHX_ OP *o) 5497 { 5498 PERL_UNUSED_CONTEXT; 5499 if (o) 5500 o->op_flags |= OPf_PARENS; 5501 return o; 5502 } 5503 5504 OP * 5505 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right) 5506 { 5507 OP *o; 5508 bool ismatchop = 0; 5509 const OPCODE ltype = left->op_type; 5510 const OPCODE rtype = right->op_type; 5511 5512 PERL_ARGS_ASSERT_BIND_MATCH; 5513 5514 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV 5515 || ltype == OP_PADHV) && ckWARN(WARN_MISC)) 5516 { 5517 const char * const desc 5518 = PL_op_desc[( 5519 rtype == OP_SUBST || rtype == OP_TRANS 5520 || rtype == OP_TRANSR 5521 ) 5522 ? (int)rtype : OP_MATCH]; 5523 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV; 5524 SV * const name = 5525 S_op_varname(aTHX_ left); 5526 if (name) 5527 Perl_warner(aTHX_ packWARN(WARN_MISC), 5528 "Applying %s to %" SVf " will act on scalar(%" SVf ")", 5529 desc, SVfARG(name), SVfARG(name)); 5530 else { 5531 const char * const sample = (isary 5532 ? "@array" : "%hash"); 5533 Perl_warner(aTHX_ packWARN(WARN_MISC), 5534 "Applying %s to %s will act on scalar(%s)", 5535 desc, sample, sample); 5536 } 5537 } 5538 5539 if (rtype == OP_CONST && 5540 cSVOPx(right)->op_private & OPpCONST_BARE && 5541 cSVOPx(right)->op_private & OPpCONST_STRICT) 5542 { 5543 no_bareword_allowed(right); 5544 } 5545 5546 /* !~ doesn't make sense with /r, so error on it for now */ 5547 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) && 5548 type == OP_NOT) 5549 /* diag_listed_as: Using !~ with %s doesn't make sense */ 5550 yyerror("Using !~ with s///r doesn't make sense"); 5551 if (rtype == OP_TRANSR && type == OP_NOT) 5552 /* diag_listed_as: Using !~ with %s doesn't make sense */ 5553 yyerror("Using !~ with tr///r doesn't make sense"); 5554 5555 ismatchop = (rtype == OP_MATCH || 5556 rtype == OP_SUBST || 5557 rtype == OP_TRANS || rtype == OP_TRANSR) 5558 && !(right->op_flags & OPf_SPECIAL); 5559 if (ismatchop && right->op_private & OPpTARGET_MY) { 5560 right->op_targ = 0; 5561 right->op_private &= ~OPpTARGET_MY; 5562 } 5563 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) { 5564 if (left->op_type == OP_PADSV 5565 && !(left->op_private & OPpLVAL_INTRO)) 5566 { 5567 right->op_targ = left->op_targ; 5568 op_free(left); 5569 o = right; 5570 } 5571 else { 5572 right->op_flags |= OPf_STACKED; 5573 if (rtype != OP_MATCH && rtype != OP_TRANSR && 5574 ! (rtype == OP_TRANS && 5575 right->op_private & OPpTRANS_IDENTICAL) && 5576 ! (rtype == OP_SUBST && 5577 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT))) 5578 left = op_lvalue(left, rtype); 5579 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR) 5580 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right); 5581 else 5582 o = op_prepend_elem(rtype, scalar(left), right); 5583 } 5584 if (type == OP_NOT) 5585 return newUNOP(OP_NOT, 0, scalar(o)); 5586 return o; 5587 } 5588 else 5589 return bind_match(type, left, 5590 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0)); 5591 } 5592 5593 OP * 5594 Perl_invert(pTHX_ OP *o) 5595 { 5596 if (!o) 5597 return NULL; 5598 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); 5599 } 5600 5601 OP * 5602 Perl_cmpchain_start(pTHX_ I32 type, OP *left, OP *right) 5603 { 5604 BINOP *bop; 5605 OP *op; 5606 5607 if (!left) 5608 left = newOP(OP_NULL, 0); 5609 if (!right) 5610 right = newOP(OP_NULL, 0); 5611 scalar(left); 5612 scalar(right); 5613 NewOp(0, bop, 1, BINOP); 5614 op = (OP*)bop; 5615 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP); 5616 OpTYPE_set(op, type); 5617 cBINOPx(op)->op_flags = OPf_KIDS; 5618 cBINOPx(op)->op_private = 2; 5619 cBINOPx(op)->op_first = left; 5620 cBINOPx(op)->op_last = right; 5621 OpMORESIB_set(left, right); 5622 OpLASTSIB_set(right, op); 5623 return op; 5624 } 5625 5626 OP * 5627 Perl_cmpchain_extend(pTHX_ I32 type, OP *ch, OP *right) 5628 { 5629 BINOP *bop; 5630 OP *op; 5631 5632 PERL_ARGS_ASSERT_CMPCHAIN_EXTEND; 5633 if (!right) 5634 right = newOP(OP_NULL, 0); 5635 scalar(right); 5636 NewOp(0, bop, 1, BINOP); 5637 op = (OP*)bop; 5638 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP); 5639 OpTYPE_set(op, type); 5640 if (ch->op_type != OP_NULL) { 5641 UNOP *lch; 5642 OP *nch, *cleft, *cright; 5643 NewOp(0, lch, 1, UNOP); 5644 nch = (OP*)lch; 5645 OpTYPE_set(nch, OP_NULL); 5646 nch->op_flags = OPf_KIDS; 5647 cleft = cBINOPx(ch)->op_first; 5648 cright = cBINOPx(ch)->op_last; 5649 cBINOPx(ch)->op_first = NULL; 5650 cBINOPx(ch)->op_last = NULL; 5651 cBINOPx(ch)->op_private = 0; 5652 cBINOPx(ch)->op_flags = 0; 5653 cUNOPx(nch)->op_first = cright; 5654 OpMORESIB_set(cright, ch); 5655 OpMORESIB_set(ch, cleft); 5656 OpLASTSIB_set(cleft, nch); 5657 ch = nch; 5658 } 5659 OpMORESIB_set(right, op); 5660 OpMORESIB_set(op, cUNOPx(ch)->op_first); 5661 cUNOPx(ch)->op_first = right; 5662 return ch; 5663 } 5664 5665 OP * 5666 Perl_cmpchain_finish(pTHX_ OP *ch) 5667 { 5668 5669 PERL_ARGS_ASSERT_CMPCHAIN_FINISH; 5670 if (ch->op_type != OP_NULL) { 5671 OPCODE cmpoptype = ch->op_type; 5672 ch = CHECKOP(cmpoptype, ch); 5673 if(!ch->op_next && ch->op_type == cmpoptype) 5674 ch = fold_constants(op_integerize(op_std_init(ch))); 5675 return ch; 5676 } else { 5677 OP *condop = NULL; 5678 OP *rightarg = cUNOPx(ch)->op_first; 5679 cUNOPx(ch)->op_first = OpSIBLING(rightarg); 5680 OpLASTSIB_set(rightarg, NULL); 5681 while (1) { 5682 OP *cmpop = cUNOPx(ch)->op_first; 5683 OP *leftarg = OpSIBLING(cmpop); 5684 OPCODE cmpoptype = cmpop->op_type; 5685 OP *nextrightarg; 5686 bool is_last; 5687 is_last = !(cUNOPx(ch)->op_first = OpSIBLING(leftarg)); 5688 OpLASTSIB_set(cmpop, NULL); 5689 OpLASTSIB_set(leftarg, NULL); 5690 if (is_last) { 5691 ch->op_flags = 0; 5692 op_free(ch); 5693 nextrightarg = NULL; 5694 } else { 5695 nextrightarg = newUNOP(OP_CMPCHAIN_DUP, 0, leftarg); 5696 leftarg = newOP(OP_NULL, 0); 5697 } 5698 cBINOPx(cmpop)->op_first = leftarg; 5699 cBINOPx(cmpop)->op_last = rightarg; 5700 OpMORESIB_set(leftarg, rightarg); 5701 OpLASTSIB_set(rightarg, cmpop); 5702 cmpop->op_flags = OPf_KIDS; 5703 cmpop->op_private = 2; 5704 cmpop = CHECKOP(cmpoptype, cmpop); 5705 if(!cmpop->op_next && cmpop->op_type == cmpoptype) 5706 cmpop = op_integerize(op_std_init(cmpop)); 5707 condop = condop ? newLOGOP(OP_CMPCHAIN_AND, 0, cmpop, condop) : 5708 cmpop; 5709 if (!nextrightarg) 5710 return condop; 5711 rightarg = nextrightarg; 5712 } 5713 } 5714 } 5715 5716 /* 5717 =for apidoc op_scope 5718 5719 Wraps up an op tree with some additional ops so that at runtime a dynamic 5720 scope will be created. The original ops run in the new dynamic scope, 5721 and then, provided that they exit normally, the scope will be unwound. 5722 The additional ops used to create and unwind the dynamic scope will 5723 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used 5724 instead if the ops are simple enough to not need the full dynamic scope 5725 structure. 5726 5727 =cut 5728 */ 5729 5730 OP * 5731 Perl_op_scope(pTHX_ OP *o) 5732 { 5733 if (o) { 5734 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) { 5735 o = op_prepend_elem(OP_LINESEQ, 5736 newOP(OP_ENTER, (o->op_flags & OPf_WANT)), o); 5737 OpTYPE_set(o, OP_LEAVE); 5738 } 5739 else if (o->op_type == OP_LINESEQ) { 5740 OP *kid; 5741 OpTYPE_set(o, OP_SCOPE); 5742 kid = ((LISTOP*)o)->op_first; 5743 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { 5744 op_null(kid); 5745 5746 /* The following deals with things like 'do {1 for 1}' */ 5747 kid = OpSIBLING(kid); 5748 if (kid && 5749 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)) 5750 op_null(kid); 5751 } 5752 } 5753 else 5754 o = newLISTOP(OP_SCOPE, 0, o, NULL); 5755 } 5756 return o; 5757 } 5758 5759 OP * 5760 Perl_op_unscope(pTHX_ OP *o) 5761 { 5762 if (o && o->op_type == OP_LINESEQ) { 5763 OP *kid = cLISTOPo->op_first; 5764 for(; kid; kid = OpSIBLING(kid)) 5765 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) 5766 op_null(kid); 5767 } 5768 return o; 5769 } 5770 5771 /* 5772 =for apidoc block_start 5773 5774 Handles compile-time scope entry. 5775 Arranges for hints to be restored on block 5776 exit and also handles pad sequence numbers to make lexical variables scope 5777 right. Returns a savestack index for use with C<block_end>. 5778 5779 =cut 5780 */ 5781 5782 int 5783 Perl_block_start(pTHX_ int full) 5784 { 5785 const int retval = PL_savestack_ix; 5786 5787 PL_compiling.cop_seq = PL_cop_seqmax; 5788 COP_SEQMAX_INC; 5789 pad_block_start(full); 5790 SAVEHINTS(); 5791 PL_hints &= ~HINT_BLOCK_SCOPE; 5792 SAVECOMPILEWARNINGS(); 5793 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings); 5794 SAVEI32(PL_compiling.cop_seq); 5795 PL_compiling.cop_seq = 0; 5796 5797 CALL_BLOCK_HOOKS(bhk_start, full); 5798 5799 return retval; 5800 } 5801 5802 /* 5803 =for apidoc block_end 5804 5805 Handles compile-time scope exit. C<floor> 5806 is the savestack index returned by 5807 C<block_start>, and C<seq> is the body of the block. Returns the block, 5808 possibly modified. 5809 5810 =cut 5811 */ 5812 5813 OP* 5814 Perl_block_end(pTHX_ I32 floor, OP *seq) 5815 { 5816 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE; 5817 OP* retval = voidnonfinal(seq); 5818 OP *o; 5819 5820 /* XXX Is the null PL_parser check necessary here? */ 5821 assert(PL_parser); /* Let’s find out under debugging builds. */ 5822 if (PL_parser && PL_parser->parsed_sub) { 5823 o = newSTATEOP(0, NULL, NULL); 5824 op_null(o); 5825 retval = op_append_elem(OP_LINESEQ, retval, o); 5826 } 5827 5828 CALL_BLOCK_HOOKS(bhk_pre_end, &retval); 5829 5830 LEAVE_SCOPE(floor); 5831 if (needblockscope) 5832 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */ 5833 o = pad_leavemy(); 5834 5835 if (o) { 5836 /* pad_leavemy has created a sequence of introcv ops for all my 5837 subs declared in the block. We have to replicate that list with 5838 clonecv ops, to deal with this situation: 5839 5840 sub { 5841 my sub s1; 5842 my sub s2; 5843 sub s1 { state sub foo { \&s2 } } 5844 }->() 5845 5846 Originally, I was going to have introcv clone the CV and turn 5847 off the stale flag. Since &s1 is declared before &s2, the 5848 introcv op for &s1 is executed (on sub entry) before the one for 5849 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is 5850 cloned, since it is a state sub) closes over &s2 and expects 5851 to see it in its outer CV’s pad. If the introcv op clones &s1, 5852 then &s2 is still marked stale. Since &s1 is not active, and 5853 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia- 5854 ble will not stay shared’ warning. Because it is the same stub 5855 that will be used when the introcv op for &s2 is executed, clos- 5856 ing over it is safe. Hence, we have to turn off the stale flag 5857 on all lexical subs in the block before we clone any of them. 5858 Hence, having introcv clone the sub cannot work. So we create a 5859 list of ops like this: 5860 5861 lineseq 5862 | 5863 +-- introcv 5864 | 5865 +-- introcv 5866 | 5867 +-- introcv 5868 | 5869 . 5870 . 5871 . 5872 | 5873 +-- clonecv 5874 | 5875 +-- clonecv 5876 | 5877 +-- clonecv 5878 | 5879 . 5880 . 5881 . 5882 */ 5883 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o; 5884 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o; 5885 for (;; kid = OpSIBLING(kid)) { 5886 OP *newkid = newOP(OP_CLONECV, 0); 5887 newkid->op_targ = kid->op_targ; 5888 o = op_append_elem(OP_LINESEQ, o, newkid); 5889 if (kid == last) break; 5890 } 5891 retval = op_prepend_elem(OP_LINESEQ, o, retval); 5892 } 5893 5894 CALL_BLOCK_HOOKS(bhk_post_end, &retval); 5895 5896 return retval; 5897 } 5898 5899 /* 5900 =for apidoc_section $scope 5901 5902 =for apidoc blockhook_register 5903 5904 Register a set of hooks to be called when the Perl lexical scope changes 5905 at compile time. See L<perlguts/"Compile-time scope hooks">. 5906 5907 =cut 5908 */ 5909 5910 void 5911 Perl_blockhook_register(pTHX_ BHK *hk) 5912 { 5913 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER; 5914 5915 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk))); 5916 } 5917 5918 void 5919 Perl_newPROG(pTHX_ OP *o) 5920 { 5921 OP *start; 5922 5923 PERL_ARGS_ASSERT_NEWPROG; 5924 5925 if (PL_in_eval) { 5926 PERL_CONTEXT *cx; 5927 I32 i; 5928 if (PL_eval_root) 5929 return; 5930 PL_eval_root = newUNOP(OP_LEAVEEVAL, 5931 ((PL_in_eval & EVAL_KEEPERR) 5932 ? OPf_SPECIAL : 0), o); 5933 5934 cx = CX_CUR(); 5935 assert(CxTYPE(cx) == CXt_EVAL); 5936 5937 if ((cx->blk_gimme & G_WANT) == G_VOID) 5938 scalarvoid(PL_eval_root); 5939 else if ((cx->blk_gimme & G_WANT) == G_LIST) 5940 list(PL_eval_root); 5941 else 5942 scalar(PL_eval_root); 5943 5944 start = op_linklist(PL_eval_root); 5945 PL_eval_root->op_next = 0; 5946 i = PL_savestack_ix; 5947 SAVEFREEOP(o); 5948 ENTER; 5949 S_process_optree(aTHX_ NULL, PL_eval_root, start); 5950 LEAVE; 5951 PL_savestack_ix = i; 5952 } 5953 else { 5954 if (o->op_type == OP_STUB) { 5955 /* This block is entered if nothing is compiled for the main 5956 program. This will be the case for an genuinely empty main 5957 program, or one which only has BEGIN blocks etc, so already 5958 run and freed. 5959 5960 Historically (5.000) the guard above was !o. However, commit 5961 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as 5962 c71fccf11fde0068, changed perly.y so that newPROG() is now 5963 called with the output of block_end(), which returns a new 5964 OP_STUB for the case of an empty optree. ByteLoader (and 5965 maybe other things) also take this path, because they set up 5966 PL_main_start and PL_main_root directly, without generating an 5967 optree. 5968 5969 If the parsing the main program aborts (due to parse errors, 5970 or due to BEGIN or similar calling exit), then newPROG() 5971 isn't even called, and hence this code path and its cleanups 5972 are skipped. This shouldn't make a make a difference: 5973 * a non-zero return from perl_parse is a failure, and 5974 perl_destruct() should be called immediately. 5975 * however, if exit(0) is called during the parse, then 5976 perl_parse() returns 0, and perl_run() is called. As 5977 PL_main_start will be NULL, perl_run() will return 5978 promptly, and the exit code will remain 0. 5979 */ 5980 5981 PL_comppad_name = 0; 5982 PL_compcv = 0; 5983 S_op_destroy(aTHX_ o); 5984 return; 5985 } 5986 PL_main_root = op_scope(sawparens(scalarvoid(o))); 5987 PL_curcop = &PL_compiling; 5988 start = LINKLIST(PL_main_root); 5989 PL_main_root->op_next = 0; 5990 S_process_optree(aTHX_ NULL, PL_main_root, start); 5991 if (!PL_parser->error_count) 5992 /* on error, leave CV slabbed so that ops left lying around 5993 * will eb cleaned up. Else unslab */ 5994 cv_forget_slab(PL_compcv); 5995 PL_compcv = 0; 5996 5997 /* Register with debugger */ 5998 if (PERLDB_INTER) { 5999 CV * const cv = get_cvs("DB::postponed", 0); 6000 if (cv) { 6001 dSP; 6002 PUSHMARK(SP); 6003 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); 6004 PUTBACK; 6005 call_sv(MUTABLE_SV(cv), G_DISCARD); 6006 } 6007 } 6008 } 6009 } 6010 6011 OP * 6012 Perl_localize(pTHX_ OP *o, I32 lex) 6013 { 6014 PERL_ARGS_ASSERT_LOCALIZE; 6015 6016 if (o->op_flags & OPf_PARENS) 6017 /* [perl #17376]: this appears to be premature, and results in code such as 6018 C< our(%x); > executing in list mode rather than void mode */ 6019 #if 0 6020 list(o); 6021 #else 6022 NOOP; 6023 #endif 6024 else { 6025 if ( PL_parser->bufptr > PL_parser->oldbufptr 6026 && PL_parser->bufptr[-1] == ',' 6027 && ckWARN(WARN_PARENTHESIS)) 6028 { 6029 char *s = PL_parser->bufptr; 6030 bool sigil = FALSE; 6031 6032 /* some heuristics to detect a potential error */ 6033 while (*s && (memCHRs(", \t\n", *s))) 6034 s++; 6035 6036 while (1) { 6037 if (*s && (memCHRs("@$%", *s) || (!lex && *s == '*')) 6038 && *++s 6039 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) { 6040 s++; 6041 sigil = TRUE; 6042 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) 6043 s++; 6044 while (*s && (memCHRs(", \t\n", *s))) 6045 s++; 6046 } 6047 else 6048 break; 6049 } 6050 if (sigil && (*s == ';' || *s == '=')) { 6051 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS), 6052 "Parentheses missing around \"%s\" list", 6053 lex 6054 ? (PL_parser->in_my == KEY_our 6055 ? "our" 6056 : PL_parser->in_my == KEY_state 6057 ? "state" 6058 : "my") 6059 : "local"); 6060 } 6061 } 6062 } 6063 if (lex) 6064 o = my(o); 6065 else 6066 o = op_lvalue(o, OP_NULL); /* a bit kludgey */ 6067 PL_parser->in_my = FALSE; 6068 PL_parser->in_my_stash = NULL; 6069 return o; 6070 } 6071 6072 OP * 6073 Perl_jmaybe(pTHX_ OP *o) 6074 { 6075 PERL_ARGS_ASSERT_JMAYBE; 6076 6077 if (o->op_type == OP_LIST) { 6078 if (FEATURE_MULTIDIMENSIONAL_IS_ENABLED) { 6079 OP * const o2 6080 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV))); 6081 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o)); 6082 } 6083 else { 6084 /* If the user disables this, then a warning might not be enough to alert 6085 them to a possible change of behaviour here, so throw an exception. 6086 */ 6087 yyerror("Multidimensional hash lookup is disabled"); 6088 } 6089 } 6090 return o; 6091 } 6092 6093 PERL_STATIC_INLINE OP * 6094 S_op_std_init(pTHX_ OP *o) 6095 { 6096 I32 type = o->op_type; 6097 6098 PERL_ARGS_ASSERT_OP_STD_INIT; 6099 6100 if (PL_opargs[type] & OA_RETSCALAR) 6101 scalar(o); 6102 if (PL_opargs[type] & OA_TARGET && !o->op_targ) 6103 o->op_targ = pad_alloc(type, SVs_PADTMP); 6104 6105 return o; 6106 } 6107 6108 PERL_STATIC_INLINE OP * 6109 S_op_integerize(pTHX_ OP *o) 6110 { 6111 I32 type = o->op_type; 6112 6113 PERL_ARGS_ASSERT_OP_INTEGERIZE; 6114 6115 /* integerize op. */ 6116 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)) 6117 { 6118 o->op_ppaddr = PL_ppaddr[++(o->op_type)]; 6119 } 6120 6121 if (type == OP_NEGATE) 6122 /* XXX might want a ck_negate() for this */ 6123 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT; 6124 6125 return o; 6126 } 6127 6128 /* This function exists solely to provide a scope to limit 6129 setjmp/longjmp() messing with auto variables. It cannot be inlined because 6130 it uses setjmp 6131 */ 6132 STATIC int 6133 S_fold_constants_eval(pTHX) { 6134 int ret = 0; 6135 dJMPENV; 6136 6137 JMPENV_PUSH(ret); 6138 6139 if (ret == 0) { 6140 CALLRUNOPS(aTHX); 6141 } 6142 6143 JMPENV_POP; 6144 6145 return ret; 6146 } 6147 6148 static OP * 6149 S_fold_constants(pTHX_ OP *const o) 6150 { 6151 OP *curop; 6152 OP *newop; 6153 I32 type = o->op_type; 6154 bool is_stringify; 6155 SV *sv = NULL; 6156 int ret = 0; 6157 OP *old_next; 6158 SV * const oldwarnhook = PL_warnhook; 6159 SV * const olddiehook = PL_diehook; 6160 COP not_compiling; 6161 U8 oldwarn = PL_dowarn; 6162 I32 old_cxix; 6163 6164 PERL_ARGS_ASSERT_FOLD_CONSTANTS; 6165 6166 if (!(PL_opargs[type] & OA_FOLDCONST)) 6167 goto nope; 6168 6169 switch (type) { 6170 case OP_UCFIRST: 6171 case OP_LCFIRST: 6172 case OP_UC: 6173 case OP_LC: 6174 case OP_FC: 6175 #ifdef USE_LOCALE_CTYPE 6176 if (IN_LC_COMPILETIME(LC_CTYPE)) 6177 goto nope; 6178 #endif 6179 break; 6180 case OP_SLT: 6181 case OP_SGT: 6182 case OP_SLE: 6183 case OP_SGE: 6184 case OP_SCMP: 6185 #ifdef USE_LOCALE_COLLATE 6186 if (IN_LC_COMPILETIME(LC_COLLATE)) 6187 goto nope; 6188 #endif 6189 break; 6190 case OP_SPRINTF: 6191 /* XXX what about the numeric ops? */ 6192 #ifdef USE_LOCALE_NUMERIC 6193 if (IN_LC_COMPILETIME(LC_NUMERIC)) 6194 goto nope; 6195 #endif 6196 break; 6197 case OP_PACK: 6198 if (!OpHAS_SIBLING(cLISTOPo->op_first) 6199 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST) 6200 goto nope; 6201 { 6202 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first)); 6203 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope; 6204 { 6205 const char *s = SvPVX_const(sv); 6206 while (s < SvEND(sv)) { 6207 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope; 6208 s++; 6209 } 6210 } 6211 } 6212 break; 6213 case OP_REPEAT: 6214 if (o->op_private & OPpREPEAT_DOLIST) goto nope; 6215 break; 6216 case OP_SREFGEN: 6217 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST 6218 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first))) 6219 goto nope; 6220 } 6221 6222 if (PL_parser && PL_parser->error_count) 6223 goto nope; /* Don't try to run w/ errors */ 6224 6225 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { 6226 switch (curop->op_type) { 6227 case OP_CONST: 6228 if ( (curop->op_private & OPpCONST_BARE) 6229 && (curop->op_private & OPpCONST_STRICT)) { 6230 no_bareword_allowed(curop); 6231 goto nope; 6232 } 6233 /* FALLTHROUGH */ 6234 case OP_LIST: 6235 case OP_SCALAR: 6236 case OP_NULL: 6237 case OP_PUSHMARK: 6238 /* Foldable; move to next op in list */ 6239 break; 6240 6241 default: 6242 /* No other op types are considered foldable */ 6243 goto nope; 6244 } 6245 } 6246 6247 curop = LINKLIST(o); 6248 old_next = o->op_next; 6249 o->op_next = 0; 6250 PL_op = curop; 6251 6252 old_cxix = cxstack_ix; 6253 create_eval_scope(NULL, G_FAKINGEVAL); 6254 6255 /* Verify that we don't need to save it: */ 6256 assert(PL_curcop == &PL_compiling); 6257 StructCopy(&PL_compiling, ¬_compiling, COP); 6258 PL_curcop = ¬_compiling; 6259 /* The above ensures that we run with all the correct hints of the 6260 currently compiling COP, but that IN_PERL_RUNTIME is true. */ 6261 assert(IN_PERL_RUNTIME); 6262 PL_warnhook = PERL_WARNHOOK_FATAL; 6263 PL_diehook = NULL; 6264 6265 /* Effective $^W=1. */ 6266 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) 6267 PL_dowarn |= G_WARN_ON; 6268 6269 ret = S_fold_constants_eval(aTHX); 6270 6271 switch (ret) { 6272 case 0: 6273 sv = *(PL_stack_sp--); 6274 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */ 6275 pad_swipe(o->op_targ, FALSE); 6276 } 6277 else if (SvTEMP(sv)) { /* grab mortal temp? */ 6278 SvREFCNT_inc_simple_void(sv); 6279 SvTEMP_off(sv); 6280 } 6281 else { assert(SvIMMORTAL(sv)); } 6282 break; 6283 case 3: 6284 /* Something tried to die. Abandon constant folding. */ 6285 /* Pretend the error never happened. */ 6286 CLEAR_ERRSV(); 6287 o->op_next = old_next; 6288 break; 6289 default: 6290 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */ 6291 PL_warnhook = oldwarnhook; 6292 PL_diehook = olddiehook; 6293 /* XXX note that this croak may fail as we've already blown away 6294 * the stack - eg any nested evals */ 6295 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret); 6296 } 6297 PL_dowarn = oldwarn; 6298 PL_warnhook = oldwarnhook; 6299 PL_diehook = olddiehook; 6300 PL_curcop = &PL_compiling; 6301 6302 /* if we croaked, depending on how we croaked the eval scope 6303 * may or may not have already been popped */ 6304 if (cxstack_ix > old_cxix) { 6305 assert(cxstack_ix == old_cxix + 1); 6306 assert(CxTYPE(CX_CUR()) == CXt_EVAL); 6307 delete_eval_scope(); 6308 } 6309 if (ret) 6310 goto nope; 6311 6312 /* OP_STRINGIFY and constant folding are used to implement qq. 6313 Here the constant folding is an implementation detail that we 6314 want to hide. If the stringify op is itself already marked 6315 folded, however, then it is actually a folded join. */ 6316 is_stringify = type == OP_STRINGIFY && !o->op_folded; 6317 op_free(o); 6318 assert(sv); 6319 if (is_stringify) 6320 SvPADTMP_off(sv); 6321 else if (!SvIMMORTAL(sv)) { 6322 SvPADTMP_on(sv); 6323 SvREADONLY_on(sv); 6324 } 6325 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv)); 6326 if (!is_stringify) newop->op_folded = 1; 6327 return newop; 6328 6329 nope: 6330 return o; 6331 } 6332 6333 /* convert a constant range in list context into an OP_RV2AV, OP_CONST pair; 6334 * the constant value being an AV holding the flattened range. 6335 */ 6336 6337 static void 6338 S_gen_constant_list(pTHX_ OP *o) 6339 { 6340 OP *curop, *old_next; 6341 SV * const oldwarnhook = PL_warnhook; 6342 SV * const olddiehook = PL_diehook; 6343 COP *old_curcop; 6344 U8 oldwarn = PL_dowarn; 6345 SV **svp; 6346 AV *av; 6347 I32 old_cxix; 6348 COP not_compiling; 6349 int ret = 0; 6350 dJMPENV; 6351 bool op_was_null; 6352 6353 list(o); 6354 if (PL_parser && PL_parser->error_count) 6355 return; /* Don't attempt to run with errors */ 6356 6357 curop = LINKLIST(o); 6358 old_next = o->op_next; 6359 o->op_next = 0; 6360 op_was_null = o->op_type == OP_NULL; 6361 if (op_was_null) /* b3698342565fb462291fba4b432cfcd05b6eb4e1 */ 6362 o->op_type = OP_CUSTOM; 6363 CALL_PEEP(curop); 6364 if (op_was_null) 6365 o->op_type = OP_NULL; 6366 S_prune_chain_head(&curop); 6367 PL_op = curop; 6368 6369 old_cxix = cxstack_ix; 6370 create_eval_scope(NULL, G_FAKINGEVAL); 6371 6372 old_curcop = PL_curcop; 6373 StructCopy(old_curcop, ¬_compiling, COP); 6374 PL_curcop = ¬_compiling; 6375 /* The above ensures that we run with all the correct hints of the 6376 current COP, but that IN_PERL_RUNTIME is true. */ 6377 assert(IN_PERL_RUNTIME); 6378 PL_warnhook = PERL_WARNHOOK_FATAL; 6379 PL_diehook = NULL; 6380 JMPENV_PUSH(ret); 6381 6382 /* Effective $^W=1. */ 6383 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) 6384 PL_dowarn |= G_WARN_ON; 6385 6386 switch (ret) { 6387 case 0: 6388 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY 6389 PL_curstackinfo->si_stack_hwm = 0; /* stop valgrind complaining */ 6390 #endif 6391 Perl_pp_pushmark(aTHX); 6392 CALLRUNOPS(aTHX); 6393 PL_op = curop; 6394 assert (!(curop->op_flags & OPf_SPECIAL)); 6395 assert(curop->op_type == OP_RANGE); 6396 Perl_pp_anonlist(aTHX); 6397 break; 6398 case 3: 6399 CLEAR_ERRSV(); 6400 o->op_next = old_next; 6401 break; 6402 default: 6403 JMPENV_POP; 6404 PL_warnhook = oldwarnhook; 6405 PL_diehook = olddiehook; 6406 Perl_croak(aTHX_ "panic: gen_constant_list JMPENV_PUSH returned %d", 6407 ret); 6408 } 6409 6410 JMPENV_POP; 6411 PL_dowarn = oldwarn; 6412 PL_warnhook = oldwarnhook; 6413 PL_diehook = olddiehook; 6414 PL_curcop = old_curcop; 6415 6416 if (cxstack_ix > old_cxix) { 6417 assert(cxstack_ix == old_cxix + 1); 6418 assert(CxTYPE(CX_CUR()) == CXt_EVAL); 6419 delete_eval_scope(); 6420 } 6421 if (ret) 6422 return; 6423 6424 OpTYPE_set(o, OP_RV2AV); 6425 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */ 6426 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */ 6427 o->op_opt = 0; /* needs to be revisited in rpeep() */ 6428 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--); 6429 6430 /* replace subtree with an OP_CONST */ 6431 curop = ((UNOP*)o)->op_first; 6432 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av)); 6433 op_free(curop); 6434 6435 if (AvFILLp(av) != -1) 6436 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp) 6437 { 6438 SvPADTMP_on(*svp); 6439 SvREADONLY_on(*svp); 6440 } 6441 LINKLIST(o); 6442 list(o); 6443 return; 6444 } 6445 6446 /* 6447 =for apidoc_section $optree_manipulation 6448 */ 6449 6450 /* List constructors */ 6451 6452 /* 6453 =for apidoc op_append_elem 6454 6455 Append an item to the list of ops contained directly within a list-type 6456 op, returning the lengthened list. C<first> is the list-type op, 6457 and C<last> is the op to append to the list. C<optype> specifies the 6458 intended opcode for the list. If C<first> is not already a list of the 6459 right type, it will be upgraded into one. If either C<first> or C<last> 6460 is null, the other is returned unchanged. 6461 6462 =cut 6463 */ 6464 6465 OP * 6466 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last) 6467 { 6468 if (!first) 6469 return last; 6470 6471 if (!last) 6472 return first; 6473 6474 if (first->op_type != (unsigned)type 6475 || (type == OP_LIST && (first->op_flags & OPf_PARENS))) 6476 { 6477 return newLISTOP(type, 0, first, last); 6478 } 6479 6480 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last); 6481 first->op_flags |= OPf_KIDS; 6482 return first; 6483 } 6484 6485 /* 6486 =for apidoc op_append_list 6487 6488 Concatenate the lists of ops contained directly within two list-type ops, 6489 returning the combined list. C<first> and C<last> are the list-type ops 6490 to concatenate. C<optype> specifies the intended opcode for the list. 6491 If either C<first> or C<last> is not already a list of the right type, 6492 it will be upgraded into one. If either C<first> or C<last> is null, 6493 the other is returned unchanged. 6494 6495 =cut 6496 */ 6497 6498 OP * 6499 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last) 6500 { 6501 if (!first) 6502 return last; 6503 6504 if (!last) 6505 return first; 6506 6507 if (first->op_type != (unsigned)type) 6508 return op_prepend_elem(type, first, last); 6509 6510 if (last->op_type != (unsigned)type) 6511 return op_append_elem(type, first, last); 6512 6513 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first); 6514 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last; 6515 OpLASTSIB_set(((LISTOP*)first)->op_last, first); 6516 first->op_flags |= (last->op_flags & OPf_KIDS); 6517 6518 S_op_destroy(aTHX_ last); 6519 6520 return first; 6521 } 6522 6523 /* 6524 =for apidoc op_prepend_elem 6525 6526 Prepend an item to the list of ops contained directly within a list-type 6527 op, returning the lengthened list. C<first> is the op to prepend to the 6528 list, and C<last> is the list-type op. C<optype> specifies the intended 6529 opcode for the list. If C<last> is not already a list of the right type, 6530 it will be upgraded into one. If either C<first> or C<last> is null, 6531 the other is returned unchanged. 6532 6533 =cut 6534 */ 6535 6536 OP * 6537 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last) 6538 { 6539 if (!first) 6540 return last; 6541 6542 if (!last) 6543 return first; 6544 6545 if (last->op_type == (unsigned)type) { 6546 if (type == OP_LIST) { /* already a PUSHMARK there */ 6547 /* insert 'first' after pushmark */ 6548 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first); 6549 if (!(first->op_flags & OPf_PARENS)) 6550 last->op_flags &= ~OPf_PARENS; 6551 } 6552 else 6553 op_sibling_splice(last, NULL, 0, first); 6554 last->op_flags |= OPf_KIDS; 6555 return last; 6556 } 6557 6558 return newLISTOP(type, 0, first, last); 6559 } 6560 6561 /* 6562 =for apidoc op_convert_list 6563 6564 Converts C<o> into a list op if it is not one already, and then converts it 6565 into the specified C<type>, calling its check function, allocating a target if 6566 it needs one, and folding constants. 6567 6568 A list-type op is usually constructed one kid at a time via C<newLISTOP>, 6569 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to 6570 C<op_convert_list> to make it the right type. 6571 6572 =cut 6573 */ 6574 6575 OP * 6576 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o) 6577 { 6578 if (type < 0) type = -type, flags |= OPf_SPECIAL; 6579 if (!o || o->op_type != OP_LIST) 6580 o = force_list(o, FALSE); 6581 else 6582 { 6583 o->op_flags &= ~OPf_WANT; 6584 o->op_private &= ~OPpLVAL_INTRO; 6585 } 6586 6587 if (!(PL_opargs[type] & OA_MARK)) 6588 op_null(cLISTOPo->op_first); 6589 else { 6590 OP * const kid2 = OpSIBLING(cLISTOPo->op_first); 6591 if (kid2 && kid2->op_type == OP_COREARGS) { 6592 op_null(cLISTOPo->op_first); 6593 kid2->op_private |= OPpCOREARGS_PUSHMARK; 6594 } 6595 } 6596 6597 if (type != OP_SPLIT) 6598 /* At this point o is a LISTOP, but OP_SPLIT is a PMOP; let 6599 * ck_split() create a real PMOP and leave the op's type as listop 6600 * for now. Otherwise op_free() etc will crash. 6601 */ 6602 OpTYPE_set(o, type); 6603 6604 o->op_flags |= flags; 6605 if (flags & OPf_FOLDED) 6606 o->op_folded = 1; 6607 6608 o = CHECKOP(type, o); 6609 if (o->op_type != (unsigned)type) 6610 return o; 6611 6612 return fold_constants(op_integerize(op_std_init(o))); 6613 } 6614 6615 /* Constructors */ 6616 6617 6618 /* 6619 =for apidoc_section $optree_construction 6620 6621 =for apidoc newNULLLIST 6622 6623 Constructs, checks, and returns a new C<stub> op, which represents an 6624 empty list expression. 6625 6626 =cut 6627 */ 6628 6629 OP * 6630 Perl_newNULLLIST(pTHX) 6631 { 6632 return newOP(OP_STUB, 0); 6633 } 6634 6635 /* promote o and any siblings to be a list if its not already; i.e. 6636 * 6637 * o - A - B 6638 * 6639 * becomes 6640 * 6641 * list 6642 * | 6643 * pushmark - o - A - B 6644 * 6645 * If nullit it true, the list op is nulled. 6646 */ 6647 6648 static OP * 6649 S_force_list(pTHX_ OP *o, bool nullit) 6650 { 6651 if (!o || o->op_type != OP_LIST) { 6652 OP *rest = NULL; 6653 if (o) { 6654 /* manually detach any siblings then add them back later */ 6655 rest = OpSIBLING(o); 6656 OpLASTSIB_set(o, NULL); 6657 } 6658 o = newLISTOP(OP_LIST, 0, o, NULL); 6659 if (rest) 6660 op_sibling_splice(o, cLISTOPo->op_last, 0, rest); 6661 } 6662 if (nullit) 6663 op_null(o); 6664 return o; 6665 } 6666 6667 /* 6668 =for apidoc newLISTOP 6669 6670 Constructs, checks, and returns an op of any list type. C<type> is 6671 the opcode. C<flags> gives the eight bits of C<op_flags>, except that 6672 C<OPf_KIDS> will be set automatically if required. C<first> and C<last> 6673 supply up to two ops to be direct children of the list op; they are 6674 consumed by this function and become part of the constructed op tree. 6675 6676 For most list operators, the check function expects all the kid ops to be 6677 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not 6678 appropriate. What you want to do in that case is create an op of type 6679 C<OP_LIST>, append more children to it, and then call L</op_convert_list>. 6680 See L</op_convert_list> for more information. 6681 6682 6683 =cut 6684 */ 6685 6686 OP * 6687 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) 6688 { 6689 LISTOP *listop; 6690 /* Note that allocating an OP_PUSHMARK can die under Safe.pm if 6691 * pushmark is banned. So do it now while existing ops are in a 6692 * consistent state, in case they suddenly get freed */ 6693 OP* const pushop = type == OP_LIST ? newOP(OP_PUSHMARK, 0) : NULL; 6694 6695 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP 6696 || type == OP_CUSTOM); 6697 6698 NewOp(1101, listop, 1, LISTOP); 6699 OpTYPE_set(listop, type); 6700 if (first || last) 6701 flags |= OPf_KIDS; 6702 listop->op_flags = (U8)flags; 6703 6704 if (!last && first) 6705 last = first; 6706 else if (!first && last) 6707 first = last; 6708 else if (first) 6709 OpMORESIB_set(first, last); 6710 listop->op_first = first; 6711 listop->op_last = last; 6712 6713 if (pushop) { 6714 OpMORESIB_set(pushop, first); 6715 listop->op_first = pushop; 6716 listop->op_flags |= OPf_KIDS; 6717 if (!last) 6718 listop->op_last = pushop; 6719 } 6720 if (listop->op_last) 6721 OpLASTSIB_set(listop->op_last, (OP*)listop); 6722 6723 return CHECKOP(type, listop); 6724 } 6725 6726 /* 6727 =for apidoc newOP 6728 6729 Constructs, checks, and returns an op of any base type (any type that 6730 has no extra fields). C<type> is the opcode. C<flags> gives the 6731 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits 6732 of C<op_private>. 6733 6734 =cut 6735 */ 6736 6737 OP * 6738 Perl_newOP(pTHX_ I32 type, I32 flags) 6739 { 6740 OP *o; 6741 6742 if (type == -OP_ENTEREVAL) { 6743 type = OP_ENTEREVAL; 6744 flags |= OPpEVAL_BYTES<<8; 6745 } 6746 6747 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP 6748 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP 6749 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 6750 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); 6751 6752 NewOp(1101, o, 1, OP); 6753 OpTYPE_set(o, type); 6754 o->op_flags = (U8)flags; 6755 6756 o->op_next = o; 6757 o->op_private = (U8)(0 | (flags >> 8)); 6758 if (PL_opargs[type] & OA_RETSCALAR) 6759 scalar(o); 6760 if (PL_opargs[type] & OA_TARGET) 6761 o->op_targ = pad_alloc(type, SVs_PADTMP); 6762 return CHECKOP(type, o); 6763 } 6764 6765 /* 6766 =for apidoc newUNOP 6767 6768 Constructs, checks, and returns an op of any unary type. C<type> is 6769 the opcode. C<flags> gives the eight bits of C<op_flags>, except that 6770 C<OPf_KIDS> will be set automatically if required, and, shifted up eight 6771 bits, the eight bits of C<op_private>, except that the bit with value 1 6772 is automatically set. C<first> supplies an optional op to be the direct 6773 child of the unary op; it is consumed by this function and become part 6774 of the constructed op tree. 6775 6776 =for apidoc Amnh||OPf_KIDS 6777 6778 =cut 6779 */ 6780 6781 OP * 6782 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first) 6783 { 6784 UNOP *unop; 6785 6786 if (type == -OP_ENTEREVAL) { 6787 type = OP_ENTEREVAL; 6788 flags |= OPpEVAL_BYTES<<8; 6789 } 6790 6791 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP 6792 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP 6793 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 6794 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP 6795 || type == OP_SASSIGN 6796 || type == OP_ENTERTRY 6797 || type == OP_ENTERTRYCATCH 6798 || type == OP_CUSTOM 6799 || type == OP_NULL ); 6800 6801 if (!first) 6802 first = newOP(OP_STUB, 0); 6803 if (PL_opargs[type] & OA_MARK) 6804 first = force_list(first, TRUE); 6805 6806 NewOp(1101, unop, 1, UNOP); 6807 OpTYPE_set(unop, type); 6808 unop->op_first = first; 6809 unop->op_flags = (U8)(flags | OPf_KIDS); 6810 unop->op_private = (U8)(1 | (flags >> 8)); 6811 6812 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */ 6813 OpLASTSIB_set(first, (OP*)unop); 6814 6815 unop = (UNOP*) CHECKOP(type, unop); 6816 if (unop->op_next) 6817 return (OP*)unop; 6818 6819 return fold_constants(op_integerize(op_std_init((OP *) unop))); 6820 } 6821 6822 /* 6823 =for apidoc newUNOP_AUX 6824 6825 Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux> 6826 initialised to C<aux> 6827 6828 =cut 6829 */ 6830 6831 OP * 6832 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux) 6833 { 6834 UNOP_AUX *unop; 6835 6836 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX 6837 || type == OP_CUSTOM); 6838 6839 NewOp(1101, unop, 1, UNOP_AUX); 6840 unop->op_type = (OPCODE)type; 6841 unop->op_ppaddr = PL_ppaddr[type]; 6842 unop->op_first = first; 6843 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0)); 6844 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8)); 6845 unop->op_aux = aux; 6846 6847 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */ 6848 OpLASTSIB_set(first, (OP*)unop); 6849 6850 unop = (UNOP_AUX*) CHECKOP(type, unop); 6851 6852 return op_std_init((OP *) unop); 6853 } 6854 6855 /* 6856 =for apidoc newMETHOP 6857 6858 Constructs, checks, and returns an op of method type with a method name 6859 evaluated at runtime. C<type> is the opcode. C<flags> gives the eight 6860 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically, 6861 and, shifted up eight bits, the eight bits of C<op_private>, except that 6862 the bit with value 1 is automatically set. C<dynamic_meth> supplies an 6863 op which evaluates method name; it is consumed by this function and 6864 become part of the constructed op tree. 6865 Supported optypes: C<OP_METHOD>. 6866 6867 =cut 6868 */ 6869 6870 static OP* 6871 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) { 6872 METHOP *methop; 6873 6874 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP 6875 || type == OP_CUSTOM); 6876 6877 NewOp(1101, methop, 1, METHOP); 6878 if (dynamic_meth) { 6879 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, TRUE); 6880 methop->op_flags = (U8)(flags | OPf_KIDS); 6881 methop->op_u.op_first = dynamic_meth; 6882 methop->op_private = (U8)(1 | (flags >> 8)); 6883 6884 if (!OpHAS_SIBLING(dynamic_meth)) 6885 OpLASTSIB_set(dynamic_meth, (OP*)methop); 6886 } 6887 else { 6888 assert(const_meth); 6889 methop->op_flags = (U8)(flags & ~OPf_KIDS); 6890 methop->op_u.op_meth_sv = const_meth; 6891 methop->op_private = (U8)(0 | (flags >> 8)); 6892 methop->op_next = (OP*)methop; 6893 } 6894 6895 #ifdef USE_ITHREADS 6896 methop->op_rclass_targ = 0; 6897 #else 6898 methop->op_rclass_sv = NULL; 6899 #endif 6900 6901 OpTYPE_set(methop, type); 6902 return CHECKOP(type, methop); 6903 } 6904 6905 OP * 6906 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) { 6907 PERL_ARGS_ASSERT_NEWMETHOP; 6908 return newMETHOP_internal(type, flags, dynamic_meth, NULL); 6909 } 6910 6911 /* 6912 =for apidoc newMETHOP_named 6913 6914 Constructs, checks, and returns an op of method type with a constant 6915 method name. C<type> is the opcode. C<flags> gives the eight bits of 6916 C<op_flags>, and, shifted up eight bits, the eight bits of 6917 C<op_private>. C<const_meth> supplies a constant method name; 6918 it must be a shared COW string. 6919 Supported optypes: C<OP_METHOD_NAMED>. 6920 6921 =cut 6922 */ 6923 6924 OP * 6925 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) { 6926 PERL_ARGS_ASSERT_NEWMETHOP_NAMED; 6927 return newMETHOP_internal(type, flags, NULL, const_meth); 6928 } 6929 6930 /* 6931 =for apidoc newBINOP 6932 6933 Constructs, checks, and returns an op of any binary type. C<type> 6934 is the opcode. C<flags> gives the eight bits of C<op_flags>, except 6935 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits, 6936 the eight bits of C<op_private>, except that the bit with value 1 or 6937 2 is automatically set as required. C<first> and C<last> supply up to 6938 two ops to be the direct children of the binary op; they are consumed 6939 by this function and become part of the constructed op tree. 6940 6941 =cut 6942 */ 6943 6944 OP * 6945 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) 6946 { 6947 BINOP *binop; 6948 6949 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP 6950 || type == OP_NULL || type == OP_CUSTOM); 6951 6952 NewOp(1101, binop, 1, BINOP); 6953 6954 if (!first) 6955 first = newOP(OP_NULL, 0); 6956 6957 OpTYPE_set(binop, type); 6958 binop->op_first = first; 6959 binop->op_flags = (U8)(flags | OPf_KIDS); 6960 if (!last) { 6961 last = first; 6962 binop->op_private = (U8)(1 | (flags >> 8)); 6963 } 6964 else { 6965 binop->op_private = (U8)(2 | (flags >> 8)); 6966 OpMORESIB_set(first, last); 6967 } 6968 6969 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */ 6970 OpLASTSIB_set(last, (OP*)binop); 6971 6972 binop->op_last = OpSIBLING(binop->op_first); 6973 if (binop->op_last) 6974 OpLASTSIB_set(binop->op_last, (OP*)binop); 6975 6976 binop = (BINOP*)CHECKOP(type, binop); 6977 if (binop->op_next || binop->op_type != (OPCODE)type) 6978 return (OP*)binop; 6979 6980 return fold_constants(op_integerize(op_std_init((OP *)binop))); 6981 } 6982 6983 void 6984 Perl_invmap_dump(pTHX_ SV* invlist, UV *map) 6985 { 6986 const char indent[] = " "; 6987 6988 UV len = _invlist_len(invlist); 6989 UV * array = invlist_array(invlist); 6990 UV i; 6991 6992 PERL_ARGS_ASSERT_INVMAP_DUMP; 6993 6994 for (i = 0; i < len; i++) { 6995 UV start = array[i]; 6996 UV end = (i + 1 < len) ? array[i+1] - 1 : IV_MAX; 6997 6998 PerlIO_printf(Perl_debug_log, "%s[%" UVuf "] 0x%04" UVXf, indent, i, start); 6999 if (end == IV_MAX) { 7000 PerlIO_printf(Perl_debug_log, " .. INFTY"); 7001 } 7002 else if (end != start) { 7003 PerlIO_printf(Perl_debug_log, " .. 0x%04" UVXf, end); 7004 } 7005 else { 7006 PerlIO_printf(Perl_debug_log, " "); 7007 } 7008 7009 PerlIO_printf(Perl_debug_log, "\t"); 7010 7011 if (map[i] == TR_UNLISTED) { 7012 PerlIO_printf(Perl_debug_log, "TR_UNLISTED\n"); 7013 } 7014 else if (map[i] == TR_SPECIAL_HANDLING) { 7015 PerlIO_printf(Perl_debug_log, "TR_SPECIAL_HANDLING\n"); 7016 } 7017 else { 7018 PerlIO_printf(Perl_debug_log, "0x%04" UVXf "\n", map[i]); 7019 } 7020 } 7021 } 7022 7023 /* Given an OP_TRANS / OP_TRANSR op o, plus OP_CONST ops expr and repl 7024 * containing the search and replacement strings, assemble into 7025 * a translation table attached as o->op_pv. 7026 * Free expr and repl. 7027 * It expects the toker to have already set the 7028 * OPpTRANS_COMPLEMENT 7029 * OPpTRANS_SQUASH 7030 * OPpTRANS_DELETE 7031 * flags as appropriate; this function may add 7032 * OPpTRANS_USE_SVOP 7033 * OPpTRANS_CAN_FORCE_UTF8 7034 * OPpTRANS_IDENTICAL 7035 * OPpTRANS_GROWS 7036 * flags 7037 */ 7038 7039 static OP * 7040 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) 7041 { 7042 /* This function compiles a tr///, from data gathered from toke.c, into a 7043 * form suitable for use by do_trans() in doop.c at runtime. 7044 * 7045 * It first normalizes the data, while discarding extraneous inputs; then 7046 * writes out the compiled data. The normalization allows for complete 7047 * analysis, and avoids some false negatives and positives earlier versions 7048 * of this code had. 7049 * 7050 * The normalization form is an inversion map (described below in detail). 7051 * This is essentially the compiled form for tr///'s that require UTF-8, 7052 * and its easy to use it to write the 257-byte table for tr///'s that 7053 * don't need UTF-8. That table is identical to what's been in use for 7054 * many perl versions, except that it doesn't handle some edge cases that 7055 * it used to, involving code points above 255. The UTF-8 form now handles 7056 * these. (This could be changed with extra coding should it shown to be 7057 * desirable.) 7058 * 7059 * If the complement (/c) option is specified, the lhs string (tstr) is 7060 * parsed into an inversion list. Complementing these is trivial. Then a 7061 * complemented tstr is built from that, and used thenceforth. This hides 7062 * the fact that it was complemented from almost all successive code. 7063 * 7064 * One of the important characteristics to know about the input is whether 7065 * the transliteration may be done in place, or does a temporary need to be 7066 * allocated, then copied. If the replacement for every character in every 7067 * possible string takes up no more bytes than the character it 7068 * replaces, then it can be edited in place. Otherwise the replacement 7069 * could overwrite a byte we are about to read, depending on the strings 7070 * being processed. The comments and variable names here refer to this as 7071 * "growing". Some inputs won't grow, and might even shrink under /d, but 7072 * some inputs could grow, so we have to assume any given one might grow. 7073 * On very long inputs, the temporary could eat up a lot of memory, so we 7074 * want to avoid it if possible. For non-UTF-8 inputs, everything is 7075 * single-byte, so can be edited in place, unless there is something in the 7076 * pattern that could force it into UTF-8. The inversion map makes it 7077 * feasible to determine this. Previous versions of this code pretty much 7078 * punted on determining if UTF-8 could be edited in place. Now, this code 7079 * is rigorous in making that determination. 7080 * 7081 * Another characteristic we need to know is whether the lhs and rhs are 7082 * identical. If so, and no other flags are present, the only effect of 7083 * the tr/// is to count the characters present in the input that are 7084 * mentioned in the lhs string. The implementation of that is easier and 7085 * runs faster than the more general case. Normalizing here allows for 7086 * accurate determination of this. Previously there were false negatives 7087 * possible. 7088 * 7089 * Instead of 'transliterated', the comments here use 'unmapped' for the 7090 * characters that are left unchanged by the operation; otherwise they are 7091 * 'mapped' 7092 * 7093 * The lhs of the tr/// is here referred to as the t side. 7094 * The rhs of the tr/// is here referred to as the r side. 7095 */ 7096 7097 SV * const tstr = ((SVOP*)expr)->op_sv; 7098 SV * const rstr = ((SVOP*)repl)->op_sv; 7099 STRLEN tlen; 7100 STRLEN rlen; 7101 const U8 * t0 = (U8*)SvPV_const(tstr, tlen); 7102 const U8 * r0 = (U8*)SvPV_const(rstr, rlen); 7103 const U8 * t = t0; 7104 const U8 * r = r0; 7105 UV t_count = 0, r_count = 0; /* Number of characters in search and 7106 replacement lists */ 7107 7108 /* khw thinks some of the private flags for this op are quaintly named. 7109 * OPpTRANS_GROWS for example is TRUE if the replacement for some lhs 7110 * character when represented in UTF-8 is longer than the original 7111 * character's UTF-8 representation */ 7112 const bool complement = cBOOL(o->op_private & OPpTRANS_COMPLEMENT); 7113 const bool squash = cBOOL(o->op_private & OPpTRANS_SQUASH); 7114 const bool del = cBOOL(o->op_private & OPpTRANS_DELETE); 7115 7116 /* Set to true if there is some character < 256 in the lhs that maps to 7117 * above 255. If so, a non-UTF-8 match string can be forced into being in 7118 * UTF-8 by a tr/// operation. */ 7119 bool can_force_utf8 = FALSE; 7120 7121 /* What is the maximum expansion factor in UTF-8 transliterations. If a 7122 * 2-byte UTF-8 encoded character is to be replaced by a 3-byte one, its 7123 * expansion factor is 1.5. This number is used at runtime to calculate 7124 * how much space to allocate for non-inplace transliterations. Without 7125 * this number, the worst case is 14, which is extremely unlikely to happen 7126 * in real life, and could require significant memory overhead. */ 7127 NV max_expansion = 1.; 7128 7129 UV t_range_count, r_range_count, min_range_count; 7130 UV* t_array; 7131 SV* t_invlist; 7132 UV* r_map; 7133 UV r_cp = 0, t_cp = 0; 7134 UV t_cp_end = (UV) -1; 7135 UV r_cp_end; 7136 Size_t len; 7137 AV* invmap; 7138 UV final_map = TR_UNLISTED; /* The final character in the replacement 7139 list, updated as we go along. Initialize 7140 to something illegal */ 7141 7142 bool rstr_utf8 = cBOOL(SvUTF8(rstr)); 7143 bool tstr_utf8 = cBOOL(SvUTF8(tstr)); 7144 7145 const U8* tend = t + tlen; 7146 const U8* rend = r + rlen; 7147 7148 SV * inverted_tstr = NULL; 7149 7150 Size_t i; 7151 unsigned int pass2; 7152 7153 /* This routine implements detection of a transliteration having a longer 7154 * UTF-8 representation than its source, by partitioning all the possible 7155 * code points of the platform into equivalence classes of the same UTF-8 7156 * byte length in the first pass. As it constructs the mappings, it carves 7157 * these up into smaller chunks, but doesn't merge any together. This 7158 * makes it easy to find the instances it's looking for. A second pass is 7159 * done after this has been determined which merges things together to 7160 * shrink the table for runtime. The table below is used for both ASCII 7161 * and EBCDIC platforms. On EBCDIC, the byte length is not monotonically 7162 * increasing for code points below 256. To correct for that, the macro 7163 * CP_ADJUST defined below converts those code points to ASCII in the first 7164 * pass, and we use the ASCII partition values. That works because the 7165 * growth factor will be unaffected, which is all that is calculated during 7166 * the first pass. */ 7167 UV PL_partition_by_byte_length[] = { 7168 0, 7169 0x80, /* Below this is 1 byte representations */ 7170 (32 * (1UL << ( UTF_ACCUMULATION_SHIFT))), /* 2 bytes below this */ 7171 (16 * (1UL << (2 * UTF_ACCUMULATION_SHIFT))), /* 3 bytes below this */ 7172 ( 8 * (1UL << (3 * UTF_ACCUMULATION_SHIFT))), /* 4 bytes below this */ 7173 ( 4 * (1UL << (4 * UTF_ACCUMULATION_SHIFT))), /* 5 bytes below this */ 7174 ( 2 * (1UL << (5 * UTF_ACCUMULATION_SHIFT))) /* 6 bytes below this */ 7175 7176 # ifdef UV_IS_QUAD 7177 , 7178 ( ((UV) 1U << (6 * UTF_ACCUMULATION_SHIFT))) /* 7 bytes below this */ 7179 # endif 7180 7181 }; 7182 7183 PERL_ARGS_ASSERT_PMTRANS; 7184 7185 PL_hints |= HINT_BLOCK_SCOPE; 7186 7187 /* If /c, the search list is sorted and complemented. This is now done by 7188 * creating an inversion list from it, and then trivially inverting that. 7189 * The previous implementation used qsort, but creating the list 7190 * automatically keeps it sorted as we go along */ 7191 if (complement) { 7192 UV start, end; 7193 SV * inverted_tlist = _new_invlist(tlen); 7194 Size_t temp_len; 7195 7196 DEBUG_y(PerlIO_printf(Perl_debug_log, 7197 "%s: %d: tstr before inversion=\n%s\n", 7198 __FILE__, __LINE__, _byte_dump_string(t, tend - t, 0))); 7199 7200 while (t < tend) { 7201 7202 /* Non-utf8 strings don't have ranges, so each character is listed 7203 * out */ 7204 if (! tstr_utf8) { 7205 inverted_tlist = add_cp_to_invlist(inverted_tlist, *t); 7206 t++; 7207 } 7208 else { /* But UTF-8 strings have been parsed in toke.c to have 7209 * ranges if appropriate. */ 7210 UV t_cp; 7211 Size_t t_char_len; 7212 7213 /* Get the first character */ 7214 t_cp = valid_utf8_to_uvchr(t, &t_char_len); 7215 t += t_char_len; 7216 7217 /* If the next byte indicates that this wasn't the first 7218 * element of a range, the range is just this one */ 7219 if (t >= tend || *t != RANGE_INDICATOR) { 7220 inverted_tlist = add_cp_to_invlist(inverted_tlist, t_cp); 7221 } 7222 else { /* Otherwise, ignore the indicator byte, and get the 7223 final element, and add the whole range */ 7224 t++; 7225 t_cp_end = valid_utf8_to_uvchr(t, &t_char_len); 7226 t += t_char_len; 7227 7228 inverted_tlist = _add_range_to_invlist(inverted_tlist, 7229 t_cp, t_cp_end); 7230 } 7231 } 7232 } /* End of parse through tstr */ 7233 7234 /* The inversion list is done; now invert it */ 7235 _invlist_invert(inverted_tlist); 7236 7237 /* Now go through the inverted list and create a new tstr for the rest 7238 * of the routine to use. Since the UTF-8 version can have ranges, and 7239 * can be much more compact than the non-UTF-8 version, we create the 7240 * string in UTF-8 even if not necessary. (This is just an intermediate 7241 * value that gets thrown away anyway.) */ 7242 invlist_iterinit(inverted_tlist); 7243 inverted_tstr = newSVpvs(""); 7244 while (invlist_iternext(inverted_tlist, &start, &end)) { 7245 U8 temp[UTF8_MAXBYTES]; 7246 U8 * temp_end_pos; 7247 7248 /* IV_MAX keeps things from going out of bounds */ 7249 start = MIN(IV_MAX, start); 7250 end = MIN(IV_MAX, end); 7251 7252 temp_end_pos = uvchr_to_utf8(temp, start); 7253 sv_catpvn(inverted_tstr, (char *) temp, temp_end_pos - temp); 7254 7255 if (start != end) { 7256 Perl_sv_catpvf(aTHX_ inverted_tstr, "%c", RANGE_INDICATOR); 7257 temp_end_pos = uvchr_to_utf8(temp, end); 7258 sv_catpvn(inverted_tstr, (char *) temp, temp_end_pos - temp); 7259 } 7260 } 7261 7262 /* Set up so the remainder of the routine uses this complement, instead 7263 * of the actual input */ 7264 t0 = t = (U8*)SvPV_const(inverted_tstr, temp_len); 7265 tend = t0 + temp_len; 7266 tstr_utf8 = TRUE; 7267 7268 SvREFCNT_dec_NN(inverted_tlist); 7269 } 7270 7271 /* For non-/d, an empty rhs means to use the lhs */ 7272 if (rlen == 0 && ! del) { 7273 r0 = t0; 7274 rend = tend; 7275 rstr_utf8 = tstr_utf8; 7276 } 7277 7278 t_invlist = _new_invlist(1); 7279 7280 /* Initialize to a single range */ 7281 t_invlist = _add_range_to_invlist(t_invlist, 0, UV_MAX); 7282 7283 /* For the first pass, the lhs is partitioned such that the 7284 * number of UTF-8 bytes required to represent a code point in each 7285 * partition is the same as the number for any other code point in 7286 * that partion. We copy the pre-compiled partion. */ 7287 len = C_ARRAY_LENGTH(PL_partition_by_byte_length); 7288 invlist_extend(t_invlist, len); 7289 t_array = invlist_array(t_invlist); 7290 Copy(PL_partition_by_byte_length, t_array, len, UV); 7291 invlist_set_len(t_invlist, len, *(get_invlist_offset_addr(t_invlist))); 7292 Newx(r_map, len + 1, UV); 7293 7294 /* Parse the (potentially adjusted) input, creating the inversion map. 7295 * This is done in two passes. The first pass is to determine if the 7296 * transliteration can be done in place. The inversion map it creates 7297 * could be used, but generally would be larger and slower to run than the 7298 * output of the second pass, which starts with a more compact table and 7299 * allows more ranges to be merged */ 7300 for (pass2 = 0; pass2 < 2; pass2++) { 7301 if (pass2) { 7302 /* Initialize to a single range */ 7303 t_invlist = _add_range_to_invlist(t_invlist, 0, UV_MAX); 7304 7305 /* In the second pass, we just have the single range */ 7306 len = 1; 7307 t_array = invlist_array(t_invlist); 7308 } 7309 7310 /* As noted earlier, we convert EBCDIC code points to Unicode in the first pass 7311 * so as to get the well-behaved length 1 vs length 2 boundary. Only code 7312 * points below 256 differ between the two character sets in this regard. For 7313 * these, we also can't have any ranges, as they have to be individually 7314 * converted. */ 7315 #ifdef EBCDIC 7316 # define CP_ADJUST(x) ((pass2) ? (x) : NATIVE_TO_UNI(x)) 7317 # define FORCE_RANGE_LEN_1(x) ((pass2) ? 0 : ((x) < 256)) 7318 # define CP_SKIP(x) ((pass2) ? UVCHR_SKIP(x) : OFFUNISKIP(x)) 7319 #else 7320 # define CP_ADJUST(x) (x) 7321 # define FORCE_RANGE_LEN_1(x) 0 7322 # define CP_SKIP(x) UVCHR_SKIP(x) 7323 #endif 7324 7325 /* And the mapping of each of the ranges is initialized. Initially, 7326 * everything is TR_UNLISTED. */ 7327 for (i = 0; i < len; i++) { 7328 r_map[i] = TR_UNLISTED; 7329 } 7330 7331 t = t0; 7332 t_count = 0; 7333 r = r0; 7334 r_count = 0; 7335 t_range_count = r_range_count = 0; 7336 7337 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d:\ntstr=%s\n", 7338 __FILE__, __LINE__, _byte_dump_string(t, tend - t, 0))); 7339 DEBUG_y(PerlIO_printf(Perl_debug_log, "rstr=%s\n", 7340 _byte_dump_string(r, rend - r, 0))); 7341 DEBUG_y(PerlIO_printf(Perl_debug_log, "/c=%d; /s=%d; /d=%d\n", 7342 complement, squash, del)); 7343 DEBUG_y(invmap_dump(t_invlist, r_map)); 7344 7345 /* Now go through the search list constructing an inversion map. The 7346 * input is not necessarily in any particular order. Making it an 7347 * inversion map orders it, potentially simplifying, and makes it easy 7348 * to deal with at run time. This is the only place in core that 7349 * generates an inversion map; if others were introduced, it might be 7350 * better to create general purpose routines to handle them. 7351 * (Inversion maps are created in perl in other places.) 7352 * 7353 * An inversion map consists of two parallel arrays. One is 7354 * essentially an inversion list: an ordered list of code points such 7355 * that each element gives the first code point of a range of 7356 * consecutive code points that map to the element in the other array 7357 * that has the same index as this one (in other words, the 7358 * corresponding element). Thus the range extends up to (but not 7359 * including) the code point given by the next higher element. In a 7360 * true inversion map, the corresponding element in the other array 7361 * gives the mapping of the first code point in the range, with the 7362 * understanding that the next higher code point in the inversion 7363 * list's range will map to the next higher code point in the map. 7364 * 7365 * So if at element [i], let's say we have: 7366 * 7367 * t_invlist r_map 7368 * [i] A a 7369 * 7370 * This means that A => a, B => b, C => c.... Let's say that the 7371 * situation is such that: 7372 * 7373 * [i+1] L -1 7374 * 7375 * This means the sequence that started at [i] stops at K => k. This 7376 * illustrates that you need to look at the next element to find where 7377 * a sequence stops. Except, the highest element in the inversion list 7378 * begins a range that is understood to extend to the platform's 7379 * infinity. 7380 * 7381 * This routine modifies traditional inversion maps to reserve two 7382 * mappings: 7383 * 7384 * TR_UNLISTED (or -1) indicates that no code point in the range 7385 * is listed in the tr/// searchlist. At runtime, these are 7386 * always passed through unchanged. In the inversion map, all 7387 * points in the range are mapped to -1, instead of increasing, 7388 * like the 'L' in the example above. 7389 * 7390 * We start the parse with every code point mapped to this, and as 7391 * we parse and find ones that are listed in the search list, we 7392 * carve out ranges as we go along that override that. 7393 * 7394 * TR_SPECIAL_HANDLING (or -2) indicates that every code point in the 7395 * range needs special handling. Again, all code points in the 7396 * range are mapped to -2, instead of increasing. 7397 * 7398 * Under /d this value means the code point should be deleted from 7399 * the transliteration when encountered. 7400 * 7401 * Otherwise, it marks that every code point in the range is to 7402 * map to the final character in the replacement list. This 7403 * happens only when the replacement list is shorter than the 7404 * search one, so there are things in the search list that have no 7405 * correspondence in the replacement list. For example, in 7406 * tr/a-z/A/, 'A' is the final value, and the inversion map 7407 * generated for this would be like this: 7408 * \0 => -1 7409 * a => A 7410 * b-z => -2 7411 * z+1 => -1 7412 * 'A' appears once, then the remainder of the range maps to -2. 7413 * The use of -2 isn't strictly necessary, as an inversion map is 7414 * capable of representing this situation, but not nearly so 7415 * compactly, and this is actually quite commonly encountered. 7416 * Indeed, the original design of this code used a full inversion 7417 * map for this. But things like 7418 * tr/\0-\x{FFFF}/A/ 7419 * generated huge data structures, slowly, and the execution was 7420 * also slow. So the current scheme was implemented. 7421 * 7422 * So, if the next element in our example is: 7423 * 7424 * [i+2] Q q 7425 * 7426 * Then all of L, M, N, O, and P map to TR_UNLISTED. If the next 7427 * elements are 7428 * 7429 * [i+3] R z 7430 * [i+4] S TR_UNLISTED 7431 * 7432 * Then Q => q; R => z; and S => TR_UNLISTED. If [i+4] (the 'S') is 7433 * the final element in the arrays, every code point from S to infinity 7434 * maps to TR_UNLISTED. 7435 * 7436 */ 7437 /* Finish up range started in what otherwise would 7438 * have been the final iteration */ 7439 while (t < tend || t_range_count > 0) { 7440 bool adjacent_to_range_above = FALSE; 7441 bool adjacent_to_range_below = FALSE; 7442 7443 bool merge_with_range_above = FALSE; 7444 bool merge_with_range_below = FALSE; 7445 7446 UV span, invmap_range_length_remaining; 7447 SSize_t j; 7448 Size_t i; 7449 7450 /* If we are in the middle of processing a range in the 'target' 7451 * side, the previous iteration has set us up. Otherwise, look at 7452 * the next character in the search list */ 7453 if (t_range_count <= 0) { 7454 if (! tstr_utf8) { 7455 7456 /* Here, not in the middle of a range, and not UTF-8. The 7457 * next code point is the single byte where we're at */ 7458 t_cp = CP_ADJUST(*t); 7459 t_range_count = 1; 7460 t++; 7461 } 7462 else { 7463 Size_t t_char_len; 7464 7465 /* Here, not in the middle of a range, and is UTF-8. The 7466 * next code point is the next UTF-8 char in the input. We 7467 * know the input is valid, because the toker constructed 7468 * it */ 7469 t_cp = CP_ADJUST(valid_utf8_to_uvchr(t, &t_char_len)); 7470 t += t_char_len; 7471 7472 /* UTF-8 strings (only) have been parsed in toke.c to have 7473 * ranges. See if the next byte indicates that this was 7474 * the first element of a range. If so, get the final 7475 * element and calculate the range size. If not, the range 7476 * size is 1 */ 7477 if ( t < tend && *t == RANGE_INDICATOR 7478 && ! FORCE_RANGE_LEN_1(t_cp)) 7479 { 7480 t++; 7481 t_range_count = valid_utf8_to_uvchr(t, &t_char_len) 7482 - t_cp + 1; 7483 t += t_char_len; 7484 } 7485 else { 7486 t_range_count = 1; 7487 } 7488 } 7489 7490 /* Count the total number of listed code points * */ 7491 t_count += t_range_count; 7492 } 7493 7494 /* Similarly, get the next character in the replacement list */ 7495 if (r_range_count <= 0) { 7496 if (r >= rend) { 7497 7498 /* But if we've exhausted the rhs, there is nothing to map 7499 * to, except the special handling one, and we make the 7500 * range the same size as the lhs one. */ 7501 r_cp = TR_SPECIAL_HANDLING; 7502 r_range_count = t_range_count; 7503 7504 if (! del) { 7505 DEBUG_yv(PerlIO_printf(Perl_debug_log, 7506 "final_map =%" UVXf "\n", final_map)); 7507 } 7508 } 7509 else { 7510 if (! rstr_utf8) { 7511 r_cp = CP_ADJUST(*r); 7512 r_range_count = 1; 7513 r++; 7514 } 7515 else { 7516 Size_t r_char_len; 7517 7518 r_cp = CP_ADJUST(valid_utf8_to_uvchr(r, &r_char_len)); 7519 r += r_char_len; 7520 if ( r < rend && *r == RANGE_INDICATOR 7521 && ! FORCE_RANGE_LEN_1(r_cp)) 7522 { 7523 r++; 7524 r_range_count = valid_utf8_to_uvchr(r, 7525 &r_char_len) - r_cp + 1; 7526 r += r_char_len; 7527 } 7528 else { 7529 r_range_count = 1; 7530 } 7531 } 7532 7533 if (r_cp == TR_SPECIAL_HANDLING) { 7534 r_range_count = t_range_count; 7535 } 7536 7537 /* This is the final character so far */ 7538 final_map = r_cp + r_range_count - 1; 7539 7540 r_count += r_range_count; 7541 } 7542 } 7543 7544 /* Here, we have the next things ready in both sides. They are 7545 * potentially ranges. We try to process as big a chunk as 7546 * possible at once, but the lhs and rhs must be synchronized, so 7547 * things like tr/A-Z/a-ij-z/ will need to be processed in 2 chunks 7548 * */ 7549 min_range_count = MIN(t_range_count, r_range_count); 7550 7551 /* Search the inversion list for the entry that contains the input 7552 * code point <cp>. The inversion map was initialized to cover the 7553 * entire range of possible inputs, so this should not fail. So 7554 * the return value is the index into the list's array of the range 7555 * that contains <cp>, that is, 'i' such that array[i] <= cp < 7556 * array[i+1] */ 7557 j = _invlist_search(t_invlist, t_cp); 7558 assert(j >= 0); 7559 i = j; 7560 7561 /* Here, the data structure might look like: 7562 * 7563 * index t r Meaning 7564 * [i-1] J j # J-L => j-l 7565 * [i] M -1 # M => default; as do N, O, P, Q 7566 * [i+1] R x # R => x, S => x+1, T => x+2 7567 * [i+2] U y # U => y, V => y+1, ... 7568 * ... 7569 * [-1] Z -1 # Z => default; as do Z+1, ... infinity 7570 * 7571 * where 'x' and 'y' above are not to be taken literally. 7572 * 7573 * The maximum chunk we can handle in this loop iteration, is the 7574 * smallest of the three components: the lhs 't_', the rhs 'r_', 7575 * and the remainder of the range in element [i]. (In pass 1, that 7576 * range will have everything in it be of the same class; we can't 7577 * cross into another class.) 'min_range_count' already contains 7578 * the smallest of the first two values. The final one is 7579 * irrelevant if the map is to the special indicator */ 7580 7581 invmap_range_length_remaining = (i + 1 < len) 7582 ? t_array[i+1] - t_cp 7583 : IV_MAX - t_cp; 7584 span = MAX(1, MIN(min_range_count, invmap_range_length_remaining)); 7585 7586 /* The end point of this chunk is where we are, plus the span, but 7587 * never larger than the platform's infinity */ 7588 t_cp_end = MIN(IV_MAX, t_cp + span - 1); 7589 7590 if (r_cp == TR_SPECIAL_HANDLING) { 7591 7592 /* If unmatched lhs code points map to the final map, use that 7593 * value. This being set to TR_SPECIAL_HANDLING indicates that 7594 * we don't have a final map: unmatched lhs code points are 7595 * simply deleted */ 7596 r_cp_end = (del) ? TR_SPECIAL_HANDLING : final_map; 7597 } 7598 else { 7599 r_cp_end = MIN(IV_MAX, r_cp + span - 1); 7600 7601 /* If something on the lhs is below 256, and something on the 7602 * rhs is above, there is a potential mapping here across that 7603 * boundary. Indeed the only way there isn't is if both sides 7604 * start at the same point. That means they both cross at the 7605 * same time. But otherwise one crosses before the other */ 7606 if (t_cp < 256 && r_cp_end > 255 && r_cp != t_cp) { 7607 can_force_utf8 = TRUE; 7608 } 7609 } 7610 7611 /* If a character appears in the search list more than once, the 7612 * 2nd and succeeding occurrences are ignored, so only do this 7613 * range if haven't already processed this character. (The range 7614 * has been set up so that all members in it will be of the same 7615 * ilk) */ 7616 if (r_map[i] == TR_UNLISTED) { 7617 DEBUG_yv(PerlIO_printf(Perl_debug_log, 7618 "Processing %" UVxf "-%" UVxf " => %" UVxf "-%" UVxf "\n", 7619 t_cp, t_cp_end, r_cp, r_cp_end)); 7620 7621 /* This is the first definition for this chunk, hence is valid 7622 * and needs to be processed. Here and in the comments below, 7623 * we use the above sample data. The t_cp chunk must be any 7624 * contiguous subset of M, N, O, P, and/or Q. 7625 * 7626 * In the first pass, calculate if there is any possible input 7627 * string that has a character whose transliteration will be 7628 * longer than it. If none, the transliteration may be done 7629 * in-place, as it can't write over a so-far unread byte. 7630 * Otherwise, a copy must first be made. This could be 7631 * expensive for long inputs. 7632 * 7633 * In the first pass, the t_invlist has been partitioned so 7634 * that all elements in any single range have the same number 7635 * of bytes in their UTF-8 representations. And the r space is 7636 * either a single byte, or a range of strictly monotonically 7637 * increasing code points. So the final element in the range 7638 * will be represented by no fewer bytes than the initial one. 7639 * That means that if the final code point in the t range has 7640 * at least as many bytes as the final code point in the r, 7641 * then all code points in the t range have at least as many 7642 * bytes as their corresponding r range element. But if that's 7643 * not true, the transliteration of at least the final code 7644 * point grows in length. As an example, suppose we had 7645 * tr/\x{fff0}-\x{fff1}/\x{ffff}-\x{10000}/ 7646 * The UTF-8 for all but 10000 occupies 3 bytes on ASCII 7647 * platforms. We have deliberately set up the data structure 7648 * so that any range in the lhs gets split into chunks for 7649 * processing, such that every code point in a chunk has the 7650 * same number of UTF-8 bytes. We only have to check the final 7651 * code point in the rhs against any code point in the lhs. */ 7652 if ( ! pass2 7653 && r_cp_end != TR_SPECIAL_HANDLING 7654 && CP_SKIP(t_cp_end) < CP_SKIP(r_cp_end)) 7655 { 7656 /* Here, we will need to make a copy of the input string 7657 * before doing the transliteration. The worst possible 7658 * case is an expansion ratio of 14:1. This is rare, and 7659 * we'd rather allocate only the necessary amount of extra 7660 * memory for that copy. We can calculate the worst case 7661 * for this particular transliteration is by keeping track 7662 * of the expansion factor for each range. 7663 * 7664 * Consider tr/\xCB/\X{E000}/. The maximum expansion 7665 * factor is 1 byte going to 3 if the target string is not 7666 * UTF-8, but 2 bytes going to 3 if it is in UTF-8. We 7667 * could pass two different values so doop could choose 7668 * based on the UTF-8ness of the target. But khw thinks 7669 * (perhaps wrongly) that is overkill. It is used only to 7670 * make sure we malloc enough space. 7671 * 7672 * If no target string can force the result to be UTF-8, 7673 * then we don't have to worry about the case of the target 7674 * string not being UTF-8 */ 7675 NV t_size = (can_force_utf8 && t_cp < 256) 7676 ? 1 7677 : CP_SKIP(t_cp_end); 7678 NV ratio = CP_SKIP(r_cp_end) / t_size; 7679 7680 o->op_private |= OPpTRANS_GROWS; 7681 7682 /* Now that we know it grows, we can keep track of the 7683 * largest ratio */ 7684 if (ratio > max_expansion) { 7685 max_expansion = ratio; 7686 DEBUG_y(PerlIO_printf(Perl_debug_log, 7687 "New expansion factor: %" NVgf "\n", 7688 max_expansion)); 7689 } 7690 } 7691 7692 /* The very first range is marked as adjacent to the 7693 * non-existent range below it, as it causes things to "just 7694 * work" (TradeMark) 7695 * 7696 * If the lowest code point in this chunk is M, it adjoins the 7697 * J-L range */ 7698 if (t_cp == t_array[i]) { 7699 adjacent_to_range_below = TRUE; 7700 7701 /* And if the map has the same offset from the beginning of 7702 * the range as does this new code point (or both are for 7703 * TR_SPECIAL_HANDLING), this chunk can be completely 7704 * merged with the range below. EXCEPT, in the first pass, 7705 * we don't merge ranges whose UTF-8 byte representations 7706 * have different lengths, so that we can more easily 7707 * detect if a replacement is longer than the source, that 7708 * is if it 'grows'. But in the 2nd pass, there's no 7709 * reason to not merge */ 7710 if ( (i > 0 && ( pass2 7711 || CP_SKIP(t_array[i-1]) 7712 == CP_SKIP(t_cp))) 7713 && ( ( r_cp == TR_SPECIAL_HANDLING 7714 && r_map[i-1] == TR_SPECIAL_HANDLING) 7715 || ( r_cp != TR_SPECIAL_HANDLING 7716 && r_cp - r_map[i-1] == t_cp - t_array[i-1]))) 7717 { 7718 merge_with_range_below = TRUE; 7719 } 7720 } 7721 7722 /* Similarly, if the highest code point in this chunk is 'Q', 7723 * it adjoins the range above, and if the map is suitable, can 7724 * be merged with it */ 7725 if ( t_cp_end >= IV_MAX - 1 7726 || ( i + 1 < len 7727 && t_cp_end + 1 == t_array[i+1])) 7728 { 7729 adjacent_to_range_above = TRUE; 7730 if (i + 1 < len) 7731 if ( ( pass2 7732 || CP_SKIP(t_cp) == CP_SKIP(t_array[i+1])) 7733 && ( ( r_cp == TR_SPECIAL_HANDLING 7734 && r_map[i+1] == (UV) TR_SPECIAL_HANDLING) 7735 || ( r_cp != TR_SPECIAL_HANDLING 7736 && r_cp_end == r_map[i+1] - 1))) 7737 { 7738 merge_with_range_above = TRUE; 7739 } 7740 } 7741 7742 if (merge_with_range_below && merge_with_range_above) { 7743 7744 /* Here the new chunk looks like M => m, ... Q => q; and 7745 * the range above is like R => r, .... Thus, the [i-1] 7746 * and [i+1] ranges should be seamlessly melded so the 7747 * result looks like 7748 * 7749 * [i-1] J j # J-T => j-t 7750 * [i] U y # U => y, V => y+1, ... 7751 * ... 7752 * [-1] Z -1 # Z => default; as do Z+1, ... infinity 7753 */ 7754 Move(t_array + i + 2, t_array + i, len - i - 2, UV); 7755 Move(r_map + i + 2, r_map + i, len - i - 2, UV); 7756 len -= 2; 7757 invlist_set_len(t_invlist, 7758 len, 7759 *(get_invlist_offset_addr(t_invlist))); 7760 } 7761 else if (merge_with_range_below) { 7762 7763 /* Here the new chunk looks like M => m, .... But either 7764 * (or both) it doesn't extend all the way up through Q; or 7765 * the range above doesn't start with R => r. */ 7766 if (! adjacent_to_range_above) { 7767 7768 /* In the first case, let's say the new chunk extends 7769 * through O. We then want: 7770 * 7771 * [i-1] J j # J-O => j-o 7772 * [i] P -1 # P => -1, Q => -1 7773 * [i+1] R x # R => x, S => x+1, T => x+2 7774 * [i+2] U y # U => y, V => y+1, ... 7775 * ... 7776 * [-1] Z -1 # Z => default; as do Z+1, ... 7777 * infinity 7778 */ 7779 t_array[i] = t_cp_end + 1; 7780 r_map[i] = TR_UNLISTED; 7781 } 7782 else { /* Adjoins the range above, but can't merge with it 7783 (because 'x' is not the next map after q) */ 7784 /* 7785 * [i-1] J j # J-Q => j-q 7786 * [i] R x # R => x, S => x+1, T => x+2 7787 * [i+1] U y # U => y, V => y+1, ... 7788 * ... 7789 * [-1] Z -1 # Z => default; as do Z+1, ... 7790 * infinity 7791 */ 7792 7793 Move(t_array + i + 1, t_array + i, len - i - 1, UV); 7794 Move(r_map + i + 1, r_map + i, len - i - 1, UV); 7795 len--; 7796 invlist_set_len(t_invlist, len, 7797 *(get_invlist_offset_addr(t_invlist))); 7798 } 7799 } 7800 else if (merge_with_range_above) { 7801 7802 /* Here the new chunk ends with Q => q, and the range above 7803 * must start with R => r, so the two can be merged. But 7804 * either (or both) the new chunk doesn't extend all the 7805 * way down to M; or the mapping of the final code point 7806 * range below isn't m */ 7807 if (! adjacent_to_range_below) { 7808 7809 /* In the first case, let's assume the new chunk starts 7810 * with P => p. Then, because it's merge-able with the 7811 * range above, that range must be R => r. We want: 7812 * 7813 * [i-1] J j # J-L => j-l 7814 * [i] M -1 # M => -1, N => -1 7815 * [i+1] P p # P-T => p-t 7816 * [i+2] U y # U => y, V => y+1, ... 7817 * ... 7818 * [-1] Z -1 # Z => default; as do Z+1, ... 7819 * infinity 7820 */ 7821 t_array[i+1] = t_cp; 7822 r_map[i+1] = r_cp; 7823 } 7824 else { /* Adjoins the range below, but can't merge with it 7825 */ 7826 /* 7827 * [i-1] J j # J-L => j-l 7828 * [i] M x # M-T => x-5 .. x+2 7829 * [i+1] U y # U => y, V => y+1, ... 7830 * ... 7831 * [-1] Z -1 # Z => default; as do Z+1, ... 7832 * infinity 7833 */ 7834 Move(t_array + i + 1, t_array + i, len - i - 1, UV); 7835 Move(r_map + i + 1, r_map + i, len - i - 1, UV); 7836 len--; 7837 t_array[i] = t_cp; 7838 r_map[i] = r_cp; 7839 invlist_set_len(t_invlist, len, 7840 *(get_invlist_offset_addr(t_invlist))); 7841 } 7842 } 7843 else if (adjacent_to_range_below && adjacent_to_range_above) { 7844 /* The new chunk completely fills the gap between the 7845 * ranges on either side, but can't merge with either of 7846 * them. 7847 * 7848 * [i-1] J j # J-L => j-l 7849 * [i] M z # M => z, N => z+1 ... Q => z+4 7850 * [i+1] R x # R => x, S => x+1, T => x+2 7851 * [i+2] U y # U => y, V => y+1, ... 7852 * ... 7853 * [-1] Z -1 # Z => default; as do Z+1, ... infinity 7854 */ 7855 r_map[i] = r_cp; 7856 } 7857 else if (adjacent_to_range_below) { 7858 /* The new chunk adjoins the range below, but not the range 7859 * above, and can't merge. Let's assume the chunk ends at 7860 * O. 7861 * 7862 * [i-1] J j # J-L => j-l 7863 * [i] M z # M => z, N => z+1, O => z+2 7864 * [i+1] P -1 # P => -1, Q => -1 7865 * [i+2] R x # R => x, S => x+1, T => x+2 7866 * [i+3] U y # U => y, V => y+1, ... 7867 * ... 7868 * [-w] Z -1 # Z => default; as do Z+1, ... infinity 7869 */ 7870 invlist_extend(t_invlist, len + 1); 7871 t_array = invlist_array(t_invlist); 7872 Renew(r_map, len + 1, UV); 7873 7874 Move(t_array + i + 1, t_array + i + 2, len - i - 1, UV); 7875 Move(r_map + i + 1, r_map + i + 2, len - i - 1, UV); 7876 r_map[i] = r_cp; 7877 t_array[i+1] = t_cp_end + 1; 7878 r_map[i+1] = TR_UNLISTED; 7879 len++; 7880 invlist_set_len(t_invlist, len, 7881 *(get_invlist_offset_addr(t_invlist))); 7882 } 7883 else if (adjacent_to_range_above) { 7884 /* The new chunk adjoins the range above, but not the range 7885 * below, and can't merge. Let's assume the new chunk 7886 * starts at O 7887 * 7888 * [i-1] J j # J-L => j-l 7889 * [i] M -1 # M => default, N => default 7890 * [i+1] O z # O => z, P => z+1, Q => z+2 7891 * [i+2] R x # R => x, S => x+1, T => x+2 7892 * [i+3] U y # U => y, V => y+1, ... 7893 * ... 7894 * [-1] Z -1 # Z => default; as do Z+1, ... infinity 7895 */ 7896 invlist_extend(t_invlist, len + 1); 7897 t_array = invlist_array(t_invlist); 7898 Renew(r_map, len + 1, UV); 7899 7900 Move(t_array + i + 1, t_array + i + 2, len - i - 1, UV); 7901 Move(r_map + i + 1, r_map + i + 2, len - i - 1, UV); 7902 t_array[i+1] = t_cp; 7903 r_map[i+1] = r_cp; 7904 len++; 7905 invlist_set_len(t_invlist, len, 7906 *(get_invlist_offset_addr(t_invlist))); 7907 } 7908 else { 7909 /* The new chunk adjoins neither the range above, nor the 7910 * range below. Lets assume it is N..P => n..p 7911 * 7912 * [i-1] J j # J-L => j-l 7913 * [i] M -1 # M => default 7914 * [i+1] N n # N..P => n..p 7915 * [i+2] Q -1 # Q => default 7916 * [i+3] R x # R => x, S => x+1, T => x+2 7917 * [i+4] U y # U => y, V => y+1, ... 7918 * ... 7919 * [-1] Z -1 # Z => default; as do Z+1, ... infinity 7920 */ 7921 7922 DEBUG_yv(PerlIO_printf(Perl_debug_log, 7923 "Before fixing up: len=%d, i=%d\n", 7924 (int) len, (int) i)); 7925 DEBUG_yv(invmap_dump(t_invlist, r_map)); 7926 7927 invlist_extend(t_invlist, len + 2); 7928 t_array = invlist_array(t_invlist); 7929 Renew(r_map, len + 2, UV); 7930 7931 Move(t_array + i + 1, 7932 t_array + i + 2 + 1, len - i - (2 - 1), UV); 7933 Move(r_map + i + 1, 7934 r_map + i + 2 + 1, len - i - (2 - 1), UV); 7935 7936 len += 2; 7937 invlist_set_len(t_invlist, len, 7938 *(get_invlist_offset_addr(t_invlist))); 7939 7940 t_array[i+1] = t_cp; 7941 r_map[i+1] = r_cp; 7942 7943 t_array[i+2] = t_cp_end + 1; 7944 r_map[i+2] = TR_UNLISTED; 7945 } 7946 DEBUG_yv(PerlIO_printf(Perl_debug_log, 7947 "After iteration: span=%" UVuf ", t_range_count=%" 7948 UVuf " r_range_count=%" UVuf "\n", 7949 span, t_range_count, r_range_count)); 7950 DEBUG_yv(invmap_dump(t_invlist, r_map)); 7951 } /* End of this chunk needs to be processed */ 7952 7953 /* Done with this chunk. */ 7954 t_cp += span; 7955 if (t_cp >= IV_MAX) { 7956 break; 7957 } 7958 t_range_count -= span; 7959 if (r_cp != TR_SPECIAL_HANDLING) { 7960 r_cp += span; 7961 r_range_count -= span; 7962 } 7963 else { 7964 r_range_count = 0; 7965 } 7966 7967 } /* End of loop through the search list */ 7968 7969 /* We don't need an exact count, but we do need to know if there is 7970 * anything left over in the replacement list. So, just assume it's 7971 * one byte per character */ 7972 if (rend > r) { 7973 r_count++; 7974 } 7975 } /* End of passes */ 7976 7977 SvREFCNT_dec(inverted_tstr); 7978 7979 DEBUG_y(PerlIO_printf(Perl_debug_log, "After everything: \n")); 7980 DEBUG_y(invmap_dump(t_invlist, r_map)); 7981 7982 /* We now have normalized the input into an inversion map. 7983 * 7984 * See if the lhs and rhs are equivalent. If so, this tr/// is a no-op 7985 * except for the count, and streamlined runtime code can be used */ 7986 if (!del && !squash) { 7987 7988 /* They are identical if they point to same address, or if everything 7989 * maps to UNLISTED or to itself. This catches things that not looking 7990 * at the normalized inversion map doesn't catch, like tr/aa/ab/ or 7991 * tr/\x{100}-\x{104}/\x{100}-\x{102}\x{103}-\x{104} */ 7992 if (r0 != t0) { 7993 for (i = 0; i < len; i++) { 7994 if (r_map[i] != TR_UNLISTED && r_map[i] != t_array[i]) { 7995 goto done_identical_check; 7996 } 7997 } 7998 } 7999 8000 /* Here have gone through entire list, and didn't find any 8001 * non-identical mappings */ 8002 o->op_private |= OPpTRANS_IDENTICAL; 8003 8004 done_identical_check: ; 8005 } 8006 8007 t_array = invlist_array(t_invlist); 8008 8009 /* If has components above 255, we generally need to use the inversion map 8010 * implementation */ 8011 if ( can_force_utf8 8012 || ( len > 0 8013 && t_array[len-1] > 255 8014 /* If the final range is 0x100-INFINITY and is a special 8015 * mapping, the table implementation can handle it */ 8016 && ! ( t_array[len-1] == 256 8017 && ( r_map[len-1] == TR_UNLISTED 8018 || r_map[len-1] == TR_SPECIAL_HANDLING)))) 8019 { 8020 SV* r_map_sv; 8021 8022 /* A UTF-8 op is generated, indicated by this flag. This op is an 8023 * sv_op */ 8024 o->op_private |= OPpTRANS_USE_SVOP; 8025 8026 if (can_force_utf8) { 8027 o->op_private |= OPpTRANS_CAN_FORCE_UTF8; 8028 } 8029 8030 /* The inversion map is pushed; first the list. */ 8031 invmap = MUTABLE_AV(newAV()); 8032 av_push(invmap, t_invlist); 8033 8034 /* 2nd is the mapping */ 8035 r_map_sv = newSVpvn((char *) r_map, len * sizeof(UV)); 8036 av_push(invmap, r_map_sv); 8037 8038 /* 3rd is the max possible expansion factor */ 8039 av_push(invmap, newSVnv(max_expansion)); 8040 8041 /* Characters that are in the search list, but not in the replacement 8042 * list are mapped to the final character in the replacement list */ 8043 if (! del && r_count < t_count) { 8044 av_push(invmap, newSVuv(final_map)); 8045 } 8046 8047 #ifdef USE_ITHREADS 8048 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY); 8049 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix)); 8050 PAD_SETSV(cPADOPo->op_padix, (SV *) invmap); 8051 SvPADTMP_on(invmap); 8052 SvREADONLY_on(invmap); 8053 #else 8054 cSVOPo->op_sv = (SV *) invmap; 8055 #endif 8056 8057 } 8058 else { 8059 OPtrans_map *tbl; 8060 unsigned short i; 8061 8062 /* The OPtrans_map struct already contains one slot; hence the -1. */ 8063 SSize_t struct_size = sizeof(OPtrans_map) 8064 + (256 - 1 + 1)*sizeof(short); 8065 8066 /* Non-utf8 case: set o->op_pv to point to a simple 256+ entry lookup 8067 * table. Entries with the value TR_UNMAPPED indicate chars not to be 8068 * translated, while TR_DELETE indicates a search char without a 8069 * corresponding replacement char under /d. 8070 * 8071 * In addition, an extra slot at the end is used to store the final 8072 * repeating char, or TR_R_EMPTY under an empty replacement list, or 8073 * TR_DELETE under /d; which makes the runtime code easier. 8074 */ 8075 8076 /* Indicate this is an op_pv */ 8077 o->op_private &= ~OPpTRANS_USE_SVOP; 8078 8079 tbl = (OPtrans_map*)PerlMemShared_calloc(struct_size, 1); 8080 tbl->size = 256; 8081 cPVOPo->op_pv = (char*)tbl; 8082 8083 for (i = 0; i < len; i++) { 8084 STATIC_ASSERT_DECL(TR_SPECIAL_HANDLING == TR_DELETE); 8085 short upper = i >= len - 1 ? 256 : (short) t_array[i+1]; 8086 short to = (short) r_map[i]; 8087 short j; 8088 bool do_increment = TRUE; 8089 8090 /* Any code points above our limit should be irrelevant */ 8091 if (t_array[i] >= tbl->size) break; 8092 8093 /* Set up the map */ 8094 if (to == (short) TR_SPECIAL_HANDLING && ! del) { 8095 to = (short) final_map; 8096 do_increment = FALSE; 8097 } 8098 else if (to < 0) { 8099 do_increment = FALSE; 8100 } 8101 8102 /* Create a map for everything in this range. The value increases 8103 * except for the special cases */ 8104 for (j = (short) t_array[i]; j < upper; j++) { 8105 tbl->map[j] = to; 8106 if (do_increment) to++; 8107 } 8108 } 8109 8110 tbl->map[tbl->size] = del 8111 ? (short) TR_DELETE 8112 : (short) rlen 8113 ? (short) final_map 8114 : (short) TR_R_EMPTY; 8115 DEBUG_y(PerlIO_printf(Perl_debug_log,"%s: %d\n", __FILE__, __LINE__)); 8116 for (i = 0; i < tbl->size; i++) { 8117 if (tbl->map[i] < 0) { 8118 DEBUG_y(PerlIO_printf(Perl_debug_log," %02x=>%d", 8119 (unsigned) i, tbl->map[i])); 8120 } 8121 else { 8122 DEBUG_y(PerlIO_printf(Perl_debug_log," %02x=>%02x", 8123 (unsigned) i, tbl->map[i])); 8124 } 8125 if ((i+1) % 8 == 0 || i + 1 == (short) tbl->size) { 8126 DEBUG_y(PerlIO_printf(Perl_debug_log,"\n")); 8127 } 8128 } 8129 DEBUG_y(PerlIO_printf(Perl_debug_log,"Final map 0x%x=>%02x\n", 8130 (unsigned) tbl->size, tbl->map[tbl->size])); 8131 8132 SvREFCNT_dec(t_invlist); 8133 8134 #if 0 /* code that added excess above-255 chars at the end of the table, in 8135 case we ever want to not use the inversion map implementation for 8136 this */ 8137 8138 ASSUME(j <= rlen); 8139 excess = rlen - j; 8140 8141 if (excess) { 8142 /* More replacement chars than search chars: 8143 * store excess replacement chars at end of main table. 8144 */ 8145 8146 struct_size += excess; 8147 tbl = (OPtrans_map*)PerlMemShared_realloc(tbl, 8148 struct_size + excess * sizeof(short)); 8149 tbl->size += excess; 8150 cPVOPo->op_pv = (char*)tbl; 8151 8152 for (i = 0; i < excess; i++) 8153 tbl->map[i + 256] = r[j+i]; 8154 } 8155 else { 8156 /* no more replacement chars than search chars */ 8157 } 8158 #endif 8159 8160 } 8161 8162 DEBUG_y(PerlIO_printf(Perl_debug_log, 8163 "/d=%d, /s=%d, /c=%d, identical=%d, grows=%d," 8164 " use_svop=%d, can_force_utf8=%d,\nexpansion=%" NVgf "\n", 8165 del, squash, complement, 8166 cBOOL(o->op_private & OPpTRANS_IDENTICAL), 8167 cBOOL(o->op_private & OPpTRANS_USE_SVOP), 8168 cBOOL(o->op_private & OPpTRANS_GROWS), 8169 cBOOL(o->op_private & OPpTRANS_CAN_FORCE_UTF8), 8170 max_expansion)); 8171 8172 Safefree(r_map); 8173 8174 if(del && rlen != 0 && r_count == t_count) { 8175 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); 8176 } else if(r_count > t_count) { 8177 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list"); 8178 } 8179 8180 op_free(expr); 8181 op_free(repl); 8182 8183 return o; 8184 } 8185 8186 8187 /* 8188 =for apidoc newPMOP 8189 8190 Constructs, checks, and returns an op of any pattern matching type. 8191 C<type> is the opcode. C<flags> gives the eight bits of C<op_flags> 8192 and, shifted up eight bits, the eight bits of C<op_private>. 8193 8194 =cut 8195 */ 8196 8197 OP * 8198 Perl_newPMOP(pTHX_ I32 type, I32 flags) 8199 { 8200 PMOP *pmop; 8201 8202 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP 8203 || type == OP_CUSTOM); 8204 8205 NewOp(1101, pmop, 1, PMOP); 8206 OpTYPE_set(pmop, type); 8207 pmop->op_flags = (U8)flags; 8208 pmop->op_private = (U8)(0 | (flags >> 8)); 8209 if (PL_opargs[type] & OA_RETSCALAR) 8210 scalar((OP *)pmop); 8211 8212 if (PL_hints & HINT_RE_TAINT) 8213 pmop->op_pmflags |= PMf_RETAINT; 8214 #ifdef USE_LOCALE_CTYPE 8215 if (IN_LC_COMPILETIME(LC_CTYPE)) { 8216 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET); 8217 } 8218 else 8219 #endif 8220 if (IN_UNI_8_BIT) { 8221 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET); 8222 } 8223 if (PL_hints & HINT_RE_FLAGS) { 8224 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_ 8225 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0 8226 ); 8227 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags); 8228 reflags = Perl_refcounted_he_fetch_pvn(aTHX_ 8229 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0 8230 ); 8231 if (reflags && SvOK(reflags)) { 8232 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags)); 8233 } 8234 } 8235 8236 8237 #ifdef USE_ITHREADS 8238 assert(SvPOK(PL_regex_pad[0])); 8239 if (SvCUR(PL_regex_pad[0])) { 8240 /* Pop off the "packed" IV from the end. */ 8241 SV *const repointer_list = PL_regex_pad[0]; 8242 const char *p = SvEND(repointer_list) - sizeof(IV); 8243 const IV offset = *((IV*)p); 8244 8245 assert(SvCUR(repointer_list) % sizeof(IV) == 0); 8246 8247 SvEND_set(repointer_list, p); 8248 8249 pmop->op_pmoffset = offset; 8250 /* This slot should be free, so assert this: */ 8251 assert(PL_regex_pad[offset] == &PL_sv_undef); 8252 } else { 8253 SV * const repointer = &PL_sv_undef; 8254 av_push(PL_regex_padav, repointer); 8255 pmop->op_pmoffset = av_top_index(PL_regex_padav); 8256 PL_regex_pad = AvARRAY(PL_regex_padav); 8257 } 8258 #endif 8259 8260 return CHECKOP(type, pmop); 8261 } 8262 8263 static void 8264 S_set_haseval(pTHX) 8265 { 8266 PADOFFSET i = 1; 8267 PL_cv_has_eval = 1; 8268 /* Any pad names in scope are potentially lvalues. */ 8269 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) { 8270 PADNAME *pn = PAD_COMPNAME_SV(i); 8271 if (!pn || !PadnameLEN(pn)) 8272 continue; 8273 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax)) 8274 S_mark_padname_lvalue(aTHX_ pn); 8275 } 8276 } 8277 8278 /* Given some sort of match op o, and an expression expr containing a 8279 * pattern, either compile expr into a regex and attach it to o (if it's 8280 * constant), or convert expr into a runtime regcomp op sequence (if it's 8281 * not) 8282 * 8283 * Flags currently has 2 bits of meaning: 8284 * 1: isreg indicates that the pattern is part of a regex construct, eg 8285 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or 8286 * split "pattern", which aren't. In the former case, expr will be a list 8287 * if the pattern contains more than one term (eg /a$b/). 8288 * 2: The pattern is for a split. 8289 * 8290 * When the pattern has been compiled within a new anon CV (for 8291 * qr/(?{...})/ ), then floor indicates the savestack level just before 8292 * the new sub was created 8293 * 8294 * tr/// is also handled. 8295 */ 8296 8297 OP * 8298 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, UV flags, I32 floor) 8299 { 8300 PMOP *pm; 8301 LOGOP *rcop; 8302 I32 repl_has_vars = 0; 8303 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR); 8304 bool is_compiletime; 8305 bool has_code; 8306 bool isreg = cBOOL(flags & 1); 8307 bool is_split = cBOOL(flags & 2); 8308 8309 PERL_ARGS_ASSERT_PMRUNTIME; 8310 8311 if (is_trans) { 8312 return pmtrans(o, expr, repl); 8313 } 8314 8315 /* find whether we have any runtime or code elements; 8316 * at the same time, temporarily set the op_next of each DO block; 8317 * then when we LINKLIST, this will cause the DO blocks to be excluded 8318 * from the op_next chain (and from having LINKLIST recursively 8319 * applied to them). We fix up the DOs specially later */ 8320 8321 is_compiletime = 1; 8322 has_code = 0; 8323 if (expr->op_type == OP_LIST) { 8324 OP *child; 8325 for (child = cLISTOPx(expr)->op_first; child; child = OpSIBLING(child)) { 8326 if (child->op_type == OP_NULL && (child->op_flags & OPf_SPECIAL)) { 8327 has_code = 1; 8328 assert(!child->op_next); 8329 if (UNLIKELY(!OpHAS_SIBLING(child))) { 8330 assert(PL_parser && PL_parser->error_count); 8331 /* This can happen with qr/ (?{(^{})/. Just fake up 8332 the op we were expecting to see, to avoid crashing 8333 elsewhere. */ 8334 op_sibling_splice(expr, child, 0, 8335 newSVOP(OP_CONST, 0, &PL_sv_no)); 8336 } 8337 child->op_next = OpSIBLING(child); 8338 } 8339 else if (child->op_type != OP_CONST && child->op_type != OP_PUSHMARK) 8340 is_compiletime = 0; 8341 } 8342 } 8343 else if (expr->op_type != OP_CONST) 8344 is_compiletime = 0; 8345 8346 LINKLIST(expr); 8347 8348 /* fix up DO blocks; treat each one as a separate little sub; 8349 * also, mark any arrays as LIST/REF */ 8350 8351 if (expr->op_type == OP_LIST) { 8352 OP *child; 8353 for (child = cLISTOPx(expr)->op_first; child; child = OpSIBLING(child)) { 8354 8355 if (child->op_type == OP_PADAV || child->op_type == OP_RV2AV) { 8356 assert( !(child->op_flags & OPf_WANT)); 8357 /* push the array rather than its contents. The regex 8358 * engine will retrieve and join the elements later */ 8359 child->op_flags |= (OPf_WANT_LIST | OPf_REF); 8360 continue; 8361 } 8362 8363 if (!(child->op_type == OP_NULL && (child->op_flags & OPf_SPECIAL))) 8364 continue; 8365 child->op_next = NULL; /* undo temporary hack from above */ 8366 scalar(child); 8367 LINKLIST(child); 8368 if (cLISTOPx(child)->op_first->op_type == OP_LEAVE) { 8369 LISTOP *leaveop = cLISTOPx(cLISTOPx(child)->op_first); 8370 /* skip ENTER */ 8371 assert(leaveop->op_first->op_type == OP_ENTER); 8372 assert(OpHAS_SIBLING(leaveop->op_first)); 8373 child->op_next = OpSIBLING(leaveop->op_first); 8374 /* skip leave */ 8375 assert(leaveop->op_flags & OPf_KIDS); 8376 assert(leaveop->op_last->op_next == (OP*)leaveop); 8377 leaveop->op_next = NULL; /* stop on last op */ 8378 op_null((OP*)leaveop); 8379 } 8380 else { 8381 /* skip SCOPE */ 8382 OP *scope = cLISTOPx(child)->op_first; 8383 assert(scope->op_type == OP_SCOPE); 8384 assert(scope->op_flags & OPf_KIDS); 8385 scope->op_next = NULL; /* stop on last op */ 8386 op_null(scope); 8387 } 8388 8389 /* XXX optimize_optree() must be called on o before 8390 * CALL_PEEP(), as currently S_maybe_multiconcat() can't 8391 * currently cope with a peephole-optimised optree. 8392 * Calling optimize_optree() here ensures that condition 8393 * is met, but may mean optimize_optree() is applied 8394 * to the same optree later (where hopefully it won't do any 8395 * harm as it can't convert an op to multiconcat if it's 8396 * already been converted */ 8397 optimize_optree(child); 8398 8399 /* have to peep the DOs individually as we've removed it from 8400 * the op_next chain */ 8401 CALL_PEEP(child); 8402 S_prune_chain_head(&(child->op_next)); 8403 if (is_compiletime) 8404 /* runtime finalizes as part of finalizing whole tree */ 8405 finalize_optree(child); 8406 } 8407 } 8408 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) { 8409 assert( !(expr->op_flags & OPf_WANT)); 8410 /* push the array rather than its contents. The regex 8411 * engine will retrieve and join the elements later */ 8412 expr->op_flags |= (OPf_WANT_LIST | OPf_REF); 8413 } 8414 8415 PL_hints |= HINT_BLOCK_SCOPE; 8416 pm = (PMOP*)o; 8417 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV)); 8418 8419 if (is_compiletime) { 8420 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME; 8421 regexp_engine const *eng = current_re_engine(); 8422 8423 if (is_split) { 8424 /* make engine handle split ' ' specially */ 8425 pm->op_pmflags |= PMf_SPLIT; 8426 rx_flags |= RXf_SPLIT; 8427 } 8428 8429 if (!has_code || !eng->op_comp) { 8430 /* compile-time simple constant pattern */ 8431 8432 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) { 8433 /* whoops! we guessed that a qr// had a code block, but we 8434 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv 8435 * that isn't required now. Note that we have to be pretty 8436 * confident that nothing used that CV's pad while the 8437 * regex was parsed, except maybe op targets for \Q etc. 8438 * If there were any op targets, though, they should have 8439 * been stolen by constant folding. 8440 */ 8441 #ifdef DEBUGGING 8442 SSize_t i = 0; 8443 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0); 8444 while (++i <= AvFILLp(PL_comppad)) { 8445 # ifdef USE_PAD_RESET 8446 /* under USE_PAD_RESET, pad swipe replaces a swiped 8447 * folded constant with a fresh padtmp */ 8448 assert(!PL_curpad[i] || SvPADTMP(PL_curpad[i])); 8449 # else 8450 assert(!PL_curpad[i]); 8451 # endif 8452 } 8453 #endif 8454 /* This LEAVE_SCOPE will restore PL_compcv to point to the 8455 * outer CV (the one whose slab holds the pm op). The 8456 * inner CV (which holds expr) will be freed later, once 8457 * all the entries on the parse stack have been popped on 8458 * return from this function. Which is why its safe to 8459 * call op_free(expr) below. 8460 */ 8461 LEAVE_SCOPE(floor); 8462 pm->op_pmflags &= ~PMf_HAS_CV; 8463 } 8464 8465 /* Skip compiling if parser found an error for this pattern */ 8466 if (pm->op_pmflags & PMf_HAS_ERROR) { 8467 return o; 8468 } 8469 8470 PM_SETRE(pm, 8471 eng->op_comp 8472 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL, 8473 rx_flags, pm->op_pmflags) 8474 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL, 8475 rx_flags, pm->op_pmflags) 8476 ); 8477 op_free(expr); 8478 } 8479 else { 8480 /* compile-time pattern that includes literal code blocks */ 8481 8482 REGEXP* re; 8483 8484 /* Skip compiling if parser found an error for this pattern */ 8485 if (pm->op_pmflags & PMf_HAS_ERROR) { 8486 return o; 8487 } 8488 8489 re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL, 8490 rx_flags, 8491 (pm->op_pmflags | 8492 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0)) 8493 ); 8494 PM_SETRE(pm, re); 8495 if (pm->op_pmflags & PMf_HAS_CV) { 8496 CV *cv; 8497 /* this QR op (and the anon sub we embed it in) is never 8498 * actually executed. It's just a placeholder where we can 8499 * squirrel away expr in op_code_list without the peephole 8500 * optimiser etc processing it for a second time */ 8501 OP *qr = newPMOP(OP_QR, 0); 8502 ((PMOP*)qr)->op_code_list = expr; 8503 8504 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */ 8505 SvREFCNT_inc_simple_void(PL_compcv); 8506 cv = newATTRSUB(floor, 0, NULL, NULL, qr); 8507 ReANY(re)->qr_anoncv = cv; 8508 8509 /* attach the anon CV to the pad so that 8510 * pad_fixup_inner_anons() can find it */ 8511 (void)pad_add_anon(cv, o->op_type); 8512 SvREFCNT_inc_simple_void(cv); 8513 } 8514 else { 8515 pm->op_code_list = expr; 8516 } 8517 } 8518 } 8519 else { 8520 /* runtime pattern: build chain of regcomp etc ops */ 8521 bool reglist; 8522 PADOFFSET cv_targ = 0; 8523 8524 reglist = isreg && expr->op_type == OP_LIST; 8525 if (reglist) 8526 op_null(expr); 8527 8528 if (has_code) { 8529 pm->op_code_list = expr; 8530 /* don't free op_code_list; its ops are embedded elsewhere too */ 8531 pm->op_pmflags |= PMf_CODELIST_PRIVATE; 8532 } 8533 8534 if (is_split) 8535 /* make engine handle split ' ' specially */ 8536 pm->op_pmflags |= PMf_SPLIT; 8537 8538 /* the OP_REGCMAYBE is a placeholder in the non-threaded case 8539 * to allow its op_next to be pointed past the regcomp and 8540 * preceding stacking ops; 8541 * OP_REGCRESET is there to reset taint before executing the 8542 * stacking ops */ 8543 if (pm->op_pmflags & PMf_KEEP || TAINTING_get) 8544 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr); 8545 8546 if (pm->op_pmflags & PMf_HAS_CV) { 8547 /* we have a runtime qr with literal code. This means 8548 * that the qr// has been wrapped in a new CV, which 8549 * means that runtime consts, vars etc will have been compiled 8550 * against a new pad. So... we need to execute those ops 8551 * within the environment of the new CV. So wrap them in a call 8552 * to a new anon sub. i.e. for 8553 * 8554 * qr/a$b(?{...})/, 8555 * 8556 * we build an anon sub that looks like 8557 * 8558 * sub { "a", $b, '(?{...})' } 8559 * 8560 * and call it, passing the returned list to regcomp. 8561 * Or to put it another way, the list of ops that get executed 8562 * are: 8563 * 8564 * normal PMf_HAS_CV 8565 * ------ ------------------- 8566 * pushmark (for regcomp) 8567 * pushmark (for entersub) 8568 * anoncode 8569 * srefgen 8570 * entersub 8571 * regcreset regcreset 8572 * pushmark pushmark 8573 * const("a") const("a") 8574 * gvsv(b) gvsv(b) 8575 * const("(?{...})") const("(?{...})") 8576 * leavesub 8577 * regcomp regcomp 8578 */ 8579 8580 SvREFCNT_inc_simple_void(PL_compcv); 8581 CvLVALUE_on(PL_compcv); 8582 /* these lines are just an unrolled newANONATTRSUB */ 8583 expr = newSVOP(OP_ANONCODE, 0, 8584 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr))); 8585 cv_targ = expr->op_targ; 8586 expr = newUNOP(OP_REFGEN, 0, expr); 8587 8588 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), TRUE)); 8589 } 8590 8591 rcop = alloc_LOGOP(OP_REGCOMP, scalar(expr), o); 8592 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0) 8593 | (reglist ? OPf_STACKED : 0); 8594 rcop->op_targ = cv_targ; 8595 8596 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */ 8597 if (PL_hints & HINT_RE_EVAL) 8598 S_set_haseval(aTHX); 8599 8600 /* establish postfix order */ 8601 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) { 8602 LINKLIST(expr); 8603 rcop->op_next = expr; 8604 ((UNOP*)expr)->op_first->op_next = (OP*)rcop; 8605 } 8606 else { 8607 rcop->op_next = LINKLIST(expr); 8608 expr->op_next = (OP*)rcop; 8609 } 8610 8611 op_prepend_elem(o->op_type, scalar((OP*)rcop), o); 8612 } 8613 8614 if (repl) { 8615 OP *curop = repl; 8616 bool konst; 8617 /* If we are looking at s//.../e with a single statement, get past 8618 the implicit do{}. */ 8619 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS 8620 && cUNOPx(curop)->op_first->op_type == OP_SCOPE 8621 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS) 8622 { 8623 OP *sib; 8624 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first; 8625 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid)) 8626 && !OpHAS_SIBLING(sib)) 8627 curop = sib; 8628 } 8629 if (curop->op_type == OP_CONST) 8630 konst = TRUE; 8631 else if (( (curop->op_type == OP_RV2SV || 8632 curop->op_type == OP_RV2AV || 8633 curop->op_type == OP_RV2HV || 8634 curop->op_type == OP_RV2GV) 8635 && cUNOPx(curop)->op_first 8636 && cUNOPx(curop)->op_first->op_type == OP_GV ) 8637 || curop->op_type == OP_PADSV 8638 || curop->op_type == OP_PADAV 8639 || curop->op_type == OP_PADHV 8640 || curop->op_type == OP_PADANY) { 8641 repl_has_vars = 1; 8642 konst = TRUE; 8643 } 8644 else konst = FALSE; 8645 if (konst 8646 && !(repl_has_vars 8647 && (!PM_GETRE(pm) 8648 || !RX_PRELEN(PM_GETRE(pm)) 8649 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN))) 8650 { 8651 pm->op_pmflags |= PMf_CONST; /* const for long enough */ 8652 op_prepend_elem(o->op_type, scalar(repl), o); 8653 } 8654 else { 8655 rcop = alloc_LOGOP(OP_SUBSTCONT, scalar(repl), o); 8656 rcop->op_private = 1; 8657 8658 /* establish postfix order */ 8659 rcop->op_next = LINKLIST(repl); 8660 repl->op_next = (OP*)rcop; 8661 8662 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop); 8663 assert(!(pm->op_pmflags & PMf_ONCE)); 8664 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop); 8665 rcop->op_next = 0; 8666 } 8667 } 8668 8669 return (OP*)pm; 8670 } 8671 8672 /* 8673 =for apidoc newSVOP 8674 8675 Constructs, checks, and returns an op of any type that involves an 8676 embedded SV. C<type> is the opcode. C<flags> gives the eight bits 8677 of C<op_flags>. C<sv> gives the SV to embed in the op; this function 8678 takes ownership of one reference to it. 8679 8680 =cut 8681 */ 8682 8683 OP * 8684 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv) 8685 { 8686 SVOP *svop; 8687 8688 PERL_ARGS_ASSERT_NEWSVOP; 8689 8690 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP 8691 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP 8692 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 8693 || type == OP_CUSTOM); 8694 8695 NewOp(1101, svop, 1, SVOP); 8696 OpTYPE_set(svop, type); 8697 svop->op_sv = sv; 8698 svop->op_next = (OP*)svop; 8699 svop->op_flags = (U8)flags; 8700 svop->op_private = (U8)(0 | (flags >> 8)); 8701 if (PL_opargs[type] & OA_RETSCALAR) 8702 scalar((OP*)svop); 8703 if (PL_opargs[type] & OA_TARGET) 8704 svop->op_targ = pad_alloc(type, SVs_PADTMP); 8705 return CHECKOP(type, svop); 8706 } 8707 8708 /* 8709 =for apidoc newDEFSVOP 8710 8711 Constructs and returns an op to access C<$_>. 8712 8713 =cut 8714 */ 8715 8716 OP * 8717 Perl_newDEFSVOP(pTHX) 8718 { 8719 return newSVREF(newGVOP(OP_GV, 0, PL_defgv)); 8720 } 8721 8722 #ifdef USE_ITHREADS 8723 8724 /* 8725 =for apidoc newPADOP 8726 8727 Constructs, checks, and returns an op of any type that involves a 8728 reference to a pad element. C<type> is the opcode. C<flags> gives the 8729 eight bits of C<op_flags>. A pad slot is automatically allocated, and 8730 is populated with C<sv>; this function takes ownership of one reference 8731 to it. 8732 8733 This function only exists if Perl has been compiled to use ithreads. 8734 8735 =cut 8736 */ 8737 8738 OP * 8739 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv) 8740 { 8741 PADOP *padop; 8742 8743 PERL_ARGS_ASSERT_NEWPADOP; 8744 8745 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP 8746 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP 8747 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 8748 || type == OP_CUSTOM); 8749 8750 NewOp(1101, padop, 1, PADOP); 8751 OpTYPE_set(padop, type); 8752 padop->op_padix = 8753 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP); 8754 SvREFCNT_dec(PAD_SVl(padop->op_padix)); 8755 PAD_SETSV(padop->op_padix, sv); 8756 assert(sv); 8757 padop->op_next = (OP*)padop; 8758 padop->op_flags = (U8)flags; 8759 if (PL_opargs[type] & OA_RETSCALAR) 8760 scalar((OP*)padop); 8761 if (PL_opargs[type] & OA_TARGET) 8762 padop->op_targ = pad_alloc(type, SVs_PADTMP); 8763 return CHECKOP(type, padop); 8764 } 8765 8766 #endif /* USE_ITHREADS */ 8767 8768 /* 8769 =for apidoc newGVOP 8770 8771 Constructs, checks, and returns an op of any type that involves an 8772 embedded reference to a GV. C<type> is the opcode. C<flags> gives the 8773 eight bits of C<op_flags>. C<gv> identifies the GV that the op should 8774 reference; calling this function does not transfer ownership of any 8775 reference to it. 8776 8777 =cut 8778 */ 8779 8780 OP * 8781 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv) 8782 { 8783 PERL_ARGS_ASSERT_NEWGVOP; 8784 8785 #ifdef USE_ITHREADS 8786 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv)); 8787 #else 8788 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv)); 8789 #endif 8790 } 8791 8792 /* 8793 =for apidoc newPVOP 8794 8795 Constructs, checks, and returns an op of any type that involves an 8796 embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives 8797 the eight bits of C<op_flags>. C<pv> supplies the C-level pointer. 8798 Depending on the op type, the memory referenced by C<pv> may be freed 8799 when the op is destroyed. If the op is of a freeing type, C<pv> must 8800 have been allocated using C<PerlMemShared_malloc>. 8801 8802 =cut 8803 */ 8804 8805 OP * 8806 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv) 8807 { 8808 const bool utf8 = cBOOL(flags & SVf_UTF8); 8809 PVOP *pvop; 8810 8811 flags &= ~SVf_UTF8; 8812 8813 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP 8814 || type == OP_RUNCV || type == OP_CUSTOM 8815 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); 8816 8817 NewOp(1101, pvop, 1, PVOP); 8818 OpTYPE_set(pvop, type); 8819 pvop->op_pv = pv; 8820 pvop->op_next = (OP*)pvop; 8821 pvop->op_flags = (U8)flags; 8822 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0; 8823 if (PL_opargs[type] & OA_RETSCALAR) 8824 scalar((OP*)pvop); 8825 if (PL_opargs[type] & OA_TARGET) 8826 pvop->op_targ = pad_alloc(type, SVs_PADTMP); 8827 return CHECKOP(type, pvop); 8828 } 8829 8830 void 8831 Perl_package(pTHX_ OP *o) 8832 { 8833 SV *const sv = cSVOPo->op_sv; 8834 8835 PERL_ARGS_ASSERT_PACKAGE; 8836 8837 SAVEGENERICSV(PL_curstash); 8838 save_item(PL_curstname); 8839 8840 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD)); 8841 8842 sv_setsv(PL_curstname, sv); 8843 8844 PL_hints |= HINT_BLOCK_SCOPE; 8845 PL_parser->copline = NOLINE; 8846 8847 op_free(o); 8848 } 8849 8850 void 8851 Perl_package_version( pTHX_ OP *v ) 8852 { 8853 U32 savehints = PL_hints; 8854 PERL_ARGS_ASSERT_PACKAGE_VERSION; 8855 PL_hints &= ~HINT_STRICT_VARS; 8856 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv ); 8857 PL_hints = savehints; 8858 op_free(v); 8859 } 8860 8861 /* Extract the first two components of a "version" object as two 8bit integers 8862 * and return them packed into a single U16 in the format of PL_prevailing_version. 8863 * This function only ever has to cope with version objects already known 8864 * bounded by the current perl version, so we know its components will fit 8865 * (Up until we reach perl version 5.256 anyway) */ 8866 static U16 S_extract_shortver(pTHX_ SV *sv) 8867 { 8868 SV *rv; 8869 if(!SvRV(sv) || !SvOBJECT(rv = SvRV(sv)) || !sv_derived_from(sv, "version")) 8870 return 0; 8871 8872 AV *av = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(rv), "version", 0))); 8873 8874 U16 shortver = 0; 8875 8876 IV major = av_count(av) > 0 ? SvIV(*av_fetch(av, 0, false)) : 0; 8877 if(major > 255) 8878 shortver |= 255 << 8; 8879 else 8880 shortver |= major << 8; 8881 8882 IV minor = av_count(av) > 1 ? SvIV(*av_fetch(av, 1, false)) : 0; 8883 if(minor > 255) 8884 shortver |= 255; 8885 else 8886 shortver |= minor; 8887 8888 return shortver; 8889 } 8890 #define SHORTVER(maj,min) ((maj << 8) | min) 8891 8892 void 8893 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg) 8894 { 8895 OP *pack; 8896 OP *imop; 8897 OP *veop; 8898 SV *use_version = NULL; 8899 8900 PERL_ARGS_ASSERT_UTILIZE; 8901 8902 if (idop->op_type != OP_CONST) 8903 Perl_croak(aTHX_ "Module name must be constant"); 8904 8905 veop = NULL; 8906 8907 if (version) { 8908 SV * const vesv = ((SVOP*)version)->op_sv; 8909 8910 if (!arg && !SvNIOKp(vesv)) { 8911 arg = version; 8912 } 8913 else { 8914 OP *pack; 8915 SV *meth; 8916 8917 if (version->op_type != OP_CONST || !SvNIOKp(vesv)) 8918 Perl_croak(aTHX_ "Version number must be a constant number"); 8919 8920 /* Make copy of idop so we don't free it twice */ 8921 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); 8922 8923 /* Fake up a method call to VERSION */ 8924 meth = newSVpvs_share("VERSION"); 8925 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, 8926 op_append_elem(OP_LIST, 8927 op_prepend_elem(OP_LIST, pack, version), 8928 newMETHOP_named(OP_METHOD_NAMED, 0, meth))); 8929 } 8930 } 8931 8932 /* Fake up an import/unimport */ 8933 if (arg && arg->op_type == OP_STUB) { 8934 imop = arg; /* no import on explicit () */ 8935 } 8936 else if (SvNIOKp(((SVOP*)idop)->op_sv)) { 8937 imop = NULL; /* use 5.0; */ 8938 if (aver) 8939 use_version = ((SVOP*)idop)->op_sv; 8940 else 8941 idop->op_private |= OPpCONST_NOVER; 8942 } 8943 else { 8944 SV *meth; 8945 8946 /* Make copy of idop so we don't free it twice */ 8947 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); 8948 8949 /* Fake up a method call to import/unimport */ 8950 meth = aver 8951 ? newSVpvs_share("import") : newSVpvs_share("unimport"); 8952 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, 8953 op_append_elem(OP_LIST, 8954 op_prepend_elem(OP_LIST, pack, arg), 8955 newMETHOP_named(OP_METHOD_NAMED, 0, meth) 8956 )); 8957 } 8958 8959 /* Fake up the BEGIN {}, which does its thing immediately. */ 8960 newATTRSUB(floor, 8961 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")), 8962 NULL, 8963 NULL, 8964 op_append_elem(OP_LINESEQ, 8965 op_append_elem(OP_LINESEQ, 8966 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)), 8967 newSTATEOP(0, NULL, veop)), 8968 newSTATEOP(0, NULL, imop) )); 8969 8970 if (use_version) { 8971 /* Enable the 8972 * feature bundle that corresponds to the required version. */ 8973 use_version = sv_2mortal(new_version(use_version)); 8974 S_enable_feature_bundle(aTHX_ use_version); 8975 8976 U16 shortver = S_extract_shortver(aTHX_ use_version); 8977 8978 /* If a version >= 5.11.0 is requested, strictures are on by default! */ 8979 if (shortver >= SHORTVER(5, 11)) { 8980 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS)) 8981 PL_hints |= HINT_STRICT_REFS; 8982 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS)) 8983 PL_hints |= HINT_STRICT_SUBS; 8984 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) 8985 PL_hints |= HINT_STRICT_VARS; 8986 8987 if (shortver >= SHORTVER(5, 35)) 8988 free_and_set_cop_warnings(&PL_compiling, pWARN_ALL); 8989 } 8990 /* otherwise they are off */ 8991 else { 8992 if(PL_prevailing_version >= SHORTVER(5, 11)) 8993 deprecate_fatal_in("5.40", 8994 "Downgrading a use VERSION declaration to below v5.11"); 8995 8996 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS)) 8997 PL_hints &= ~HINT_STRICT_REFS; 8998 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS)) 8999 PL_hints &= ~HINT_STRICT_SUBS; 9000 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) 9001 PL_hints &= ~HINT_STRICT_VARS; 9002 } 9003 9004 PL_prevailing_version = shortver; 9005 } 9006 9007 /* The "did you use incorrect case?" warning used to be here. 9008 * The problem is that on case-insensitive filesystems one 9009 * might get false positives for "use" (and "require"): 9010 * "use Strict" or "require CARP" will work. This causes 9011 * portability problems for the script: in case-strict 9012 * filesystems the script will stop working. 9013 * 9014 * The "incorrect case" warning checked whether "use Foo" 9015 * imported "Foo" to your namespace, but that is wrong, too: 9016 * there is no requirement nor promise in the language that 9017 * a Foo.pm should or would contain anything in package "Foo". 9018 * 9019 * There is very little Configure-wise that can be done, either: 9020 * the case-sensitivity of the build filesystem of Perl does not 9021 * help in guessing the case-sensitivity of the runtime environment. 9022 */ 9023 9024 PL_hints |= HINT_BLOCK_SCOPE; 9025 PL_parser->copline = NOLINE; 9026 COP_SEQMAX_INC; /* Purely for B::*'s benefit */ 9027 } 9028 9029 /* 9030 =for apidoc_section $embedding 9031 9032 =for apidoc load_module 9033 =for apidoc_item load_module_nocontext 9034 9035 These load the module whose name is pointed to by the string part of C<name>. 9036 Note that the actual module name, not its filename, should be given. 9037 Eg, "Foo::Bar" instead of "Foo/Bar.pm". ver, if specified and not NULL, 9038 provides version semantics similar to C<use Foo::Bar VERSION>. The optional 9039 trailing arguments can be used to specify arguments to the module's C<import()> 9040 method, similar to C<use Foo::Bar VERSION LIST>; their precise handling depends 9041 on the flags. The flags argument is a bitwise-ORed collection of any of 9042 C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS> 9043 (or 0 for no flags). 9044 9045 If C<PERL_LOADMOD_NOIMPORT> is set, the module is loaded as if with an empty 9046 import list, as in C<use Foo::Bar ()>; this is the only circumstance in which 9047 the trailing optional arguments may be omitted entirely. Otherwise, if 9048 C<PERL_LOADMOD_IMPORT_OPS> is set, the trailing arguments must consist of 9049 exactly one C<OP*>, containing the op tree that produces the relevant import 9050 arguments. Otherwise, the trailing arguments must all be C<SV*> values that 9051 will be used as import arguments; and the list must be terminated with C<(SV*) 9052 NULL>. If neither C<PERL_LOADMOD_NOIMPORT> nor C<PERL_LOADMOD_IMPORT_OPS> is 9053 set, the trailing C<NULL> pointer is needed even if no import arguments are 9054 desired. The reference count for each specified C<SV*> argument is 9055 decremented. In addition, the C<name> argument is modified. 9056 9057 If C<PERL_LOADMOD_DENY> is set, the module is loaded as if with C<no> rather 9058 than C<use>. 9059 9060 C<load_module> and C<load_module_nocontext> have the same apparent signature, 9061 but the former hides the fact that it is accessing a thread context parameter. 9062 So use the latter when you get a compilation error about C<pTHX>. 9063 9064 =for apidoc Amnh||PERL_LOADMOD_DENY 9065 =for apidoc Amnh||PERL_LOADMOD_NOIMPORT 9066 =for apidoc Amnh||PERL_LOADMOD_IMPORT_OPS 9067 9068 =for apidoc vload_module 9069 Like C<L</load_module>> but the arguments are an encapsulated argument list. 9070 9071 =cut */ 9072 9073 void 9074 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...) 9075 { 9076 va_list args; 9077 9078 PERL_ARGS_ASSERT_LOAD_MODULE; 9079 9080 va_start(args, ver); 9081 vload_module(flags, name, ver, &args); 9082 va_end(args); 9083 } 9084 9085 #ifdef MULTIPLICITY 9086 void 9087 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...) 9088 { 9089 dTHX; 9090 va_list args; 9091 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT; 9092 va_start(args, ver); 9093 vload_module(flags, name, ver, &args); 9094 va_end(args); 9095 } 9096 #endif 9097 9098 void 9099 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args) 9100 { 9101 OP *veop, *imop; 9102 OP * modname; 9103 I32 floor; 9104 9105 PERL_ARGS_ASSERT_VLOAD_MODULE; 9106 9107 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure 9108 * that it has a PL_parser to play with while doing that, and also 9109 * that it doesn't mess with any existing parser, by creating a tmp 9110 * new parser with lex_start(). This won't actually be used for much, 9111 * since pp_require() will create another parser for the real work. 9112 * The ENTER/LEAVE pair protect callers from any side effects of use. 9113 * 9114 * start_subparse() creates a new PL_compcv. This means that any ops 9115 * allocated below will be allocated from that CV's op slab, and so 9116 * will be automatically freed if the utilise() fails 9117 */ 9118 9119 ENTER; 9120 SAVEVPTR(PL_curcop); 9121 lex_start(NULL, NULL, LEX_START_SAME_FILTER); 9122 floor = start_subparse(FALSE, 0); 9123 9124 modname = newSVOP(OP_CONST, 0, name); 9125 modname->op_private |= OPpCONST_BARE; 9126 if (ver) { 9127 veop = newSVOP(OP_CONST, 0, ver); 9128 } 9129 else 9130 veop = NULL; 9131 if (flags & PERL_LOADMOD_NOIMPORT) { 9132 imop = sawparens(newNULLLIST()); 9133 } 9134 else if (flags & PERL_LOADMOD_IMPORT_OPS) { 9135 imop = va_arg(*args, OP*); 9136 } 9137 else { 9138 SV *sv; 9139 imop = NULL; 9140 sv = va_arg(*args, SV*); 9141 while (sv) { 9142 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv)); 9143 sv = va_arg(*args, SV*); 9144 } 9145 } 9146 9147 utilize(!(flags & PERL_LOADMOD_DENY), floor, veop, modname, imop); 9148 LEAVE; 9149 } 9150 9151 PERL_STATIC_INLINE OP * 9152 S_new_entersubop(pTHX_ GV *gv, OP *arg) 9153 { 9154 return newUNOP(OP_ENTERSUB, OPf_STACKED, 9155 newLISTOP(OP_LIST, 0, arg, 9156 newUNOP(OP_RV2CV, 0, 9157 newGVOP(OP_GV, 0, gv)))); 9158 } 9159 9160 OP * 9161 Perl_dofile(pTHX_ OP *term, I32 force_builtin) 9162 { 9163 OP *doop; 9164 GV *gv; 9165 9166 PERL_ARGS_ASSERT_DOFILE; 9167 9168 if (!force_builtin && (gv = gv_override("do", 2))) { 9169 doop = S_new_entersubop(aTHX_ gv, term); 9170 } 9171 else { 9172 doop = newUNOP(OP_DOFILE, 0, scalar(term)); 9173 } 9174 return doop; 9175 } 9176 9177 /* 9178 =for apidoc_section $optree_construction 9179 9180 =for apidoc newSLICEOP 9181 9182 Constructs, checks, and returns an C<lslice> (list slice) op. C<flags> 9183 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will 9184 be set automatically, and, shifted up eight bits, the eight bits of 9185 C<op_private>, except that the bit with value 1 or 2 is automatically 9186 set as required. C<listval> and C<subscript> supply the parameters of 9187 the slice; they are consumed by this function and become part of the 9188 constructed op tree. 9189 9190 =cut 9191 */ 9192 9193 OP * 9194 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval) 9195 { 9196 return newBINOP(OP_LSLICE, flags, 9197 list(force_list(subscript, TRUE)), 9198 list(force_list(listval, TRUE))); 9199 } 9200 9201 #define ASSIGN_SCALAR 0 9202 #define ASSIGN_LIST 1 9203 #define ASSIGN_REF 2 9204 9205 /* given the optree o on the LHS of an assignment, determine whether its: 9206 * ASSIGN_SCALAR $x = ... 9207 * ASSIGN_LIST ($x) = ... 9208 * ASSIGN_REF \$x = ... 9209 */ 9210 9211 STATIC I32 9212 S_assignment_type(pTHX_ const OP *o) 9213 { 9214 unsigned type; 9215 U8 flags; 9216 U8 ret; 9217 9218 if (!o) 9219 return ASSIGN_LIST; 9220 9221 if (o->op_type == OP_SREFGEN) 9222 { 9223 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first; 9224 type = kid->op_type; 9225 flags = o->op_flags | kid->op_flags; 9226 if (!(flags & OPf_PARENS) 9227 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV || 9228 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV )) 9229 return ASSIGN_REF; 9230 ret = ASSIGN_REF; 9231 } else { 9232 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS)) 9233 o = cUNOPo->op_first; 9234 flags = o->op_flags; 9235 type = o->op_type; 9236 ret = ASSIGN_SCALAR; 9237 } 9238 9239 if (type == OP_COND_EXPR) { 9240 OP * const sib = OpSIBLING(cLOGOPo->op_first); 9241 const I32 t = assignment_type(sib); 9242 const I32 f = assignment_type(OpSIBLING(sib)); 9243 9244 if (t == ASSIGN_LIST && f == ASSIGN_LIST) 9245 return ASSIGN_LIST; 9246 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST)) 9247 yyerror("Assignment to both a list and a scalar"); 9248 return ASSIGN_SCALAR; 9249 } 9250 9251 if (type == OP_LIST && 9252 (flags & OPf_WANT) == OPf_WANT_SCALAR && 9253 o->op_private & OPpLVAL_INTRO) 9254 return ret; 9255 9256 if (type == OP_LIST || flags & OPf_PARENS || 9257 type == OP_RV2AV || type == OP_RV2HV || 9258 type == OP_ASLICE || type == OP_HSLICE || 9259 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN) 9260 return ASSIGN_LIST; 9261 9262 if (type == OP_PADAV || type == OP_PADHV) 9263 return ASSIGN_LIST; 9264 9265 if (type == OP_RV2SV) 9266 return ret; 9267 9268 return ret; 9269 } 9270 9271 static OP * 9272 S_newONCEOP(pTHX_ OP *initop, OP *padop) 9273 { 9274 const PADOFFSET target = padop->op_targ; 9275 OP *const other = newOP(OP_PADSV, 9276 padop->op_flags 9277 | ((padop->op_private & ~OPpLVAL_INTRO) << 8)); 9278 OP *const first = newOP(OP_NULL, 0); 9279 OP *const nullop = newCONDOP(0, first, initop, other); 9280 /* XXX targlex disabled for now; see ticket #124160 9281 newCONDOP(0, first, S_maybe_targlex(aTHX_ initop), other); 9282 */ 9283 OP *const condop = first->op_next; 9284 9285 OpTYPE_set(condop, OP_ONCE); 9286 other->op_targ = target; 9287 nullop->op_flags |= OPf_WANT_SCALAR; 9288 9289 /* Store the initializedness of state vars in a separate 9290 pad entry. */ 9291 condop->op_targ = 9292 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0); 9293 /* hijacking PADSTALE for uninitialized state variables */ 9294 SvPADSTALE_on(PAD_SVl(condop->op_targ)); 9295 9296 return nullop; 9297 } 9298 9299 /* 9300 =for apidoc newASSIGNOP 9301 9302 Constructs, checks, and returns an assignment op. C<left> and C<right> 9303 supply the parameters of the assignment; they are consumed by this 9304 function and become part of the constructed op tree. 9305 9306 If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then 9307 a suitable conditional optree is constructed. If C<optype> is the opcode 9308 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that 9309 performs the binary operation and assigns the result to the left argument. 9310 Either way, if C<optype> is non-zero then C<flags> has no effect. 9311 9312 If C<optype> is zero, then a plain scalar or list assignment is 9313 constructed. Which type of assignment it is is automatically determined. 9314 C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS> 9315 will be set automatically, and, shifted up eight bits, the eight bits 9316 of C<op_private>, except that the bit with value 1 or 2 is automatically 9317 set as required. 9318 9319 =cut 9320 */ 9321 9322 OP * 9323 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) 9324 { 9325 OP *o; 9326 I32 assign_type; 9327 9328 switch (optype) { 9329 case 0: break; 9330 case OP_ANDASSIGN: 9331 case OP_ORASSIGN: 9332 case OP_DORASSIGN: 9333 right = scalar(right); 9334 return newLOGOP(optype, 0, 9335 op_lvalue(scalar(left), optype), 9336 newBINOP(OP_SASSIGN, OPpASSIGN_BACKWARDS<<8, right, right)); 9337 default: 9338 return newBINOP(optype, OPf_STACKED, 9339 op_lvalue(scalar(left), optype), scalar(right)); 9340 } 9341 9342 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) { 9343 OP *state_var_op = NULL; 9344 static const char no_list_state[] = "Initialization of state variables" 9345 " in list currently forbidden"; 9346 OP *curop; 9347 9348 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE) 9349 left->op_private &= ~ OPpSLICEWARNING; 9350 9351 PL_modcount = 0; 9352 left = op_lvalue(left, OP_AASSIGN); 9353 curop = list(force_list(left, TRUE)); 9354 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, TRUE)), curop); 9355 o->op_private = (U8)(0 | (flags >> 8)); 9356 9357 if (OP_TYPE_IS_OR_WAS(left, OP_LIST)) 9358 { 9359 OP *lop = ((LISTOP*)left)->op_first, *vop, *eop; 9360 if (!(left->op_flags & OPf_PARENS) && 9361 lop->op_type == OP_PUSHMARK && 9362 (vop = OpSIBLING(lop)) && 9363 (vop->op_type == OP_PADAV || vop->op_type == OP_PADHV) && 9364 !(vop->op_flags & OPf_PARENS) && 9365 (vop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == 9366 (OPpLVAL_INTRO|OPpPAD_STATE) && 9367 (eop = OpSIBLING(vop)) && 9368 eop->op_type == OP_ENTERSUB && 9369 !OpHAS_SIBLING(eop)) { 9370 state_var_op = vop; 9371 } else { 9372 while (lop) { 9373 if ((lop->op_type == OP_PADSV || 9374 lop->op_type == OP_PADAV || 9375 lop->op_type == OP_PADHV || 9376 lop->op_type == OP_PADANY) 9377 && (lop->op_private & OPpPAD_STATE) 9378 ) 9379 yyerror(no_list_state); 9380 lop = OpSIBLING(lop); 9381 } 9382 } 9383 } 9384 else if ( (left->op_private & OPpLVAL_INTRO) 9385 && (left->op_private & OPpPAD_STATE) 9386 && ( left->op_type == OP_PADSV 9387 || left->op_type == OP_PADAV 9388 || left->op_type == OP_PADHV 9389 || left->op_type == OP_PADANY) 9390 ) { 9391 /* All single variable list context state assignments, hence 9392 state ($a) = ... 9393 (state $a) = ... 9394 state @a = ... 9395 state (@a) = ... 9396 (state @a) = ... 9397 state %a = ... 9398 state (%a) = ... 9399 (state %a) = ... 9400 */ 9401 if (left->op_flags & OPf_PARENS) 9402 yyerror(no_list_state); 9403 else 9404 state_var_op = left; 9405 } 9406 9407 /* optimise @a = split(...) into: 9408 * @{expr}: split(..., @{expr}) (where @a is not flattened) 9409 * @a, my @a, local @a: split(...) (where @a is attached to 9410 * the split op itself) 9411 */ 9412 9413 if ( right 9414 && right->op_type == OP_SPLIT 9415 /* don't do twice, e.g. @b = (@a = split) */ 9416 && !(right->op_private & OPpSPLIT_ASSIGN)) 9417 { 9418 OP *gvop = NULL; 9419 9420 if ( ( left->op_type == OP_RV2AV 9421 && (gvop=((UNOP*)left)->op_first)->op_type==OP_GV) 9422 || left->op_type == OP_PADAV) 9423 { 9424 /* @pkg or @lex or local @pkg' or 'my @lex' */ 9425 OP *tmpop; 9426 if (gvop) { 9427 #ifdef USE_ITHREADS 9428 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff 9429 = cPADOPx(gvop)->op_padix; 9430 cPADOPx(gvop)->op_padix = 0; /* steal it */ 9431 #else 9432 ((PMOP*)right)->op_pmreplrootu.op_pmtargetgv 9433 = MUTABLE_GV(cSVOPx(gvop)->op_sv); 9434 cSVOPx(gvop)->op_sv = NULL; /* steal it */ 9435 #endif 9436 right->op_private |= 9437 left->op_private & OPpOUR_INTRO; 9438 } 9439 else { 9440 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff = left->op_targ; 9441 left->op_targ = 0; /* steal it */ 9442 right->op_private |= OPpSPLIT_LEX; 9443 } 9444 right->op_private |= left->op_private & OPpLVAL_INTRO; 9445 9446 detach_split: 9447 tmpop = cUNOPo->op_first; /* to list (nulled) */ 9448 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ 9449 assert(OpSIBLING(tmpop) == right); 9450 assert(!OpHAS_SIBLING(right)); 9451 /* detach the split subtreee from the o tree, 9452 * then free the residual o tree */ 9453 op_sibling_splice(cUNOPo->op_first, tmpop, 1, NULL); 9454 op_free(o); /* blow off assign */ 9455 right->op_private |= OPpSPLIT_ASSIGN; 9456 right->op_flags &= ~OPf_WANT; 9457 /* "I don't know and I don't care." */ 9458 return right; 9459 } 9460 else if (left->op_type == OP_RV2AV) { 9461 /* @{expr} */ 9462 9463 OP *pushop = cUNOPx(cBINOPo->op_last)->op_first; 9464 assert(OpSIBLING(pushop) == left); 9465 /* Detach the array ... */ 9466 op_sibling_splice(cBINOPo->op_last, pushop, 1, NULL); 9467 /* ... and attach it to the split. */ 9468 op_sibling_splice(right, cLISTOPx(right)->op_last, 9469 0, left); 9470 right->op_flags |= OPf_STACKED; 9471 /* Detach split and expunge aassign as above. */ 9472 goto detach_split; 9473 } 9474 else if (PL_modcount < RETURN_UNLIMITED_NUMBER && 9475 ((LISTOP*)right)->op_last->op_type == OP_CONST) 9476 { 9477 /* convert split(...,0) to split(..., PL_modcount+1) */ 9478 SV ** const svp = 9479 &((SVOP*)((LISTOP*)right)->op_last)->op_sv; 9480 SV * const sv = *svp; 9481 if (SvIOK(sv) && SvIVX(sv) == 0) 9482 { 9483 if (right->op_private & OPpSPLIT_IMPLIM) { 9484 /* our own SV, created in ck_split */ 9485 SvREADONLY_off(sv); 9486 sv_setiv(sv, PL_modcount+1); 9487 } 9488 else { 9489 /* SV may belong to someone else */ 9490 SvREFCNT_dec(sv); 9491 *svp = newSViv(PL_modcount+1); 9492 } 9493 } 9494 } 9495 } 9496 9497 if (state_var_op) 9498 o = S_newONCEOP(aTHX_ o, state_var_op); 9499 return o; 9500 } 9501 if (assign_type == ASSIGN_REF) 9502 return newBINOP(OP_REFASSIGN, flags, scalar(right), left); 9503 if (!right) 9504 right = newOP(OP_UNDEF, 0); 9505 if (right->op_type == OP_READLINE) { 9506 right->op_flags |= OPf_STACKED; 9507 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN), 9508 scalar(right)); 9509 } 9510 else { 9511 o = newBINOP(OP_SASSIGN, flags, 9512 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) ); 9513 } 9514 return o; 9515 } 9516 9517 /* 9518 =for apidoc newSTATEOP 9519 9520 Constructs a state op (COP). The state op is normally a C<nextstate> op, 9521 but will be a C<dbstate> op if debugging is enabled for currently-compiled 9522 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>). 9523 If C<label> is non-null, it supplies the name of a label to attach to 9524 the state op; this function takes ownership of the memory pointed at by 9525 C<label>, and will free it. C<flags> gives the eight bits of C<op_flags> 9526 for the state op. 9527 9528 If C<o> is null, the state op is returned. Otherwise the state op is 9529 combined with C<o> into a C<lineseq> list op, which is returned. C<o> 9530 is consumed by this function and becomes part of the returned op tree. 9531 9532 =cut 9533 */ 9534 9535 OP * 9536 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o) 9537 { 9538 const U32 seq = intro_my(); 9539 const U32 utf8 = flags & SVf_UTF8; 9540 COP *cop; 9541 9542 assert(PL_parser); 9543 PL_parser->parsed_sub = 0; 9544 9545 flags &= ~SVf_UTF8; 9546 9547 NewOp(1101, cop, 1, COP); 9548 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) { 9549 OpTYPE_set(cop, OP_DBSTATE); 9550 } 9551 else { 9552 OpTYPE_set(cop, OP_NEXTSTATE); 9553 } 9554 cop->op_flags = (U8)flags; 9555 CopHINTS_set(cop, PL_hints); 9556 #ifdef VMS 9557 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH; 9558 #endif 9559 cop->op_next = (OP*)cop; 9560 9561 cop->cop_seq = seq; 9562 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); 9563 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop))); 9564 if (label) { 9565 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8); 9566 9567 PL_hints |= HINT_BLOCK_SCOPE; 9568 /* It seems that we need to defer freeing this pointer, as other parts 9569 of the grammar end up wanting to copy it after this op has been 9570 created. */ 9571 SAVEFREEPV(label); 9572 } 9573 9574 if (PL_parser->preambling != NOLINE) { 9575 CopLINE_set(cop, PL_parser->preambling); 9576 PL_parser->copline = NOLINE; 9577 } 9578 else if (PL_parser->copline == NOLINE) 9579 CopLINE_set(cop, CopLINE(PL_curcop)); 9580 else { 9581 CopLINE_set(cop, PL_parser->copline); 9582 PL_parser->copline = NOLINE; 9583 } 9584 #ifdef USE_ITHREADS 9585 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */ 9586 #else 9587 CopFILEGV_set(cop, CopFILEGV(PL_curcop)); 9588 #endif 9589 CopSTASH_set(cop, PL_curstash); 9590 9591 if (cop->op_type == OP_DBSTATE) { 9592 /* this line can have a breakpoint - store the cop in IV */ 9593 AV *av = CopFILEAVx(PL_curcop); 9594 if (av) { 9595 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE); 9596 if (svp && *svp != &PL_sv_undef ) { 9597 (void)SvIOK_on(*svp); 9598 SvIV_set(*svp, PTR2IV(cop)); 9599 } 9600 } 9601 } 9602 9603 if (flags & OPf_SPECIAL) 9604 op_null((OP*)cop); 9605 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o); 9606 } 9607 9608 /* 9609 =for apidoc newLOGOP 9610 9611 Constructs, checks, and returns a logical (flow control) op. C<type> 9612 is the opcode. C<flags> gives the eight bits of C<op_flags>, except 9613 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits, 9614 the eight bits of C<op_private>, except that the bit with value 1 is 9615 automatically set. C<first> supplies the expression controlling the 9616 flow, and C<other> supplies the side (alternate) chain of ops; they are 9617 consumed by this function and become part of the constructed op tree. 9618 9619 =cut 9620 */ 9621 9622 OP * 9623 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other) 9624 { 9625 PERL_ARGS_ASSERT_NEWLOGOP; 9626 9627 return new_logop(type, flags, &first, &other); 9628 } 9629 9630 9631 /* See if the optree o contains a single OP_CONST (plus possibly 9632 * surrounding enter/nextstate/null etc). If so, return it, else return 9633 * NULL. 9634 */ 9635 9636 STATIC OP * 9637 S_search_const(pTHX_ OP *o) 9638 { 9639 PERL_ARGS_ASSERT_SEARCH_CONST; 9640 9641 redo: 9642 switch (o->op_type) { 9643 case OP_CONST: 9644 return o; 9645 case OP_NULL: 9646 if (o->op_flags & OPf_KIDS) { 9647 o = cUNOPo->op_first; 9648 goto redo; 9649 } 9650 break; 9651 case OP_LEAVE: 9652 case OP_SCOPE: 9653 case OP_LINESEQ: 9654 { 9655 OP *kid; 9656 if (!(o->op_flags & OPf_KIDS)) 9657 return NULL; 9658 kid = cLISTOPo->op_first; 9659 9660 do { 9661 switch (kid->op_type) { 9662 case OP_ENTER: 9663 case OP_NULL: 9664 case OP_NEXTSTATE: 9665 kid = OpSIBLING(kid); 9666 break; 9667 default: 9668 if (kid != cLISTOPo->op_last) 9669 return NULL; 9670 goto last; 9671 } 9672 } while (kid); 9673 9674 if (!kid) 9675 kid = cLISTOPo->op_last; 9676 last: 9677 o = kid; 9678 goto redo; 9679 } 9680 } 9681 9682 return NULL; 9683 } 9684 9685 9686 STATIC OP * 9687 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp) 9688 { 9689 LOGOP *logop; 9690 OP *o; 9691 OP *first; 9692 OP *other; 9693 OP *cstop = NULL; 9694 int prepend_not = 0; 9695 9696 PERL_ARGS_ASSERT_NEW_LOGOP; 9697 9698 first = *firstp; 9699 other = *otherp; 9700 9701 /* [perl #59802]: Warn about things like "return $a or $b", which 9702 is parsed as "(return $a) or $b" rather than "return ($a or 9703 $b)". NB: This also applies to xor, which is why we do it 9704 here. 9705 */ 9706 switch (first->op_type) { 9707 case OP_NEXT: 9708 case OP_LAST: 9709 case OP_REDO: 9710 /* XXX: Perhaps we should emit a stronger warning for these. 9711 Even with the high-precedence operator they don't seem to do 9712 anything sensible. 9713 9714 But until we do, fall through here. 9715 */ 9716 case OP_RETURN: 9717 case OP_EXIT: 9718 case OP_DIE: 9719 case OP_GOTO: 9720 /* XXX: Currently we allow people to "shoot themselves in the 9721 foot" by explicitly writing "(return $a) or $b". 9722 9723 Warn unless we are looking at the result from folding or if 9724 the programmer explicitly grouped the operators like this. 9725 The former can occur with e.g. 9726 9727 use constant FEATURE => ( $] >= ... ); 9728 sub { not FEATURE and return or do_stuff(); } 9729 */ 9730 if (!first->op_folded && !(first->op_flags & OPf_PARENS)) 9731 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), 9732 "Possible precedence issue with control flow operator"); 9733 /* XXX: Should we optimze this to "return $a;" (i.e. remove 9734 the "or $b" part)? 9735 */ 9736 break; 9737 } 9738 9739 if (type == OP_XOR) /* Not short circuit, but here by precedence. */ 9740 return newBINOP(type, flags, scalar(first), scalar(other)); 9741 9742 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP 9743 || type == OP_CUSTOM); 9744 9745 scalarboolean(first); 9746 9747 /* search for a constant op that could let us fold the test */ 9748 if ((cstop = search_const(first))) { 9749 if (cstop->op_private & OPpCONST_STRICT) 9750 no_bareword_allowed(cstop); 9751 else if ((cstop->op_private & OPpCONST_BARE)) 9752 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional"); 9753 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) || 9754 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) || 9755 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) { 9756 /* Elide the (constant) lhs, since it can't affect the outcome */ 9757 *firstp = NULL; 9758 if (other->op_type == OP_CONST) 9759 other->op_private |= OPpCONST_SHORTCIRCUIT; 9760 op_free(first); 9761 if (other->op_type == OP_LEAVE) 9762 other = newUNOP(OP_NULL, OPf_SPECIAL, other); 9763 else if (other->op_type == OP_MATCH 9764 || other->op_type == OP_SUBST 9765 || other->op_type == OP_TRANSR 9766 || other->op_type == OP_TRANS) 9767 /* Mark the op as being unbindable with =~ */ 9768 other->op_flags |= OPf_SPECIAL; 9769 9770 other->op_folded = 1; 9771 return other; 9772 } 9773 else { 9774 /* Elide the rhs, since the outcome is entirely determined by 9775 * the (constant) lhs */ 9776 9777 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */ 9778 const OP *o2 = other; 9779 if ( ! (o2->op_type == OP_LIST 9780 && (( o2 = cUNOPx(o2)->op_first)) 9781 && o2->op_type == OP_PUSHMARK 9782 && (( o2 = OpSIBLING(o2))) ) 9783 ) 9784 o2 = other; 9785 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV 9786 || o2->op_type == OP_PADHV) 9787 && o2->op_private & OPpLVAL_INTRO 9788 && !(o2->op_private & OPpPAD_STATE)) 9789 { 9790 Perl_croak(aTHX_ "This use of my() in false conditional is " 9791 "no longer allowed"); 9792 } 9793 9794 *otherp = NULL; 9795 if (cstop->op_type == OP_CONST) 9796 cstop->op_private |= OPpCONST_SHORTCIRCUIT; 9797 op_free(other); 9798 return first; 9799 } 9800 } 9801 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR 9802 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */ 9803 { 9804 const OP * const k1 = ((UNOP*)first)->op_first; 9805 const OP * const k2 = OpSIBLING(k1); 9806 OPCODE warnop = 0; 9807 switch (first->op_type) 9808 { 9809 case OP_NULL: 9810 if (k2 && k2->op_type == OP_READLINE 9811 && (k2->op_flags & OPf_STACKED) 9812 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 9813 { 9814 warnop = k2->op_type; 9815 } 9816 break; 9817 9818 case OP_SASSIGN: 9819 if (k1->op_type == OP_READDIR 9820 || k1->op_type == OP_GLOB 9821 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) 9822 || k1->op_type == OP_EACH 9823 || k1->op_type == OP_AEACH) 9824 { 9825 warnop = ((k1->op_type == OP_NULL) 9826 ? (OPCODE)k1->op_targ : k1->op_type); 9827 } 9828 break; 9829 } 9830 if (warnop) { 9831 const line_t oldline = CopLINE(PL_curcop); 9832 /* This ensures that warnings are reported at the first line 9833 of the construction, not the last. */ 9834 CopLINE_set(PL_curcop, PL_parser->copline); 9835 Perl_warner(aTHX_ packWARN(WARN_MISC), 9836 "Value of %s%s can be \"0\"; test with defined()", 9837 PL_op_desc[warnop], 9838 ((warnop == OP_READLINE || warnop == OP_GLOB) 9839 ? " construct" : "() operator")); 9840 CopLINE_set(PL_curcop, oldline); 9841 } 9842 } 9843 9844 /* optimize AND and OR ops that have NOTs as children */ 9845 if (first->op_type == OP_NOT 9846 && (first->op_flags & OPf_KIDS) 9847 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */ 9848 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */ 9849 ) { 9850 if (type == OP_AND || type == OP_OR) { 9851 if (type == OP_AND) 9852 type = OP_OR; 9853 else 9854 type = OP_AND; 9855 op_null(first); 9856 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */ 9857 op_null(other); 9858 prepend_not = 1; /* prepend a NOT op later */ 9859 } 9860 } 9861 } 9862 9863 logop = alloc_LOGOP(type, first, LINKLIST(other)); 9864 logop->op_flags |= (U8)flags; 9865 logop->op_private = (U8)(1 | (flags >> 8)); 9866 9867 /* establish postfix order */ 9868 logop->op_next = LINKLIST(first); 9869 first->op_next = (OP*)logop; 9870 assert(!OpHAS_SIBLING(first)); 9871 op_sibling_splice((OP*)logop, first, 0, other); 9872 9873 CHECKOP(type,logop); 9874 9875 o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 9876 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0, 9877 (OP*)logop); 9878 other->op_next = o; 9879 9880 return o; 9881 } 9882 9883 /* 9884 =for apidoc newCONDOP 9885 9886 Constructs, checks, and returns a conditional-expression (C<cond_expr>) 9887 op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS> 9888 will be set automatically, and, shifted up eight bits, the eight bits of 9889 C<op_private>, except that the bit with value 1 is automatically set. 9890 C<first> supplies the expression selecting between the two branches, 9891 and C<trueop> and C<falseop> supply the branches; they are consumed by 9892 this function and become part of the constructed op tree. 9893 9894 =cut 9895 */ 9896 9897 OP * 9898 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop) 9899 { 9900 LOGOP *logop; 9901 OP *start; 9902 OP *o; 9903 OP *cstop; 9904 9905 PERL_ARGS_ASSERT_NEWCONDOP; 9906 9907 if (!falseop) 9908 return newLOGOP(OP_AND, 0, first, trueop); 9909 if (!trueop) 9910 return newLOGOP(OP_OR, 0, first, falseop); 9911 9912 scalarboolean(first); 9913 if ((cstop = search_const(first))) { 9914 /* Left or right arm of the conditional? */ 9915 const bool left = SvTRUE(((SVOP*)cstop)->op_sv); 9916 OP *live = left ? trueop : falseop; 9917 OP *const dead = left ? falseop : trueop; 9918 if (cstop->op_private & OPpCONST_BARE && 9919 cstop->op_private & OPpCONST_STRICT) { 9920 no_bareword_allowed(cstop); 9921 } 9922 op_free(first); 9923 op_free(dead); 9924 if (live->op_type == OP_LEAVE) 9925 live = newUNOP(OP_NULL, OPf_SPECIAL, live); 9926 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST 9927 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR) 9928 /* Mark the op as being unbindable with =~ */ 9929 live->op_flags |= OPf_SPECIAL; 9930 live->op_folded = 1; 9931 return live; 9932 } 9933 logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop)); 9934 logop->op_flags |= (U8)flags; 9935 logop->op_private = (U8)(1 | (flags >> 8)); 9936 logop->op_next = LINKLIST(falseop); 9937 9938 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */ 9939 logop); 9940 9941 /* establish postfix order */ 9942 start = LINKLIST(first); 9943 first->op_next = (OP*)logop; 9944 9945 /* make first, trueop, falseop siblings */ 9946 op_sibling_splice((OP*)logop, first, 0, trueop); 9947 op_sibling_splice((OP*)logop, trueop, 0, falseop); 9948 9949 o = newUNOP(OP_NULL, 0, (OP*)logop); 9950 9951 trueop->op_next = falseop->op_next = o; 9952 9953 o->op_next = start; 9954 return o; 9955 } 9956 9957 /* 9958 =for apidoc newTRYCATCHOP 9959 9960 Constructs and returns a conditional execution statement that implements 9961 the C<try>/C<catch> semantics. First the op tree in C<tryblock> is executed, 9962 inside a context that traps exceptions. If an exception occurs then the 9963 optree in C<catchblock> is executed, with the trapped exception set into the 9964 lexical variable given by C<catchvar> (which must be an op of type 9965 C<OP_PADSV>). All the optrees are consumed by this function and become part 9966 of the returned op tree. 9967 9968 The C<flags> argument is currently ignored. 9969 9970 =cut 9971 */ 9972 9973 OP * 9974 Perl_newTRYCATCHOP(pTHX_ I32 flags, OP *tryblock, OP *catchvar, OP *catchblock) 9975 { 9976 OP *o, *catchop; 9977 9978 PERL_ARGS_ASSERT_NEWTRYCATCHOP; 9979 assert(catchvar->op_type == OP_PADSV); 9980 9981 PERL_UNUSED_ARG(flags); 9982 9983 /* The returned optree is shaped as: 9984 * LISTOP leavetrycatch 9985 * LOGOP entertrycatch 9986 * LISTOP poptry 9987 * $tryblock here 9988 * LOGOP catch 9989 * $catchblock here 9990 */ 9991 9992 if(tryblock->op_type != OP_LINESEQ) 9993 tryblock = op_convert_list(OP_LINESEQ, 0, tryblock); 9994 OpTYPE_set(tryblock, OP_POPTRY); 9995 9996 /* Manually construct a naked LOGOP. 9997 * Normally if we call newLOGOP the returned value is a UNOP(OP_NULL) 9998 * containing the LOGOP we wanted as its op_first */ 9999 catchop = (OP *)alloc_LOGOP(OP_CATCH, newOP(OP_NULL, 0), catchblock); 10000 OpMORESIB_set(cUNOPx(catchop)->op_first, catchblock); 10001 OpLASTSIB_set(catchblock, catchop); 10002 10003 /* Inject the catchvar's pad offset into the OP_CATCH targ */ 10004 cLOGOPx(catchop)->op_targ = catchvar->op_targ; 10005 op_free(catchvar); 10006 10007 /* Build the optree structure */ 10008 o = newLISTOP(OP_LIST, 0, tryblock, catchop); 10009 o = op_convert_list(OP_ENTERTRYCATCH, 0, o); 10010 10011 return o; 10012 } 10013 10014 /* 10015 =for apidoc newRANGE 10016 10017 Constructs and returns a C<range> op, with subordinate C<flip> and 10018 C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the 10019 C<flip> op and, shifted up eight bits, the eight bits of C<op_private> 10020 for both the C<flip> and C<range> ops, except that the bit with value 10021 1 is automatically set. C<left> and C<right> supply the expressions 10022 controlling the endpoints of the range; they are consumed by this function 10023 and become part of the constructed op tree. 10024 10025 =cut 10026 */ 10027 10028 OP * 10029 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right) 10030 { 10031 LOGOP *range; 10032 OP *flip; 10033 OP *flop; 10034 OP *leftstart; 10035 OP *o; 10036 10037 PERL_ARGS_ASSERT_NEWRANGE; 10038 10039 range = alloc_LOGOP(OP_RANGE, left, LINKLIST(right)); 10040 range->op_flags = OPf_KIDS; 10041 leftstart = LINKLIST(left); 10042 range->op_private = (U8)(1 | (flags >> 8)); 10043 10044 /* make left and right siblings */ 10045 op_sibling_splice((OP*)range, left, 0, right); 10046 10047 range->op_next = (OP*)range; 10048 flip = newUNOP(OP_FLIP, flags, (OP*)range); 10049 flop = newUNOP(OP_FLOP, 0, flip); 10050 o = newUNOP(OP_NULL, 0, flop); 10051 LINKLIST(flop); 10052 range->op_next = leftstart; 10053 10054 left->op_next = flip; 10055 right->op_next = flop; 10056 10057 range->op_targ = 10058 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0); 10059 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV); 10060 flip->op_targ = 10061 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);; 10062 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); 10063 SvPADTMP_on(PAD_SV(flip->op_targ)); 10064 10065 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; 10066 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; 10067 10068 /* check barewords before they might be optimized aways */ 10069 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT) 10070 no_bareword_allowed(left); 10071 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT) 10072 no_bareword_allowed(right); 10073 10074 flip->op_next = o; 10075 if (!flip->op_private || !flop->op_private) 10076 LINKLIST(o); /* blow off optimizer unless constant */ 10077 10078 return o; 10079 } 10080 10081 /* 10082 =for apidoc newLOOPOP 10083 10084 Constructs, checks, and returns an op tree expressing a loop. This is 10085 only a loop in the control flow through the op tree; it does not have 10086 the heavyweight loop structure that allows exiting the loop by C<last> 10087 and suchlike. C<flags> gives the eight bits of C<op_flags> for the 10088 top-level op, except that some bits will be set automatically as required. 10089 C<expr> supplies the expression controlling loop iteration, and C<block> 10090 supplies the body of the loop; they are consumed by this function and 10091 become part of the constructed op tree. C<debuggable> is currently 10092 unused and should always be 1. 10093 10094 =cut 10095 */ 10096 10097 OP * 10098 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block) 10099 { 10100 OP* listop; 10101 OP* o; 10102 const bool once = block && block->op_flags & OPf_SPECIAL && 10103 block->op_type == OP_NULL; 10104 10105 PERL_UNUSED_ARG(debuggable); 10106 10107 if (expr) { 10108 if (once && ( 10109 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) 10110 || ( expr->op_type == OP_NOT 10111 && cUNOPx(expr)->op_first->op_type == OP_CONST 10112 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first)) 10113 ) 10114 )) 10115 /* Return the block now, so that S_new_logop does not try to 10116 fold it away. */ 10117 { 10118 op_free(expr); 10119 return block; /* do {} while 0 does once */ 10120 } 10121 10122 if (expr->op_type == OP_READLINE 10123 || expr->op_type == OP_READDIR 10124 || expr->op_type == OP_GLOB 10125 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH 10126 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { 10127 expr = newUNOP(OP_DEFINED, 0, 10128 newASSIGNOP(0, newDEFSVOP(), 0, expr) ); 10129 } else if (expr->op_flags & OPf_KIDS) { 10130 const OP * const k1 = ((UNOP*)expr)->op_first; 10131 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL; 10132 switch (expr->op_type) { 10133 case OP_NULL: 10134 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR) 10135 && (k2->op_flags & OPf_STACKED) 10136 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 10137 expr = newUNOP(OP_DEFINED, 0, expr); 10138 break; 10139 10140 case OP_SASSIGN: 10141 if (k1 && (k1->op_type == OP_READDIR 10142 || k1->op_type == OP_GLOB 10143 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) 10144 || k1->op_type == OP_EACH 10145 || k1->op_type == OP_AEACH)) 10146 expr = newUNOP(OP_DEFINED, 0, expr); 10147 break; 10148 } 10149 } 10150 } 10151 10152 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar 10153 * op, in listop. This is wrong. [perl #27024] */ 10154 if (!block) 10155 block = newOP(OP_NULL, 0); 10156 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); 10157 o = new_logop(OP_AND, 0, &expr, &listop); 10158 10159 if (once) { 10160 ASSUME(listop); 10161 } 10162 10163 if (listop) 10164 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); 10165 10166 if (once && o != listop) 10167 { 10168 assert(cUNOPo->op_first->op_type == OP_AND 10169 || cUNOPo->op_first->op_type == OP_OR); 10170 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; 10171 } 10172 10173 if (o == listop) 10174 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ 10175 10176 o->op_flags |= flags; 10177 o = op_scope(o); 10178 o->op_flags |= OPf_SPECIAL; /* suppress cx_popblock() curpm restoration*/ 10179 return o; 10180 } 10181 10182 /* 10183 =for apidoc newWHILEOP 10184 10185 Constructs, checks, and returns an op tree expressing a C<while> loop. 10186 This is a heavyweight loop, with structure that allows exiting the loop 10187 by C<last> and suchlike. 10188 10189 C<loop> is an optional preconstructed C<enterloop> op to use in the 10190 loop; if it is null then a suitable op will be constructed automatically. 10191 C<expr> supplies the loop's controlling expression. C<block> supplies the 10192 main body of the loop, and C<cont> optionally supplies a C<continue> block 10193 that operates as a second half of the body. All of these optree inputs 10194 are consumed by this function and become part of the constructed op tree. 10195 10196 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop> 10197 op and, shifted up eight bits, the eight bits of C<op_private> for 10198 the C<leaveloop> op, except that (in both cases) some bits will be set 10199 automatically. C<debuggable> is currently unused and should always be 1. 10200 C<has_my> can be supplied as true to force the 10201 loop body to be enclosed in its own scope. 10202 10203 =cut 10204 */ 10205 10206 OP * 10207 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, 10208 OP *expr, OP *block, OP *cont, I32 has_my) 10209 { 10210 OP *redo; 10211 OP *next = NULL; 10212 OP *listop; 10213 OP *o; 10214 U8 loopflags = 0; 10215 10216 PERL_UNUSED_ARG(debuggable); 10217 10218 if (expr) { 10219 if (expr->op_type == OP_READLINE 10220 || expr->op_type == OP_READDIR 10221 || expr->op_type == OP_GLOB 10222 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH 10223 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { 10224 expr = newUNOP(OP_DEFINED, 0, 10225 newASSIGNOP(0, newDEFSVOP(), 0, expr) ); 10226 } else if (expr->op_flags & OPf_KIDS) { 10227 const OP * const k1 = ((UNOP*)expr)->op_first; 10228 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL; 10229 switch (expr->op_type) { 10230 case OP_NULL: 10231 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR) 10232 && (k2->op_flags & OPf_STACKED) 10233 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 10234 expr = newUNOP(OP_DEFINED, 0, expr); 10235 break; 10236 10237 case OP_SASSIGN: 10238 if (k1 && (k1->op_type == OP_READDIR 10239 || k1->op_type == OP_GLOB 10240 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) 10241 || k1->op_type == OP_EACH 10242 || k1->op_type == OP_AEACH)) 10243 expr = newUNOP(OP_DEFINED, 0, expr); 10244 break; 10245 } 10246 } 10247 } 10248 10249 if (!block) 10250 block = newOP(OP_NULL, 0); 10251 else if (cont || has_my) { 10252 block = op_scope(block); 10253 } 10254 10255 if (cont) { 10256 next = LINKLIST(cont); 10257 } 10258 if (expr) { 10259 OP * const unstack = newOP(OP_UNSTACK, 0); 10260 if (!next) 10261 next = unstack; 10262 cont = op_append_elem(OP_LINESEQ, cont, unstack); 10263 } 10264 10265 assert(block); 10266 listop = op_append_list(OP_LINESEQ, block, cont); 10267 assert(listop); 10268 redo = LINKLIST(listop); 10269 10270 if (expr) { 10271 scalar(listop); 10272 o = new_logop(OP_AND, 0, &expr, &listop); 10273 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { 10274 op_free((OP*)loop); 10275 return expr; /* listop already freed by new_logop */ 10276 } 10277 if (listop) 10278 ((LISTOP*)listop)->op_last->op_next = 10279 (o == listop ? redo : LINKLIST(o)); 10280 } 10281 else 10282 o = listop; 10283 10284 if (!loop) { 10285 NewOp(1101,loop,1,LOOP); 10286 OpTYPE_set(loop, OP_ENTERLOOP); 10287 loop->op_private = 0; 10288 loop->op_next = (OP*)loop; 10289 } 10290 10291 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); 10292 10293 loop->op_redoop = redo; 10294 loop->op_lastop = o; 10295 o->op_private |= loopflags; 10296 10297 if (next) 10298 loop->op_nextop = next; 10299 else 10300 loop->op_nextop = o; 10301 10302 o->op_flags |= flags; 10303 o->op_private |= (flags >> 8); 10304 return o; 10305 } 10306 10307 /* 10308 =for apidoc newFOROP 10309 10310 Constructs, checks, and returns an op tree expressing a C<foreach> 10311 loop (iteration through a list of values). This is a heavyweight loop, 10312 with structure that allows exiting the loop by C<last> and suchlike. 10313 10314 C<sv> optionally supplies the variable(s) that will be aliased to each 10315 item in turn; if null, it defaults to C<$_>. 10316 C<expr> supplies the list of values to iterate over. C<block> supplies 10317 the main body of the loop, and C<cont> optionally supplies a C<continue> 10318 block that operates as a second half of the body. All of these optree 10319 inputs are consumed by this function and become part of the constructed 10320 op tree. 10321 10322 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop> 10323 op and, shifted up eight bits, the eight bits of C<op_private> for 10324 the C<leaveloop> op, except that (in both cases) some bits will be set 10325 automatically. 10326 10327 =cut 10328 */ 10329 10330 OP * 10331 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont) 10332 { 10333 LOOP *loop; 10334 OP *iter; 10335 PADOFFSET padoff = 0; 10336 PADOFFSET how_many_more = 0; 10337 I32 iterflags = 0; 10338 I32 iterpflags = 0; 10339 bool parens = 0; 10340 10341 PERL_ARGS_ASSERT_NEWFOROP; 10342 10343 if (sv) { 10344 if (sv->op_type == OP_RV2SV) { /* symbol table variable */ 10345 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */ 10346 OpTYPE_set(sv, OP_RV2GV); 10347 10348 /* The op_type check is needed to prevent a possible segfault 10349 * if the loop variable is undeclared and 'strict vars' is in 10350 * effect. This is illegal but is nonetheless parsed, so we 10351 * may reach this point with an OP_CONST where we're expecting 10352 * an OP_GV. 10353 */ 10354 if (cUNOPx(sv)->op_first->op_type == OP_GV 10355 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv) 10356 iterpflags |= OPpITER_DEF; 10357 } 10358 else if (sv->op_type == OP_PADSV) { /* private variable */ 10359 if (sv->op_flags & OPf_PARENS) { 10360 /* handle degenerate 1-var form of "for my ($x, ...)" */ 10361 sv->op_private |= OPpLVAL_INTRO; 10362 parens = 1; 10363 } 10364 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */ 10365 padoff = sv->op_targ; 10366 sv->op_targ = 0; 10367 op_free(sv); 10368 sv = NULL; 10369 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX); 10370 } 10371 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN) 10372 NOOP; 10373 else if (sv->op_type == OP_LIST) { 10374 LISTOP *list = (LISTOP *) sv; 10375 OP *pushmark = list->op_first; 10376 OP *first_padsv; 10377 UNOP *padsv; 10378 PADOFFSET i; 10379 10380 iterpflags = OPpLVAL_INTRO; /* for my ($k, $v) () */ 10381 parens = 1; 10382 10383 if (!pushmark || pushmark->op_type != OP_PUSHMARK) { 10384 Perl_croak(aTHX_ "panic: newFORLOOP, found %s, expecting pushmark", 10385 pushmark ? PL_op_desc[pushmark->op_type] : "NULL"); 10386 } 10387 first_padsv = OpSIBLING(pushmark); 10388 if (!first_padsv || first_padsv->op_type != OP_PADSV) { 10389 Perl_croak(aTHX_ "panic: newFORLOOP, found %s, expecting padsv", 10390 first_padsv ? PL_op_desc[first_padsv->op_type] : "NULL"); 10391 } 10392 padoff = first_padsv->op_targ; 10393 10394 /* There should be at least one more PADSV to find, and the ops 10395 should have consecutive values in targ: */ 10396 padsv = (UNOP *) OpSIBLING(first_padsv); 10397 do { 10398 if (!padsv || padsv->op_type != OP_PADSV) { 10399 Perl_croak(aTHX_ "panic: newFORLOOP, found %s at %zd, expecting padsv", 10400 padsv ? PL_op_desc[padsv->op_type] : "NULL", 10401 how_many_more); 10402 } 10403 ++how_many_more; 10404 if (padsv->op_targ != padoff + how_many_more) { 10405 Perl_croak(aTHX_ "panic: newFORLOOP, padsv at %zd targ is %zd, not %zd", 10406 how_many_more, padsv->op_targ, padoff + how_many_more); 10407 } 10408 10409 padsv = (UNOP *) OpSIBLING(padsv); 10410 } while (padsv); 10411 10412 /* OK, this optree has the shape that we expected. So now *we* 10413 "claim" the Pad slots: */ 10414 first_padsv->op_targ = 0; 10415 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX); 10416 10417 i = padoff; 10418 10419 padsv = (UNOP *) OpSIBLING(first_padsv); 10420 do { 10421 ++i; 10422 padsv->op_targ = 0; 10423 PAD_COMPNAME_GEN_set(i, PERL_INT_MAX); 10424 10425 padsv = (UNOP *) OpSIBLING(padsv); 10426 } while (padsv); 10427 10428 op_free(sv); 10429 sv = NULL; 10430 } 10431 else 10432 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]); 10433 if (padoff) { 10434 PADNAME * const pn = PAD_COMPNAME(padoff); 10435 const char * const name = PadnamePV(pn); 10436 10437 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_') 10438 iterpflags |= OPpITER_DEF; 10439 } 10440 } 10441 else { 10442 sv = newGVOP(OP_GV, 0, PL_defgv); 10443 iterpflags |= OPpITER_DEF; 10444 } 10445 10446 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { 10447 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), TRUE), OP_GREPSTART); 10448 iterflags |= OPf_STACKED; 10449 } 10450 else if (expr->op_type == OP_NULL && 10451 (expr->op_flags & OPf_KIDS) && 10452 ((BINOP*)expr)->op_first->op_type == OP_FLOP) 10453 { 10454 /* Basically turn for($x..$y) into the same as for($x,$y), but we 10455 * set the STACKED flag to indicate that these values are to be 10456 * treated as min/max values by 'pp_enteriter'. 10457 */ 10458 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first; 10459 LOGOP* const range = (LOGOP*) flip->op_first; 10460 OP* const left = range->op_first; 10461 OP* const right = OpSIBLING(left); 10462 LISTOP* listop; 10463 10464 range->op_flags &= ~OPf_KIDS; 10465 /* detach range's children */ 10466 op_sibling_splice((OP*)range, NULL, -1, NULL); 10467 10468 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right); 10469 listop->op_first->op_next = range->op_next; 10470 left->op_next = range->op_other; 10471 right->op_next = (OP*)listop; 10472 listop->op_next = listop->op_first; 10473 10474 op_free(expr); 10475 expr = (OP*)(listop); 10476 op_null(expr); 10477 iterflags |= OPf_STACKED; 10478 } 10479 else { 10480 expr = op_lvalue(force_list(expr, TRUE), OP_GREPSTART); 10481 } 10482 10483 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags, 10484 op_append_elem(OP_LIST, list(expr), 10485 scalar(sv))); 10486 assert(!loop->op_next); 10487 /* for my $x () sets OPpLVAL_INTRO; 10488 * for our $x () sets OPpOUR_INTRO */ 10489 loop->op_private = (U8)iterpflags; 10490 10491 /* upgrade loop from a LISTOP to a LOOPOP; 10492 * keep it in-place if there's space */ 10493 if (loop->op_slabbed 10494 && OpSLOT(loop)->opslot_size 10495 < SIZE_TO_PSIZE(sizeof(LOOP) + OPSLOT_HEADER)) 10496 { 10497 /* no space; allocate new op */ 10498 LOOP *tmp; 10499 NewOp(1234,tmp,1,LOOP); 10500 Copy(loop,tmp,1,LISTOP); 10501 assert(loop->op_last->op_sibparent == (OP*)loop); 10502 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */ 10503 S_op_destroy(aTHX_ (OP*)loop); 10504 loop = tmp; 10505 } 10506 else if (!loop->op_slabbed) 10507 { 10508 /* loop was malloc()ed */ 10509 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP)); 10510 OpLASTSIB_set(loop->op_last, (OP*)loop); 10511 } 10512 loop->op_targ = padoff; 10513 if (parens) 10514 /* hint to deparser that this: for my (...) ... */ 10515 loop->op_flags |= OPf_PARENS; 10516 iter = newOP(OP_ITER, 0); 10517 iter->op_targ = how_many_more; 10518 return newWHILEOP(flags, 1, loop, iter, block, cont, 0); 10519 } 10520 10521 /* 10522 =for apidoc newLOOPEX 10523 10524 Constructs, checks, and returns a loop-exiting op (such as C<goto> 10525 or C<last>). C<type> is the opcode. C<label> supplies the parameter 10526 determining the target of the op; it is consumed by this function and 10527 becomes part of the constructed op tree. 10528 10529 =cut 10530 */ 10531 10532 OP* 10533 Perl_newLOOPEX(pTHX_ I32 type, OP *label) 10534 { 10535 OP *o = NULL; 10536 10537 PERL_ARGS_ASSERT_NEWLOOPEX; 10538 10539 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP 10540 || type == OP_CUSTOM); 10541 10542 if (type != OP_GOTO) { 10543 /* "last()" means "last" */ 10544 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) { 10545 o = newOP(type, OPf_SPECIAL); 10546 } 10547 } 10548 else { 10549 /* Check whether it's going to be a goto &function */ 10550 if (label->op_type == OP_ENTERSUB 10551 && !(label->op_flags & OPf_STACKED)) 10552 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN)); 10553 } 10554 10555 /* Check for a constant argument */ 10556 if (label->op_type == OP_CONST) { 10557 SV * const sv = ((SVOP *)label)->op_sv; 10558 STRLEN l; 10559 const char *s = SvPV_const(sv,l); 10560 if (l == strlen(s)) { 10561 o = newPVOP(type, 10562 SvUTF8(((SVOP*)label)->op_sv), 10563 savesharedpv( 10564 SvPV_nolen_const(((SVOP*)label)->op_sv))); 10565 } 10566 } 10567 10568 /* If we have already created an op, we do not need the label. */ 10569 if (o) 10570 op_free(label); 10571 else o = newUNOP(type, OPf_STACKED, label); 10572 10573 PL_hints |= HINT_BLOCK_SCOPE; 10574 return o; 10575 } 10576 10577 /* if the condition is a literal array or hash 10578 (or @{ ... } etc), make a reference to it. 10579 */ 10580 STATIC OP * 10581 S_ref_array_or_hash(pTHX_ OP *cond) 10582 { 10583 if (cond 10584 && (cond->op_type == OP_RV2AV 10585 || cond->op_type == OP_PADAV 10586 || cond->op_type == OP_RV2HV 10587 || cond->op_type == OP_PADHV)) 10588 10589 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN)); 10590 10591 else if(cond 10592 && (cond->op_type == OP_ASLICE 10593 || cond->op_type == OP_KVASLICE 10594 || cond->op_type == OP_HSLICE 10595 || cond->op_type == OP_KVHSLICE)) { 10596 10597 /* anonlist now needs a list from this op, was previously used in 10598 * scalar context */ 10599 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF); 10600 cond->op_flags |= OPf_WANT_LIST; 10601 10602 return newANONLIST(op_lvalue(cond, OP_ANONLIST)); 10603 } 10604 10605 else 10606 return cond; 10607 } 10608 10609 /* These construct the optree fragments representing given() 10610 and when() blocks. 10611 10612 entergiven and enterwhen are LOGOPs; the op_other pointer 10613 points up to the associated leave op. We need this so we 10614 can put it in the context and make break/continue work. 10615 (Also, of course, pp_enterwhen will jump straight to 10616 op_other if the match fails.) 10617 */ 10618 10619 STATIC OP * 10620 S_newGIVWHENOP(pTHX_ OP *cond, OP *block, 10621 I32 enter_opcode, I32 leave_opcode, 10622 PADOFFSET entertarg) 10623 { 10624 LOGOP *enterop; 10625 OP *o; 10626 10627 PERL_ARGS_ASSERT_NEWGIVWHENOP; 10628 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */ 10629 10630 enterop = alloc_LOGOP(enter_opcode, block, NULL); 10631 enterop->op_targ = 0; 10632 enterop->op_private = 0; 10633 10634 o = newUNOP(leave_opcode, 0, (OP *) enterop); 10635 10636 if (cond) { 10637 /* prepend cond if we have one */ 10638 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond)); 10639 10640 o->op_next = LINKLIST(cond); 10641 cond->op_next = (OP *) enterop; 10642 } 10643 else { 10644 /* This is a default {} block */ 10645 enterop->op_flags |= OPf_SPECIAL; 10646 o ->op_flags |= OPf_SPECIAL; 10647 10648 o->op_next = (OP *) enterop; 10649 } 10650 10651 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since 10652 entergiven and enterwhen both 10653 use ck_null() */ 10654 10655 enterop->op_next = LINKLIST(block); 10656 block->op_next = enterop->op_other = o; 10657 10658 return o; 10659 } 10660 10661 10662 /* For the purposes of 'when(implied_smartmatch)' 10663 * versus 'when(boolean_expression)', 10664 * does this look like a boolean operation? For these purposes 10665 a boolean operation is: 10666 - a subroutine call [*] 10667 - a logical connective 10668 - a comparison operator 10669 - a filetest operator, with the exception of -s -M -A -C 10670 - defined(), exists() or eof() 10671 - /$re/ or $foo =~ /$re/ 10672 10673 [*] possibly surprising 10674 */ 10675 STATIC bool 10676 S_looks_like_bool(pTHX_ const OP *o) 10677 { 10678 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL; 10679 10680 switch(o->op_type) { 10681 case OP_OR: 10682 case OP_DOR: 10683 return looks_like_bool(cLOGOPo->op_first); 10684 10685 case OP_AND: 10686 { 10687 OP* sibl = OpSIBLING(cLOGOPo->op_first); 10688 ASSUME(sibl); 10689 return ( 10690 looks_like_bool(cLOGOPo->op_first) 10691 && looks_like_bool(sibl)); 10692 } 10693 10694 case OP_NULL: 10695 case OP_SCALAR: 10696 return ( 10697 o->op_flags & OPf_KIDS 10698 && looks_like_bool(cUNOPo->op_first)); 10699 10700 case OP_ENTERSUB: 10701 10702 case OP_NOT: case OP_XOR: 10703 10704 case OP_EQ: case OP_NE: case OP_LT: 10705 case OP_GT: case OP_LE: case OP_GE: 10706 10707 case OP_I_EQ: case OP_I_NE: case OP_I_LT: 10708 case OP_I_GT: case OP_I_LE: case OP_I_GE: 10709 10710 case OP_SEQ: case OP_SNE: case OP_SLT: 10711 case OP_SGT: case OP_SLE: case OP_SGE: 10712 10713 case OP_SMARTMATCH: 10714 10715 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC: 10716 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC: 10717 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED: 10718 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR: 10719 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR: 10720 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID: 10721 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY: 10722 case OP_FTTEXT: case OP_FTBINARY: 10723 10724 case OP_DEFINED: case OP_EXISTS: 10725 case OP_MATCH: case OP_EOF: 10726 10727 case OP_FLOP: 10728 10729 return TRUE; 10730 10731 case OP_INDEX: 10732 case OP_RINDEX: 10733 /* optimised-away (index() != -1) or similar comparison */ 10734 if (o->op_private & OPpTRUEBOOL) 10735 return TRUE; 10736 return FALSE; 10737 10738 case OP_CONST: 10739 /* Detect comparisons that have been optimized away */ 10740 if (cSVOPo->op_sv == &PL_sv_yes 10741 || cSVOPo->op_sv == &PL_sv_no) 10742 10743 return TRUE; 10744 else 10745 return FALSE; 10746 /* FALLTHROUGH */ 10747 default: 10748 return FALSE; 10749 } 10750 } 10751 10752 10753 /* 10754 =for apidoc newGIVENOP 10755 10756 Constructs, checks, and returns an op tree expressing a C<given> block. 10757 C<cond> supplies the expression to whose value C<$_> will be locally 10758 aliased, and C<block> supplies the body of the C<given> construct; they 10759 are consumed by this function and become part of the constructed op tree. 10760 C<defsv_off> must be zero (it used to identity the pad slot of lexical $_). 10761 10762 =cut 10763 */ 10764 10765 OP * 10766 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off) 10767 { 10768 PERL_ARGS_ASSERT_NEWGIVENOP; 10769 PERL_UNUSED_ARG(defsv_off); 10770 10771 assert(!defsv_off); 10772 return newGIVWHENOP( 10773 ref_array_or_hash(cond), 10774 block, 10775 OP_ENTERGIVEN, OP_LEAVEGIVEN, 10776 0); 10777 } 10778 10779 /* 10780 =for apidoc newWHENOP 10781 10782 Constructs, checks, and returns an op tree expressing a C<when> block. 10783 C<cond> supplies the test expression, and C<block> supplies the block 10784 that will be executed if the test evaluates to true; they are consumed 10785 by this function and become part of the constructed op tree. C<cond> 10786 will be interpreted DWIMically, often as a comparison against C<$_>, 10787 and may be null to generate a C<default> block. 10788 10789 =cut 10790 */ 10791 10792 OP * 10793 Perl_newWHENOP(pTHX_ OP *cond, OP *block) 10794 { 10795 const bool cond_llb = (!cond || looks_like_bool(cond)); 10796 OP *cond_op; 10797 10798 PERL_ARGS_ASSERT_NEWWHENOP; 10799 10800 if (cond_llb) 10801 cond_op = cond; 10802 else { 10803 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL, 10804 newDEFSVOP(), 10805 scalar(ref_array_or_hash(cond))); 10806 } 10807 10808 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0); 10809 } 10810 10811 /* 10812 =for apidoc newDEFEROP 10813 10814 Constructs and returns a deferred-block statement that implements the 10815 C<defer> semantics. The C<block> optree is consumed by this function and 10816 becomes part of the returned optree. 10817 10818 The C<flags> argument carries additional flags to set on the returned op, 10819 including the C<op_private> field. 10820 10821 =cut 10822 */ 10823 10824 OP * 10825 Perl_newDEFEROP(pTHX_ I32 flags, OP *block) 10826 { 10827 OP *o, *start, *blockfirst; 10828 10829 PERL_ARGS_ASSERT_NEWDEFEROP; 10830 10831 start = LINKLIST(block); 10832 10833 /* Hide the block inside an OP_NULL with no exection */ 10834 block = newUNOP(OP_NULL, 0, block); 10835 block->op_next = block; 10836 10837 o = (OP *)alloc_LOGOP(OP_PUSHDEFER, block, start); 10838 o->op_flags |= OPf_WANT_VOID | (U8)(flags); 10839 o->op_private = (U8)(flags >> 8); 10840 10841 /* Terminate the block */ 10842 blockfirst = cUNOPx(block)->op_first; 10843 assert(blockfirst->op_type == OP_SCOPE || blockfirst->op_type == OP_LEAVE); 10844 blockfirst->op_next = NULL; 10845 10846 return o; 10847 } 10848 10849 /* 10850 =for apidoc op_wrap_finally 10851 10852 Wraps the given C<block> optree fragment in its own scoped block, arranging 10853 for the C<finally> optree fragment to be invoked when leaving that block for 10854 any reason. Both optree fragments are consumed and the combined result is 10855 returned. 10856 10857 =cut 10858 */ 10859 10860 OP * 10861 Perl_op_wrap_finally(pTHX_ OP *block, OP *finally) 10862 { 10863 PERL_ARGS_ASSERT_OP_WRAP_FINALLY; 10864 10865 /* TODO: If block is already an ENTER/LEAVE-wrapped line sequence we can 10866 * just splice the DEFEROP in at the top, for efficiency. 10867 */ 10868 10869 OP *o = newLISTOP(OP_LINESEQ, 0, newDEFEROP((OPpDEFER_FINALLY << 8), finally), block); 10870 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); 10871 OpTYPE_set(o, OP_LEAVE); 10872 10873 return o; 10874 } 10875 10876 /* must not conflict with SVf_UTF8 */ 10877 #define CV_CKPROTO_CURSTASH 0x1 10878 10879 void 10880 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p, 10881 const STRLEN len, const U32 flags) 10882 { 10883 SV *name = NULL, *msg; 10884 const char * cvp = SvROK(cv) 10885 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV 10886 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv)) 10887 : "" 10888 : CvPROTO(cv); 10889 STRLEN clen = CvPROTOLEN(cv), plen = len; 10890 10891 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS; 10892 10893 if (p == NULL && cvp == NULL) 10894 return; 10895 10896 if (!ckWARN_d(WARN_PROTOTYPE)) 10897 return; 10898 10899 if (p && cvp) { 10900 p = S_strip_spaces(aTHX_ p, &plen); 10901 cvp = S_strip_spaces(aTHX_ cvp, &clen); 10902 if ((flags & SVf_UTF8) == SvUTF8(cv)) { 10903 if (plen == clen && memEQ(cvp, p, plen)) 10904 return; 10905 } else { 10906 if (flags & SVf_UTF8) { 10907 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0) 10908 return; 10909 } 10910 else { 10911 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0) 10912 return; 10913 } 10914 } 10915 } 10916 10917 msg = sv_newmortal(); 10918 10919 if (gv) 10920 { 10921 if (isGV(gv)) 10922 gv_efullname3(name = sv_newmortal(), gv, NULL); 10923 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&') 10924 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP); 10925 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) { 10926 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash))); 10927 sv_catpvs(name, "::"); 10928 if (SvROK(gv)) { 10929 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV); 10930 assert (CvNAMED(SvRV_const(gv))); 10931 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv)))); 10932 } 10933 else sv_catsv(name, (SV *)gv); 10934 } 10935 else name = (SV *)gv; 10936 } 10937 sv_setpvs(msg, "Prototype mismatch:"); 10938 if (name) 10939 Perl_sv_catpvf(aTHX_ msg, " sub %" SVf, SVfARG(name)); 10940 if (cvp) 10941 Perl_sv_catpvf(aTHX_ msg, " (%" UTF8f ")", 10942 UTF8fARG(SvUTF8(cv),clen,cvp) 10943 ); 10944 else 10945 sv_catpvs(msg, ": none"); 10946 sv_catpvs(msg, " vs "); 10947 if (p) 10948 Perl_sv_catpvf(aTHX_ msg, "(%" UTF8f ")", UTF8fARG(flags & SVf_UTF8,len,p)); 10949 else 10950 sv_catpvs(msg, "none"); 10951 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%" SVf, SVfARG(msg)); 10952 } 10953 10954 static void const_sv_xsub(pTHX_ CV* cv); 10955 static void const_av_xsub(pTHX_ CV* cv); 10956 10957 /* 10958 10959 =for apidoc_section $optree_manipulation 10960 10961 =for apidoc cv_const_sv 10962 10963 If C<cv> is a constant sub eligible for inlining, returns the constant 10964 value returned by the sub. Otherwise, returns C<NULL>. 10965 10966 Constant subs can be created with C<newCONSTSUB> or as described in 10967 L<perlsub/"Constant Functions">. 10968 10969 =cut 10970 */ 10971 SV * 10972 Perl_cv_const_sv(const CV *const cv) 10973 { 10974 SV *sv; 10975 if (!cv) 10976 return NULL; 10977 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM)) 10978 return NULL; 10979 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL; 10980 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL; 10981 return sv; 10982 } 10983 10984 SV * 10985 Perl_cv_const_sv_or_av(const CV * const cv) 10986 { 10987 if (!cv) 10988 return NULL; 10989 if (SvROK(cv)) return SvRV((SV *)cv); 10990 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM); 10991 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL; 10992 } 10993 10994 /* op_const_sv: examine an optree to determine whether it's in-lineable. 10995 * Can be called in 2 ways: 10996 * 10997 * !allow_lex 10998 * look for a single OP_CONST with attached value: return the value 10999 * 11000 * allow_lex && !CvCONST(cv); 11001 * 11002 * examine the clone prototype, and if contains only a single 11003 * OP_CONST, return the value; or if it contains a single PADSV ref- 11004 * erencing an outer lexical, turn on CvCONST to indicate the CV is 11005 * a candidate for "constizing" at clone time, and return NULL. 11006 */ 11007 11008 static SV * 11009 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex) 11010 { 11011 SV *sv = NULL; 11012 bool padsv = FALSE; 11013 11014 assert(o); 11015 assert(cv); 11016 11017 for (; o; o = o->op_next) { 11018 const OPCODE type = o->op_type; 11019 11020 if (type == OP_NEXTSTATE || type == OP_LINESEQ 11021 || type == OP_NULL 11022 || type == OP_PUSHMARK) 11023 continue; 11024 if (type == OP_DBSTATE) 11025 continue; 11026 if (type == OP_LEAVESUB) 11027 break; 11028 if (sv) 11029 return NULL; 11030 if (type == OP_CONST && cSVOPo->op_sv) 11031 sv = cSVOPo->op_sv; 11032 else if (type == OP_UNDEF && !o->op_private) { 11033 sv = newSV_type(SVt_NULL); 11034 SAVEFREESV(sv); 11035 } 11036 else if (allow_lex && type == OP_PADSV) { 11037 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER) 11038 { 11039 sv = &PL_sv_undef; /* an arbitrary non-null value */ 11040 padsv = TRUE; 11041 } 11042 else 11043 return NULL; 11044 } 11045 else { 11046 return NULL; 11047 } 11048 } 11049 if (padsv) { 11050 CvCONST_on(cv); 11051 return NULL; 11052 } 11053 return sv; 11054 } 11055 11056 static void 11057 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o, 11058 PADNAME * const name, SV ** const const_svp) 11059 { 11060 assert (cv); 11061 assert (o || name); 11062 assert (const_svp); 11063 if (!block) { 11064 if (CvFLAGS(PL_compcv)) { 11065 /* might have had built-in attrs applied */ 11066 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv); 11067 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl 11068 && ckWARN(WARN_MISC)) 11069 { 11070 /* protect against fatal warnings leaking compcv */ 11071 SAVEFREESV(PL_compcv); 11072 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined"); 11073 SvREFCNT_inc_simple_void_NN(PL_compcv); 11074 } 11075 CvFLAGS(cv) |= 11076 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS 11077 & ~(CVf_LVALUE * pureperl)); 11078 } 11079 return; 11080 } 11081 11082 /* redundant check for speed: */ 11083 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) { 11084 const line_t oldline = CopLINE(PL_curcop); 11085 SV *namesv = o 11086 ? cSVOPo->op_sv 11087 : newSVpvn_flags( PadnamePV(name)+1,PadnameLEN(name)-1, 11088 (PadnameUTF8(name)) ? SVf_UTF8|SVs_TEMP : SVs_TEMP 11089 ); 11090 if (PL_parser && PL_parser->copline != NOLINE) 11091 /* This ensures that warnings are reported at the first 11092 line of a redefinition, not the last. */ 11093 CopLINE_set(PL_curcop, PL_parser->copline); 11094 /* protect against fatal warnings leaking compcv */ 11095 SAVEFREESV(PL_compcv); 11096 report_redefined_cv(namesv, cv, const_svp); 11097 SvREFCNT_inc_simple_void_NN(PL_compcv); 11098 CopLINE_set(PL_curcop, oldline); 11099 } 11100 SAVEFREESV(cv); 11101 return; 11102 } 11103 11104 CV * 11105 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block) 11106 { 11107 CV **spot; 11108 SV **svspot; 11109 const char *ps; 11110 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */ 11111 U32 ps_utf8 = 0; 11112 CV *cv = NULL; 11113 CV *compcv = PL_compcv; 11114 SV *const_sv; 11115 PADNAME *name; 11116 PADOFFSET pax = o->op_targ; 11117 CV *outcv = CvOUTSIDE(PL_compcv); 11118 CV *clonee = NULL; 11119 HEK *hek = NULL; 11120 bool reusable = FALSE; 11121 OP *start = NULL; 11122 #ifdef PERL_DEBUG_READONLY_OPS 11123 OPSLAB *slab = NULL; 11124 #endif 11125 11126 PERL_ARGS_ASSERT_NEWMYSUB; 11127 11128 PL_hints |= HINT_BLOCK_SCOPE; 11129 11130 /* Find the pad slot for storing the new sub. 11131 We cannot use PL_comppad, as it is the pad owned by the new sub. We 11132 need to look in CvOUTSIDE and find the pad belonging to the enclos- 11133 ing sub. And then we need to dig deeper if this is a lexical from 11134 outside, as in: 11135 my sub foo; sub { sub foo { } } 11136 */ 11137 redo: 11138 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax]; 11139 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) { 11140 pax = PARENT_PAD_INDEX(name); 11141 outcv = CvOUTSIDE(outcv); 11142 assert(outcv); 11143 goto redo; 11144 } 11145 svspot = 11146 &PadARRAY(PadlistARRAY(CvPADLIST(outcv)) 11147 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax]; 11148 spot = (CV **)svspot; 11149 11150 if (!(PL_parser && PL_parser->error_count)) 11151 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name), 0); 11152 11153 if (proto) { 11154 assert(proto->op_type == OP_CONST); 11155 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len); 11156 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv); 11157 } 11158 else 11159 ps = NULL; 11160 11161 if (proto) 11162 SAVEFREEOP(proto); 11163 if (attrs) 11164 SAVEFREEOP(attrs); 11165 11166 if (PL_parser && PL_parser->error_count) { 11167 op_free(block); 11168 SvREFCNT_dec(PL_compcv); 11169 PL_compcv = 0; 11170 goto done; 11171 } 11172 11173 if (CvDEPTH(outcv) && CvCLONE(compcv)) { 11174 cv = *spot; 11175 svspot = (SV **)(spot = &clonee); 11176 } 11177 else if (PadnameIsSTATE(name) || CvDEPTH(outcv)) 11178 cv = *spot; 11179 else { 11180 assert (SvTYPE(*spot) == SVt_PVCV); 11181 if (CvNAMED(*spot)) 11182 hek = CvNAME_HEK(*spot); 11183 else { 11184 U32 hash; 11185 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1); 11186 CvNAME_HEK_set(*spot, hek = 11187 share_hek( 11188 PadnamePV(name)+1, 11189 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1), 11190 hash 11191 ) 11192 ); 11193 CvLEXICAL_on(*spot); 11194 } 11195 cv = PadnamePROTOCV(name); 11196 svspot = (SV **)(spot = &PadnamePROTOCV(name)); 11197 } 11198 11199 if (block) { 11200 /* This makes sub {}; work as expected. */ 11201 if (block->op_type == OP_STUB) { 11202 const line_t l = PL_parser->copline; 11203 op_free(block); 11204 block = newSTATEOP(0, NULL, 0); 11205 PL_parser->copline = l; 11206 } 11207 block = CvLVALUE(compcv) 11208 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)) 11209 ? newUNOP(OP_LEAVESUBLV, 0, 11210 op_lvalue(voidnonfinal(block), OP_LEAVESUBLV)) 11211 : newUNOP(OP_LEAVESUB, 0, voidnonfinal(block)); 11212 start = LINKLIST(block); 11213 block->op_next = 0; 11214 if (ps && !*ps && !attrs && !CvLVALUE(compcv)) 11215 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE); 11216 else 11217 const_sv = NULL; 11218 } 11219 else 11220 const_sv = NULL; 11221 11222 if (cv) { 11223 const bool exists = CvROOT(cv) || CvXSUB(cv); 11224 11225 /* if the subroutine doesn't exist and wasn't pre-declared 11226 * with a prototype, assume it will be AUTOLOADed, 11227 * skipping the prototype check 11228 */ 11229 if (exists || SvPOK(cv)) 11230 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len, 11231 ps_utf8); 11232 /* already defined? */ 11233 if (exists) { 11234 S_already_defined(aTHX_ cv, block, NULL, name, &const_sv); 11235 if (block) 11236 cv = NULL; 11237 else { 11238 if (attrs) 11239 goto attrs; 11240 /* just a "sub foo;" when &foo is already defined */ 11241 SAVEFREESV(compcv); 11242 goto done; 11243 } 11244 } 11245 else if (CvDEPTH(outcv) && CvCLONE(compcv)) { 11246 cv = NULL; 11247 reusable = TRUE; 11248 } 11249 } 11250 11251 if (const_sv) { 11252 SvREFCNT_inc_simple_void_NN(const_sv); 11253 SvFLAGS(const_sv) |= SVs_PADTMP; 11254 if (cv) { 11255 assert(!CvROOT(cv) && !CvCONST(cv)); 11256 cv_forget_slab(cv); 11257 } 11258 else { 11259 cv = MUTABLE_CV(newSV_type(SVt_PVCV)); 11260 CvFILE_set_from_cop(cv, PL_curcop); 11261 CvSTASH_set(cv, PL_curstash); 11262 *spot = cv; 11263 } 11264 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */ 11265 CvXSUBANY(cv).any_ptr = const_sv; 11266 CvXSUB(cv) = const_sv_xsub; 11267 CvCONST_on(cv); 11268 CvISXSUB_on(cv); 11269 PoisonPADLIST(cv); 11270 CvFLAGS(cv) |= CvMETHOD(compcv); 11271 op_free(block); 11272 SvREFCNT_dec(compcv); 11273 PL_compcv = NULL; 11274 goto setname; 11275 } 11276 11277 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to 11278 determine whether this sub definition is in the same scope as its 11279 declaration. If this sub definition is inside an inner named pack- 11280 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to 11281 the package sub. So check PadnameOUTER(name) too. 11282 */ 11283 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) { 11284 assert(!CvWEAKOUTSIDE(compcv)); 11285 SvREFCNT_dec(CvOUTSIDE(compcv)); 11286 CvWEAKOUTSIDE_on(compcv); 11287 } 11288 /* XXX else do we have a circular reference? */ 11289 11290 if (cv) { /* must reuse cv in case stub is referenced elsewhere */ 11291 /* transfer PL_compcv to cv */ 11292 if (block) { 11293 bool free_file = CvFILE(cv) && CvDYNFILE(cv); 11294 cv_flags_t preserved_flags = 11295 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED); 11296 PADLIST *const temp_padl = CvPADLIST(cv); 11297 CV *const temp_cv = CvOUTSIDE(cv); 11298 const cv_flags_t other_flags = 11299 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE); 11300 OP * const cvstart = CvSTART(cv); 11301 11302 SvPOK_off(cv); 11303 CvFLAGS(cv) = 11304 CvFLAGS(compcv) | preserved_flags; 11305 CvOUTSIDE(cv) = CvOUTSIDE(compcv); 11306 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv); 11307 CvPADLIST_set(cv, CvPADLIST(compcv)); 11308 CvOUTSIDE(compcv) = temp_cv; 11309 CvPADLIST_set(compcv, temp_padl); 11310 CvSTART(cv) = CvSTART(compcv); 11311 CvSTART(compcv) = cvstart; 11312 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE); 11313 CvFLAGS(compcv) |= other_flags; 11314 11315 if (free_file) { 11316 Safefree(CvFILE(cv)); 11317 CvFILE(cv) = NULL; 11318 } 11319 11320 /* inner references to compcv must be fixed up ... */ 11321 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv); 11322 if (PERLDB_INTER)/* Advice debugger on the new sub. */ 11323 ++PL_sub_generation; 11324 } 11325 else { 11326 /* Might have had built-in attributes applied -- propagate them. */ 11327 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS); 11328 } 11329 /* ... before we throw it away */ 11330 SvREFCNT_dec(compcv); 11331 PL_compcv = compcv = cv; 11332 } 11333 else { 11334 cv = compcv; 11335 *spot = cv; 11336 } 11337 11338 setname: 11339 CvLEXICAL_on(cv); 11340 if (!CvNAME_HEK(cv)) { 11341 if (hek) (void)share_hek_hek(hek); 11342 else { 11343 U32 hash; 11344 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1); 11345 hek = share_hek(PadnamePV(name)+1, 11346 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1), 11347 hash); 11348 } 11349 CvNAME_HEK_set(cv, hek); 11350 } 11351 11352 if (const_sv) 11353 goto clone; 11354 11355 if (CvFILE(cv) && CvDYNFILE(cv)) 11356 Safefree(CvFILE(cv)); 11357 CvFILE_set_from_cop(cv, PL_curcop); 11358 CvSTASH_set(cv, PL_curstash); 11359 11360 if (ps) { 11361 sv_setpvn(MUTABLE_SV(cv), ps, ps_len); 11362 if (ps_utf8) 11363 SvUTF8_on(MUTABLE_SV(cv)); 11364 } 11365 11366 if (block) { 11367 /* If we assign an optree to a PVCV, then we've defined a 11368 * subroutine that the debugger could be able to set a breakpoint 11369 * in, so signal to pp_entereval that it should not throw away any 11370 * saved lines at scope exit. */ 11371 11372 PL_breakable_sub_gen++; 11373 CvROOT(cv) = block; 11374 /* The cv no longer needs to hold a refcount on the slab, as CvROOT 11375 itself has a refcount. */ 11376 CvSLABBED_off(cv); 11377 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv)); 11378 #ifdef PERL_DEBUG_READONLY_OPS 11379 slab = (OPSLAB *)CvSTART(cv); 11380 #endif 11381 S_process_optree(aTHX_ cv, block, start); 11382 } 11383 11384 attrs: 11385 if (attrs) { 11386 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */ 11387 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs); 11388 } 11389 11390 if (block) { 11391 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { 11392 SV * const tmpstr = sv_newmortal(); 11393 GV * const db_postponed = gv_fetchpvs("DB::postponed", 11394 GV_ADDMULTI, SVt_PVHV); 11395 HV *hv; 11396 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld", 11397 CopFILE(PL_curcop), 11398 (long)PL_subline, 11399 (long)CopLINE(PL_curcop)); 11400 if (HvNAME_HEK(PL_curstash)) { 11401 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash)); 11402 sv_catpvs(tmpstr, "::"); 11403 } 11404 else 11405 sv_setpvs(tmpstr, "__ANON__::"); 11406 11407 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1, 11408 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES); 11409 (void)hv_store_ent(GvHV(PL_DBsub), tmpstr, sv, 0); 11410 hv = GvHVn(db_postponed); 11411 if (HvTOTALKEYS(hv) > 0 && hv_exists_ent(hv, tmpstr, 0)) { 11412 CV * const pcv = GvCV(db_postponed); 11413 if (pcv) { 11414 dSP; 11415 PUSHMARK(SP); 11416 XPUSHs(tmpstr); 11417 PUTBACK; 11418 call_sv(MUTABLE_SV(pcv), G_DISCARD); 11419 } 11420 } 11421 } 11422 } 11423 11424 clone: 11425 if (clonee) { 11426 assert(CvDEPTH(outcv)); 11427 spot = (CV **) 11428 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax]; 11429 if (reusable) 11430 cv_clone_into(clonee, *spot); 11431 else *spot = cv_clone(clonee); 11432 SvREFCNT_dec_NN(clonee); 11433 cv = *spot; 11434 } 11435 11436 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) { 11437 PADOFFSET depth = CvDEPTH(outcv); 11438 while (--depth) { 11439 SV *oldcv; 11440 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax]; 11441 oldcv = *svspot; 11442 *svspot = SvREFCNT_inc_simple_NN(cv); 11443 SvREFCNT_dec(oldcv); 11444 } 11445 } 11446 11447 done: 11448 if (PL_parser) 11449 PL_parser->copline = NOLINE; 11450 LEAVE_SCOPE(floor); 11451 #ifdef PERL_DEBUG_READONLY_OPS 11452 if (slab) 11453 Slab_to_ro(slab); 11454 #endif 11455 op_free(o); 11456 return cv; 11457 } 11458 11459 /* 11460 =for apidoc newATTRSUB_x 11461 11462 Construct a Perl subroutine, also performing some surrounding jobs. 11463 11464 This function is expected to be called in a Perl compilation context, 11465 and some aspects of the subroutine are taken from global variables 11466 associated with compilation. In particular, C<PL_compcv> represents 11467 the subroutine that is currently being compiled. It must be non-null 11468 when this function is called, and some aspects of the subroutine being 11469 constructed are taken from it. The constructed subroutine may actually 11470 be a reuse of the C<PL_compcv> object, but will not necessarily be so. 11471 11472 If C<block> is null then the subroutine will have no body, and for the 11473 time being it will be an error to call it. This represents a forward 11474 subroutine declaration such as S<C<sub foo ($$);>>. If C<block> is 11475 non-null then it provides the Perl code of the subroutine body, which 11476 will be executed when the subroutine is called. This body includes 11477 any argument unwrapping code resulting from a subroutine signature or 11478 similar. The pad use of the code must correspond to the pad attached 11479 to C<PL_compcv>. The code is not expected to include a C<leavesub> or 11480 C<leavesublv> op; this function will add such an op. C<block> is consumed 11481 by this function and will become part of the constructed subroutine. 11482 11483 C<proto> specifies the subroutine's prototype, unless one is supplied 11484 as an attribute (see below). If C<proto> is null, then the subroutine 11485 will not have a prototype. If C<proto> is non-null, it must point to a 11486 C<const> op whose value is a string, and the subroutine will have that 11487 string as its prototype. If a prototype is supplied as an attribute, the 11488 attribute takes precedence over C<proto>, but in that case C<proto> should 11489 preferably be null. In any case, C<proto> is consumed by this function. 11490 11491 C<attrs> supplies attributes to be applied the subroutine. A handful of 11492 attributes take effect by built-in means, being applied to C<PL_compcv> 11493 immediately when seen. Other attributes are collected up and attached 11494 to the subroutine by this route. C<attrs> may be null to supply no 11495 attributes, or point to a C<const> op for a single attribute, or point 11496 to a C<list> op whose children apart from the C<pushmark> are C<const> 11497 ops for one or more attributes. Each C<const> op must be a string, 11498 giving the attribute name optionally followed by parenthesised arguments, 11499 in the manner in which attributes appear in Perl source. The attributes 11500 will be applied to the sub by this function. C<attrs> is consumed by 11501 this function. 11502 11503 If C<o_is_gv> is false and C<o> is null, then the subroutine will 11504 be anonymous. If C<o_is_gv> is false and C<o> is non-null, then C<o> 11505 must point to a C<const> OP, which will be consumed by this function, 11506 and its string value supplies a name for the subroutine. The name may 11507 be qualified or unqualified, and if it is unqualified then a default 11508 stash will be selected in some manner. If C<o_is_gv> is true, then C<o> 11509 doesn't point to an C<OP> at all, but is instead a cast pointer to a C<GV> 11510 by which the subroutine will be named. 11511 11512 If there is already a subroutine of the specified name, then the new 11513 sub will either replace the existing one in the glob or be merged with 11514 the existing one. A warning may be generated about redefinition. 11515 11516 If the subroutine has one of a few special names, such as C<BEGIN> or 11517 C<END>, then it will be claimed by the appropriate queue for automatic 11518 running of phase-related subroutines. In this case the relevant glob will 11519 be left not containing any subroutine, even if it did contain one before. 11520 In the case of C<BEGIN>, the subroutine will be executed and the reference 11521 to it disposed of before this function returns. 11522 11523 The function returns a pointer to the constructed subroutine. If the sub 11524 is anonymous then ownership of one counted reference to the subroutine 11525 is transferred to the caller. If the sub is named then the caller does 11526 not get ownership of a reference. In most such cases, where the sub 11527 has a non-phase name, the sub will be alive at the point it is returned 11528 by virtue of being contained in the glob that names it. A phase-named 11529 subroutine will usually be alive by virtue of the reference owned by the 11530 phase's automatic run queue. But a C<BEGIN> subroutine, having already 11531 been executed, will quite likely have been destroyed already by the 11532 time this function returns, making it erroneous for the caller to make 11533 any use of the returned pointer. It is the caller's responsibility to 11534 ensure that it knows which of these situations applies. 11535 11536 =for apidoc newATTRSUB 11537 Construct a Perl subroutine, also performing some surrounding jobs. 11538 11539 This is the same as L<perlintern/C<newATTRSUB_x>> with its C<o_is_gv> parameter set to 11540 FALSE. This means that if C<o> is null, the new sub will be anonymous; otherwise 11541 the name will be derived from C<o> in the way described (as with all other 11542 details) in L<perlintern/C<newATTRSUB_x>>. 11543 11544 =for apidoc newSUB 11545 Like C<L</newATTRSUB>>, but without attributes. 11546 11547 =cut 11548 */ 11549 11550 /* _x = extended */ 11551 CV * 11552 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, 11553 OP *block, bool o_is_gv) 11554 { 11555 GV *gv; 11556 const char *ps; 11557 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */ 11558 U32 ps_utf8 = 0; 11559 CV *cv = NULL; /* the previous CV with this name, if any */ 11560 SV *const_sv; 11561 const bool ec = PL_parser && PL_parser->error_count; 11562 /* If the subroutine has no body, no attributes, and no builtin attributes 11563 then it's just a sub declaration, and we may be able to get away with 11564 storing with a placeholder scalar in the symbol table, rather than a 11565 full CV. If anything is present then it will take a full CV to 11566 store it. */ 11567 const I32 gv_fetch_flags 11568 = ec ? GV_NOADD_NOINIT : 11569 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) 11570 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT; 11571 STRLEN namlen = 0; 11572 const char * const name = 11573 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL; 11574 bool has_name; 11575 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv); 11576 bool evanescent = FALSE; 11577 OP *start = NULL; 11578 #ifdef PERL_DEBUG_READONLY_OPS 11579 OPSLAB *slab = NULL; 11580 #endif 11581 11582 if (o_is_gv) { 11583 gv = (GV*)o; 11584 o = NULL; 11585 has_name = TRUE; 11586 } else if (name) { 11587 /* Try to optimise and avoid creating a GV. Instead, the CV’s name 11588 hek and CvSTASH pointer together can imply the GV. If the name 11589 contains a package name, then GvSTASH(CvGV(cv)) may differ from 11590 CvSTASH, so forego the optimisation if we find any. 11591 Also, we may be called from load_module at run time, so 11592 PL_curstash (which sets CvSTASH) may not point to the stash the 11593 sub is stored in. */ 11594 /* XXX This optimization is currently disabled for packages other 11595 than main, since there was too much CPAN breakage. */ 11596 const I32 flags = 11597 ec ? GV_NOADD_NOINIT 11598 : (IN_PERL_RUNTIME && PL_curstash != CopSTASH(PL_curcop)) 11599 || PL_curstash != PL_defstash 11600 || memchr(name, ':', namlen) || memchr(name, '\'', namlen) 11601 ? gv_fetch_flags 11602 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL; 11603 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV); 11604 has_name = TRUE; 11605 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) { 11606 SV * const sv = sv_newmortal(); 11607 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%" IVdf "]", 11608 PL_curstash ? "__ANON__" : "__ANON__::__ANON__", 11609 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); 11610 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV); 11611 has_name = TRUE; 11612 } else if (PL_curstash) { 11613 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV); 11614 has_name = FALSE; 11615 } else { 11616 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV); 11617 has_name = FALSE; 11618 } 11619 11620 if (!ec) { 11621 if (isGV(gv)) { 11622 move_proto_attr(&proto, &attrs, gv, 0); 11623 } else { 11624 assert(cSVOPo); 11625 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv, 1); 11626 } 11627 } 11628 11629 if (proto) { 11630 assert(proto->op_type == OP_CONST); 11631 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len); 11632 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv); 11633 } 11634 else 11635 ps = NULL; 11636 11637 if (o) 11638 SAVEFREEOP(o); 11639 if (proto) 11640 SAVEFREEOP(proto); 11641 if (attrs) 11642 SAVEFREEOP(attrs); 11643 11644 if (ec) { 11645 op_free(block); 11646 11647 if (name) 11648 SvREFCNT_dec(PL_compcv); 11649 else 11650 cv = PL_compcv; 11651 11652 PL_compcv = 0; 11653 if (name && block) { 11654 const char *s = (char *) my_memrchr(name, ':', namlen); 11655 s = s ? s+1 : name; 11656 if (strEQ(s, "BEGIN")) { 11657 if (PL_in_eval & EVAL_KEEPERR) 11658 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted"); 11659 else { 11660 SV * const errsv = ERRSV; 11661 /* force display of errors found but not reported */ 11662 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted"); 11663 Perl_croak_nocontext("%" SVf, SVfARG(errsv)); 11664 } 11665 } 11666 } 11667 goto done; 11668 } 11669 11670 if (!block && SvTYPE(gv) != SVt_PVGV) { 11671 /* If we are not defining a new sub and the existing one is not a 11672 full GV + CV... */ 11673 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) { 11674 /* We are applying attributes to an existing sub, so we need it 11675 upgraded if it is a constant. */ 11676 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV) 11677 gv_init_pvn(gv, PL_curstash, name, namlen, 11678 SVf_UTF8 * name_is_utf8); 11679 } 11680 else { /* Maybe prototype now, and had at maximum 11681 a prototype or const/sub ref before. */ 11682 if (SvTYPE(gv) > SVt_NULL) { 11683 cv_ckproto_len_flags((const CV *)gv, 11684 o ? (const GV *)cSVOPo->op_sv : NULL, ps, 11685 ps_len, ps_utf8); 11686 } 11687 11688 if (!SvROK(gv)) { 11689 if (ps) { 11690 sv_setpvn(MUTABLE_SV(gv), ps, ps_len); 11691 if (ps_utf8) 11692 SvUTF8_on(MUTABLE_SV(gv)); 11693 } 11694 else 11695 sv_setiv(MUTABLE_SV(gv), -1); 11696 } 11697 11698 SvREFCNT_dec(PL_compcv); 11699 cv = PL_compcv = NULL; 11700 goto done; 11701 } 11702 } 11703 11704 cv = (!name || (isGV(gv) && GvCVGEN(gv))) 11705 ? NULL 11706 : isGV(gv) 11707 ? GvCV(gv) 11708 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV 11709 ? (CV *)SvRV(gv) 11710 : NULL; 11711 11712 if (block) { 11713 assert(PL_parser); 11714 /* This makes sub {}; work as expected. */ 11715 if (block->op_type == OP_STUB) { 11716 const line_t l = PL_parser->copline; 11717 op_free(block); 11718 block = newSTATEOP(0, NULL, 0); 11719 PL_parser->copline = l; 11720 } 11721 block = CvLVALUE(PL_compcv) 11722 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv) 11723 && (!isGV(gv) || !GvASSUMECV(gv))) 11724 ? newUNOP(OP_LEAVESUBLV, 0, 11725 op_lvalue(voidnonfinal(block), OP_LEAVESUBLV)) 11726 : newUNOP(OP_LEAVESUB, 0, voidnonfinal(block)); 11727 start = LINKLIST(block); 11728 block->op_next = 0; 11729 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv)) 11730 const_sv = 11731 S_op_const_sv(aTHX_ start, PL_compcv, 11732 cBOOL(CvCLONE(PL_compcv))); 11733 else 11734 const_sv = NULL; 11735 } 11736 else 11737 const_sv = NULL; 11738 11739 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) { 11740 cv_ckproto_len_flags((const CV *)gv, 11741 o ? (const GV *)cSVOPo->op_sv : NULL, ps, 11742 ps_len, ps_utf8|CV_CKPROTO_CURSTASH); 11743 if (SvROK(gv)) { 11744 /* All the other code for sub redefinition warnings expects the 11745 clobbered sub to be a CV. Instead of making all those code 11746 paths more complex, just inline the RV version here. */ 11747 const line_t oldline = CopLINE(PL_curcop); 11748 assert(IN_PERL_COMPILETIME); 11749 if (PL_parser && PL_parser->copline != NOLINE) 11750 /* This ensures that warnings are reported at the first 11751 line of a redefinition, not the last. */ 11752 CopLINE_set(PL_curcop, PL_parser->copline); 11753 /* protect against fatal warnings leaking compcv */ 11754 SAVEFREESV(PL_compcv); 11755 11756 if (ckWARN(WARN_REDEFINE) 11757 || ( ckWARN_d(WARN_REDEFINE) 11758 && ( !const_sv || SvRV(gv) == const_sv 11759 || sv_cmp(SvRV(gv), const_sv) ))) { 11760 assert(cSVOPo); 11761 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 11762 "Constant subroutine %" SVf " redefined", 11763 SVfARG(cSVOPo->op_sv)); 11764 } 11765 11766 SvREFCNT_inc_simple_void_NN(PL_compcv); 11767 CopLINE_set(PL_curcop, oldline); 11768 SvREFCNT_dec(SvRV(gv)); 11769 } 11770 } 11771 11772 if (cv) { 11773 const bool exists = CvROOT(cv) || CvXSUB(cv); 11774 11775 /* if the subroutine doesn't exist and wasn't pre-declared 11776 * with a prototype, assume it will be AUTOLOADed, 11777 * skipping the prototype check 11778 */ 11779 if (exists || SvPOK(cv)) 11780 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8); 11781 /* already defined (or promised)? */ 11782 if (exists || (isGV(gv) && GvASSUMECV(gv))) { 11783 S_already_defined(aTHX_ cv, block, o, NULL, &const_sv); 11784 if (block) 11785 cv = NULL; 11786 else { 11787 if (attrs) 11788 goto attrs; 11789 /* just a "sub foo;" when &foo is already defined */ 11790 SAVEFREESV(PL_compcv); 11791 goto done; 11792 } 11793 } 11794 } 11795 11796 if (const_sv) { 11797 SvREFCNT_inc_simple_void_NN(const_sv); 11798 SvFLAGS(const_sv) |= SVs_PADTMP; 11799 if (cv) { 11800 assert(!CvROOT(cv) && !CvCONST(cv)); 11801 cv_forget_slab(cv); 11802 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */ 11803 CvXSUBANY(cv).any_ptr = const_sv; 11804 CvXSUB(cv) = const_sv_xsub; 11805 CvCONST_on(cv); 11806 CvISXSUB_on(cv); 11807 PoisonPADLIST(cv); 11808 CvFLAGS(cv) |= CvMETHOD(PL_compcv); 11809 } 11810 else { 11811 if (isGV(gv) || CvMETHOD(PL_compcv)) { 11812 if (name && isGV(gv)) 11813 GvCV_set(gv, NULL); 11814 cv = newCONSTSUB_flags( 11815 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0, 11816 const_sv 11817 ); 11818 assert(cv); 11819 assert(SvREFCNT((SV*)cv) != 0); 11820 CvFLAGS(cv) |= CvMETHOD(PL_compcv); 11821 } 11822 else { 11823 if (!SvROK(gv)) { 11824 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv); 11825 prepare_SV_for_RV((SV *)gv); 11826 SvOK_off((SV *)gv); 11827 SvROK_on(gv); 11828 } 11829 SvRV_set(gv, const_sv); 11830 } 11831 } 11832 op_free(block); 11833 SvREFCNT_dec(PL_compcv); 11834 PL_compcv = NULL; 11835 goto done; 11836 } 11837 11838 /* don't copy new BEGIN CV to old BEGIN CV - RT #129099 */ 11839 if (name && cv && *name == 'B' && strEQ(name, "BEGIN")) 11840 cv = NULL; 11841 11842 if (cv) { /* must reuse cv if autoloaded */ 11843 /* transfer PL_compcv to cv */ 11844 if (block) { 11845 bool free_file = CvFILE(cv) && CvDYNFILE(cv); 11846 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS; 11847 PADLIST *const temp_av = CvPADLIST(cv); 11848 CV *const temp_cv = CvOUTSIDE(cv); 11849 const cv_flags_t other_flags = 11850 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE); 11851 OP * const cvstart = CvSTART(cv); 11852 11853 if (isGV(gv)) { 11854 CvGV_set(cv,gv); 11855 assert(!CvCVGV_RC(cv)); 11856 assert(CvGV(cv) == gv); 11857 } 11858 else { 11859 U32 hash; 11860 PERL_HASH(hash, name, namlen); 11861 CvNAME_HEK_set(cv, 11862 share_hek(name, 11863 name_is_utf8 11864 ? -(SSize_t)namlen 11865 : (SSize_t)namlen, 11866 hash)); 11867 } 11868 11869 SvPOK_off(cv); 11870 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs 11871 | CvNAMED(cv); 11872 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv); 11873 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv); 11874 CvPADLIST_set(cv,CvPADLIST(PL_compcv)); 11875 CvOUTSIDE(PL_compcv) = temp_cv; 11876 CvPADLIST_set(PL_compcv, temp_av); 11877 CvSTART(cv) = CvSTART(PL_compcv); 11878 CvSTART(PL_compcv) = cvstart; 11879 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE); 11880 CvFLAGS(PL_compcv) |= other_flags; 11881 11882 if (free_file) { 11883 Safefree(CvFILE(cv)); 11884 } 11885 CvFILE_set_from_cop(cv, PL_curcop); 11886 CvSTASH_set(cv, PL_curstash); 11887 11888 /* inner references to PL_compcv must be fixed up ... */ 11889 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv); 11890 if (PERLDB_INTER)/* Advice debugger on the new sub. */ 11891 ++PL_sub_generation; 11892 } 11893 else { 11894 /* Might have had built-in attributes applied -- propagate them. */ 11895 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS); 11896 } 11897 /* ... before we throw it away */ 11898 SvREFCNT_dec(PL_compcv); 11899 PL_compcv = cv; 11900 } 11901 else { 11902 cv = PL_compcv; 11903 if (name && isGV(gv)) { 11904 GvCV_set(gv, cv); 11905 GvCVGEN(gv) = 0; 11906 if (HvENAME_HEK(GvSTASH(gv))) 11907 /* sub Foo::bar { (shift)+1 } */ 11908 gv_method_changed(gv); 11909 } 11910 else if (name) { 11911 if (!SvROK(gv)) { 11912 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv); 11913 prepare_SV_for_RV((SV *)gv); 11914 SvOK_off((SV *)gv); 11915 SvROK_on(gv); 11916 } 11917 SvRV_set(gv, (SV *)cv); 11918 if (HvENAME_HEK(PL_curstash)) 11919 mro_method_changed_in(PL_curstash); 11920 } 11921 } 11922 assert(cv); 11923 assert(SvREFCNT((SV*)cv) != 0); 11924 11925 if (!CvHASGV(cv)) { 11926 if (isGV(gv)) 11927 CvGV_set(cv, gv); 11928 else { 11929 U32 hash; 11930 PERL_HASH(hash, name, namlen); 11931 CvNAME_HEK_set(cv, share_hek(name, 11932 name_is_utf8 11933 ? -(SSize_t)namlen 11934 : (SSize_t)namlen, 11935 hash)); 11936 } 11937 CvFILE_set_from_cop(cv, PL_curcop); 11938 CvSTASH_set(cv, PL_curstash); 11939 } 11940 11941 if (ps) { 11942 sv_setpvn(MUTABLE_SV(cv), ps, ps_len); 11943 if ( ps_utf8 ) 11944 SvUTF8_on(MUTABLE_SV(cv)); 11945 } 11946 11947 if (block) { 11948 /* If we assign an optree to a PVCV, then we've defined a 11949 * subroutine that the debugger could be able to set a breakpoint 11950 * in, so signal to pp_entereval that it should not throw away any 11951 * saved lines at scope exit. */ 11952 11953 PL_breakable_sub_gen++; 11954 CvROOT(cv) = block; 11955 /* The cv no longer needs to hold a refcount on the slab, as CvROOT 11956 itself has a refcount. */ 11957 CvSLABBED_off(cv); 11958 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv)); 11959 #ifdef PERL_DEBUG_READONLY_OPS 11960 slab = (OPSLAB *)CvSTART(cv); 11961 #endif 11962 S_process_optree(aTHX_ cv, block, start); 11963 } 11964 11965 attrs: 11966 if (attrs) { 11967 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */ 11968 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv)) 11969 ? GvSTASH(CvGV(cv)) 11970 : PL_curstash; 11971 if (!name) 11972 SAVEFREESV(cv); 11973 apply_attrs(stash, MUTABLE_SV(cv), attrs); 11974 if (!name) 11975 SvREFCNT_inc_simple_void_NN(cv); 11976 } 11977 11978 if (block && has_name) { 11979 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { 11980 SV * const tmpstr = cv_name(cv,NULL,0); 11981 GV * const db_postponed = gv_fetchpvs("DB::postponed", 11982 GV_ADDMULTI, SVt_PVHV); 11983 HV *hv; 11984 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld", 11985 CopFILE(PL_curcop), 11986 (long)PL_subline, 11987 (long)CopLINE(PL_curcop)); 11988 (void)hv_store_ent(GvHV(PL_DBsub), tmpstr, sv, 0); 11989 hv = GvHVn(db_postponed); 11990 if (HvTOTALKEYS(hv) > 0 && hv_exists_ent(hv, tmpstr, 0)) { 11991 CV * const pcv = GvCV(db_postponed); 11992 if (pcv) { 11993 dSP; 11994 PUSHMARK(SP); 11995 XPUSHs(tmpstr); 11996 PUTBACK; 11997 call_sv(MUTABLE_SV(pcv), G_DISCARD); 11998 } 11999 } 12000 } 12001 12002 if (name) { 12003 if (PL_parser && PL_parser->error_count) 12004 clear_special_blocks(name, gv, cv); 12005 else 12006 evanescent = 12007 process_special_blocks(floor, name, gv, cv); 12008 } 12009 } 12010 assert(cv); 12011 12012 done: 12013 assert(!cv || evanescent || SvREFCNT((SV*)cv) != 0); 12014 if (PL_parser) 12015 PL_parser->copline = NOLINE; 12016 LEAVE_SCOPE(floor); 12017 12018 assert(!cv || evanescent || SvREFCNT((SV*)cv) != 0); 12019 if (!evanescent) { 12020 #ifdef PERL_DEBUG_READONLY_OPS 12021 if (slab) 12022 Slab_to_ro(slab); 12023 #endif 12024 if (cv && name && block && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv))) 12025 pad_add_weakref(cv); 12026 } 12027 return cv; 12028 } 12029 12030 STATIC void 12031 S_clear_special_blocks(pTHX_ const char *const fullname, 12032 GV *const gv, CV *const cv) { 12033 const char *colon; 12034 const char *name; 12035 12036 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS; 12037 12038 colon = strrchr(fullname,':'); 12039 name = colon ? colon + 1 : fullname; 12040 12041 if ((*name == 'B' && strEQ(name, "BEGIN")) 12042 || (*name == 'E' && strEQ(name, "END")) 12043 || (*name == 'U' && strEQ(name, "UNITCHECK")) 12044 || (*name == 'C' && strEQ(name, "CHECK")) 12045 || (*name == 'I' && strEQ(name, "INIT"))) { 12046 if (!isGV(gv)) { 12047 (void)CvGV(cv); 12048 assert(isGV(gv)); 12049 } 12050 GvCV_set(gv, NULL); 12051 SvREFCNT_dec_NN(MUTABLE_SV(cv)); 12052 } 12053 } 12054 12055 /* Returns true if the sub has been freed. */ 12056 STATIC bool 12057 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname, 12058 GV *const gv, 12059 CV *const cv) 12060 { 12061 const char *const colon = strrchr(fullname,':'); 12062 const char *const name = colon ? colon + 1 : fullname; 12063 12064 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS; 12065 12066 if (*name == 'B') { 12067 if (strEQ(name, "BEGIN")) { 12068 const I32 oldscope = PL_scopestack_ix; 12069 dSP; 12070 (void)CvGV(cv); 12071 if (floor) LEAVE_SCOPE(floor); 12072 ENTER; 12073 12074 SAVEVPTR(PL_curcop); 12075 if (PL_curcop == &PL_compiling) { 12076 /* Avoid pushing the "global" &PL_compiling onto the 12077 * context stack. For example, a stack trace inside 12078 * nested use's would show all calls coming from whoever 12079 * most recently updated PL_compiling.cop_file and 12080 * cop_line. So instead, temporarily set PL_curcop to a 12081 * private copy of &PL_compiling. PL_curcop will soon be 12082 * set to point back to &PL_compiling anyway but only 12083 * after the temp value has been pushed onto the context 12084 * stack as blk_oldcop. 12085 * This is slightly hacky, but necessary. Note also 12086 * that in the brief window before PL_curcop is set back 12087 * to PL_compiling, IN_PERL_COMPILETIME/IN_PERL_RUNTIME 12088 * will give the wrong answer. 12089 */ 12090 PL_curcop = (COP*)newSTATEOP(PL_compiling.op_flags, NULL, NULL); 12091 CopLINE_set(PL_curcop, CopLINE(&PL_compiling)); 12092 SAVEFREEOP(PL_curcop); 12093 } 12094 12095 PUSHSTACKi(PERLSI_REQUIRE); 12096 SAVECOPFILE(&PL_compiling); 12097 SAVECOPLINE(&PL_compiling); 12098 12099 DEBUG_x( dump_sub(gv) ); 12100 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv)); 12101 GvCV_set(gv,0); /* cv has been hijacked */ 12102 call_list(oldscope, PL_beginav); 12103 12104 POPSTACK; 12105 LEAVE; 12106 return !PL_savebegin; 12107 } 12108 else 12109 return FALSE; 12110 } else { 12111 if (*name == 'E') { 12112 if (strEQ(name, "END")) { 12113 DEBUG_x( dump_sub(gv) ); 12114 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv)); 12115 } else 12116 return FALSE; 12117 } else if (*name == 'U') { 12118 if (strEQ(name, "UNITCHECK")) { 12119 /* It's never too late to run a unitcheck block */ 12120 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv)); 12121 } 12122 else 12123 return FALSE; 12124 } else if (*name == 'C') { 12125 if (strEQ(name, "CHECK")) { 12126 if (PL_main_start) 12127 /* diag_listed_as: Too late to run %s block */ 12128 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 12129 "Too late to run CHECK block"); 12130 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv)); 12131 } 12132 else 12133 return FALSE; 12134 } else if (*name == 'I') { 12135 if (strEQ(name, "INIT")) { 12136 if (PL_main_start) 12137 /* diag_listed_as: Too late to run %s block */ 12138 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 12139 "Too late to run INIT block"); 12140 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv)); 12141 } 12142 else 12143 return FALSE; 12144 } else 12145 return FALSE; 12146 DEBUG_x( dump_sub(gv) ); 12147 (void)CvGV(cv); 12148 GvCV_set(gv,0); /* cv has been hijacked */ 12149 return FALSE; 12150 } 12151 } 12152 12153 /* 12154 =for apidoc newCONSTSUB 12155 12156 Behaves like L</newCONSTSUB_flags>, except that C<name> is nul-terminated 12157 rather than of counted length, and no flags are set. (This means that 12158 C<name> is always interpreted as Latin-1.) 12159 12160 =cut 12161 */ 12162 12163 CV * 12164 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv) 12165 { 12166 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv); 12167 } 12168 12169 /* 12170 =for apidoc newCONSTSUB_flags 12171 12172 Construct a constant subroutine, also performing some surrounding 12173 jobs. A scalar constant-valued subroutine is eligible for inlining 12174 at compile-time, and in Perl code can be created by S<C<sub FOO () { 12175 123 }>>. Other kinds of constant subroutine have other treatment. 12176 12177 The subroutine will have an empty prototype and will ignore any arguments 12178 when called. Its constant behaviour is determined by C<sv>. If C<sv> 12179 is null, the subroutine will yield an empty list. If C<sv> points to a 12180 scalar, the subroutine will always yield that scalar. If C<sv> points 12181 to an array, the subroutine will always yield a list of the elements of 12182 that array in list context, or the number of elements in the array in 12183 scalar context. This function takes ownership of one counted reference 12184 to the scalar or array, and will arrange for the object to live as long 12185 as the subroutine does. If C<sv> points to a scalar then the inlining 12186 assumes that the value of the scalar will never change, so the caller 12187 must ensure that the scalar is not subsequently written to. If C<sv> 12188 points to an array then no such assumption is made, so it is ostensibly 12189 safe to mutate the array or its elements, but whether this is really 12190 supported has not been determined. 12191 12192 The subroutine will have C<CvFILE> set according to C<PL_curcop>. 12193 Other aspects of the subroutine will be left in their default state. 12194 The caller is free to mutate the subroutine beyond its initial state 12195 after this function has returned. 12196 12197 If C<name> is null then the subroutine will be anonymous, with its 12198 C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the 12199 subroutine will be named accordingly, referenced by the appropriate glob. 12200 C<name> is a string of length C<len> bytes giving a sigilless symbol 12201 name, in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1 12202 otherwise. The name may be either qualified or unqualified. If the 12203 name is unqualified then it defaults to being in the stash specified by 12204 C<stash> if that is non-null, or to C<PL_curstash> if C<stash> is null. 12205 The symbol is always added to the stash if necessary, with C<GV_ADDMULTI> 12206 semantics. 12207 12208 C<flags> should not have bits set other than C<SVf_UTF8>. 12209 12210 If there is already a subroutine of the specified name, then the new sub 12211 will replace the existing one in the glob. A warning may be generated 12212 about the redefinition. 12213 12214 If the subroutine has one of a few special names, such as C<BEGIN> or 12215 C<END>, then it will be claimed by the appropriate queue for automatic 12216 running of phase-related subroutines. In this case the relevant glob will 12217 be left not containing any subroutine, even if it did contain one before. 12218 Execution of the subroutine will likely be a no-op, unless C<sv> was 12219 a tied array or the caller modified the subroutine in some interesting 12220 way before it was executed. In the case of C<BEGIN>, the treatment is 12221 buggy: the sub will be executed when only half built, and may be deleted 12222 prematurely, possibly causing a crash. 12223 12224 The function returns a pointer to the constructed subroutine. If the sub 12225 is anonymous then ownership of one counted reference to the subroutine 12226 is transferred to the caller. If the sub is named then the caller does 12227 not get ownership of a reference. In most such cases, where the sub 12228 has a non-phase name, the sub will be alive at the point it is returned 12229 by virtue of being contained in the glob that names it. A phase-named 12230 subroutine will usually be alive by virtue of the reference owned by 12231 the phase's automatic run queue. A C<BEGIN> subroutine may have been 12232 destroyed already by the time this function returns, but currently bugs 12233 occur in that case before the caller gets control. It is the caller's 12234 responsibility to ensure that it knows which of these situations applies. 12235 12236 =cut 12237 */ 12238 12239 CV * 12240 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len, 12241 U32 flags, SV *sv) 12242 { 12243 CV* cv; 12244 const char *const file = CopFILE(PL_curcop); 12245 12246 ENTER; 12247 12248 if (IN_PERL_RUNTIME) { 12249 /* at runtime, it's not safe to manipulate PL_curcop: it may be 12250 * an op shared between threads. Use a non-shared COP for our 12251 * dirty work */ 12252 SAVEVPTR(PL_curcop); 12253 SAVECOMPILEWARNINGS(); 12254 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); 12255 PL_curcop = &PL_compiling; 12256 } 12257 SAVECOPLINE(PL_curcop); 12258 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE); 12259 12260 SAVEHINTS(); 12261 PL_hints &= ~HINT_BLOCK_SCOPE; 12262 12263 if (stash) { 12264 SAVEGENERICSV(PL_curstash); 12265 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash); 12266 } 12267 12268 /* Protect sv against leakage caused by fatal warnings. */ 12269 if (sv) SAVEFREESV(sv); 12270 12271 /* file becomes the CvFILE. For an XS, it's usually static storage, 12272 and so doesn't get free()d. (It's expected to be from the C pre- 12273 processor __FILE__ directive). But we need a dynamically allocated one, 12274 and we need it to get freed. */ 12275 cv = newXS_len_flags(name, len, 12276 sv && SvTYPE(sv) == SVt_PVAV 12277 ? const_av_xsub 12278 : const_sv_xsub, 12279 file ? file : "", "", 12280 &sv, XS_DYNAMIC_FILENAME | flags); 12281 assert(cv); 12282 assert(SvREFCNT((SV*)cv) != 0); 12283 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv); 12284 CvCONST_on(cv); 12285 12286 LEAVE; 12287 12288 return cv; 12289 } 12290 12291 /* 12292 =for apidoc newXS 12293 12294 Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be 12295 static storage, as it is used directly as CvFILE(), without a copy being made. 12296 12297 =cut 12298 */ 12299 12300 CV * 12301 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename) 12302 { 12303 PERL_ARGS_ASSERT_NEWXS; 12304 return newXS_len_flags( 12305 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0 12306 ); 12307 } 12308 12309 CV * 12310 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr, 12311 const char *const filename, const char *const proto, 12312 U32 flags) 12313 { 12314 PERL_ARGS_ASSERT_NEWXS_FLAGS; 12315 return newXS_len_flags( 12316 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags 12317 ); 12318 } 12319 12320 CV * 12321 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr) 12322 { 12323 PERL_ARGS_ASSERT_NEWXS_DEFFILE; 12324 return newXS_len_flags( 12325 name, strlen(name), subaddr, NULL, NULL, NULL, 0 12326 ); 12327 } 12328 12329 /* 12330 =for apidoc newXS_len_flags 12331 12332 Construct an XS subroutine, also performing some surrounding jobs. 12333 12334 The subroutine will have the entry point C<subaddr>. It will have 12335 the prototype specified by the nul-terminated string C<proto>, or 12336 no prototype if C<proto> is null. The prototype string is copied; 12337 the caller can mutate the supplied string afterwards. If C<filename> 12338 is non-null, it must be a nul-terminated filename, and the subroutine 12339 will have its C<CvFILE> set accordingly. By default C<CvFILE> is set to 12340 point directly to the supplied string, which must be static. If C<flags> 12341 has the C<XS_DYNAMIC_FILENAME> bit set, then a copy of the string will 12342 be taken instead. 12343 12344 Other aspects of the subroutine will be left in their default state. 12345 If anything else needs to be done to the subroutine for it to function 12346 correctly, it is the caller's responsibility to do that after this 12347 function has constructed it. However, beware of the subroutine 12348 potentially being destroyed before this function returns, as described 12349 below. 12350 12351 If C<name> is null then the subroutine will be anonymous, with its 12352 C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the 12353 subroutine will be named accordingly, referenced by the appropriate glob. 12354 C<name> is a string of length C<len> bytes giving a sigilless symbol name, 12355 in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1 otherwise. 12356 The name may be either qualified or unqualified, with the stash defaulting 12357 in the same manner as for C<gv_fetchpvn_flags>. C<flags> may contain 12358 flag bits understood by C<gv_fetchpvn_flags> with the same meaning as 12359 they have there, such as C<GV_ADDWARN>. The symbol is always added to 12360 the stash if necessary, with C<GV_ADDMULTI> semantics. 12361 12362 If there is already a subroutine of the specified name, then the new sub 12363 will replace the existing one in the glob. A warning may be generated 12364 about the redefinition. If the old subroutine was C<CvCONST> then the 12365 decision about whether to warn is influenced by an expectation about 12366 whether the new subroutine will become a constant of similar value. 12367 That expectation is determined by C<const_svp>. (Note that the call to 12368 this function doesn't make the new subroutine C<CvCONST> in any case; 12369 that is left to the caller.) If C<const_svp> is null then it indicates 12370 that the new subroutine will not become a constant. If C<const_svp> 12371 is non-null then it indicates that the new subroutine will become a 12372 constant, and it points to an C<SV*> that provides the constant value 12373 that the subroutine will have. 12374 12375 If the subroutine has one of a few special names, such as C<BEGIN> or 12376 C<END>, then it will be claimed by the appropriate queue for automatic 12377 running of phase-related subroutines. In this case the relevant glob will 12378 be left not containing any subroutine, even if it did contain one before. 12379 In the case of C<BEGIN>, the subroutine will be executed and the reference 12380 to it disposed of before this function returns, and also before its 12381 prototype is set. If a C<BEGIN> subroutine would not be sufficiently 12382 constructed by this function to be ready for execution then the caller 12383 must prevent this happening by giving the subroutine a different name. 12384 12385 The function returns a pointer to the constructed subroutine. If the sub 12386 is anonymous then ownership of one counted reference to the subroutine 12387 is transferred to the caller. If the sub is named then the caller does 12388 not get ownership of a reference. In most such cases, where the sub 12389 has a non-phase name, the sub will be alive at the point it is returned 12390 by virtue of being contained in the glob that names it. A phase-named 12391 subroutine will usually be alive by virtue of the reference owned by the 12392 phase's automatic run queue. But a C<BEGIN> subroutine, having already 12393 been executed, will quite likely have been destroyed already by the 12394 time this function returns, making it erroneous for the caller to make 12395 any use of the returned pointer. It is the caller's responsibility to 12396 ensure that it knows which of these situations applies. 12397 12398 =cut 12399 */ 12400 12401 CV * 12402 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len, 12403 XSUBADDR_t subaddr, const char *const filename, 12404 const char *const proto, SV **const_svp, 12405 U32 flags) 12406 { 12407 CV *cv; 12408 bool interleave = FALSE; 12409 bool evanescent = FALSE; 12410 12411 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS; 12412 12413 { 12414 GV * const gv = gv_fetchpvn( 12415 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__", 12416 name ? len : PL_curstash ? sizeof("__ANON__") - 1: 12417 sizeof("__ANON__::__ANON__") - 1, 12418 GV_ADDMULTI | flags, SVt_PVCV); 12419 12420 if ((cv = (name ? GvCV(gv) : NULL))) { 12421 if (GvCVGEN(gv)) { 12422 /* just a cached method */ 12423 SvREFCNT_dec(cv); 12424 cv = NULL; 12425 } 12426 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { 12427 /* already defined (or promised) */ 12428 /* Redundant check that allows us to avoid creating an SV 12429 most of the time: */ 12430 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) { 12431 report_redefined_cv(newSVpvn_flags( 12432 name,len,(flags&SVf_UTF8)|SVs_TEMP 12433 ), 12434 cv, const_svp); 12435 } 12436 interleave = TRUE; 12437 ENTER; 12438 SAVEFREESV(cv); 12439 cv = NULL; 12440 } 12441 } 12442 12443 if (cv) /* must reuse cv if autoloaded */ 12444 cv_undef(cv); 12445 else { 12446 cv = MUTABLE_CV(newSV_type(SVt_PVCV)); 12447 if (name) { 12448 GvCV_set(gv,cv); 12449 GvCVGEN(gv) = 0; 12450 if (HvENAME_HEK(GvSTASH(gv))) 12451 gv_method_changed(gv); /* newXS */ 12452 } 12453 } 12454 assert(cv); 12455 assert(SvREFCNT((SV*)cv) != 0); 12456 12457 CvGV_set(cv, gv); 12458 if(filename) { 12459 /* XSUBs can't be perl lang/perl5db.pl debugged 12460 if (PERLDB_LINE_OR_SAVESRC) 12461 (void)gv_fetchfile(filename); */ 12462 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */ 12463 if (flags & XS_DYNAMIC_FILENAME) { 12464 CvDYNFILE_on(cv); 12465 CvFILE(cv) = savepv(filename); 12466 } else { 12467 /* NOTE: not copied, as it is expected to be an external constant string */ 12468 CvFILE(cv) = (char *)filename; 12469 } 12470 } else { 12471 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename); 12472 CvFILE(cv) = (char*)PL_xsubfilename; 12473 } 12474 CvISXSUB_on(cv); 12475 CvXSUB(cv) = subaddr; 12476 #ifndef MULTIPLICITY 12477 CvHSCXT(cv) = &PL_stack_sp; 12478 #else 12479 PoisonPADLIST(cv); 12480 #endif 12481 12482 if (name) 12483 evanescent = process_special_blocks(0, name, gv, cv); 12484 else 12485 CvANON_on(cv); 12486 } /* <- not a conditional branch */ 12487 12488 assert(cv); 12489 assert(evanescent || SvREFCNT((SV*)cv) != 0); 12490 12491 if (!evanescent) sv_setpv(MUTABLE_SV(cv), proto); 12492 if (interleave) LEAVE; 12493 assert(evanescent || SvREFCNT((SV*)cv) != 0); 12494 return cv; 12495 } 12496 12497 /* Add a stub CV to a typeglob. 12498 * This is the implementation of a forward declaration, 'sub foo';' 12499 */ 12500 12501 CV * 12502 Perl_newSTUB(pTHX_ GV *gv, bool fake) 12503 { 12504 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV)); 12505 GV *cvgv; 12506 PERL_ARGS_ASSERT_NEWSTUB; 12507 assert(!GvCVu(gv)); 12508 GvCV_set(gv, cv); 12509 GvCVGEN(gv) = 0; 12510 if (!fake && GvSTASH(gv) && HvENAME_HEK(GvSTASH(gv))) 12511 gv_method_changed(gv); 12512 if (SvFAKE(gv)) { 12513 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV); 12514 SvFAKE_off(cvgv); 12515 } 12516 else cvgv = gv; 12517 CvGV_set(cv, cvgv); 12518 CvFILE_set_from_cop(cv, PL_curcop); 12519 CvSTASH_set(cv, PL_curstash); 12520 GvMULTI_on(gv); 12521 return cv; 12522 } 12523 12524 void 12525 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block) 12526 { 12527 CV *cv; 12528 GV *gv; 12529 OP *root; 12530 OP *start; 12531 12532 if (PL_parser && PL_parser->error_count) { 12533 op_free(block); 12534 goto finish; 12535 } 12536 12537 gv = o 12538 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM) 12539 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM); 12540 12541 GvMULTI_on(gv); 12542 if ((cv = GvFORM(gv))) { 12543 if (ckWARN(WARN_REDEFINE)) { 12544 const line_t oldline = CopLINE(PL_curcop); 12545 if (PL_parser && PL_parser->copline != NOLINE) 12546 CopLINE_set(PL_curcop, PL_parser->copline); 12547 if (o) { 12548 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 12549 "Format %" SVf " redefined", SVfARG(cSVOPo->op_sv)); 12550 } else { 12551 /* diag_listed_as: Format %s redefined */ 12552 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 12553 "Format STDOUT redefined"); 12554 } 12555 CopLINE_set(PL_curcop, oldline); 12556 } 12557 SvREFCNT_dec(cv); 12558 } 12559 cv = PL_compcv; 12560 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv); 12561 CvGV_set(cv, gv); 12562 CvFILE_set_from_cop(cv, PL_curcop); 12563 12564 12565 root = newUNOP(OP_LEAVEWRITE, 0, voidnonfinal(block)); 12566 CvROOT(cv) = root; 12567 start = LINKLIST(root); 12568 root->op_next = 0; 12569 S_process_optree(aTHX_ cv, root, start); 12570 cv_forget_slab(cv); 12571 12572 finish: 12573 op_free(o); 12574 if (PL_parser) 12575 PL_parser->copline = NOLINE; 12576 LEAVE_SCOPE(floor); 12577 PL_compiling.cop_seq = 0; 12578 } 12579 12580 OP * 12581 Perl_newANONLIST(pTHX_ OP *o) 12582 { 12583 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o); 12584 } 12585 12586 OP * 12587 Perl_newANONHASH(pTHX_ OP *o) 12588 { 12589 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o); 12590 } 12591 12592 OP * 12593 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block) 12594 { 12595 return newANONATTRSUB(floor, proto, NULL, block); 12596 } 12597 12598 OP * 12599 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block) 12600 { 12601 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block)); 12602 OP * anoncode = 12603 newSVOP(OP_ANONCODE, 0, 12604 cv); 12605 if (CvANONCONST(cv)) 12606 anoncode = newUNOP(OP_ANONCONST, 0, 12607 op_convert_list(OP_ENTERSUB, 12608 OPf_STACKED|OPf_WANT_SCALAR, 12609 anoncode)); 12610 return newUNOP(OP_REFGEN, 0, anoncode); 12611 } 12612 12613 OP * 12614 Perl_oopsAV(pTHX_ OP *o) 12615 { 12616 12617 PERL_ARGS_ASSERT_OOPSAV; 12618 12619 switch (o->op_type) { 12620 case OP_PADSV: 12621 case OP_PADHV: 12622 OpTYPE_set(o, OP_PADAV); 12623 return ref(o, OP_RV2AV); 12624 12625 case OP_RV2SV: 12626 case OP_RV2HV: 12627 OpTYPE_set(o, OP_RV2AV); 12628 ref(o, OP_RV2AV); 12629 break; 12630 12631 default: 12632 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV"); 12633 break; 12634 } 12635 return o; 12636 } 12637 12638 OP * 12639 Perl_oopsHV(pTHX_ OP *o) 12640 { 12641 12642 PERL_ARGS_ASSERT_OOPSHV; 12643 12644 switch (o->op_type) { 12645 case OP_PADSV: 12646 case OP_PADAV: 12647 OpTYPE_set(o, OP_PADHV); 12648 return ref(o, OP_RV2HV); 12649 12650 case OP_RV2SV: 12651 case OP_RV2AV: 12652 OpTYPE_set(o, OP_RV2HV); 12653 /* rv2hv steals the bottom bit for its own uses */ 12654 o->op_private &= ~OPpARG1_MASK; 12655 ref(o, OP_RV2HV); 12656 break; 12657 12658 default: 12659 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV"); 12660 break; 12661 } 12662 return o; 12663 } 12664 12665 OP * 12666 Perl_newAVREF(pTHX_ OP *o) 12667 { 12668 12669 PERL_ARGS_ASSERT_NEWAVREF; 12670 12671 if (o->op_type == OP_PADANY) { 12672 OpTYPE_set(o, OP_PADAV); 12673 return o; 12674 } 12675 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) { 12676 Perl_croak(aTHX_ "Can't use an array as a reference"); 12677 } 12678 return newUNOP(OP_RV2AV, 0, scalar(o)); 12679 } 12680 12681 OP * 12682 Perl_newGVREF(pTHX_ I32 type, OP *o) 12683 { 12684 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT) 12685 return newUNOP(OP_NULL, 0, o); 12686 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type); 12687 } 12688 12689 OP * 12690 Perl_newHVREF(pTHX_ OP *o) 12691 { 12692 12693 PERL_ARGS_ASSERT_NEWHVREF; 12694 12695 if (o->op_type == OP_PADANY) { 12696 OpTYPE_set(o, OP_PADHV); 12697 return o; 12698 } 12699 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) { 12700 Perl_croak(aTHX_ "Can't use a hash as a reference"); 12701 } 12702 return newUNOP(OP_RV2HV, 0, scalar(o)); 12703 } 12704 12705 OP * 12706 Perl_newCVREF(pTHX_ I32 flags, OP *o) 12707 { 12708 if (o->op_type == OP_PADANY) { 12709 OpTYPE_set(o, OP_PADCV); 12710 } 12711 return newUNOP(OP_RV2CV, flags, scalar(o)); 12712 } 12713 12714 OP * 12715 Perl_newSVREF(pTHX_ OP *o) 12716 { 12717 12718 PERL_ARGS_ASSERT_NEWSVREF; 12719 12720 if (o->op_type == OP_PADANY) { 12721 OpTYPE_set(o, OP_PADSV); 12722 scalar(o); 12723 return o; 12724 } 12725 return newUNOP(OP_RV2SV, 0, scalar(o)); 12726 } 12727 12728 /* Check routines. See the comments at the top of this file for details 12729 * on when these are called */ 12730 12731 OP * 12732 Perl_ck_anoncode(pTHX_ OP *o) 12733 { 12734 PERL_ARGS_ASSERT_CK_ANONCODE; 12735 12736 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type); 12737 cSVOPo->op_sv = NULL; 12738 return o; 12739 } 12740 12741 static void 12742 S_io_hints(pTHX_ OP *o) 12743 { 12744 #if O_BINARY != 0 || O_TEXT != 0 12745 HV * const table = 12746 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;; 12747 if (table) { 12748 SV **svp = hv_fetchs(table, "open_IN", FALSE); 12749 if (svp && *svp) { 12750 STRLEN len = 0; 12751 const char *d = SvPV_const(*svp, len); 12752 const I32 mode = mode_from_discipline(d, len); 12753 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */ 12754 # if O_BINARY != 0 12755 if (mode & O_BINARY) 12756 o->op_private |= OPpOPEN_IN_RAW; 12757 # endif 12758 # if O_TEXT != 0 12759 if (mode & O_TEXT) 12760 o->op_private |= OPpOPEN_IN_CRLF; 12761 # endif 12762 } 12763 12764 svp = hv_fetchs(table, "open_OUT", FALSE); 12765 if (svp && *svp) { 12766 STRLEN len = 0; 12767 const char *d = SvPV_const(*svp, len); 12768 const I32 mode = mode_from_discipline(d, len); 12769 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */ 12770 # if O_BINARY != 0 12771 if (mode & O_BINARY) 12772 o->op_private |= OPpOPEN_OUT_RAW; 12773 # endif 12774 # if O_TEXT != 0 12775 if (mode & O_TEXT) 12776 o->op_private |= OPpOPEN_OUT_CRLF; 12777 # endif 12778 } 12779 } 12780 #else 12781 PERL_UNUSED_CONTEXT; 12782 PERL_UNUSED_ARG(o); 12783 #endif 12784 } 12785 12786 OP * 12787 Perl_ck_backtick(pTHX_ OP *o) 12788 { 12789 GV *gv; 12790 OP *newop = NULL; 12791 OP *sibl; 12792 PERL_ARGS_ASSERT_CK_BACKTICK; 12793 o = ck_fun(o); 12794 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */ 12795 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first)) 12796 && (gv = gv_override("readpipe",8))) 12797 { 12798 /* detach rest of siblings from o and its first child */ 12799 op_sibling_splice(o, cUNOPo->op_first, -1, NULL); 12800 newop = S_new_entersubop(aTHX_ gv, sibl); 12801 } 12802 else if (!(o->op_flags & OPf_KIDS)) 12803 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP()); 12804 if (newop) { 12805 op_free(o); 12806 return newop; 12807 } 12808 S_io_hints(aTHX_ o); 12809 return o; 12810 } 12811 12812 OP * 12813 Perl_ck_bitop(pTHX_ OP *o) 12814 { 12815 PERL_ARGS_ASSERT_CK_BITOP; 12816 12817 /* get rid of arg count and indicate if in the scope of 'use integer' */ 12818 o->op_private = (PL_hints & HINT_INTEGER) ? OPpUSEINT : 0; 12819 12820 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */ 12821 && OP_IS_INFIX_BIT(o->op_type)) 12822 { 12823 const OP * const left = cBINOPo->op_first; 12824 const OP * const right = OpSIBLING(left); 12825 if ((OP_IS_NUMCOMPARE(left->op_type) && 12826 (left->op_flags & OPf_PARENS) == 0) || 12827 (OP_IS_NUMCOMPARE(right->op_type) && 12828 (right->op_flags & OPf_PARENS) == 0)) 12829 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE), 12830 "Possible precedence problem on bitwise %s operator", 12831 o->op_type == OP_BIT_OR 12832 ||o->op_type == OP_NBIT_OR ? "|" 12833 : o->op_type == OP_BIT_AND 12834 ||o->op_type == OP_NBIT_AND ? "&" 12835 : o->op_type == OP_BIT_XOR 12836 ||o->op_type == OP_NBIT_XOR ? "^" 12837 : o->op_type == OP_SBIT_OR ? "|." 12838 : o->op_type == OP_SBIT_AND ? "&." : "^." 12839 ); 12840 } 12841 return o; 12842 } 12843 12844 PERL_STATIC_INLINE bool 12845 is_dollar_bracket(pTHX_ const OP * const o) 12846 { 12847 const OP *kid; 12848 PERL_UNUSED_CONTEXT; 12849 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS 12850 && (kid = cUNOPx(o)->op_first) 12851 && kid->op_type == OP_GV 12852 && strEQ(GvNAME(cGVOPx_gv(kid)), "["); 12853 } 12854 12855 /* for lt, gt, le, ge, eq, ne and their i_ variants */ 12856 12857 OP * 12858 Perl_ck_cmp(pTHX_ OP *o) 12859 { 12860 bool is_eq; 12861 bool neg; 12862 bool reverse; 12863 bool iv0; 12864 OP *indexop, *constop, *start; 12865 SV *sv; 12866 IV iv; 12867 12868 PERL_ARGS_ASSERT_CK_CMP; 12869 12870 is_eq = ( o->op_type == OP_EQ 12871 || o->op_type == OP_NE 12872 || o->op_type == OP_I_EQ 12873 || o->op_type == OP_I_NE); 12874 12875 if (!is_eq && ckWARN(WARN_SYNTAX)) { 12876 const OP *kid = cUNOPo->op_first; 12877 if (kid && 12878 ( 12879 ( is_dollar_bracket(aTHX_ kid) 12880 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST 12881 ) 12882 || ( kid->op_type == OP_CONST 12883 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid) 12884 ) 12885 ) 12886 ) 12887 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 12888 "$[ used in %s (did you mean $] ?)", OP_DESC(o)); 12889 } 12890 12891 /* convert (index(...) == -1) and variations into 12892 * (r)index/BOOL(,NEG) 12893 */ 12894 12895 reverse = FALSE; 12896 12897 indexop = cUNOPo->op_first; 12898 constop = OpSIBLING(indexop); 12899 start = NULL; 12900 if (indexop->op_type == OP_CONST) { 12901 constop = indexop; 12902 indexop = OpSIBLING(constop); 12903 start = constop; 12904 reverse = TRUE; 12905 } 12906 12907 if (indexop->op_type != OP_INDEX && indexop->op_type != OP_RINDEX) 12908 return o; 12909 12910 /* ($lex = index(....)) == -1 */ 12911 if (indexop->op_private & OPpTARGET_MY) 12912 return o; 12913 12914 if (constop->op_type != OP_CONST) 12915 return o; 12916 12917 sv = cSVOPx_sv(constop); 12918 if (!(sv && SvIOK_notUV(sv))) 12919 return o; 12920 12921 iv = SvIVX(sv); 12922 if (iv != -1 && iv != 0) 12923 return o; 12924 iv0 = (iv == 0); 12925 12926 if (o->op_type == OP_LT || o->op_type == OP_I_LT) { 12927 if (!(iv0 ^ reverse)) 12928 return o; 12929 neg = iv0; 12930 } 12931 else if (o->op_type == OP_LE || o->op_type == OP_I_LE) { 12932 if (iv0 ^ reverse) 12933 return o; 12934 neg = !iv0; 12935 } 12936 else if (o->op_type == OP_GE || o->op_type == OP_I_GE) { 12937 if (!(iv0 ^ reverse)) 12938 return o; 12939 neg = !iv0; 12940 } 12941 else if (o->op_type == OP_GT || o->op_type == OP_I_GT) { 12942 if (iv0 ^ reverse) 12943 return o; 12944 neg = iv0; 12945 } 12946 else if (o->op_type == OP_EQ || o->op_type == OP_I_EQ) { 12947 if (iv0) 12948 return o; 12949 neg = TRUE; 12950 } 12951 else { 12952 assert(o->op_type == OP_NE || o->op_type == OP_I_NE); 12953 if (iv0) 12954 return o; 12955 neg = FALSE; 12956 } 12957 12958 indexop->op_flags &= ~OPf_PARENS; 12959 indexop->op_flags |= (o->op_flags & OPf_PARENS); 12960 indexop->op_private |= OPpTRUEBOOL; 12961 if (neg) 12962 indexop->op_private |= OPpINDEX_BOOLNEG; 12963 /* cut out the index op and free the eq,const ops */ 12964 (void)op_sibling_splice(o, start, 1, NULL); 12965 op_free(o); 12966 12967 return indexop; 12968 } 12969 12970 12971 OP * 12972 Perl_ck_concat(pTHX_ OP *o) 12973 { 12974 const OP * const kid = cUNOPo->op_first; 12975 12976 PERL_ARGS_ASSERT_CK_CONCAT; 12977 PERL_UNUSED_CONTEXT; 12978 12979 /* reuse the padtmp returned by the concat child */ 12980 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) && 12981 !(kUNOP->op_first->op_flags & OPf_MOD)) 12982 { 12983 o->op_flags |= OPf_STACKED; 12984 o->op_private |= OPpCONCAT_NESTED; 12985 } 12986 return o; 12987 } 12988 12989 OP * 12990 Perl_ck_spair(pTHX_ OP *o) 12991 { 12992 12993 PERL_ARGS_ASSERT_CK_SPAIR; 12994 12995 if (o->op_flags & OPf_KIDS) { 12996 OP* newop; 12997 OP* kid; 12998 OP* kidkid; 12999 const OPCODE type = o->op_type; 13000 o = modkids(ck_fun(o), type); 13001 kid = cUNOPo->op_first; 13002 kidkid = kUNOP->op_first; 13003 newop = OpSIBLING(kidkid); 13004 if (newop) { 13005 const OPCODE type = newop->op_type; 13006 if (OpHAS_SIBLING(newop)) 13007 return o; 13008 if (o->op_type == OP_REFGEN 13009 && ( type == OP_RV2CV 13010 || ( !(newop->op_flags & OPf_PARENS) 13011 && ( type == OP_RV2AV || type == OP_PADAV 13012 || type == OP_RV2HV || type == OP_PADHV)))) 13013 NOOP; /* OK (allow srefgen for \@a and \%h) */ 13014 else if (OP_GIMME(newop,0) != G_SCALAR) 13015 return o; 13016 } 13017 /* excise first sibling */ 13018 op_sibling_splice(kid, NULL, 1, NULL); 13019 op_free(kidkid); 13020 } 13021 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP, 13022 * and OP_CHOMP into OP_SCHOMP */ 13023 o->op_ppaddr = PL_ppaddr[++o->op_type]; 13024 return ck_fun(o); 13025 } 13026 13027 OP * 13028 Perl_ck_delete(pTHX_ OP *o) 13029 { 13030 PERL_ARGS_ASSERT_CK_DELETE; 13031 13032 o = ck_fun(o); 13033 o->op_private = 0; 13034 if (o->op_flags & OPf_KIDS) { 13035 OP * const kid = cUNOPo->op_first; 13036 switch (kid->op_type) { 13037 case OP_ASLICE: 13038 o->op_flags |= OPf_SPECIAL; 13039 /* FALLTHROUGH */ 13040 case OP_HSLICE: 13041 o->op_private |= OPpSLICE; 13042 break; 13043 case OP_AELEM: 13044 o->op_flags |= OPf_SPECIAL; 13045 /* FALLTHROUGH */ 13046 case OP_HELEM: 13047 break; 13048 case OP_KVASLICE: 13049 o->op_flags |= OPf_SPECIAL; 13050 /* FALLTHROUGH */ 13051 case OP_KVHSLICE: 13052 o->op_private |= OPpKVSLICE; 13053 break; 13054 default: 13055 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY " 13056 "element or slice"); 13057 } 13058 if (kid->op_private & OPpLVAL_INTRO) 13059 o->op_private |= OPpLVAL_INTRO; 13060 op_null(kid); 13061 } 13062 return o; 13063 } 13064 13065 OP * 13066 Perl_ck_eof(pTHX_ OP *o) 13067 { 13068 PERL_ARGS_ASSERT_CK_EOF; 13069 13070 if (o->op_flags & OPf_KIDS) { 13071 OP *kid; 13072 if (cLISTOPo->op_first->op_type == OP_STUB) { 13073 OP * const newop 13074 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv)); 13075 op_free(o); 13076 o = newop; 13077 } 13078 o = ck_fun(o); 13079 kid = cLISTOPo->op_first; 13080 if (kid->op_type == OP_RV2GV) 13081 kid->op_private |= OPpALLOW_FAKE; 13082 } 13083 return o; 13084 } 13085 13086 13087 OP * 13088 Perl_ck_eval(pTHX_ OP *o) 13089 { 13090 13091 PERL_ARGS_ASSERT_CK_EVAL; 13092 13093 PL_hints |= HINT_BLOCK_SCOPE; 13094 if (o->op_flags & OPf_KIDS) { 13095 SVOP * const kid = (SVOP*)cUNOPo->op_first; 13096 assert(kid); 13097 13098 if (o->op_type == OP_ENTERTRY) { 13099 LOGOP *enter; 13100 13101 /* cut whole sibling chain free from o */ 13102 op_sibling_splice(o, NULL, -1, NULL); 13103 op_free(o); 13104 13105 enter = alloc_LOGOP(OP_ENTERTRY, NULL, NULL); 13106 13107 /* establish postfix order */ 13108 enter->op_next = (OP*)enter; 13109 13110 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid); 13111 OpTYPE_set(o, OP_LEAVETRY); 13112 enter->op_other = o; 13113 return o; 13114 } 13115 else { 13116 scalar((OP*)kid); 13117 S_set_haseval(aTHX); 13118 } 13119 } 13120 else { 13121 const U8 priv = o->op_private; 13122 op_free(o); 13123 /* the newUNOP will recursively call ck_eval(), which will handle 13124 * all the stuff at the end of this function, like adding 13125 * OP_HINTSEVAL 13126 */ 13127 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP()); 13128 } 13129 o->op_targ = (PADOFFSET)PL_hints; 13130 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8; 13131 if ((PL_hints & HINT_LOCALIZE_HH) != 0 13132 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) { 13133 /* Store a copy of %^H that pp_entereval can pick up. */ 13134 HV *hh = hv_copy_hints_hv(GvHV(PL_hintgv)); 13135 OP *hhop; 13136 STOREFEATUREBITSHH(hh); 13137 hhop = newSVOP(OP_HINTSEVAL, 0, MUTABLE_SV(hh)); 13138 /* append hhop to only child */ 13139 op_sibling_splice(o, cUNOPo->op_first, 0, hhop); 13140 13141 o->op_private |= OPpEVAL_HAS_HH; 13142 } 13143 if (!(o->op_private & OPpEVAL_BYTES) 13144 && FEATURE_UNIEVAL_IS_ENABLED) 13145 o->op_private |= OPpEVAL_UNICODE; 13146 return o; 13147 } 13148 13149 OP * 13150 Perl_ck_trycatch(pTHX_ OP *o) 13151 { 13152 LOGOP *enter; 13153 OP *to_free = NULL; 13154 OP *trykid, *catchkid; 13155 OP *catchroot, *catchstart; 13156 13157 PERL_ARGS_ASSERT_CK_TRYCATCH; 13158 13159 trykid = cUNOPo->op_first; 13160 if(trykid->op_type == OP_NULL || trykid->op_type == OP_PUSHMARK) { 13161 to_free = trykid; 13162 trykid = OpSIBLING(trykid); 13163 } 13164 catchkid = OpSIBLING(trykid); 13165 13166 assert(trykid->op_type == OP_POPTRY); 13167 assert(catchkid->op_type == OP_CATCH); 13168 13169 /* cut whole sibling chain free from o */ 13170 op_sibling_splice(o, NULL, -1, NULL); 13171 if(to_free) 13172 op_free(to_free); 13173 op_free(o); 13174 13175 enter = alloc_LOGOP(OP_ENTERTRYCATCH, NULL, NULL); 13176 13177 /* establish postfix order */ 13178 enter->op_next = (OP*)enter; 13179 13180 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, trykid); 13181 op_append_elem(OP_LINESEQ, (OP*)o, catchkid); 13182 13183 OpTYPE_set(o, OP_LEAVETRYCATCH); 13184 13185 /* The returned optree is actually threaded up slightly nonobviously in 13186 * terms of its ->op_next pointers. 13187 * 13188 * This way, if the tryblock dies, its retop points at the OP_CATCH, but 13189 * if it does not then its leavetry skips over that and continues 13190 * execution past it. 13191 */ 13192 13193 /* First, link up the actual body of the catch block */ 13194 catchroot = OpSIBLING(cUNOPx(catchkid)->op_first); 13195 catchstart = LINKLIST(catchroot); 13196 cLOGOPx(catchkid)->op_other = catchstart; 13197 13198 o->op_next = LINKLIST(o); 13199 13200 /* die within try block should jump to the catch */ 13201 enter->op_other = catchkid; 13202 13203 /* after try block that doesn't die, just skip straight to leavetrycatch */ 13204 trykid->op_next = o; 13205 13206 /* after catch block, skip back up to the leavetrycatch */ 13207 catchroot->op_next = o; 13208 13209 return o; 13210 } 13211 13212 OP * 13213 Perl_ck_exec(pTHX_ OP *o) 13214 { 13215 PERL_ARGS_ASSERT_CK_EXEC; 13216 13217 if (o->op_flags & OPf_STACKED) { 13218 OP *kid; 13219 o = ck_fun(o); 13220 kid = OpSIBLING(cUNOPo->op_first); 13221 if (kid->op_type == OP_RV2GV) 13222 op_null(kid); 13223 } 13224 else 13225 o = listkids(o); 13226 return o; 13227 } 13228 13229 OP * 13230 Perl_ck_exists(pTHX_ OP *o) 13231 { 13232 PERL_ARGS_ASSERT_CK_EXISTS; 13233 13234 o = ck_fun(o); 13235 if (o->op_flags & OPf_KIDS) { 13236 OP * const kid = cUNOPo->op_first; 13237 if (kid->op_type == OP_ENTERSUB) { 13238 (void) ref(kid, o->op_type); 13239 if (kid->op_type != OP_RV2CV 13240 && !(PL_parser && PL_parser->error_count)) 13241 Perl_croak(aTHX_ 13242 "exists argument is not a subroutine name"); 13243 o->op_private |= OPpEXISTS_SUB; 13244 } 13245 else if (kid->op_type == OP_AELEM) 13246 o->op_flags |= OPf_SPECIAL; 13247 else if (kid->op_type != OP_HELEM) 13248 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY " 13249 "element or a subroutine"); 13250 op_null(kid); 13251 } 13252 return o; 13253 } 13254 13255 OP * 13256 Perl_ck_rvconst(pTHX_ OP *o) 13257 { 13258 SVOP * const kid = (SVOP*)cUNOPo->op_first; 13259 13260 PERL_ARGS_ASSERT_CK_RVCONST; 13261 13262 if (o->op_type == OP_RV2HV) 13263 /* rv2hv steals the bottom bit for its own uses */ 13264 o->op_private &= ~OPpARG1_MASK; 13265 13266 o->op_private |= (PL_hints & HINT_STRICT_REFS); 13267 13268 if (kid->op_type == OP_CONST) { 13269 int iscv; 13270 GV *gv; 13271 SV * const kidsv = kid->op_sv; 13272 13273 /* Is it a constant from cv_const_sv()? */ 13274 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) { 13275 return o; 13276 } 13277 if (SvTYPE(kidsv) == SVt_PVAV) return o; 13278 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) { 13279 const char *badthing; 13280 switch (o->op_type) { 13281 case OP_RV2SV: 13282 badthing = "a SCALAR"; 13283 break; 13284 case OP_RV2AV: 13285 badthing = "an ARRAY"; 13286 break; 13287 case OP_RV2HV: 13288 badthing = "a HASH"; 13289 break; 13290 default: 13291 badthing = NULL; 13292 break; 13293 } 13294 if (badthing) 13295 Perl_croak(aTHX_ 13296 "Can't use bareword (\"%" SVf "\") as %s ref while \"strict refs\" in use", 13297 SVfARG(kidsv), badthing); 13298 } 13299 /* 13300 * This is a little tricky. We only want to add the symbol if we 13301 * didn't add it in the lexer. Otherwise we get duplicate strict 13302 * warnings. But if we didn't add it in the lexer, we must at 13303 * least pretend like we wanted to add it even if it existed before, 13304 * or we get possible typo warnings. OPpCONST_ENTERED says 13305 * whether the lexer already added THIS instance of this symbol. 13306 */ 13307 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0; 13308 gv = gv_fetchsv(kidsv, 13309 o->op_type == OP_RV2CV 13310 && o->op_private & OPpMAY_RETURN_CONSTANT 13311 ? GV_NOEXPAND 13312 : iscv | !(kid->op_private & OPpCONST_ENTERED), 13313 iscv 13314 ? SVt_PVCV 13315 : o->op_type == OP_RV2SV 13316 ? SVt_PV 13317 : o->op_type == OP_RV2AV 13318 ? SVt_PVAV 13319 : o->op_type == OP_RV2HV 13320 ? SVt_PVHV 13321 : SVt_PVGV); 13322 if (gv) { 13323 if (!isGV(gv)) { 13324 assert(iscv); 13325 assert(SvROK(gv)); 13326 if (!(o->op_private & OPpMAY_RETURN_CONSTANT) 13327 && SvTYPE(SvRV(gv)) != SVt_PVCV) 13328 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV); 13329 } 13330 OpTYPE_set(kid, OP_GV); 13331 SvREFCNT_dec(kid->op_sv); 13332 #ifdef USE_ITHREADS 13333 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */ 13334 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP)); 13335 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY); 13336 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix)); 13337 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv))); 13338 #else 13339 kid->op_sv = SvREFCNT_inc_simple_NN(gv); 13340 #endif 13341 kid->op_private = 0; 13342 /* FAKE globs in the symbol table cause weird bugs (#77810) */ 13343 SvFAKE_off(gv); 13344 } 13345 } 13346 return o; 13347 } 13348 13349 OP * 13350 Perl_ck_ftst(pTHX_ OP *o) 13351 { 13352 const I32 type = o->op_type; 13353 13354 PERL_ARGS_ASSERT_CK_FTST; 13355 13356 if (o->op_flags & OPf_REF) { 13357 NOOP; 13358 } 13359 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) { 13360 SVOP * const kid = (SVOP*)cUNOPo->op_first; 13361 const OPCODE kidtype = kid->op_type; 13362 13363 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE) 13364 && !kid->op_folded) { 13365 OP * const newop = newGVOP(type, OPf_REF, 13366 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO)); 13367 op_free(o); 13368 return newop; 13369 } 13370 13371 if ((kidtype == OP_RV2AV || kidtype == OP_PADAV) && ckWARN(WARN_SYNTAX)) { 13372 SV *name = S_op_varname_subscript(aTHX_ (OP*)kid, 2); 13373 if (name) { 13374 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */ 13375 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s (did you want stat %" SVf "?)", 13376 array_passed_to_stat, name); 13377 } 13378 else { 13379 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */ 13380 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s", array_passed_to_stat); 13381 } 13382 } 13383 scalar((OP *) kid); 13384 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type)) 13385 o->op_private |= OPpFT_ACCESS; 13386 if (OP_IS_FILETEST(type) 13387 && OP_IS_FILETEST(kidtype) 13388 ) { 13389 o->op_private |= OPpFT_STACKED; 13390 kid->op_private |= OPpFT_STACKING; 13391 if (kidtype == OP_FTTTY && ( 13392 !(kid->op_private & OPpFT_STACKED) 13393 || kid->op_private & OPpFT_AFTER_t 13394 )) 13395 o->op_private |= OPpFT_AFTER_t; 13396 } 13397 } 13398 else { 13399 op_free(o); 13400 if (type == OP_FTTTY) 13401 o = newGVOP(type, OPf_REF, PL_stdingv); 13402 else 13403 o = newUNOP(type, 0, newDEFSVOP()); 13404 } 13405 return o; 13406 } 13407 13408 OP * 13409 Perl_ck_fun(pTHX_ OP *o) 13410 { 13411 const int type = o->op_type; 13412 I32 oa = PL_opargs[type] >> OASHIFT; 13413 13414 PERL_ARGS_ASSERT_CK_FUN; 13415 13416 if (o->op_flags & OPf_STACKED) { 13417 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL)) 13418 oa &= ~OA_OPTIONAL; 13419 else 13420 return no_fh_allowed(o); 13421 } 13422 13423 if (o->op_flags & OPf_KIDS) { 13424 OP *prev_kid = NULL; 13425 OP *kid = cLISTOPo->op_first; 13426 I32 numargs = 0; 13427 bool seen_optional = FALSE; 13428 13429 if (kid->op_type == OP_PUSHMARK || 13430 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)) 13431 { 13432 prev_kid = kid; 13433 kid = OpSIBLING(kid); 13434 } 13435 if (kid && kid->op_type == OP_COREARGS) { 13436 bool optional = FALSE; 13437 while (oa) { 13438 numargs++; 13439 if (oa & OA_OPTIONAL) optional = TRUE; 13440 oa = oa >> 4; 13441 } 13442 if (optional) o->op_private |= numargs; 13443 return o; 13444 } 13445 13446 while (oa) { 13447 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) { 13448 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) { 13449 kid = newDEFSVOP(); 13450 /* append kid to chain */ 13451 op_sibling_splice(o, prev_kid, 0, kid); 13452 } 13453 seen_optional = TRUE; 13454 } 13455 if (!kid) break; 13456 13457 numargs++; 13458 switch (oa & 7) { 13459 case OA_SCALAR: 13460 /* list seen where single (scalar) arg expected? */ 13461 if (numargs == 1 && !(oa >> 4) 13462 && kid->op_type == OP_LIST && type != OP_SCALAR) 13463 { 13464 return too_many_arguments_pv(o,PL_op_desc[type], 0); 13465 } 13466 if (type != OP_DELETE) scalar(kid); 13467 break; 13468 case OA_LIST: 13469 if (oa < 16) { 13470 kid = 0; 13471 continue; 13472 } 13473 else 13474 list(kid); 13475 break; 13476 case OA_AVREF: 13477 if ((type == OP_PUSH || type == OP_UNSHIFT) 13478 && !OpHAS_SIBLING(kid)) 13479 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), 13480 "Useless use of %s with no values", 13481 PL_op_desc[type]); 13482 13483 if (kid->op_type == OP_CONST 13484 && ( !SvROK(cSVOPx_sv(kid)) 13485 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV ) 13486 ) 13487 bad_type_pv(numargs, "array", o, kid); 13488 else if (kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV 13489 || kid->op_type == OP_RV2GV) { 13490 bad_type_pv(1, "array", o, kid); 13491 } 13492 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) { 13493 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden", 13494 PL_op_desc[type]), 0); 13495 } 13496 else { 13497 op_lvalue(kid, type); 13498 } 13499 break; 13500 case OA_HVREF: 13501 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV) 13502 bad_type_pv(numargs, "hash", o, kid); 13503 op_lvalue(kid, type); 13504 break; 13505 case OA_CVREF: 13506 { 13507 /* replace kid with newop in chain */ 13508 OP * const newop = 13509 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0); 13510 newop->op_next = newop; 13511 kid = newop; 13512 } 13513 break; 13514 case OA_FILEREF: 13515 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) { 13516 if (kid->op_type == OP_CONST && 13517 (kid->op_private & OPpCONST_BARE)) 13518 { 13519 OP * const newop = newGVOP(OP_GV, 0, 13520 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO)); 13521 /* a first argument is handled by toke.c, ideally we'd 13522 just check here but several ops don't use ck_fun() */ 13523 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED && numargs > 1) { 13524 no_bareword_filehandle(SvPVX(cSVOPx_sv((SVOP*)kid))); 13525 } 13526 /* replace kid with newop in chain */ 13527 op_sibling_splice(o, prev_kid, 1, newop); 13528 op_free(kid); 13529 kid = newop; 13530 } 13531 else if (kid->op_type == OP_READLINE) { 13532 /* neophyte patrol: open(<FH>), close(<FH>) etc. */ 13533 bad_type_pv(numargs, "HANDLE", o, kid); 13534 } 13535 else { 13536 I32 flags = OPf_SPECIAL; 13537 I32 priv = 0; 13538 PADOFFSET targ = 0; 13539 13540 /* is this op a FH constructor? */ 13541 if (is_handle_constructor(o,numargs)) { 13542 const char *name = NULL; 13543 STRLEN len = 0; 13544 U32 name_utf8 = 0; 13545 bool want_dollar = TRUE; 13546 13547 flags = 0; 13548 /* Set a flag to tell rv2gv to vivify 13549 * need to "prove" flag does not mean something 13550 * else already - NI-S 1999/05/07 13551 */ 13552 priv = OPpDEREF; 13553 if (kid->op_type == OP_PADSV) { 13554 PADNAME * const pn 13555 = PAD_COMPNAME_SV(kid->op_targ); 13556 name = PadnamePV (pn); 13557 len = PadnameLEN(pn); 13558 name_utf8 = PadnameUTF8(pn); 13559 } 13560 else if (kid->op_type == OP_RV2SV 13561 && kUNOP->op_first->op_type == OP_GV) 13562 { 13563 GV * const gv = cGVOPx_gv(kUNOP->op_first); 13564 name = GvNAME(gv); 13565 len = GvNAMELEN(gv); 13566 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0; 13567 } 13568 else if (kid->op_type == OP_AELEM 13569 || kid->op_type == OP_HELEM) 13570 { 13571 OP *firstop; 13572 OP *op = ((BINOP*)kid)->op_first; 13573 name = NULL; 13574 if (op) { 13575 SV *tmpstr = NULL; 13576 const char * const a = 13577 kid->op_type == OP_AELEM ? 13578 "[]" : "{}"; 13579 if (((op->op_type == OP_RV2AV) || 13580 (op->op_type == OP_RV2HV)) && 13581 (firstop = ((UNOP*)op)->op_first) && 13582 (firstop->op_type == OP_GV)) { 13583 /* packagevar $a[] or $h{} */ 13584 GV * const gv = cGVOPx_gv(firstop); 13585 if (gv) 13586 tmpstr = 13587 Perl_newSVpvf(aTHX_ 13588 "%s%c...%c", 13589 GvNAME(gv), 13590 a[0], a[1]); 13591 } 13592 else if (op->op_type == OP_PADAV 13593 || op->op_type == OP_PADHV) { 13594 /* lexicalvar $a[] or $h{} */ 13595 const char * const padname = 13596 PAD_COMPNAME_PV(op->op_targ); 13597 if (padname) 13598 tmpstr = 13599 Perl_newSVpvf(aTHX_ 13600 "%s%c...%c", 13601 padname + 1, 13602 a[0], a[1]); 13603 } 13604 if (tmpstr) { 13605 name = SvPV_const(tmpstr, len); 13606 name_utf8 = SvUTF8(tmpstr); 13607 sv_2mortal(tmpstr); 13608 } 13609 } 13610 if (!name) { 13611 name = "__ANONIO__"; 13612 len = 10; 13613 want_dollar = FALSE; 13614 } 13615 op_lvalue(kid, type); 13616 } 13617 if (name) { 13618 SV *namesv; 13619 targ = pad_alloc(OP_RV2GV, SVf_READONLY); 13620 namesv = PAD_SVl(targ); 13621 if (want_dollar && *name != '$') 13622 sv_setpvs(namesv, "$"); 13623 else 13624 SvPVCLEAR(namesv); 13625 sv_catpvn(namesv, name, len); 13626 if ( name_utf8 ) SvUTF8_on(namesv); 13627 } 13628 } 13629 scalar(kid); 13630 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid, 13631 OP_RV2GV, flags); 13632 kid->op_targ = targ; 13633 kid->op_private |= priv; 13634 } 13635 } 13636 scalar(kid); 13637 break; 13638 case OA_SCALARREF: 13639 if ((type == OP_UNDEF || type == OP_POS) 13640 && numargs == 1 && !(oa >> 4) 13641 && kid->op_type == OP_LIST) 13642 return too_many_arguments_pv(o,PL_op_desc[type], 0); 13643 op_lvalue(scalar(kid), type); 13644 break; 13645 } 13646 oa >>= 4; 13647 prev_kid = kid; 13648 kid = OpSIBLING(kid); 13649 } 13650 /* FIXME - should the numargs or-ing move after the too many 13651 * arguments check? */ 13652 o->op_private |= numargs; 13653 if (kid) 13654 return too_many_arguments_pv(o,OP_DESC(o), 0); 13655 listkids(o); 13656 } 13657 else if (PL_opargs[type] & OA_DEFGV) { 13658 /* Ordering of these two is important to keep f_map.t passing. */ 13659 op_free(o); 13660 return newUNOP(type, 0, newDEFSVOP()); 13661 } 13662 13663 if (oa) { 13664 while (oa & OA_OPTIONAL) 13665 oa >>= 4; 13666 if (oa && oa != OA_LIST) 13667 return too_few_arguments_pv(o,OP_DESC(o), 0); 13668 } 13669 return o; 13670 } 13671 13672 OP * 13673 Perl_ck_glob(pTHX_ OP *o) 13674 { 13675 GV *gv; 13676 13677 PERL_ARGS_ASSERT_CK_GLOB; 13678 13679 o = ck_fun(o); 13680 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first)) 13681 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */ 13682 13683 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4))) 13684 { 13685 /* convert 13686 * glob 13687 * \ null - const(wildcard) 13688 * into 13689 * null 13690 * \ enter 13691 * \ list 13692 * \ mark - glob - rv2cv 13693 * | \ gv(CORE::GLOBAL::glob) 13694 * | 13695 * \ null - const(wildcard) 13696 */ 13697 o->op_flags |= OPf_SPECIAL; 13698 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP); 13699 o = S_new_entersubop(aTHX_ gv, o); 13700 o = newUNOP(OP_NULL, 0, o); 13701 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */ 13702 return o; 13703 } 13704 else o->op_flags &= ~OPf_SPECIAL; 13705 #if !defined(PERL_EXTERNAL_GLOB) 13706 if (!PL_globhook) { 13707 ENTER; 13708 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, 13709 newSVpvs("File::Glob"), NULL, NULL, NULL); 13710 LEAVE; 13711 } 13712 #endif /* !PERL_EXTERNAL_GLOB */ 13713 gv = (GV *)newSV_type(SVt_NULL); 13714 gv_init(gv, 0, "", 0, 0); 13715 gv_IOadd(gv); 13716 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv)); 13717 SvREFCNT_dec_NN(gv); /* newGVOP increased it */ 13718 scalarkids(o); 13719 return o; 13720 } 13721 13722 OP * 13723 Perl_ck_grep(pTHX_ OP *o) 13724 { 13725 LOGOP *gwop; 13726 OP *kid; 13727 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE; 13728 13729 PERL_ARGS_ASSERT_CK_GREP; 13730 13731 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */ 13732 13733 if (o->op_flags & OPf_STACKED) { 13734 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first; 13735 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE) 13736 return no_fh_allowed(o); 13737 o->op_flags &= ~OPf_STACKED; 13738 } 13739 kid = OpSIBLING(cLISTOPo->op_first); 13740 if (type == OP_MAPWHILE) 13741 list(kid); 13742 else 13743 scalar(kid); 13744 o = ck_fun(o); 13745 if (PL_parser && PL_parser->error_count) 13746 return o; 13747 kid = OpSIBLING(cLISTOPo->op_first); 13748 if (kid->op_type != OP_NULL) 13749 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type); 13750 kid = kUNOP->op_first; 13751 13752 gwop = alloc_LOGOP(type, o, LINKLIST(kid)); 13753 kid->op_next = (OP*)gwop; 13754 o->op_private = gwop->op_private = 0; 13755 gwop->op_targ = pad_alloc(type, SVs_PADTMP); 13756 13757 kid = OpSIBLING(cLISTOPo->op_first); 13758 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid)) 13759 op_lvalue(kid, OP_GREPSTART); 13760 13761 return (OP*)gwop; 13762 } 13763 13764 OP * 13765 Perl_ck_index(pTHX_ OP *o) 13766 { 13767 PERL_ARGS_ASSERT_CK_INDEX; 13768 13769 if (o->op_flags & OPf_KIDS) { 13770 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 13771 if (kid) 13772 kid = OpSIBLING(kid); /* get past "big" */ 13773 if (kid && kid->op_type == OP_CONST) { 13774 const bool save_taint = TAINT_get; 13775 SV *sv = kSVOP->op_sv; 13776 if ( (!SvPOK(sv) || SvNIOKp(sv) || isREGEXP(sv)) 13777 && SvOK(sv) && !SvROK(sv)) 13778 { 13779 sv = newSV_type(SVt_NULL); 13780 sv_copypv(sv, kSVOP->op_sv); 13781 SvREFCNT_dec_NN(kSVOP->op_sv); 13782 kSVOP->op_sv = sv; 13783 } 13784 if (SvOK(sv)) fbm_compile(sv, 0); 13785 TAINT_set(save_taint); 13786 #ifdef NO_TAINT_SUPPORT 13787 PERL_UNUSED_VAR(save_taint); 13788 #endif 13789 } 13790 } 13791 return ck_fun(o); 13792 } 13793 13794 OP * 13795 Perl_ck_lfun(pTHX_ OP *o) 13796 { 13797 const OPCODE type = o->op_type; 13798 13799 PERL_ARGS_ASSERT_CK_LFUN; 13800 13801 return modkids(ck_fun(o), type); 13802 } 13803 13804 OP * 13805 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */ 13806 { 13807 PERL_ARGS_ASSERT_CK_DEFINED; 13808 13809 if ((o->op_flags & OPf_KIDS)) { 13810 switch (cUNOPo->op_first->op_type) { 13811 case OP_RV2AV: 13812 case OP_PADAV: 13813 Perl_croak(aTHX_ "Can't use 'defined(@array)'" 13814 " (Maybe you should just omit the defined()?)"); 13815 NOT_REACHED; /* NOTREACHED */ 13816 break; 13817 case OP_RV2HV: 13818 case OP_PADHV: 13819 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'" 13820 " (Maybe you should just omit the defined()?)"); 13821 NOT_REACHED; /* NOTREACHED */ 13822 break; 13823 default: 13824 /* no warning */ 13825 break; 13826 } 13827 } 13828 return ck_rfun(o); 13829 } 13830 13831 OP * 13832 Perl_ck_readline(pTHX_ OP *o) 13833 { 13834 PERL_ARGS_ASSERT_CK_READLINE; 13835 13836 if (o->op_flags & OPf_KIDS) { 13837 OP *kid = cLISTOPo->op_first; 13838 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE; 13839 scalar(kid); 13840 } 13841 else { 13842 OP * const newop 13843 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv)); 13844 op_free(o); 13845 return newop; 13846 } 13847 return o; 13848 } 13849 13850 OP * 13851 Perl_ck_rfun(pTHX_ OP *o) 13852 { 13853 const OPCODE type = o->op_type; 13854 13855 PERL_ARGS_ASSERT_CK_RFUN; 13856 13857 return refkids(ck_fun(o), type); 13858 } 13859 13860 OP * 13861 Perl_ck_listiob(pTHX_ OP *o) 13862 { 13863 OP *kid; 13864 13865 PERL_ARGS_ASSERT_CK_LISTIOB; 13866 13867 kid = cLISTOPo->op_first; 13868 if (!kid) { 13869 o = force_list(o, TRUE); 13870 kid = cLISTOPo->op_first; 13871 } 13872 if (kid->op_type == OP_PUSHMARK) 13873 kid = OpSIBLING(kid); 13874 if (kid && o->op_flags & OPf_STACKED) 13875 kid = OpSIBLING(kid); 13876 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */ 13877 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE 13878 && !kid->op_folded) { 13879 o->op_flags |= OPf_STACKED; /* make it a filehandle */ 13880 scalar(kid); 13881 /* replace old const op with new OP_RV2GV parent */ 13882 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first, 13883 OP_RV2GV, OPf_REF); 13884 kid = OpSIBLING(kid); 13885 } 13886 } 13887 13888 if (!kid) 13889 op_append_elem(o->op_type, o, newDEFSVOP()); 13890 13891 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF); 13892 return listkids(o); 13893 } 13894 13895 OP * 13896 Perl_ck_smartmatch(pTHX_ OP *o) 13897 { 13898 PERL_ARGS_ASSERT_CK_SMARTMATCH; 13899 if (0 == (o->op_flags & OPf_SPECIAL)) { 13900 OP *first = cBINOPo->op_first; 13901 OP *second = OpSIBLING(first); 13902 13903 /* Implicitly take a reference to an array or hash */ 13904 13905 /* remove the original two siblings, then add back the 13906 * (possibly different) first and second sibs. 13907 */ 13908 op_sibling_splice(o, NULL, 1, NULL); 13909 op_sibling_splice(o, NULL, 1, NULL); 13910 first = ref_array_or_hash(first); 13911 second = ref_array_or_hash(second); 13912 op_sibling_splice(o, NULL, 0, second); 13913 op_sibling_splice(o, NULL, 0, first); 13914 13915 /* Implicitly take a reference to a regular expression */ 13916 if (first->op_type == OP_MATCH && !(first->op_flags & OPf_STACKED)) { 13917 OpTYPE_set(first, OP_QR); 13918 } 13919 if (second->op_type == OP_MATCH && !(second->op_flags & OPf_STACKED)) { 13920 OpTYPE_set(second, OP_QR); 13921 } 13922 } 13923 13924 return o; 13925 } 13926 13927 13928 static OP * 13929 S_maybe_targlex(pTHX_ OP *o) 13930 { 13931 OP * const kid = cLISTOPo->op_first; 13932 /* has a disposable target? */ 13933 if ((PL_opargs[kid->op_type] & OA_TARGLEX) 13934 && !(kid->op_flags & OPf_STACKED) 13935 /* Cannot steal the second time! */ 13936 && !(kid->op_private & OPpTARGET_MY) 13937 ) 13938 { 13939 OP * const kkid = OpSIBLING(kid); 13940 13941 /* Can just relocate the target. */ 13942 if (kkid && kkid->op_type == OP_PADSV 13943 && (!(kkid->op_private & OPpLVAL_INTRO) 13944 || kkid->op_private & OPpPAD_STATE)) 13945 { 13946 kid->op_targ = kkid->op_targ; 13947 kkid->op_targ = 0; 13948 /* Now we do not need PADSV and SASSIGN. 13949 * Detach kid and free the rest. */ 13950 op_sibling_splice(o, NULL, 1, NULL); 13951 op_free(o); 13952 kid->op_private |= OPpTARGET_MY; /* Used for context settings */ 13953 return kid; 13954 } 13955 } 13956 return o; 13957 } 13958 13959 OP * 13960 Perl_ck_sassign(pTHX_ OP *o) 13961 { 13962 OP * const kid = cBINOPo->op_first; 13963 13964 PERL_ARGS_ASSERT_CK_SASSIGN; 13965 13966 if (OpHAS_SIBLING(kid)) { 13967 OP *kkid = OpSIBLING(kid); 13968 /* For state variable assignment with attributes, kkid is a list op 13969 whose op_last is a padsv. */ 13970 if ((kkid->op_type == OP_PADSV || 13971 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) && 13972 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV 13973 ) 13974 ) 13975 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) 13976 == (OPpLVAL_INTRO|OPpPAD_STATE)) { 13977 return S_newONCEOP(aTHX_ o, kkid); 13978 } 13979 } 13980 return S_maybe_targlex(aTHX_ o); 13981 } 13982 13983 13984 OP * 13985 Perl_ck_match(pTHX_ OP *o) 13986 { 13987 PERL_UNUSED_CONTEXT; 13988 PERL_ARGS_ASSERT_CK_MATCH; 13989 13990 return o; 13991 } 13992 13993 OP * 13994 Perl_ck_method(pTHX_ OP *o) 13995 { 13996 SV *sv, *methsv, *rclass; 13997 const char* method; 13998 char* compatptr; 13999 int utf8; 14000 STRLEN len, nsplit = 0, i; 14001 OP* new_op; 14002 OP * const kid = cUNOPo->op_first; 14003 14004 PERL_ARGS_ASSERT_CK_METHOD; 14005 if (kid->op_type != OP_CONST) return o; 14006 14007 sv = kSVOP->op_sv; 14008 14009 /* replace ' with :: */ 14010 while ((compatptr = (char *) memchr(SvPVX(sv), '\'', 14011 SvEND(sv) - SvPVX(sv) ))) 14012 { 14013 *compatptr = ':'; 14014 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1); 14015 } 14016 14017 method = SvPVX_const(sv); 14018 len = SvCUR(sv); 14019 utf8 = SvUTF8(sv) ? -1 : 1; 14020 14021 for (i = len - 1; i > 0; --i) if (method[i] == ':') { 14022 nsplit = i+1; 14023 break; 14024 } 14025 14026 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0); 14027 14028 if (!nsplit) { /* $proto->method() */ 14029 op_free(o); 14030 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv); 14031 } 14032 14033 if (memEQs(method, nsplit, "SUPER::")) { /* $proto->SUPER::method() */ 14034 op_free(o); 14035 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv); 14036 } 14037 14038 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */ 14039 if (nsplit >= 9 && strBEGINs(method+nsplit-9, "::SUPER::")) { 14040 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0); 14041 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv); 14042 } else { 14043 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0); 14044 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv); 14045 } 14046 #ifdef USE_ITHREADS 14047 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ); 14048 #else 14049 cMETHOPx(new_op)->op_rclass_sv = rclass; 14050 #endif 14051 op_free(o); 14052 return new_op; 14053 } 14054 14055 OP * 14056 Perl_ck_null(pTHX_ OP *o) 14057 { 14058 PERL_ARGS_ASSERT_CK_NULL; 14059 PERL_UNUSED_CONTEXT; 14060 return o; 14061 } 14062 14063 OP * 14064 Perl_ck_open(pTHX_ OP *o) 14065 { 14066 PERL_ARGS_ASSERT_CK_OPEN; 14067 14068 S_io_hints(aTHX_ o); 14069 { 14070 /* In case of three-arg dup open remove strictness 14071 * from the last arg if it is a bareword. */ 14072 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */ 14073 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */ 14074 OP *oa; 14075 const char *mode; 14076 14077 if ((last->op_type == OP_CONST) && /* The bareword. */ 14078 (last->op_private & OPpCONST_BARE) && 14079 (last->op_private & OPpCONST_STRICT) && 14080 (oa = OpSIBLING(first)) && /* The fh. */ 14081 (oa = OpSIBLING(oa)) && /* The mode. */ 14082 (oa->op_type == OP_CONST) && 14083 SvPOK(((SVOP*)oa)->op_sv) && 14084 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) && 14085 mode[0] == '>' && mode[1] == '&' && /* A dup open. */ 14086 (last == OpSIBLING(oa))) /* The bareword. */ 14087 last->op_private &= ~OPpCONST_STRICT; 14088 } 14089 return ck_fun(o); 14090 } 14091 14092 OP * 14093 Perl_ck_prototype(pTHX_ OP *o) 14094 { 14095 PERL_ARGS_ASSERT_CK_PROTOTYPE; 14096 if (!(o->op_flags & OPf_KIDS)) { 14097 op_free(o); 14098 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP()); 14099 } 14100 return o; 14101 } 14102 14103 OP * 14104 Perl_ck_refassign(pTHX_ OP *o) 14105 { 14106 OP * const right = cLISTOPo->op_first; 14107 OP * const left = OpSIBLING(right); 14108 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first; 14109 bool stacked = 0; 14110 14111 PERL_ARGS_ASSERT_CK_REFASSIGN; 14112 assert (left); 14113 assert (left->op_type == OP_SREFGEN); 14114 14115 o->op_private = 0; 14116 /* we use OPpPAD_STATE in refassign to mean either of those things, 14117 * and the code assumes the two flags occupy the same bit position 14118 * in the various ops below */ 14119 assert(OPpPAD_STATE == OPpOUR_INTRO); 14120 14121 switch (varop->op_type) { 14122 case OP_PADAV: 14123 o->op_private |= OPpLVREF_AV; 14124 goto settarg; 14125 case OP_PADHV: 14126 o->op_private |= OPpLVREF_HV; 14127 /* FALLTHROUGH */ 14128 case OP_PADSV: 14129 settarg: 14130 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)); 14131 o->op_targ = varop->op_targ; 14132 varop->op_targ = 0; 14133 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); 14134 break; 14135 14136 case OP_RV2AV: 14137 o->op_private |= OPpLVREF_AV; 14138 goto checkgv; 14139 NOT_REACHED; /* NOTREACHED */ 14140 case OP_RV2HV: 14141 o->op_private |= OPpLVREF_HV; 14142 /* FALLTHROUGH */ 14143 case OP_RV2SV: 14144 checkgv: 14145 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)); 14146 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad; 14147 detach_and_stack: 14148 /* Point varop to its GV kid, detached. */ 14149 varop = op_sibling_splice(varop, NULL, -1, NULL); 14150 stacked = TRUE; 14151 break; 14152 case OP_RV2CV: { 14153 OP * const kidparent = 14154 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first); 14155 OP * const kid = cUNOPx(kidparent)->op_first; 14156 o->op_private |= OPpLVREF_CV; 14157 if (kid->op_type == OP_GV) { 14158 SV *sv = (SV*)cGVOPx_gv(kid); 14159 varop = kidparent; 14160 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) { 14161 /* a CVREF here confuses pp_refassign, so make sure 14162 it gets a GV */ 14163 CV *const cv = (CV*)SvRV(sv); 14164 SV *name_sv = sv_2mortal(newSVhek(CvNAME_HEK(cv))); 14165 (void)gv_init_sv((GV*)sv, CvSTASH(cv), name_sv, 0); 14166 assert(SvTYPE(sv) == SVt_PVGV); 14167 } 14168 goto detach_and_stack; 14169 } 14170 if (kid->op_type != OP_PADCV) goto bad; 14171 o->op_targ = kid->op_targ; 14172 kid->op_targ = 0; 14173 break; 14174 } 14175 case OP_AELEM: 14176 case OP_HELEM: 14177 o->op_private |= (varop->op_private & OPpLVAL_INTRO); 14178 o->op_private |= OPpLVREF_ELEM; 14179 op_null(varop); 14180 stacked = TRUE; 14181 /* Detach varop. */ 14182 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL); 14183 break; 14184 default: 14185 bad: 14186 /* diag_listed_as: Can't modify reference to %s in %s assignment */ 14187 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar " 14188 "assignment", 14189 OP_DESC(varop))); 14190 return o; 14191 } 14192 if (!FEATURE_REFALIASING_IS_ENABLED) 14193 Perl_croak(aTHX_ 14194 "Experimental aliasing via reference not enabled"); 14195 Perl_ck_warner_d(aTHX_ 14196 packWARN(WARN_EXPERIMENTAL__REFALIASING), 14197 "Aliasing via reference is experimental"); 14198 if (stacked) { 14199 o->op_flags |= OPf_STACKED; 14200 op_sibling_splice(o, right, 1, varop); 14201 } 14202 else { 14203 o->op_flags &=~ OPf_STACKED; 14204 op_sibling_splice(o, right, 1, NULL); 14205 } 14206 op_free(left); 14207 return o; 14208 } 14209 14210 OP * 14211 Perl_ck_repeat(pTHX_ OP *o) 14212 { 14213 PERL_ARGS_ASSERT_CK_REPEAT; 14214 14215 if (cBINOPo->op_first->op_flags & OPf_PARENS) { 14216 OP* kids; 14217 o->op_private |= OPpREPEAT_DOLIST; 14218 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */ 14219 kids = force_list(kids, TRUE); /* promote it to a list */ 14220 op_sibling_splice(o, NULL, 0, kids); /* and add back */ 14221 } 14222 else 14223 scalar(o); 14224 return o; 14225 } 14226 14227 OP * 14228 Perl_ck_require(pTHX_ OP *o) 14229 { 14230 GV* gv; 14231 14232 PERL_ARGS_ASSERT_CK_REQUIRE; 14233 14234 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */ 14235 SVOP * const kid = (SVOP*)cUNOPo->op_first; 14236 U32 hash; 14237 char *s; 14238 STRLEN len; 14239 if (kid->op_type == OP_CONST) { 14240 SV * const sv = kid->op_sv; 14241 U32 const was_readonly = SvREADONLY(sv); 14242 if (kid->op_private & OPpCONST_BARE) { 14243 const char *end; 14244 HEK *hek; 14245 14246 if (was_readonly) { 14247 SvREADONLY_off(sv); 14248 } 14249 14250 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0); 14251 14252 s = SvPVX(sv); 14253 len = SvCUR(sv); 14254 end = s + len; 14255 /* treat ::foo::bar as foo::bar */ 14256 if (len >= 2 && s[0] == ':' && s[1] == ':') 14257 DIE(aTHX_ "Bareword in require must not start with a double-colon: \"%s\"\n", s); 14258 if (s == end) 14259 DIE(aTHX_ "Bareword in require maps to empty filename"); 14260 14261 for (; s < end; s++) { 14262 if (*s == ':' && s[1] == ':') { 14263 *s = '/'; 14264 Move(s+2, s+1, end - s - 1, char); 14265 --end; 14266 } 14267 } 14268 SvEND_set(sv, end); 14269 sv_catpvs(sv, ".pm"); 14270 PERL_HASH(hash, SvPVX(sv), SvCUR(sv)); 14271 hek = share_hek(SvPVX(sv), 14272 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1), 14273 hash); 14274 sv_sethek(sv, hek); 14275 unshare_hek(hek); 14276 SvFLAGS(sv) |= was_readonly; 14277 } 14278 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv) 14279 && !SvVOK(sv)) { 14280 s = SvPV(sv, len); 14281 if (SvREFCNT(sv) > 1) { 14282 kid->op_sv = newSVpvn_share( 14283 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0); 14284 SvREFCNT_dec_NN(sv); 14285 } 14286 else { 14287 HEK *hek; 14288 if (was_readonly) SvREADONLY_off(sv); 14289 PERL_HASH(hash, s, len); 14290 hek = share_hek(s, 14291 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 14292 hash); 14293 sv_sethek(sv, hek); 14294 unshare_hek(hek); 14295 SvFLAGS(sv) |= was_readonly; 14296 } 14297 } 14298 } 14299 } 14300 14301 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */ 14302 /* handle override, if any */ 14303 && (gv = gv_override("require", 7))) { 14304 OP *kid, *newop; 14305 if (o->op_flags & OPf_KIDS) { 14306 kid = cUNOPo->op_first; 14307 op_sibling_splice(o, NULL, -1, NULL); 14308 } 14309 else { 14310 kid = newDEFSVOP(); 14311 } 14312 op_free(o); 14313 newop = S_new_entersubop(aTHX_ gv, kid); 14314 return newop; 14315 } 14316 14317 return ck_fun(o); 14318 } 14319 14320 OP * 14321 Perl_ck_return(pTHX_ OP *o) 14322 { 14323 OP *kid; 14324 14325 PERL_ARGS_ASSERT_CK_RETURN; 14326 14327 kid = OpSIBLING(cLISTOPo->op_first); 14328 if (PL_compcv && CvLVALUE(PL_compcv)) { 14329 for (; kid; kid = OpSIBLING(kid)) 14330 op_lvalue(kid, OP_LEAVESUBLV); 14331 } 14332 14333 return o; 14334 } 14335 14336 OP * 14337 Perl_ck_select(pTHX_ OP *o) 14338 { 14339 OP* kid; 14340 14341 PERL_ARGS_ASSERT_CK_SELECT; 14342 14343 if (o->op_flags & OPf_KIDS) { 14344 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 14345 if (kid && OpHAS_SIBLING(kid)) { 14346 OpTYPE_set(o, OP_SSELECT); 14347 o = ck_fun(o); 14348 return fold_constants(op_integerize(op_std_init(o))); 14349 } 14350 } 14351 o = ck_fun(o); 14352 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 14353 if (kid && kid->op_type == OP_RV2GV) 14354 kid->op_private &= ~HINT_STRICT_REFS; 14355 return o; 14356 } 14357 14358 OP * 14359 Perl_ck_shift(pTHX_ OP *o) 14360 { 14361 const I32 type = o->op_type; 14362 14363 PERL_ARGS_ASSERT_CK_SHIFT; 14364 14365 if (!(o->op_flags & OPf_KIDS)) { 14366 OP *argop; 14367 14368 if (!CvUNIQUE(PL_compcv)) { 14369 o->op_flags |= OPf_SPECIAL; 14370 return o; 14371 } 14372 14373 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv))); 14374 op_free(o); 14375 return newUNOP(type, 0, scalar(argop)); 14376 } 14377 return scalar(ck_fun(o)); 14378 } 14379 14380 OP * 14381 Perl_ck_sort(pTHX_ OP *o) 14382 { 14383 OP *firstkid; 14384 OP *kid; 14385 U8 stacked; 14386 14387 PERL_ARGS_ASSERT_CK_SORT; 14388 14389 if (o->op_flags & OPf_STACKED) 14390 simplify_sort(o); 14391 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 14392 14393 if (!firstkid) 14394 return too_few_arguments_pv(o,OP_DESC(o), 0); 14395 14396 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */ 14397 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */ 14398 14399 /* if the first arg is a code block, process it and mark sort as 14400 * OPf_SPECIAL */ 14401 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) { 14402 LINKLIST(kid); 14403 if (kid->op_type == OP_LEAVE) 14404 op_null(kid); /* wipe out leave */ 14405 /* Prevent execution from escaping out of the sort block. */ 14406 kid->op_next = 0; 14407 14408 /* provide scalar context for comparison function/block */ 14409 kid = scalar(firstkid); 14410 kid->op_next = kid; 14411 o->op_flags |= OPf_SPECIAL; 14412 } 14413 else if (kid->op_type == OP_CONST 14414 && kid->op_private & OPpCONST_BARE) { 14415 char tmpbuf[256]; 14416 STRLEN len; 14417 PADOFFSET off; 14418 const char * const name = SvPV(kSVOP_sv, len); 14419 *tmpbuf = '&'; 14420 assert (len < 256); 14421 Copy(name, tmpbuf+1, len, char); 14422 off = pad_findmy_pvn(tmpbuf, len+1, 0); 14423 if (off != NOT_IN_PAD) { 14424 if (PAD_COMPNAME_FLAGS_isOUR(off)) { 14425 SV * const fq = 14426 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off))); 14427 sv_catpvs(fq, "::"); 14428 sv_catsv(fq, kSVOP_sv); 14429 SvREFCNT_dec_NN(kSVOP_sv); 14430 kSVOP->op_sv = fq; 14431 } 14432 else { 14433 OP * const padop = newOP(OP_PADCV, 0); 14434 padop->op_targ = off; 14435 /* replace the const op with the pad op */ 14436 op_sibling_splice(firstkid, NULL, 1, padop); 14437 op_free(kid); 14438 } 14439 } 14440 } 14441 14442 firstkid = OpSIBLING(firstkid); 14443 } 14444 14445 for (kid = firstkid; kid; kid = OpSIBLING(kid)) { 14446 /* provide list context for arguments */ 14447 list(kid); 14448 if (stacked) 14449 op_lvalue(kid, OP_GREPSTART); 14450 } 14451 14452 return o; 14453 } 14454 14455 /* for sort { X } ..., where X is one of 14456 * $a <=> $b, $b <=> $a, $a cmp $b, $b cmp $a 14457 * elide the second child of the sort (the one containing X), 14458 * and set these flags as appropriate 14459 OPpSORT_NUMERIC; 14460 OPpSORT_INTEGER; 14461 OPpSORT_DESCEND; 14462 * Also, check and warn on lexical $a, $b. 14463 */ 14464 14465 STATIC void 14466 S_simplify_sort(pTHX_ OP *o) 14467 { 14468 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 14469 OP *k; 14470 int descending; 14471 GV *gv; 14472 const char *gvname; 14473 bool have_scopeop; 14474 14475 PERL_ARGS_ASSERT_SIMPLIFY_SORT; 14476 14477 kid = kUNOP->op_first; /* get past null */ 14478 if (!(have_scopeop = kid->op_type == OP_SCOPE) 14479 && kid->op_type != OP_LEAVE) 14480 return; 14481 kid = kLISTOP->op_last; /* get past scope */ 14482 switch(kid->op_type) { 14483 case OP_NCMP: 14484 case OP_I_NCMP: 14485 case OP_SCMP: 14486 if (!have_scopeop) goto padkids; 14487 break; 14488 default: 14489 return; 14490 } 14491 k = kid; /* remember this node*/ 14492 if (kBINOP->op_first->op_type != OP_RV2SV 14493 || kBINOP->op_last ->op_type != OP_RV2SV) 14494 { 14495 /* 14496 Warn about my($a) or my($b) in a sort block, *if* $a or $b is 14497 then used in a comparison. This catches most, but not 14498 all cases. For instance, it catches 14499 sort { my($a); $a <=> $b } 14500 but not 14501 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; } 14502 (although why you'd do that is anyone's guess). 14503 */ 14504 14505 padkids: 14506 if (!ckWARN(WARN_SYNTAX)) return; 14507 kid = kBINOP->op_first; 14508 do { 14509 if (kid->op_type == OP_PADSV) { 14510 PADNAME * const name = PAD_COMPNAME(kid->op_targ); 14511 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$' 14512 && ( PadnamePV(name)[1] == 'a' 14513 || PadnamePV(name)[1] == 'b' )) 14514 /* diag_listed_as: "my %s" used in sort comparison */ 14515 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 14516 "\"%s %s\" used in sort comparison", 14517 PadnameIsSTATE(name) 14518 ? "state" 14519 : "my", 14520 PadnamePV(name)); 14521 } 14522 } while ((kid = OpSIBLING(kid))); 14523 return; 14524 } 14525 kid = kBINOP->op_first; /* get past cmp */ 14526 if (kUNOP->op_first->op_type != OP_GV) 14527 return; 14528 kid = kUNOP->op_first; /* get past rv2sv */ 14529 gv = kGVOP_gv; 14530 if (GvSTASH(gv) != PL_curstash) 14531 return; 14532 gvname = GvNAME(gv); 14533 if (*gvname == 'a' && gvname[1] == '\0') 14534 descending = 0; 14535 else if (*gvname == 'b' && gvname[1] == '\0') 14536 descending = 1; 14537 else 14538 return; 14539 14540 kid = k; /* back to cmp */ 14541 /* already checked above that it is rv2sv */ 14542 kid = kBINOP->op_last; /* down to 2nd arg */ 14543 if (kUNOP->op_first->op_type != OP_GV) 14544 return; 14545 kid = kUNOP->op_first; /* get past rv2sv */ 14546 gv = kGVOP_gv; 14547 if (GvSTASH(gv) != PL_curstash) 14548 return; 14549 gvname = GvNAME(gv); 14550 if ( descending 14551 ? !(*gvname == 'a' && gvname[1] == '\0') 14552 : !(*gvname == 'b' && gvname[1] == '\0')) 14553 return; 14554 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL); 14555 if (descending) 14556 o->op_private |= OPpSORT_DESCEND; 14557 if (k->op_type == OP_NCMP) 14558 o->op_private |= OPpSORT_NUMERIC; 14559 if (k->op_type == OP_I_NCMP) 14560 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER; 14561 kid = OpSIBLING(cLISTOPo->op_first); 14562 /* cut out and delete old block (second sibling) */ 14563 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL); 14564 op_free(kid); 14565 } 14566 14567 OP * 14568 Perl_ck_split(pTHX_ OP *o) 14569 { 14570 OP *kid; 14571 OP *sibs; 14572 14573 PERL_ARGS_ASSERT_CK_SPLIT; 14574 14575 assert(o->op_type == OP_LIST); 14576 14577 if (o->op_flags & OPf_STACKED) 14578 return no_fh_allowed(o); 14579 14580 kid = cLISTOPo->op_first; 14581 /* delete leading NULL node, then add a CONST if no other nodes */ 14582 assert(kid->op_type == OP_NULL); 14583 op_sibling_splice(o, NULL, 1, 14584 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" "))); 14585 op_free(kid); 14586 kid = cLISTOPo->op_first; 14587 14588 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) { 14589 /* remove match expression, and replace with new optree with 14590 * a match op at its head */ 14591 op_sibling_splice(o, NULL, 1, NULL); 14592 /* pmruntime will handle split " " behavior with flag==2 */ 14593 kid = pmruntime(newPMOP(OP_MATCH, 0), kid, NULL, 2, 0); 14594 op_sibling_splice(o, NULL, 0, kid); 14595 } 14596 14597 assert(kid->op_type == OP_MATCH || kid->op_type == OP_SPLIT); 14598 14599 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) { 14600 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), 14601 "Use of /g modifier is meaningless in split"); 14602 } 14603 14604 /* eliminate the split op, and move the match op (plus any children) 14605 * into its place, then convert the match op into a split op. i.e. 14606 * 14607 * SPLIT MATCH SPLIT(ex-MATCH) 14608 * | | | 14609 * MATCH - A - B - C => R - A - B - C => R - A - B - C 14610 * | | | 14611 * R X - Y X - Y 14612 * | 14613 * X - Y 14614 * 14615 * (R, if it exists, will be a regcomp op) 14616 */ 14617 14618 op_sibling_splice(o, NULL, 1, NULL); /* detach match op from o */ 14619 sibs = op_sibling_splice(o, NULL, -1, NULL); /* detach any other sibs */ 14620 op_sibling_splice(kid, cLISTOPx(kid)->op_last, 0, sibs); /* and reattach */ 14621 OpTYPE_set(kid, OP_SPLIT); 14622 kid->op_flags = (o->op_flags | (kid->op_flags & OPf_KIDS)); 14623 kid->op_private = o->op_private; 14624 op_free(o); 14625 o = kid; 14626 kid = sibs; /* kid is now the string arg of the split */ 14627 14628 if (!kid) { 14629 kid = newDEFSVOP(); 14630 op_append_elem(OP_SPLIT, o, kid); 14631 } 14632 scalar(kid); 14633 14634 kid = OpSIBLING(kid); 14635 if (!kid) { 14636 kid = newSVOP(OP_CONST, 0, newSViv(0)); 14637 op_append_elem(OP_SPLIT, o, kid); 14638 o->op_private |= OPpSPLIT_IMPLIM; 14639 } 14640 scalar(kid); 14641 14642 if (OpHAS_SIBLING(kid)) 14643 return too_many_arguments_pv(o,OP_DESC(o), 0); 14644 14645 return o; 14646 } 14647 14648 OP * 14649 Perl_ck_stringify(pTHX_ OP *o) 14650 { 14651 OP * const kid = OpSIBLING(cUNOPo->op_first); 14652 PERL_ARGS_ASSERT_CK_STRINGIFY; 14653 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA 14654 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST 14655 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST) 14656 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */ 14657 { 14658 op_sibling_splice(o, cUNOPo->op_first, -1, NULL); 14659 op_free(o); 14660 return kid; 14661 } 14662 return ck_fun(o); 14663 } 14664 14665 OP * 14666 Perl_ck_join(pTHX_ OP *o) 14667 { 14668 OP * const kid = OpSIBLING(cLISTOPo->op_first); 14669 14670 PERL_ARGS_ASSERT_CK_JOIN; 14671 14672 if (kid && kid->op_type == OP_MATCH) { 14673 if (ckWARN(WARN_SYNTAX)) { 14674 const REGEXP *re = PM_GETRE(kPMOP); 14675 const SV *msg = re 14676 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re), 14677 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) ) 14678 : newSVpvs_flags( "STRING", SVs_TEMP ); 14679 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 14680 "/%" SVf "/ should probably be written as \"%" SVf "\"", 14681 SVfARG(msg), SVfARG(msg)); 14682 } 14683 } 14684 if (kid 14685 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */ 14686 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO)) 14687 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV 14688 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))))) 14689 { 14690 const OP * const bairn = OpSIBLING(kid); /* the list */ 14691 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */ 14692 && OP_GIMME(bairn,0) == G_SCALAR) 14693 { 14694 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED, 14695 op_sibling_splice(o, kid, 1, NULL)); 14696 op_free(o); 14697 return ret; 14698 } 14699 } 14700 14701 return ck_fun(o); 14702 } 14703 14704 /* 14705 =for apidoc rv2cv_op_cv 14706 14707 Examines an op, which is expected to identify a subroutine at runtime, 14708 and attempts to determine at compile time which subroutine it identifies. 14709 This is normally used during Perl compilation to determine whether 14710 a prototype can be applied to a function call. C<cvop> is the op 14711 being considered, normally an C<rv2cv> op. A pointer to the identified 14712 subroutine is returned, if it could be determined statically, and a null 14713 pointer is returned if it was not possible to determine statically. 14714 14715 Currently, the subroutine can be identified statically if the RV that the 14716 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op. 14717 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is 14718 suitable if the constant value must be an RV pointing to a CV. Details of 14719 this process may change in future versions of Perl. If the C<rv2cv> op 14720 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify 14721 the subroutine statically: this flag is used to suppress compile-time 14722 magic on a subroutine call, forcing it to use default runtime behaviour. 14723 14724 If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling 14725 of a GV reference is modified. If a GV was examined and its CV slot was 14726 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set. 14727 If the op is not optimised away, and the CV slot is later populated with 14728 a subroutine having a prototype, that flag eventually triggers the warning 14729 "called too early to check prototype". 14730 14731 If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead 14732 of returning a pointer to the subroutine it returns a pointer to the 14733 GV giving the most appropriate name for the subroutine in this context. 14734 Normally this is just the C<CvGV> of the subroutine, but for an anonymous 14735 (C<CvANON>) subroutine that is referenced through a GV it will be the 14736 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned. 14737 A null pointer is returned as usual if there is no statically-determinable 14738 subroutine. 14739 14740 =for apidoc Amnh||OPpEARLY_CV 14741 =for apidoc Amnh||OPpENTERSUB_AMPER 14742 =for apidoc Amnh||RV2CVOPCV_MARK_EARLY 14743 =for apidoc Amnh||RV2CVOPCV_RETURN_NAME_GV 14744 14745 =cut 14746 */ 14747 14748 /* shared by toke.c:yylex */ 14749 CV * 14750 Perl_find_lexical_cv(pTHX_ PADOFFSET off) 14751 { 14752 PADNAME *name = PAD_COMPNAME(off); 14753 CV *compcv = PL_compcv; 14754 while (PadnameOUTER(name)) { 14755 assert(PARENT_PAD_INDEX(name)); 14756 compcv = CvOUTSIDE(compcv); 14757 name = PadlistNAMESARRAY(CvPADLIST(compcv)) 14758 [off = PARENT_PAD_INDEX(name)]; 14759 } 14760 assert(!PadnameIsOUR(name)); 14761 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) { 14762 return PadnamePROTOCV(name); 14763 } 14764 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off]; 14765 } 14766 14767 CV * 14768 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags) 14769 { 14770 OP *rvop; 14771 CV *cv; 14772 GV *gv; 14773 PERL_ARGS_ASSERT_RV2CV_OP_CV; 14774 if (flags & ~RV2CVOPCV_FLAG_MASK) 14775 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags); 14776 if (cvop->op_type != OP_RV2CV) 14777 return NULL; 14778 if (cvop->op_private & OPpENTERSUB_AMPER) 14779 return NULL; 14780 if (!(cvop->op_flags & OPf_KIDS)) 14781 return NULL; 14782 rvop = cUNOPx(cvop)->op_first; 14783 switch (rvop->op_type) { 14784 case OP_GV: { 14785 gv = cGVOPx_gv(rvop); 14786 if (!isGV(gv)) { 14787 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) { 14788 cv = MUTABLE_CV(SvRV(gv)); 14789 gv = NULL; 14790 break; 14791 } 14792 if (flags & RV2CVOPCV_RETURN_STUB) 14793 return (CV *)gv; 14794 else return NULL; 14795 } 14796 cv = GvCVu(gv); 14797 if (!cv) { 14798 if (flags & RV2CVOPCV_MARK_EARLY) 14799 rvop->op_private |= OPpEARLY_CV; 14800 return NULL; 14801 } 14802 } break; 14803 case OP_CONST: { 14804 SV *rv = cSVOPx_sv(rvop); 14805 if (!SvROK(rv)) 14806 return NULL; 14807 cv = (CV*)SvRV(rv); 14808 gv = NULL; 14809 } break; 14810 case OP_PADCV: { 14811 cv = find_lexical_cv(rvop->op_targ); 14812 gv = NULL; 14813 } break; 14814 default: { 14815 return NULL; 14816 } NOT_REACHED; /* NOTREACHED */ 14817 } 14818 if (SvTYPE((SV*)cv) != SVt_PVCV) 14819 return NULL; 14820 if (flags & RV2CVOPCV_RETURN_NAME_GV) { 14821 if ((!CvANON(cv) && !CvLEXICAL(cv)) || !gv) 14822 gv = CvGV(cv); 14823 return (CV*)gv; 14824 } 14825 else if (flags & RV2CVOPCV_MAYBE_NAME_GV) { 14826 if (CvLEXICAL(cv) || CvNAMED(cv)) 14827 return NULL; 14828 if (!CvANON(cv) || !gv) 14829 gv = CvGV(cv); 14830 return (CV*)gv; 14831 14832 } else { 14833 return cv; 14834 } 14835 } 14836 14837 /* 14838 =for apidoc ck_entersub_args_list 14839 14840 Performs the default fixup of the arguments part of an C<entersub> 14841 op tree. This consists of applying list context to each of the 14842 argument ops. This is the standard treatment used on a call marked 14843 with C<&>, or a method call, or a call through a subroutine reference, 14844 or any other call where the callee can't be identified at compile time, 14845 or a call where the callee has no prototype. 14846 14847 =cut 14848 */ 14849 14850 OP * 14851 Perl_ck_entersub_args_list(pTHX_ OP *entersubop) 14852 { 14853 OP *aop; 14854 14855 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST; 14856 14857 aop = cUNOPx(entersubop)->op_first; 14858 if (!OpHAS_SIBLING(aop)) 14859 aop = cUNOPx(aop)->op_first; 14860 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) { 14861 /* skip the extra attributes->import() call implicitly added in 14862 * something like foo(my $x : bar) 14863 */ 14864 if ( aop->op_type == OP_ENTERSUB 14865 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID 14866 ) 14867 continue; 14868 list(aop); 14869 op_lvalue(aop, OP_ENTERSUB); 14870 } 14871 return entersubop; 14872 } 14873 14874 /* 14875 =for apidoc ck_entersub_args_proto 14876 14877 Performs the fixup of the arguments part of an C<entersub> op tree 14878 based on a subroutine prototype. This makes various modifications to 14879 the argument ops, from applying context up to inserting C<refgen> ops, 14880 and checking the number and syntactic types of arguments, as directed by 14881 the prototype. This is the standard treatment used on a subroutine call, 14882 not marked with C<&>, where the callee can be identified at compile time 14883 and has a prototype. 14884 14885 C<protosv> supplies the subroutine prototype to be applied to the call. 14886 It may be a normal defined scalar, of which the string value will be used. 14887 Alternatively, for convenience, it may be a subroutine object (a C<CV*> 14888 that has been cast to C<SV*>) which has a prototype. The prototype 14889 supplied, in whichever form, does not need to match the actual callee 14890 referenced by the op tree. 14891 14892 If the argument ops disagree with the prototype, for example by having 14893 an unacceptable number of arguments, a valid op tree is returned anyway. 14894 The error is reflected in the parser state, normally resulting in a single 14895 exception at the top level of parsing which covers all the compilation 14896 errors that occurred. In the error message, the callee is referred to 14897 by the name defined by the C<namegv> parameter. 14898 14899 =cut 14900 */ 14901 14902 OP * 14903 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv) 14904 { 14905 STRLEN proto_len; 14906 const char *proto, *proto_end; 14907 OP *aop, *prev, *cvop, *parent; 14908 int optional = 0; 14909 I32 arg = 0; 14910 I32 contextclass = 0; 14911 const char *e = NULL; 14912 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO; 14913 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv)) 14914 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, " 14915 "flags=%lx", (unsigned long) SvFLAGS(protosv)); 14916 if (SvTYPE(protosv) == SVt_PVCV) 14917 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv); 14918 else proto = SvPV(protosv, proto_len); 14919 proto = S_strip_spaces(aTHX_ proto, &proto_len); 14920 proto_end = proto + proto_len; 14921 parent = entersubop; 14922 aop = cUNOPx(entersubop)->op_first; 14923 if (!OpHAS_SIBLING(aop)) { 14924 parent = aop; 14925 aop = cUNOPx(aop)->op_first; 14926 } 14927 prev = aop; 14928 aop = OpSIBLING(aop); 14929 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 14930 while (aop != cvop) { 14931 OP* o3 = aop; 14932 14933 if (proto >= proto_end) 14934 { 14935 SV * const namesv = cv_name((CV *)namegv, NULL, 0); 14936 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf, 14937 SVfARG(namesv)), SvUTF8(namesv)); 14938 return entersubop; 14939 } 14940 14941 switch (*proto) { 14942 case ';': 14943 optional = 1; 14944 proto++; 14945 continue; 14946 case '_': 14947 /* _ must be at the end */ 14948 if (proto[1] && !memCHRs(";@%", proto[1])) 14949 goto oops; 14950 /* FALLTHROUGH */ 14951 case '$': 14952 proto++; 14953 arg++; 14954 scalar(aop); 14955 break; 14956 case '%': 14957 case '@': 14958 list(aop); 14959 arg++; 14960 break; 14961 case '&': 14962 proto++; 14963 arg++; 14964 if ( o3->op_type != OP_UNDEF 14965 && (o3->op_type != OP_SREFGEN 14966 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type 14967 != OP_ANONCODE 14968 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type 14969 != OP_RV2CV))) 14970 bad_type_gv(arg, namegv, o3, 14971 arg == 1 ? "block or sub {}" : "sub {}"); 14972 break; 14973 case '*': 14974 /* '*' allows any scalar type, including bareword */ 14975 proto++; 14976 arg++; 14977 if (o3->op_type == OP_RV2GV) 14978 goto wrapref; /* autoconvert GLOB -> GLOBref */ 14979 else if (o3->op_type == OP_CONST) 14980 o3->op_private &= ~OPpCONST_STRICT; 14981 scalar(aop); 14982 break; 14983 case '+': 14984 proto++; 14985 arg++; 14986 if (o3->op_type == OP_RV2AV || 14987 o3->op_type == OP_PADAV || 14988 o3->op_type == OP_RV2HV || 14989 o3->op_type == OP_PADHV 14990 ) { 14991 goto wrapref; 14992 } 14993 scalar(aop); 14994 break; 14995 case '[': case ']': 14996 goto oops; 14997 14998 case '\\': 14999 proto++; 15000 arg++; 15001 again: 15002 switch (*proto++) { 15003 case '[': 15004 if (contextclass++ == 0) { 15005 e = (char *) memchr(proto, ']', proto_end - proto); 15006 if (!e || e == proto) 15007 goto oops; 15008 } 15009 else 15010 goto oops; 15011 goto again; 15012 15013 case ']': 15014 if (contextclass) { 15015 const char *p = proto; 15016 const char *const end = proto; 15017 contextclass = 0; 15018 while (*--p != '[') 15019 /* \[$] accepts any scalar lvalue */ 15020 if (*p == '$' 15021 && Perl_op_lvalue_flags(aTHX_ 15022 scalar(o3), 15023 OP_READ, /* not entersub */ 15024 OP_LVALUE_NO_CROAK 15025 )) goto wrapref; 15026 bad_type_gv(arg, namegv, o3, 15027 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p)); 15028 } else 15029 goto oops; 15030 break; 15031 case '*': 15032 if (o3->op_type == OP_RV2GV) 15033 goto wrapref; 15034 if (!contextclass) 15035 bad_type_gv(arg, namegv, o3, "symbol"); 15036 break; 15037 case '&': 15038 if (o3->op_type == OP_ENTERSUB 15039 && !(o3->op_flags & OPf_STACKED)) 15040 goto wrapref; 15041 if (!contextclass) 15042 bad_type_gv(arg, namegv, o3, "subroutine"); 15043 break; 15044 case '$': 15045 if (o3->op_type == OP_RV2SV || 15046 o3->op_type == OP_PADSV || 15047 o3->op_type == OP_HELEM || 15048 o3->op_type == OP_AELEM) 15049 goto wrapref; 15050 if (!contextclass) { 15051 /* \$ accepts any scalar lvalue */ 15052 if (Perl_op_lvalue_flags(aTHX_ 15053 scalar(o3), 15054 OP_READ, /* not entersub */ 15055 OP_LVALUE_NO_CROAK 15056 )) goto wrapref; 15057 bad_type_gv(arg, namegv, o3, "scalar"); 15058 } 15059 break; 15060 case '@': 15061 if (o3->op_type == OP_RV2AV || 15062 o3->op_type == OP_PADAV) 15063 { 15064 o3->op_flags &=~ OPf_PARENS; 15065 goto wrapref; 15066 } 15067 if (!contextclass) 15068 bad_type_gv(arg, namegv, o3, "array"); 15069 break; 15070 case '%': 15071 if (o3->op_type == OP_RV2HV || 15072 o3->op_type == OP_PADHV) 15073 { 15074 o3->op_flags &=~ OPf_PARENS; 15075 goto wrapref; 15076 } 15077 if (!contextclass) 15078 bad_type_gv(arg, namegv, o3, "hash"); 15079 break; 15080 wrapref: 15081 aop = S_op_sibling_newUNOP(aTHX_ parent, prev, 15082 OP_REFGEN, 0); 15083 if (contextclass && e) { 15084 proto = e + 1; 15085 contextclass = 0; 15086 } 15087 break; 15088 default: goto oops; 15089 } 15090 if (contextclass) 15091 goto again; 15092 break; 15093 case ' ': 15094 proto++; 15095 continue; 15096 default: 15097 oops: { 15098 Perl_croak(aTHX_ "Malformed prototype for %" SVf ": %" SVf, 15099 SVfARG(cv_name((CV *)namegv, NULL, 0)), 15100 SVfARG(protosv)); 15101 } 15102 } 15103 15104 op_lvalue(aop, OP_ENTERSUB); 15105 prev = aop; 15106 aop = OpSIBLING(aop); 15107 } 15108 if (aop == cvop && *proto == '_') { 15109 /* generate an access to $_ */ 15110 op_sibling_splice(parent, prev, 0, newDEFSVOP()); 15111 } 15112 if (!optional && proto_end > proto && 15113 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_')) 15114 { 15115 SV * const namesv = cv_name((CV *)namegv, NULL, 0); 15116 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %" SVf, 15117 SVfARG(namesv)), SvUTF8(namesv)); 15118 } 15119 return entersubop; 15120 } 15121 15122 /* 15123 =for apidoc ck_entersub_args_proto_or_list 15124 15125 Performs the fixup of the arguments part of an C<entersub> op tree either 15126 based on a subroutine prototype or using default list-context processing. 15127 This is the standard treatment used on a subroutine call, not marked 15128 with C<&>, where the callee can be identified at compile time. 15129 15130 C<protosv> supplies the subroutine prototype to be applied to the call, 15131 or indicates that there is no prototype. It may be a normal scalar, 15132 in which case if it is defined then the string value will be used 15133 as a prototype, and if it is undefined then there is no prototype. 15134 Alternatively, for convenience, it may be a subroutine object (a C<CV*> 15135 that has been cast to C<SV*>), of which the prototype will be used if it 15136 has one. The prototype (or lack thereof) supplied, in whichever form, 15137 does not need to match the actual callee referenced by the op tree. 15138 15139 If the argument ops disagree with the prototype, for example by having 15140 an unacceptable number of arguments, a valid op tree is returned anyway. 15141 The error is reflected in the parser state, normally resulting in a single 15142 exception at the top level of parsing which covers all the compilation 15143 errors that occurred. In the error message, the callee is referred to 15144 by the name defined by the C<namegv> parameter. 15145 15146 =cut 15147 */ 15148 15149 OP * 15150 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop, 15151 GV *namegv, SV *protosv) 15152 { 15153 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST; 15154 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv)) 15155 return ck_entersub_args_proto(entersubop, namegv, protosv); 15156 else 15157 return ck_entersub_args_list(entersubop); 15158 } 15159 15160 OP * 15161 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv) 15162 { 15163 IV cvflags = SvIVX(protosv); 15164 int opnum = cvflags & 0xffff; 15165 OP *aop = cUNOPx(entersubop)->op_first; 15166 15167 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE; 15168 15169 if (!opnum) { 15170 OP *cvop; 15171 if (!OpHAS_SIBLING(aop)) 15172 aop = cUNOPx(aop)->op_first; 15173 aop = OpSIBLING(aop); 15174 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ; 15175 if (aop != cvop) { 15176 SV *namesv = cv_name((CV *)namegv, NULL, CV_NAME_NOTQUAL); 15177 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf, 15178 SVfARG(namesv)), SvUTF8(namesv)); 15179 } 15180 15181 op_free(entersubop); 15182 switch(cvflags >> 16) { 15183 case 'F': return newSVOP(OP_CONST, 0, 15184 newSVpv(CopFILE(PL_curcop),0)); 15185 case 'L': return newSVOP( 15186 OP_CONST, 0, 15187 Perl_newSVpvf(aTHX_ 15188 "%" IVdf, (IV)CopLINE(PL_curcop) 15189 ) 15190 ); 15191 case 'P': return newSVOP(OP_CONST, 0, 15192 (PL_curstash 15193 ? newSVhek(HvNAME_HEK(PL_curstash)) 15194 : &PL_sv_undef 15195 ) 15196 ); 15197 } 15198 NOT_REACHED; /* NOTREACHED */ 15199 } 15200 else { 15201 OP *prev, *cvop, *first, *parent; 15202 U32 flags = 0; 15203 15204 parent = entersubop; 15205 if (!OpHAS_SIBLING(aop)) { 15206 parent = aop; 15207 aop = cUNOPx(aop)->op_first; 15208 } 15209 15210 first = prev = aop; 15211 aop = OpSIBLING(aop); 15212 /* find last sibling */ 15213 for (cvop = aop; 15214 OpHAS_SIBLING(cvop); 15215 prev = cvop, cvop = OpSIBLING(cvop)) 15216 ; 15217 if (!(cvop->op_private & OPpENTERSUB_NOPAREN) 15218 /* Usually, OPf_SPECIAL on an op with no args means that it had 15219 * parens, but these have their own meaning for that flag: */ 15220 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH 15221 && opnum != OP_DELETE && opnum != OP_EXISTS) 15222 flags |= OPf_SPECIAL; 15223 /* excise cvop from end of sibling chain */ 15224 op_sibling_splice(parent, prev, 1, NULL); 15225 op_free(cvop); 15226 if (aop == cvop) aop = NULL; 15227 15228 /* detach remaining siblings from the first sibling, then 15229 * dispose of original optree */ 15230 15231 if (aop) 15232 op_sibling_splice(parent, first, -1, NULL); 15233 op_free(entersubop); 15234 15235 if (cvflags == (OP_ENTEREVAL | (1<<16))) 15236 flags |= OPpEVAL_BYTES <<8; 15237 15238 switch (PL_opargs[opnum] & OA_CLASS_MASK) { 15239 case OA_UNOP: 15240 case OA_BASEOP_OR_UNOP: 15241 case OA_FILESTATOP: 15242 if (!aop) 15243 return newOP(opnum,flags); /* zero args */ 15244 if (aop == prev) 15245 return newUNOP(opnum,flags,aop); /* one arg */ 15246 /* too many args */ 15247 /* FALLTHROUGH */ 15248 case OA_BASEOP: 15249 if (aop) { 15250 SV *namesv; 15251 OP *nextop; 15252 15253 namesv = cv_name((CV *)namegv, NULL, CV_NAME_NOTQUAL); 15254 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf, 15255 SVfARG(namesv)), SvUTF8(namesv)); 15256 while (aop) { 15257 nextop = OpSIBLING(aop); 15258 op_free(aop); 15259 aop = nextop; 15260 } 15261 15262 } 15263 return opnum == OP_RUNCV 15264 ? newPVOP(OP_RUNCV,0,NULL) 15265 : newOP(opnum,0); 15266 default: 15267 return op_convert_list(opnum,0,aop); 15268 } 15269 } 15270 NOT_REACHED; /* NOTREACHED */ 15271 return entersubop; 15272 } 15273 15274 /* 15275 =for apidoc cv_get_call_checker_flags 15276 15277 Retrieves the function that will be used to fix up a call to C<cv>. 15278 Specifically, the function is applied to an C<entersub> op tree for a 15279 subroutine call, not marked with C<&>, where the callee can be identified 15280 at compile time as C<cv>. 15281 15282 The C-level function pointer is returned in C<*ckfun_p>, an SV argument 15283 for it is returned in C<*ckobj_p>, and control flags are returned in 15284 C<*ckflags_p>. The function is intended to be called in this manner: 15285 15286 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p)); 15287 15288 In this call, C<entersubop> is a pointer to the C<entersub> op, 15289 which may be replaced by the check function, and C<namegv> supplies 15290 the name that should be used by the check function to refer 15291 to the callee of the C<entersub> op if it needs to emit any diagnostics. 15292 It is permitted to apply the check function in non-standard situations, 15293 such as to a call to a different subroutine or to a method call. 15294 15295 C<namegv> may not actually be a GV. If the C<CALL_CHECKER_REQUIRE_GV> 15296 bit is clear in C<*ckflags_p>, it is permitted to pass a CV or other SV 15297 instead, anything that can be used as the first argument to L</cv_name>. 15298 If the C<CALL_CHECKER_REQUIRE_GV> bit is set in C<*ckflags_p> then the 15299 check function requires C<namegv> to be a genuine GV. 15300 15301 By default, the check function is 15302 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>, 15303 the SV parameter is C<cv> itself, and the C<CALL_CHECKER_REQUIRE_GV> 15304 flag is clear. This implements standard prototype processing. It can 15305 be changed, for a particular subroutine, by L</cv_set_call_checker_flags>. 15306 15307 If the C<CALL_CHECKER_REQUIRE_GV> bit is set in C<gflags> then it 15308 indicates that the caller only knows about the genuine GV version of 15309 C<namegv>, and accordingly the corresponding bit will always be set in 15310 C<*ckflags_p>, regardless of the check function's recorded requirements. 15311 If the C<CALL_CHECKER_REQUIRE_GV> bit is clear in C<gflags> then it 15312 indicates the caller knows about the possibility of passing something 15313 other than a GV as C<namegv>, and accordingly the corresponding bit may 15314 be either set or clear in C<*ckflags_p>, indicating the check function's 15315 recorded requirements. 15316 15317 C<gflags> is a bitset passed into C<cv_get_call_checker_flags>, in which 15318 only the C<CALL_CHECKER_REQUIRE_GV> bit currently has a defined meaning 15319 (for which see above). All other bits should be clear. 15320 15321 =for apidoc Amnh||CALL_CHECKER_REQUIRE_GV 15322 15323 =for apidoc cv_get_call_checker 15324 15325 The original form of L</cv_get_call_checker_flags>, which does not return 15326 checker flags. When using a checker function returned by this function, 15327 it is only safe to call it with a genuine GV as its C<namegv> argument. 15328 15329 =cut 15330 */ 15331 15332 void 15333 Perl_cv_get_call_checker_flags(pTHX_ CV *cv, U32 gflags, 15334 Perl_call_checker *ckfun_p, SV **ckobj_p, U32 *ckflags_p) 15335 { 15336 MAGIC *callmg; 15337 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER_FLAGS; 15338 PERL_UNUSED_CONTEXT; 15339 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL; 15340 if (callmg) { 15341 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr); 15342 *ckobj_p = callmg->mg_obj; 15343 *ckflags_p = (callmg->mg_flags | gflags) & MGf_REQUIRE_GV; 15344 } else { 15345 *ckfun_p = Perl_ck_entersub_args_proto_or_list; 15346 *ckobj_p = (SV*)cv; 15347 *ckflags_p = gflags & MGf_REQUIRE_GV; 15348 } 15349 } 15350 15351 void 15352 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p) 15353 { 15354 U32 ckflags; 15355 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER; 15356 PERL_UNUSED_CONTEXT; 15357 cv_get_call_checker_flags(cv, CALL_CHECKER_REQUIRE_GV, ckfun_p, ckobj_p, 15358 &ckflags); 15359 } 15360 15361 /* 15362 =for apidoc cv_set_call_checker_flags 15363 15364 Sets the function that will be used to fix up a call to C<cv>. 15365 Specifically, the function is applied to an C<entersub> op tree for a 15366 subroutine call, not marked with C<&>, where the callee can be identified 15367 at compile time as C<cv>. 15368 15369 The C-level function pointer is supplied in C<ckfun>, an SV argument for 15370 it is supplied in C<ckobj>, and control flags are supplied in C<ckflags>. 15371 The function should be defined like this: 15372 15373 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj) 15374 15375 It is intended to be called in this manner: 15376 15377 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj); 15378 15379 In this call, C<entersubop> is a pointer to the C<entersub> op, 15380 which may be replaced by the check function, and C<namegv> supplies 15381 the name that should be used by the check function to refer 15382 to the callee of the C<entersub> op if it needs to emit any diagnostics. 15383 It is permitted to apply the check function in non-standard situations, 15384 such as to a call to a different subroutine or to a method call. 15385 15386 C<namegv> may not actually be a GV. For efficiency, perl may pass a 15387 CV or other SV instead. Whatever is passed can be used as the first 15388 argument to L</cv_name>. You can force perl to pass a GV by including 15389 C<CALL_CHECKER_REQUIRE_GV> in the C<ckflags>. 15390 15391 C<ckflags> is a bitset, in which only the C<CALL_CHECKER_REQUIRE_GV> 15392 bit currently has a defined meaning (for which see above). All other 15393 bits should be clear. 15394 15395 The current setting for a particular CV can be retrieved by 15396 L</cv_get_call_checker_flags>. 15397 15398 =for apidoc cv_set_call_checker 15399 15400 The original form of L</cv_set_call_checker_flags>, which passes it the 15401 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility. The effect 15402 of that flag setting is that the check function is guaranteed to get a 15403 genuine GV as its C<namegv> argument. 15404 15405 =cut 15406 */ 15407 15408 void 15409 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj) 15410 { 15411 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER; 15412 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV); 15413 } 15414 15415 void 15416 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun, 15417 SV *ckobj, U32 ckflags) 15418 { 15419 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS; 15420 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) { 15421 if (SvMAGICAL((SV*)cv)) 15422 mg_free_type((SV*)cv, PERL_MAGIC_checkcall); 15423 } else { 15424 MAGIC *callmg; 15425 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0); 15426 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall); 15427 assert(callmg); 15428 if (callmg->mg_flags & MGf_REFCOUNTED) { 15429 SvREFCNT_dec(callmg->mg_obj); 15430 callmg->mg_flags &= ~MGf_REFCOUNTED; 15431 } 15432 callmg->mg_ptr = FPTR2DPTR(char *, ckfun); 15433 callmg->mg_obj = ckobj; 15434 if (ckobj != (SV*)cv) { 15435 SvREFCNT_inc_simple_void_NN(ckobj); 15436 callmg->mg_flags |= MGf_REFCOUNTED; 15437 } 15438 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV) 15439 | (U8)(ckflags & MGf_REQUIRE_GV) | MGf_COPY; 15440 } 15441 } 15442 15443 static void 15444 S_entersub_alloc_targ(pTHX_ OP * const o) 15445 { 15446 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP); 15447 o->op_private |= OPpENTERSUB_HASTARG; 15448 } 15449 15450 OP * 15451 Perl_ck_subr(pTHX_ OP *o) 15452 { 15453 OP *aop, *cvop; 15454 CV *cv; 15455 GV *namegv; 15456 SV **const_class = NULL; 15457 15458 PERL_ARGS_ASSERT_CK_SUBR; 15459 15460 aop = cUNOPx(o)->op_first; 15461 if (!OpHAS_SIBLING(aop)) 15462 aop = cUNOPx(aop)->op_first; 15463 aop = OpSIBLING(aop); 15464 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 15465 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY); 15466 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL; 15467 15468 o->op_private &= ~1; 15469 o->op_private |= (PL_hints & HINT_STRICT_REFS); 15470 if (PERLDB_SUB && PL_curstash != PL_debstash) 15471 o->op_private |= OPpENTERSUB_DB; 15472 switch (cvop->op_type) { 15473 case OP_RV2CV: 15474 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER); 15475 op_null(cvop); 15476 break; 15477 case OP_METHOD: 15478 case OP_METHOD_NAMED: 15479 case OP_METHOD_SUPER: 15480 case OP_METHOD_REDIR: 15481 case OP_METHOD_REDIR_SUPER: 15482 o->op_flags |= OPf_REF; 15483 if (aop->op_type == OP_CONST) { 15484 aop->op_private &= ~OPpCONST_STRICT; 15485 const_class = &cSVOPx(aop)->op_sv; 15486 } 15487 else if (aop->op_type == OP_LIST) { 15488 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first); 15489 if (sib && sib->op_type == OP_CONST) { 15490 sib->op_private &= ~OPpCONST_STRICT; 15491 const_class = &cSVOPx(sib)->op_sv; 15492 } 15493 } 15494 /* make class name a shared cow string to speedup method calls */ 15495 /* constant string might be replaced with object, f.e. bigint */ 15496 if (const_class && SvPOK(*const_class)) { 15497 STRLEN len; 15498 const char* str = SvPV(*const_class, len); 15499 if (len) { 15500 SV* const shared = newSVpvn_share( 15501 str, SvUTF8(*const_class) 15502 ? -(SSize_t)len : (SSize_t)len, 15503 0 15504 ); 15505 if (SvREADONLY(*const_class)) 15506 SvREADONLY_on(shared); 15507 SvREFCNT_dec(*const_class); 15508 *const_class = shared; 15509 } 15510 } 15511 break; 15512 } 15513 15514 if (!cv) { 15515 S_entersub_alloc_targ(aTHX_ o); 15516 return ck_entersub_args_list(o); 15517 } else { 15518 Perl_call_checker ckfun; 15519 SV *ckobj; 15520 U32 ckflags; 15521 cv_get_call_checker_flags(cv, 0, &ckfun, &ckobj, &ckflags); 15522 if (CvISXSUB(cv) || !CvROOT(cv)) 15523 S_entersub_alloc_targ(aTHX_ o); 15524 if (!namegv) { 15525 /* The original call checker API guarantees that a GV will 15526 be provided with the right name. So, if the old API was 15527 used (or the REQUIRE_GV flag was passed), we have to reify 15528 the CV’s GV, unless this is an anonymous sub. This is not 15529 ideal for lexical subs, as its stringification will include 15530 the package. But it is the best we can do. */ 15531 if (ckflags & CALL_CHECKER_REQUIRE_GV) { 15532 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv))) 15533 namegv = CvGV(cv); 15534 } 15535 else namegv = MUTABLE_GV(cv); 15536 /* After a syntax error in a lexical sub, the cv that 15537 rv2cv_op_cv returns may be a nameless stub. */ 15538 if (!namegv) return ck_entersub_args_list(o); 15539 15540 } 15541 return ckfun(aTHX_ o, namegv, ckobj); 15542 } 15543 } 15544 15545 OP * 15546 Perl_ck_svconst(pTHX_ OP *o) 15547 { 15548 SV * const sv = cSVOPo->op_sv; 15549 PERL_ARGS_ASSERT_CK_SVCONST; 15550 PERL_UNUSED_CONTEXT; 15551 #ifdef PERL_COPY_ON_WRITE 15552 /* Since the read-only flag may be used to protect a string buffer, we 15553 cannot do copy-on-write with existing read-only scalars that are not 15554 already copy-on-write scalars. To allow $_ = "hello" to do COW with 15555 that constant, mark the constant as COWable here, if it is not 15556 already read-only. */ 15557 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) { 15558 SvIsCOW_on(sv); 15559 CowREFCNT(sv) = 0; 15560 # ifdef PERL_DEBUG_READONLY_COW 15561 sv_buf_to_ro(sv); 15562 # endif 15563 } 15564 #endif 15565 SvREADONLY_on(sv); 15566 return o; 15567 } 15568 15569 OP * 15570 Perl_ck_trunc(pTHX_ OP *o) 15571 { 15572 PERL_ARGS_ASSERT_CK_TRUNC; 15573 15574 if (o->op_flags & OPf_KIDS) { 15575 SVOP *kid = (SVOP*)cUNOPo->op_first; 15576 15577 if (kid->op_type == OP_NULL) 15578 kid = (SVOP*)OpSIBLING(kid); 15579 if (kid && kid->op_type == OP_CONST && 15580 (kid->op_private & OPpCONST_BARE) && 15581 !kid->op_folded) 15582 { 15583 o->op_flags |= OPf_SPECIAL; 15584 kid->op_private &= ~OPpCONST_STRICT; 15585 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED) { 15586 no_bareword_filehandle(SvPVX(cSVOPx_sv(kid))); 15587 } 15588 } 15589 } 15590 return ck_fun(o); 15591 } 15592 15593 OP * 15594 Perl_ck_substr(pTHX_ OP *o) 15595 { 15596 PERL_ARGS_ASSERT_CK_SUBSTR; 15597 15598 o = ck_fun(o); 15599 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) { 15600 OP *kid = cLISTOPo->op_first; 15601 15602 if (kid->op_type == OP_NULL) 15603 kid = OpSIBLING(kid); 15604 if (kid) 15605 /* Historically, substr(delete $foo{bar},...) has been allowed 15606 with 4-arg substr. Keep it working by applying entersub 15607 lvalue context. */ 15608 op_lvalue(kid, OP_ENTERSUB); 15609 15610 } 15611 return o; 15612 } 15613 15614 OP * 15615 Perl_ck_tell(pTHX_ OP *o) 15616 { 15617 PERL_ARGS_ASSERT_CK_TELL; 15618 o = ck_fun(o); 15619 if (o->op_flags & OPf_KIDS) { 15620 OP *kid = cLISTOPo->op_first; 15621 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid); 15622 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE; 15623 } 15624 return o; 15625 } 15626 15627 PERL_STATIC_INLINE OP * 15628 S_last_non_null_kid(OP *o) { 15629 OP *last = NULL; 15630 if (cUNOPo->op_flags & OPf_KIDS) { 15631 OP *k = cLISTOPo->op_first; 15632 while (k) { 15633 if (k->op_type != OP_NULL) { 15634 last = k; 15635 } 15636 k = OpSIBLING(k); 15637 } 15638 } 15639 15640 return last; 15641 } 15642 15643 OP * 15644 Perl_ck_each(pTHX_ OP *o) 15645 { 15646 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL; 15647 const unsigned orig_type = o->op_type; 15648 15649 PERL_ARGS_ASSERT_CK_EACH; 15650 15651 if (kid) { 15652 switch (kid->op_type) { 15653 case OP_PADHV: 15654 break; 15655 15656 case OP_RV2HV: 15657 /* Catch out an anonhash here, since the behaviour might be 15658 * confusing. 15659 * 15660 * The typical tree is: 15661 * 15662 * rv2hv 15663 * scope 15664 * null 15665 * anonhash 15666 * 15667 * If the contents of the block is more complex you might get: 15668 * 15669 * rv2hv 15670 * leave 15671 * enter 15672 * ... 15673 * anonhash 15674 * 15675 * Similarly for the anonlist version below. 15676 */ 15677 if (orig_type == OP_EACH && 15678 ckWARN(WARN_SYNTAX) && 15679 (cUNOPx(kid)->op_flags & OPf_KIDS) && 15680 ( cUNOPx(kid)->op_first->op_type == OP_SCOPE || 15681 cUNOPx(kid)->op_first->op_type == OP_LEAVE) && 15682 (cUNOPx(kid)->op_first->op_flags & OPf_KIDS)) { 15683 /* look for last non-null kid, since we might have: 15684 each %{ some code ; +{ anon hash } } 15685 */ 15686 OP *k = S_last_non_null_kid(cUNOPx(kid)->op_first); 15687 if (k && k->op_type == OP_ANONHASH) { 15688 /* diag_listed_as: each on anonymous %s will always start from the beginning */ 15689 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "each on anonymous hash will always start from the beginning"); 15690 } 15691 } 15692 break; 15693 case OP_RV2AV: 15694 if (orig_type == OP_EACH && 15695 ckWARN(WARN_SYNTAX) && 15696 (cUNOPx(kid)->op_flags & OPf_KIDS) && 15697 (cUNOPx(kid)->op_first->op_type == OP_SCOPE || 15698 cUNOPx(kid)->op_first->op_type == OP_LEAVE) && 15699 (cUNOPx(kid)->op_first->op_flags & OPf_KIDS)) { 15700 OP *k = S_last_non_null_kid(cUNOPx(kid)->op_first); 15701 if (k && k->op_type == OP_ANONLIST) { 15702 /* diag_listed_as: each on anonymous %s will always start from the beginning */ 15703 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "each on anonymous array will always start from the beginning"); 15704 } 15705 } 15706 /* FALLTHROUGH */ 15707 case OP_PADAV: 15708 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH 15709 : orig_type == OP_KEYS ? OP_AKEYS 15710 : OP_AVALUES); 15711 break; 15712 case OP_CONST: 15713 if (kid->op_private == OPpCONST_BARE 15714 || !SvROK(cSVOPx_sv(kid)) 15715 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV 15716 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV ) 15717 ) 15718 goto bad; 15719 /* FALLTHROUGH */ 15720 default: 15721 qerror(Perl_mess(aTHX_ 15722 "Experimental %s on scalar is now forbidden", 15723 PL_op_desc[orig_type])); 15724 bad: 15725 bad_type_pv(1, "hash or array", o, kid); 15726 return o; 15727 } 15728 } 15729 return ck_fun(o); 15730 } 15731 15732 OP * 15733 Perl_ck_length(pTHX_ OP *o) 15734 { 15735 PERL_ARGS_ASSERT_CK_LENGTH; 15736 15737 o = ck_fun(o); 15738 15739 if (ckWARN(WARN_SYNTAX)) { 15740 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL; 15741 15742 if (kid) { 15743 SV *name = NULL; 15744 const bool hash = kid->op_type == OP_PADHV 15745 || kid->op_type == OP_RV2HV; 15746 switch (kid->op_type) { 15747 case OP_PADHV: 15748 case OP_PADAV: 15749 case OP_RV2HV: 15750 case OP_RV2AV: 15751 name = S_op_varname(aTHX_ kid); 15752 break; 15753 default: 15754 return o; 15755 } 15756 if (name) 15757 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 15758 "length() used on %" SVf " (did you mean \"scalar(%s%" SVf 15759 ")\"?)", 15760 SVfARG(name), hash ? "keys " : "", SVfARG(name) 15761 ); 15762 else if (hash) 15763 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */ 15764 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 15765 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)"); 15766 else 15767 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */ 15768 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 15769 "length() used on @array (did you mean \"scalar(@array)\"?)"); 15770 } 15771 } 15772 15773 return o; 15774 } 15775 15776 15777 OP * 15778 Perl_ck_isa(pTHX_ OP *o) 15779 { 15780 OP *classop = cBINOPo->op_last; 15781 15782 PERL_ARGS_ASSERT_CK_ISA; 15783 15784 /* Convert barename into PV */ 15785 if(classop->op_type == OP_CONST && classop->op_private & OPpCONST_BARE) { 15786 /* TODO: Optionally convert package to raw HV here */ 15787 classop->op_private &= ~(OPpCONST_BARE|OPpCONST_STRICT); 15788 } 15789 15790 return o; 15791 } 15792 15793 15794 /* 15795 --------------------------------------------------------- 15796 15797 Common vars in list assignment 15798 15799 There now follows some enums and static functions for detecting 15800 common variables in list assignments. Here is a little essay I wrote 15801 for myself when trying to get my head around this. DAPM. 15802 15803 ---- 15804 15805 First some random observations: 15806 15807 * If a lexical var is an alias of something else, e.g. 15808 for my $x ($lex, $pkg, $a[0]) {...} 15809 then the act of aliasing will increase the reference count of the SV 15810 15811 * If a package var is an alias of something else, it may still have a 15812 reference count of 1, depending on how the alias was created, e.g. 15813 in *a = *b, $a may have a refcount of 1 since the GP is shared 15814 with a single GvSV pointer to the SV. So If it's an alias of another 15815 package var, then RC may be 1; if it's an alias of another scalar, e.g. 15816 a lexical var or an array element, then it will have RC > 1. 15817 15818 * There are many ways to create a package alias; ultimately, XS code 15819 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so 15820 run-time tracing mechanisms are unlikely to be able to catch all cases. 15821 15822 * When the LHS is all my declarations, the same vars can't appear directly 15823 on the RHS, but they can indirectly via closures, aliasing and lvalue 15824 subs. But those techniques all involve an increase in the lexical 15825 scalar's ref count. 15826 15827 * When the LHS is all lexical vars (but not necessarily my declarations), 15828 it is possible for the same lexicals to appear directly on the RHS, and 15829 without an increased ref count, since the stack isn't refcounted. 15830 This case can be detected at compile time by scanning for common lex 15831 vars with PL_generation. 15832 15833 * lvalue subs defeat common var detection, but they do at least 15834 return vars with a temporary ref count increment. Also, you can't 15835 tell at compile time whether a sub call is lvalue. 15836 15837 15838 So... 15839 15840 A: There are a few circumstances where there definitely can't be any 15841 commonality: 15842 15843 LHS empty: () = (...); 15844 RHS empty: (....) = (); 15845 RHS contains only constants or other 'can't possibly be shared' 15846 elements (e.g. ops that return PADTMPs): (...) = (1,2, length) 15847 i.e. they only contain ops not marked as dangerous, whose children 15848 are also not dangerous; 15849 LHS ditto; 15850 LHS contains a single scalar element: e.g. ($x) = (....); because 15851 after $x has been modified, it won't be used again on the RHS; 15852 RHS contains a single element with no aggregate on LHS: e.g. 15853 ($a,$b,$c) = ($x); again, once $a has been modified, its value 15854 won't be used again. 15855 15856 B: If LHS are all 'my' lexical var declarations (or safe ops, which 15857 we can ignore): 15858 15859 my ($a, $b, @c) = ...; 15860 15861 Due to closure and goto tricks, these vars may already have content. 15862 For the same reason, an element on the RHS may be a lexical or package 15863 alias of one of the vars on the left, or share common elements, for 15864 example: 15865 15866 my ($x,$y) = f(); # $x and $y on both sides 15867 sub f : lvalue { ($x,$y) = (1,2); $y, $x } 15868 15869 and 15870 15871 my $ra = f(); 15872 my @a = @$ra; # elements of @a on both sides 15873 sub f { @a = 1..4; \@a } 15874 15875 15876 First, just consider scalar vars on LHS: 15877 15878 RHS is safe only if (A), or in addition, 15879 * contains only lexical *scalar* vars, where neither side's 15880 lexicals have been flagged as aliases 15881 15882 If RHS is not safe, then it's always legal to check LHS vars for 15883 RC==1, since the only RHS aliases will always be associated 15884 with an RC bump. 15885 15886 Note that in particular, RHS is not safe if: 15887 15888 * it contains package scalar vars; e.g.: 15889 15890 f(); 15891 my ($x, $y) = (2, $x_alias); 15892 sub f { $x = 1; *x_alias = \$x; } 15893 15894 * It contains other general elements, such as flattened or 15895 * spliced or single array or hash elements, e.g. 15896 15897 f(); 15898 my ($x,$y) = @a; # or $a[0] or @a{@b} etc 15899 15900 sub f { 15901 ($x, $y) = (1,2); 15902 use feature 'refaliasing'; 15903 \($a[0], $a[1]) = \($y,$x); 15904 } 15905 15906 It doesn't matter if the array/hash is lexical or package. 15907 15908 * it contains a function call that happens to be an lvalue 15909 sub which returns one or more of the above, e.g. 15910 15911 f(); 15912 my ($x,$y) = f(); 15913 15914 sub f : lvalue { 15915 ($x, $y) = (1,2); 15916 *x1 = \$x; 15917 $y, $x1; 15918 } 15919 15920 (so a sub call on the RHS should be treated the same 15921 as having a package var on the RHS). 15922 15923 * any other "dangerous" thing, such an op or built-in that 15924 returns one of the above, e.g. pp_preinc 15925 15926 15927 If RHS is not safe, what we can do however is at compile time flag 15928 that the LHS are all my declarations, and at run time check whether 15929 all the LHS have RC == 1, and if so skip the full scan. 15930 15931 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...; 15932 15933 Here the issue is whether there can be elements of @a on the RHS 15934 which will get prematurely freed when @a is cleared prior to 15935 assignment. This is only a problem if the aliasing mechanism 15936 is one which doesn't increase the refcount - only if RC == 1 15937 will the RHS element be prematurely freed. 15938 15939 Because the array/hash is being INTROed, it or its elements 15940 can't directly appear on the RHS: 15941 15942 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE 15943 15944 but can indirectly, e.g.: 15945 15946 my $r = f(); 15947 my (@a) = @$r; 15948 sub f { @a = 1..3; \@a } 15949 15950 So if the RHS isn't safe as defined by (A), we must always 15951 mortalise and bump the ref count of any remaining RHS elements 15952 when assigning to a non-empty LHS aggregate. 15953 15954 Lexical scalars on the RHS aren't safe if they've been involved in 15955 aliasing, e.g. 15956 15957 use feature 'refaliasing'; 15958 15959 f(); 15960 \(my $lex) = \$pkg; 15961 my @a = ($lex,3); # equivalent to ($a[0],3) 15962 15963 sub f { 15964 @a = (1,2); 15965 \$pkg = \$a[0]; 15966 } 15967 15968 Similarly with lexical arrays and hashes on the RHS: 15969 15970 f(); 15971 my @b; 15972 my @a = (@b); 15973 15974 sub f { 15975 @a = (1,2); 15976 \$b[0] = \$a[1]; 15977 \$b[1] = \$a[0]; 15978 } 15979 15980 15981 15982 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g. 15983 my $a; ($a, my $b) = (....); 15984 15985 The difference between (B) and (C) is that it is now physically 15986 possible for the LHS vars to appear on the RHS too, where they 15987 are not reference counted; but in this case, the compile-time 15988 PL_generation sweep will detect such common vars. 15989 15990 So the rules for (C) differ from (B) in that if common vars are 15991 detected, the runtime "test RC==1" optimisation can no longer be used, 15992 and a full mark and sweep is required 15993 15994 D: As (C), but in addition the LHS may contain package vars. 15995 15996 Since package vars can be aliased without a corresponding refcount 15997 increase, all bets are off. It's only safe if (A). E.g. 15998 15999 my ($x, $y) = (1,2); 16000 16001 for $x_alias ($x) { 16002 ($x_alias, $y) = (3, $x); # whoops 16003 } 16004 16005 Ditto for LHS aggregate package vars. 16006 16007 E: Any other dangerous ops on LHS, e.g. 16008 (f(), $a[0], @$r) = (...); 16009 16010 this is similar to (E) in that all bets are off. In addition, it's 16011 impossible to determine at compile time whether the LHS 16012 contains a scalar or an aggregate, e.g. 16013 16014 sub f : lvalue { @a } 16015 (f()) = 1..3; 16016 16017 * --------------------------------------------------------- 16018 */ 16019 16020 16021 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates 16022 * that at least one of the things flagged was seen. 16023 */ 16024 16025 enum { 16026 AAS_MY_SCALAR = 0x001, /* my $scalar */ 16027 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */ 16028 AAS_LEX_SCALAR = 0x004, /* $lexical */ 16029 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */ 16030 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */ 16031 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */ 16032 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */ 16033 AAS_DANGEROUS = 0x080, /* an op (other than the above) 16034 that's flagged OA_DANGEROUS */ 16035 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's 16036 not in any of the categories above */ 16037 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */ 16038 }; 16039 16040 16041 16042 /* helper function for S_aassign_scan(). 16043 * check a PAD-related op for commonality and/or set its generation number. 16044 * Returns a boolean indicating whether its shared */ 16045 16046 static bool 16047 S_aassign_padcheck(pTHX_ OP* o, bool rhs) 16048 { 16049 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX) 16050 /* lexical used in aliasing */ 16051 return TRUE; 16052 16053 if (rhs) 16054 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation); 16055 else 16056 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation); 16057 16058 return FALSE; 16059 } 16060 16061 16062 /* 16063 Helper function for OPpASSIGN_COMMON* detection in rpeep(). 16064 It scans the left or right hand subtree of the aassign op, and returns a 16065 set of flags indicating what sorts of things it found there. 16066 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we 16067 set PL_generation on lexical vars; if the latter, we see if 16068 PL_generation matches. 16069 'scalars_p' is a pointer to a counter of the number of scalar SVs seen. 16070 This fn will increment it by the number seen. It's not intended to 16071 be an accurate count (especially as many ops can push a variable 16072 number of SVs onto the stack); rather it's used as to test whether there 16073 can be at most 1 SV pushed; so it's only meanings are "0, 1, many". 16074 */ 16075 16076 static int 16077 S_aassign_scan(pTHX_ OP* o, bool rhs, int *scalars_p) 16078 { 16079 OP *top_op = o; 16080 OP *effective_top_op = o; 16081 int all_flags = 0; 16082 16083 while (1) { 16084 bool top = o == effective_top_op; 16085 int flags = 0; 16086 OP* next_kid = NULL; 16087 16088 /* first, look for a solitary @_ on the RHS */ 16089 if ( rhs 16090 && top 16091 && (o->op_flags & OPf_KIDS) 16092 && OP_TYPE_IS_OR_WAS(o, OP_LIST) 16093 ) { 16094 OP *kid = cUNOPo->op_first; 16095 if ( ( kid->op_type == OP_PUSHMARK 16096 || kid->op_type == OP_PADRANGE) /* ex-pushmark */ 16097 && ((kid = OpSIBLING(kid))) 16098 && !OpHAS_SIBLING(kid) 16099 && kid->op_type == OP_RV2AV 16100 && !(kid->op_flags & OPf_REF) 16101 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB)) 16102 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST) 16103 && ((kid = cUNOPx(kid)->op_first)) 16104 && kid->op_type == OP_GV 16105 && cGVOPx_gv(kid) == PL_defgv 16106 ) 16107 flags = AAS_DEFAV; 16108 } 16109 16110 switch (o->op_type) { 16111 case OP_GVSV: 16112 (*scalars_p)++; 16113 all_flags |= AAS_PKG_SCALAR; 16114 goto do_next; 16115 16116 case OP_PADAV: 16117 case OP_PADHV: 16118 (*scalars_p) += 2; 16119 /* if !top, could be e.g. @a[0,1] */ 16120 all_flags |= (top && (o->op_flags & OPf_REF)) 16121 ? ((o->op_private & OPpLVAL_INTRO) 16122 ? AAS_MY_AGG : AAS_LEX_AGG) 16123 : AAS_DANGEROUS; 16124 goto do_next; 16125 16126 case OP_PADSV: 16127 { 16128 int comm = S_aassign_padcheck(aTHX_ o, rhs) 16129 ? AAS_LEX_SCALAR_COMM : 0; 16130 (*scalars_p)++; 16131 all_flags |= (o->op_private & OPpLVAL_INTRO) 16132 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm); 16133 goto do_next; 16134 16135 } 16136 16137 case OP_RV2AV: 16138 case OP_RV2HV: 16139 (*scalars_p) += 2; 16140 if (cUNOPx(o)->op_first->op_type != OP_GV) 16141 all_flags |= AAS_DANGEROUS; /* @{expr}, %{expr} */ 16142 /* @pkg, %pkg */ 16143 /* if !top, could be e.g. @a[0,1] */ 16144 else if (top && (o->op_flags & OPf_REF)) 16145 all_flags |= AAS_PKG_AGG; 16146 else 16147 all_flags |= AAS_DANGEROUS; 16148 goto do_next; 16149 16150 case OP_RV2SV: 16151 (*scalars_p)++; 16152 if (cUNOPx(o)->op_first->op_type != OP_GV) { 16153 (*scalars_p) += 2; 16154 all_flags |= AAS_DANGEROUS; /* ${expr} */ 16155 } 16156 else 16157 all_flags |= AAS_PKG_SCALAR; /* $pkg */ 16158 goto do_next; 16159 16160 case OP_SPLIT: 16161 if (o->op_private & OPpSPLIT_ASSIGN) { 16162 /* the assign in @a = split() has been optimised away 16163 * and the @a attached directly to the split op 16164 * Treat the array as appearing on the RHS, i.e. 16165 * ... = (@a = split) 16166 * is treated like 16167 * ... = @a; 16168 */ 16169 16170 if (o->op_flags & OPf_STACKED) { 16171 /* @{expr} = split() - the array expression is tacked 16172 * on as an extra child to split - process kid */ 16173 next_kid = cLISTOPo->op_last; 16174 goto do_next; 16175 } 16176 16177 /* ... else array is directly attached to split op */ 16178 (*scalars_p) += 2; 16179 all_flags |= (PL_op->op_private & OPpSPLIT_LEX) 16180 ? ((o->op_private & OPpLVAL_INTRO) 16181 ? AAS_MY_AGG : AAS_LEX_AGG) 16182 : AAS_PKG_AGG; 16183 goto do_next; 16184 } 16185 (*scalars_p)++; 16186 /* other args of split can't be returned */ 16187 all_flags |= AAS_SAFE_SCALAR; 16188 goto do_next; 16189 16190 case OP_UNDEF: 16191 /* undef on LHS following a var is significant, e.g. 16192 * my $x = 1; 16193 * @a = (($x, undef) = (2 => $x)); 16194 * # @a shoul be (2,1) not (2,2) 16195 * 16196 * undef on RHS counts as a scalar: 16197 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe 16198 */ 16199 if ((!rhs && *scalars_p) || rhs) 16200 (*scalars_p)++; 16201 flags = AAS_SAFE_SCALAR; 16202 break; 16203 16204 case OP_PUSHMARK: 16205 case OP_STUB: 16206 /* these are all no-ops; they don't push a potentially common SV 16207 * onto the stack, so they are neither AAS_DANGEROUS nor 16208 * AAS_SAFE_SCALAR */ 16209 goto do_next; 16210 16211 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */ 16212 break; 16213 16214 case OP_NULL: 16215 case OP_LIST: 16216 /* these do nothing, but may have children */ 16217 break; 16218 16219 default: 16220 if (PL_opargs[o->op_type] & OA_DANGEROUS) { 16221 (*scalars_p) += 2; 16222 flags = AAS_DANGEROUS; 16223 break; 16224 } 16225 16226 if ( (PL_opargs[o->op_type] & OA_TARGLEX) 16227 && (o->op_private & OPpTARGET_MY)) 16228 { 16229 (*scalars_p)++; 16230 all_flags |= S_aassign_padcheck(aTHX_ o, rhs) 16231 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR; 16232 goto do_next; 16233 } 16234 16235 /* if its an unrecognised, non-dangerous op, assume that it 16236 * is the cause of at least one safe scalar */ 16237 (*scalars_p)++; 16238 flags = AAS_SAFE_SCALAR; 16239 break; 16240 } 16241 16242 all_flags |= flags; 16243 16244 /* by default, process all kids next 16245 * XXX this assumes that all other ops are "transparent" - i.e. that 16246 * they can return some of their children. While this true for e.g. 16247 * sort and grep, it's not true for e.g. map. We really need a 16248 * 'transparent' flag added to regen/opcodes 16249 */ 16250 if (o->op_flags & OPf_KIDS) { 16251 next_kid = cUNOPo->op_first; 16252 /* these ops do nothing but may have children; but their 16253 * children should also be treated as top-level */ 16254 if ( o == effective_top_op 16255 && (o->op_type == OP_NULL || o->op_type == OP_LIST) 16256 ) 16257 effective_top_op = next_kid; 16258 } 16259 16260 16261 /* If next_kid is set, someone in the code above wanted us to process 16262 * that kid and all its remaining siblings. Otherwise, work our way 16263 * back up the tree */ 16264 do_next: 16265 while (!next_kid) { 16266 if (o == top_op) 16267 return all_flags; /* at top; no parents/siblings to try */ 16268 if (OpHAS_SIBLING(o)) { 16269 next_kid = o->op_sibparent; 16270 if (o == effective_top_op) 16271 effective_top_op = next_kid; 16272 } 16273 else 16274 if (o == effective_top_op) 16275 effective_top_op = o->op_sibparent; 16276 o = o->op_sibparent; /* try parent's next sibling */ 16277 16278 } 16279 o = next_kid; 16280 } /* while */ 16281 16282 } 16283 16284 16285 /* Check for in place reverse and sort assignments like "@a = reverse @a" 16286 and modify the optree to make them work inplace */ 16287 16288 STATIC void 16289 S_inplace_aassign(pTHX_ OP *o) { 16290 16291 OP *modop, *modop_pushmark; 16292 OP *oright; 16293 OP *oleft, *oleft_pushmark; 16294 16295 PERL_ARGS_ASSERT_INPLACE_AASSIGN; 16296 16297 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID); 16298 16299 assert(cUNOPo->op_first->op_type == OP_NULL); 16300 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first; 16301 assert(modop_pushmark->op_type == OP_PUSHMARK); 16302 modop = OpSIBLING(modop_pushmark); 16303 16304 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE) 16305 return; 16306 16307 /* no other operation except sort/reverse */ 16308 if (OpHAS_SIBLING(modop)) 16309 return; 16310 16311 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK); 16312 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return; 16313 16314 if (modop->op_flags & OPf_STACKED) { 16315 /* skip sort subroutine/block */ 16316 assert(oright->op_type == OP_NULL); 16317 oright = OpSIBLING(oright); 16318 } 16319 16320 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL); 16321 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first; 16322 assert(oleft_pushmark->op_type == OP_PUSHMARK); 16323 oleft = OpSIBLING(oleft_pushmark); 16324 16325 /* Check the lhs is an array */ 16326 if (!oleft || 16327 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV) 16328 || OpHAS_SIBLING(oleft) 16329 || (oleft->op_private & OPpLVAL_INTRO) 16330 ) 16331 return; 16332 16333 /* Only one thing on the rhs */ 16334 if (OpHAS_SIBLING(oright)) 16335 return; 16336 16337 /* check the array is the same on both sides */ 16338 if (oleft->op_type == OP_RV2AV) { 16339 if (oright->op_type != OP_RV2AV 16340 || !cUNOPx(oright)->op_first 16341 || cUNOPx(oright)->op_first->op_type != OP_GV 16342 || cUNOPx(oleft )->op_first->op_type != OP_GV 16343 || cGVOPx_gv(cUNOPx(oleft)->op_first) != 16344 cGVOPx_gv(cUNOPx(oright)->op_first) 16345 ) 16346 return; 16347 } 16348 else if (oright->op_type != OP_PADAV 16349 || oright->op_targ != oleft->op_targ 16350 ) 16351 return; 16352 16353 /* This actually is an inplace assignment */ 16354 16355 modop->op_private |= OPpSORT_INPLACE; 16356 16357 /* transfer MODishness etc from LHS arg to RHS arg */ 16358 oright->op_flags = oleft->op_flags; 16359 16360 /* remove the aassign op and the lhs */ 16361 op_null(o); 16362 op_null(oleft_pushmark); 16363 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first) 16364 op_null(cUNOPx(oleft)->op_first); 16365 op_null(oleft); 16366 } 16367 16368 16369 16370 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start' 16371 * that potentially represent a series of one or more aggregate derefs 16372 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert 16373 * the whole chain to a single OP_MULTIDEREF op (maybe with a few 16374 * additional ops left in too). 16375 * 16376 * The caller will have already verified that the first few ops in the 16377 * chain following 'start' indicate a multideref candidate, and will have 16378 * set 'orig_o' to the point further on in the chain where the first index 16379 * expression (if any) begins. 'orig_action' specifies what type of 16380 * beginning has already been determined by the ops between start..orig_o 16381 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc). 16382 * 16383 * 'hints' contains any hints flags that need adding (currently just 16384 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller. 16385 */ 16386 16387 STATIC void 16388 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints) 16389 { 16390 int pass; 16391 UNOP_AUX_item *arg_buf = NULL; 16392 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */ 16393 int index_skip = -1; /* don't output index arg on this action */ 16394 16395 /* similar to regex compiling, do two passes; the first pass 16396 * determines whether the op chain is convertible and calculates the 16397 * buffer size; the second pass populates the buffer and makes any 16398 * changes necessary to ops (such as moving consts to the pad on 16399 * threaded builds). 16400 * 16401 * NB: for things like Coverity, note that both passes take the same 16402 * path through the logic tree (except for 'if (pass)' bits), since 16403 * both passes are following the same op_next chain; and in 16404 * particular, if it would return early on the second pass, it would 16405 * already have returned early on the first pass. 16406 */ 16407 for (pass = 0; pass < 2; pass++) { 16408 OP *o = orig_o; 16409 UV action = orig_action; 16410 OP *first_elem_op = NULL; /* first seen aelem/helem */ 16411 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */ 16412 int action_count = 0; /* number of actions seen so far */ 16413 int action_ix = 0; /* action_count % (actions per IV) */ 16414 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */ 16415 bool is_last = FALSE; /* no more derefs to follow */ 16416 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */ 16417 UV action_word = 0; /* all actions so far */ 16418 UNOP_AUX_item *arg = arg_buf; 16419 UNOP_AUX_item *action_ptr = arg_buf; 16420 16421 arg++; /* reserve slot for first action word */ 16422 16423 switch (action) { 16424 case MDEREF_HV_gvsv_vivify_rv2hv_helem: 16425 case MDEREF_HV_gvhv_helem: 16426 next_is_hash = TRUE; 16427 /* FALLTHROUGH */ 16428 case MDEREF_AV_gvsv_vivify_rv2av_aelem: 16429 case MDEREF_AV_gvav_aelem: 16430 if (pass) { 16431 #ifdef USE_ITHREADS 16432 arg->pad_offset = cPADOPx(start)->op_padix; 16433 /* stop it being swiped when nulled */ 16434 cPADOPx(start)->op_padix = 0; 16435 #else 16436 arg->sv = cSVOPx(start)->op_sv; 16437 cSVOPx(start)->op_sv = NULL; 16438 #endif 16439 } 16440 arg++; 16441 break; 16442 16443 case MDEREF_HV_padhv_helem: 16444 case MDEREF_HV_padsv_vivify_rv2hv_helem: 16445 next_is_hash = TRUE; 16446 /* FALLTHROUGH */ 16447 case MDEREF_AV_padav_aelem: 16448 case MDEREF_AV_padsv_vivify_rv2av_aelem: 16449 if (pass) { 16450 arg->pad_offset = start->op_targ; 16451 /* we skip setting op_targ = 0 for now, since the intact 16452 * OP_PADXV is needed by S_check_hash_fields_and_hekify */ 16453 reset_start_targ = TRUE; 16454 } 16455 arg++; 16456 break; 16457 16458 case MDEREF_HV_pop_rv2hv_helem: 16459 next_is_hash = TRUE; 16460 /* FALLTHROUGH */ 16461 case MDEREF_AV_pop_rv2av_aelem: 16462 break; 16463 16464 default: 16465 NOT_REACHED; /* NOTREACHED */ 16466 return; 16467 } 16468 16469 while (!is_last) { 16470 /* look for another (rv2av/hv; get index; 16471 * aelem/helem/exists/delele) sequence */ 16472 16473 OP *kid; 16474 bool is_deref; 16475 bool ok; 16476 UV index_type = MDEREF_INDEX_none; 16477 16478 if (action_count) { 16479 /* if this is not the first lookup, consume the rv2av/hv */ 16480 16481 /* for N levels of aggregate lookup, we normally expect 16482 * that the first N-1 [ah]elem ops will be flagged as 16483 * /DEREF (so they autovivifiy if necessary), and the last 16484 * lookup op not to be. 16485 * For other things (like @{$h{k1}{k2}}) extra scope or 16486 * leave ops can appear, so abandon the effort in that 16487 * case */ 16488 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV) 16489 return; 16490 16491 /* rv2av or rv2hv sKR/1 */ 16492 16493 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 16494 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 16495 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF)) 16496 return; 16497 16498 /* at this point, we wouldn't expect any of these 16499 * possible private flags: 16500 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO 16501 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only) 16502 */ 16503 ASSUME(!(o->op_private & 16504 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING))); 16505 16506 hints = (o->op_private & OPpHINT_STRICT_REFS); 16507 16508 /* make sure the type of the previous /DEREF matches the 16509 * type of the next lookup */ 16510 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV)); 16511 top_op = o; 16512 16513 action = next_is_hash 16514 ? MDEREF_HV_vivify_rv2hv_helem 16515 : MDEREF_AV_vivify_rv2av_aelem; 16516 o = o->op_next; 16517 } 16518 16519 /* if this is the second pass, and we're at the depth where 16520 * previously we encountered a non-simple index expression, 16521 * stop processing the index at this point */ 16522 if (action_count != index_skip) { 16523 16524 /* look for one or more simple ops that return an array 16525 * index or hash key */ 16526 16527 switch (o->op_type) { 16528 case OP_PADSV: 16529 /* it may be a lexical var index */ 16530 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS 16531 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 16532 ASSUME(!(o->op_private & 16533 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO))); 16534 16535 if ( OP_GIMME(o,0) == G_SCALAR 16536 && !(o->op_flags & (OPf_REF|OPf_MOD)) 16537 && o->op_private == 0) 16538 { 16539 if (pass) 16540 arg->pad_offset = o->op_targ; 16541 arg++; 16542 index_type = MDEREF_INDEX_padsv; 16543 o = o->op_next; 16544 } 16545 break; 16546 16547 case OP_CONST: 16548 if (next_is_hash) { 16549 /* it's a constant hash index */ 16550 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK))) 16551 /* "use constant foo => FOO; $h{+foo}" for 16552 * some weird FOO, can leave you with constants 16553 * that aren't simple strings. It's not worth 16554 * the extra hassle for those edge cases */ 16555 break; 16556 16557 { 16558 UNOP *rop = NULL; 16559 OP * helem_op = o->op_next; 16560 16561 ASSUME( helem_op->op_type == OP_HELEM 16562 || helem_op->op_type == OP_NULL 16563 || pass == 0); 16564 if (helem_op->op_type == OP_HELEM) { 16565 rop = (UNOP*)(((BINOP*)helem_op)->op_first); 16566 if ( helem_op->op_private & OPpLVAL_INTRO 16567 || rop->op_type != OP_RV2HV 16568 ) 16569 rop = NULL; 16570 } 16571 /* on first pass just check; on second pass 16572 * hekify */ 16573 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo, 16574 pass); 16575 } 16576 16577 if (pass) { 16578 #ifdef USE_ITHREADS 16579 /* Relocate sv to the pad for thread safety */ 16580 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ); 16581 arg->pad_offset = o->op_targ; 16582 o->op_targ = 0; 16583 #else 16584 arg->sv = cSVOPx_sv(o); 16585 #endif 16586 } 16587 } 16588 else { 16589 /* it's a constant array index */ 16590 IV iv; 16591 SV *ix_sv = cSVOPo->op_sv; 16592 if (!SvIOK(ix_sv)) 16593 break; 16594 iv = SvIV(ix_sv); 16595 16596 if ( action_count == 0 16597 && iv >= -128 16598 && iv <= 127 16599 && ( action == MDEREF_AV_padav_aelem 16600 || action == MDEREF_AV_gvav_aelem) 16601 ) 16602 maybe_aelemfast = TRUE; 16603 16604 if (pass) { 16605 arg->iv = iv; 16606 SvREFCNT_dec_NN(cSVOPo->op_sv); 16607 } 16608 } 16609 if (pass) 16610 /* we've taken ownership of the SV */ 16611 cSVOPo->op_sv = NULL; 16612 arg++; 16613 index_type = MDEREF_INDEX_const; 16614 o = o->op_next; 16615 break; 16616 16617 case OP_GV: 16618 /* it may be a package var index */ 16619 16620 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS|OPf_SPECIAL))); 16621 ASSUME(!(o->op_private & ~(OPpEARLY_CV))); 16622 if ( (o->op_flags & ~(OPf_PARENS|OPf_SPECIAL)) != OPf_WANT_SCALAR 16623 || o->op_private != 0 16624 ) 16625 break; 16626 16627 kid = o->op_next; 16628 if (kid->op_type != OP_RV2SV) 16629 break; 16630 16631 ASSUME(!(kid->op_flags & 16632 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF 16633 |OPf_SPECIAL|OPf_PARENS))); 16634 ASSUME(!(kid->op_private & 16635 ~(OPpARG1_MASK 16636 |OPpHINT_STRICT_REFS|OPpOUR_INTRO 16637 |OPpDEREF|OPpLVAL_INTRO))); 16638 if( (kid->op_flags &~ OPf_PARENS) 16639 != (OPf_WANT_SCALAR|OPf_KIDS) 16640 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS)) 16641 ) 16642 break; 16643 16644 if (pass) { 16645 #ifdef USE_ITHREADS 16646 arg->pad_offset = cPADOPx(o)->op_padix; 16647 /* stop it being swiped when nulled */ 16648 cPADOPx(o)->op_padix = 0; 16649 #else 16650 arg->sv = cSVOPx(o)->op_sv; 16651 cSVOPo->op_sv = NULL; 16652 #endif 16653 } 16654 arg++; 16655 index_type = MDEREF_INDEX_gvsv; 16656 o = kid->op_next; 16657 break; 16658 16659 } /* switch */ 16660 } /* action_count != index_skip */ 16661 16662 action |= index_type; 16663 16664 16665 /* at this point we have either: 16666 * * detected what looks like a simple index expression, 16667 * and expect the next op to be an [ah]elem, or 16668 * an nulled [ah]elem followed by a delete or exists; 16669 * * found a more complex expression, so something other 16670 * than the above follows. 16671 */ 16672 16673 /* possibly an optimised away [ah]elem (where op_next is 16674 * exists or delete) */ 16675 if (o->op_type == OP_NULL) 16676 o = o->op_next; 16677 16678 /* at this point we're looking for an OP_AELEM, OP_HELEM, 16679 * OP_EXISTS or OP_DELETE */ 16680 16681 /* if a custom array/hash access checker is in scope, 16682 * abandon optimisation attempt */ 16683 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM) 16684 && PL_check[o->op_type] != Perl_ck_null) 16685 return; 16686 /* similarly for customised exists and delete */ 16687 if ( (o->op_type == OP_EXISTS) 16688 && PL_check[o->op_type] != Perl_ck_exists) 16689 return; 16690 if ( (o->op_type == OP_DELETE) 16691 && PL_check[o->op_type] != Perl_ck_delete) 16692 return; 16693 16694 if ( o->op_type != OP_AELEM 16695 || (o->op_private & 16696 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) 16697 ) 16698 maybe_aelemfast = FALSE; 16699 16700 /* look for aelem/helem/exists/delete. If it's not the last elem 16701 * lookup, it *must* have OPpDEREF_AV/HV, but not many other 16702 * flags; if it's the last, then it mustn't have 16703 * OPpDEREF_AV/HV, but may have lots of other flags, like 16704 * OPpLVAL_INTRO etc 16705 */ 16706 16707 if ( index_type == MDEREF_INDEX_none 16708 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM 16709 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE) 16710 ) 16711 ok = FALSE; 16712 else { 16713 /* we have aelem/helem/exists/delete with valid simple index */ 16714 16715 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM) 16716 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV 16717 || (o->op_private & OPpDEREF) == OPpDEREF_HV); 16718 16719 /* This doesn't make much sense but is legal: 16720 * @{ local $x[0][0] } = 1 16721 * Since scope exit will undo the autovivification, 16722 * don't bother in the first place. The OP_LEAVE 16723 * assertion is in case there are other cases of both 16724 * OPpLVAL_INTRO and OPpDEREF which don't include a scope 16725 * exit that would undo the local - in which case this 16726 * block of code would need rethinking. 16727 */ 16728 if (is_deref && (o->op_private & OPpLVAL_INTRO)) { 16729 #ifdef DEBUGGING 16730 OP *n = o->op_next; 16731 while (n && ( n->op_type == OP_NULL 16732 || n->op_type == OP_LIST 16733 || n->op_type == OP_SCALAR)) 16734 n = n->op_next; 16735 assert(n && n->op_type == OP_LEAVE); 16736 #endif 16737 o->op_private &= ~OPpDEREF; 16738 is_deref = FALSE; 16739 } 16740 16741 if (is_deref) { 16742 ASSUME(!(o->op_flags & 16743 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS))); 16744 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF))); 16745 16746 ok = (o->op_flags &~ OPf_PARENS) 16747 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD) 16748 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK)); 16749 } 16750 else if (o->op_type == OP_EXISTS) { 16751 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 16752 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 16753 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB))); 16754 ok = !(o->op_private & ~OPpARG1_MASK); 16755 } 16756 else if (o->op_type == OP_DELETE) { 16757 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 16758 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 16759 ASSUME(!(o->op_private & 16760 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO))); 16761 /* don't handle slices or 'local delete'; the latter 16762 * is fairly rare, and has a complex runtime */ 16763 ok = !(o->op_private & ~OPpARG1_MASK); 16764 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM)) 16765 /* skip handling run-tome error */ 16766 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL)); 16767 } 16768 else { 16769 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM); 16770 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD 16771 |OPf_PARENS|OPf_REF|OPf_SPECIAL))); 16772 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB 16773 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO))); 16774 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV; 16775 } 16776 } 16777 16778 if (ok) { 16779 if (!first_elem_op) 16780 first_elem_op = o; 16781 top_op = o; 16782 if (is_deref) { 16783 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV); 16784 o = o->op_next; 16785 } 16786 else { 16787 is_last = TRUE; 16788 action |= MDEREF_FLAG_last; 16789 } 16790 } 16791 else { 16792 /* at this point we have something that started 16793 * promisingly enough (with rv2av or whatever), but failed 16794 * to find a simple index followed by an 16795 * aelem/helem/exists/delete. If this is the first action, 16796 * give up; but if we've already seen at least one 16797 * aelem/helem, then keep them and add a new action with 16798 * MDEREF_INDEX_none, which causes it to do the vivify 16799 * from the end of the previous lookup, and do the deref, 16800 * but stop at that point. So $a[0][expr] will do one 16801 * av_fetch, vivify and deref, then continue executing at 16802 * expr */ 16803 if (!action_count) 16804 return; 16805 is_last = TRUE; 16806 index_skip = action_count; 16807 action |= MDEREF_FLAG_last; 16808 if (index_type != MDEREF_INDEX_none) 16809 arg--; 16810 } 16811 16812 action_word |= (action << (action_ix * MDEREF_SHIFT)); 16813 action_ix++; 16814 action_count++; 16815 /* if there's no space for the next action, reserve a new slot 16816 * for it *before* we start adding args for that action */ 16817 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) { 16818 if (pass) 16819 action_ptr->uv = action_word; 16820 action_word = 0; 16821 action_ptr = arg; 16822 arg++; 16823 action_ix = 0; 16824 } 16825 } /* while !is_last */ 16826 16827 /* success! */ 16828 16829 if (!action_ix) 16830 /* slot reserved for next action word not now needed */ 16831 arg--; 16832 else if (pass) 16833 action_ptr->uv = action_word; 16834 16835 if (pass) { 16836 OP *mderef; 16837 OP *p, *q; 16838 16839 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf); 16840 if (index_skip == -1) { 16841 mderef->op_flags = o->op_flags 16842 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0)); 16843 if (o->op_type == OP_EXISTS) 16844 mderef->op_private = OPpMULTIDEREF_EXISTS; 16845 else if (o->op_type == OP_DELETE) 16846 mderef->op_private = OPpMULTIDEREF_DELETE; 16847 else 16848 mderef->op_private = o->op_private 16849 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO); 16850 } 16851 /* accumulate strictness from every level (although I don't think 16852 * they can actually vary) */ 16853 mderef->op_private |= hints; 16854 16855 /* integrate the new multideref op into the optree and the 16856 * op_next chain. 16857 * 16858 * In general an op like aelem or helem has two child 16859 * sub-trees: the aggregate expression (a_expr) and the 16860 * index expression (i_expr): 16861 * 16862 * aelem 16863 * | 16864 * a_expr - i_expr 16865 * 16866 * The a_expr returns an AV or HV, while the i-expr returns an 16867 * index. In general a multideref replaces most or all of a 16868 * multi-level tree, e.g. 16869 * 16870 * exists 16871 * | 16872 * ex-aelem 16873 * | 16874 * rv2av - i_expr1 16875 * | 16876 * helem 16877 * | 16878 * rv2hv - i_expr2 16879 * | 16880 * aelem 16881 * | 16882 * a_expr - i_expr3 16883 * 16884 * With multideref, all the i_exprs will be simple vars or 16885 * constants, except that i_expr1 may be arbitrary in the case 16886 * of MDEREF_INDEX_none. 16887 * 16888 * The bottom-most a_expr will be either: 16889 * 1) a simple var (so padXv or gv+rv2Xv); 16890 * 2) a simple scalar var dereferenced (e.g. $r->[0]): 16891 * so a simple var with an extra rv2Xv; 16892 * 3) or an arbitrary expression. 16893 * 16894 * 'start', the first op in the execution chain, will point to 16895 * 1),2): the padXv or gv op; 16896 * 3): the rv2Xv which forms the last op in the a_expr 16897 * execution chain, and the top-most op in the a_expr 16898 * subtree. 16899 * 16900 * For all cases, the 'start' node is no longer required, 16901 * but we can't free it since one or more external nodes 16902 * may point to it. E.g. consider 16903 * $h{foo} = $a ? $b : $c 16904 * Here, both the op_next and op_other branches of the 16905 * cond_expr point to the gv[*h] of the hash expression, so 16906 * we can't free the 'start' op. 16907 * 16908 * For expr->[...], we need to save the subtree containing the 16909 * expression; for the other cases, we just need to save the 16910 * start node. 16911 * So in all cases, we null the start op and keep it around by 16912 * making it the child of the multideref op; for the expr-> 16913 * case, the expr will be a subtree of the start node. 16914 * 16915 * So in the simple 1,2 case the optree above changes to 16916 * 16917 * ex-exists 16918 * | 16919 * multideref 16920 * | 16921 * ex-gv (or ex-padxv) 16922 * 16923 * with the op_next chain being 16924 * 16925 * -> ex-gv -> multideref -> op-following-ex-exists -> 16926 * 16927 * In the 3 case, we have 16928 * 16929 * ex-exists 16930 * | 16931 * multideref 16932 * | 16933 * ex-rv2xv 16934 * | 16935 * rest-of-a_expr 16936 * subtree 16937 * 16938 * and 16939 * 16940 * -> rest-of-a_expr subtree -> 16941 * ex-rv2xv -> multideref -> op-following-ex-exists -> 16942 * 16943 * 16944 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none, 16945 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the 16946 * multideref attached as the child, e.g. 16947 * 16948 * exists 16949 * | 16950 * ex-aelem 16951 * | 16952 * ex-rv2av - i_expr1 16953 * | 16954 * multideref 16955 * | 16956 * ex-whatever 16957 * 16958 */ 16959 16960 /* if we free this op, don't free the pad entry */ 16961 if (reset_start_targ) 16962 start->op_targ = 0; 16963 16964 16965 /* Cut the bit we need to save out of the tree and attach to 16966 * the multideref op, then free the rest of the tree */ 16967 16968 /* find parent of node to be detached (for use by splice) */ 16969 p = first_elem_op; 16970 if ( orig_action == MDEREF_AV_pop_rv2av_aelem 16971 || orig_action == MDEREF_HV_pop_rv2hv_helem) 16972 { 16973 /* there is an arbitrary expression preceding us, e.g. 16974 * expr->[..]? so we need to save the 'expr' subtree */ 16975 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE) 16976 p = cUNOPx(p)->op_first; 16977 ASSUME( start->op_type == OP_RV2AV 16978 || start->op_type == OP_RV2HV); 16979 } 16980 else { 16981 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem 16982 * above for exists/delete. */ 16983 while ( (p->op_flags & OPf_KIDS) 16984 && cUNOPx(p)->op_first != start 16985 ) 16986 p = cUNOPx(p)->op_first; 16987 } 16988 ASSUME(cUNOPx(p)->op_first == start); 16989 16990 /* detach from main tree, and re-attach under the multideref */ 16991 op_sibling_splice(mderef, NULL, 0, 16992 op_sibling_splice(p, NULL, 1, NULL)); 16993 op_null(start); 16994 16995 start->op_next = mderef; 16996 16997 mderef->op_next = index_skip == -1 ? o->op_next : o; 16998 16999 /* excise and free the original tree, and replace with 17000 * the multideref op */ 17001 p = op_sibling_splice(top_op, NULL, -1, mderef); 17002 while (p) { 17003 q = OpSIBLING(p); 17004 op_free(p); 17005 p = q; 17006 } 17007 op_null(top_op); 17008 } 17009 else { 17010 Size_t size = arg - arg_buf; 17011 17012 if (maybe_aelemfast && action_count == 1) 17013 return; 17014 17015 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc( 17016 sizeof(UNOP_AUX_item) * (size + 1)); 17017 /* for dumping etc: store the length in a hidden first slot; 17018 * we set the op_aux pointer to the second slot */ 17019 arg_buf->uv = size; 17020 arg_buf++; 17021 } 17022 } /* for (pass = ...) */ 17023 } 17024 17025 /* See if the ops following o are such that o will always be executed in 17026 * boolean context: that is, the SV which o pushes onto the stack will 17027 * only ever be consumed by later ops via SvTRUE(sv) or similar. 17028 * If so, set a suitable private flag on o. Normally this will be 17029 * bool_flag; but see below why maybe_flag is needed too. 17030 * 17031 * Typically the two flags you pass will be the generic OPpTRUEBOOL and 17032 * OPpMAYBE_TRUEBOOL, buts it's possible that for some ops those bits may 17033 * already be taken, so you'll have to give that op two different flags. 17034 * 17035 * More explanation of 'maybe_flag' and 'safe_and' parameters. 17036 * The binary logical ops &&, ||, // (plus 'if' and 'unless' which use 17037 * those underlying ops) short-circuit, which means that rather than 17038 * necessarily returning a truth value, they may return the LH argument, 17039 * which may not be boolean. For example in $x = (keys %h || -1), keys 17040 * should return a key count rather than a boolean, even though its 17041 * sort-of being used in boolean context. 17042 * 17043 * So we only consider such logical ops to provide boolean context to 17044 * their LH argument if they themselves are in void or boolean context. 17045 * However, sometimes the context isn't known until run-time. In this 17046 * case the op is marked with the maybe_flag flag it. 17047 * 17048 * Consider the following. 17049 * 17050 * sub f { ....; if (%h) { .... } } 17051 * 17052 * This is actually compiled as 17053 * 17054 * sub f { ....; %h && do { .... } } 17055 * 17056 * Here we won't know until runtime whether the final statement (and hence 17057 * the &&) is in void context and so is safe to return a boolean value. 17058 * So mark o with maybe_flag rather than the bool_flag. 17059 * Note that there is cost associated with determining context at runtime 17060 * (e.g. a call to block_gimme()), so it may not be worth setting (at 17061 * compile time) and testing (at runtime) maybe_flag if the scalar verses 17062 * boolean costs savings are marginal. 17063 * 17064 * However, we can do slightly better with && (compared to || and //): 17065 * this op only returns its LH argument when that argument is false. In 17066 * this case, as long as the op promises to return a false value which is 17067 * valid in both boolean and scalar contexts, we can mark an op consumed 17068 * by && with bool_flag rather than maybe_flag. 17069 * For example as long as pp_padhv and pp_rv2hv return &PL_sv_zero rather 17070 * than &PL_sv_no for a false result in boolean context, then it's safe. An 17071 * op which promises to handle this case is indicated by setting safe_and 17072 * to true. 17073 */ 17074 17075 static void 17076 S_check_for_bool_cxt(OP*o, bool safe_and, U8 bool_flag, U8 maybe_flag) 17077 { 17078 OP *lop; 17079 U8 flag = 0; 17080 17081 assert((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR); 17082 17083 /* OPpTARGET_MY and boolean context probably don't mix well. 17084 * If someone finds a valid use case, maybe add an extra flag to this 17085 * function which indicates its safe to do so for this op? */ 17086 assert(!( (PL_opargs[o->op_type] & OA_TARGLEX) 17087 && (o->op_private & OPpTARGET_MY))); 17088 17089 lop = o->op_next; 17090 17091 while (lop) { 17092 switch (lop->op_type) { 17093 case OP_NULL: 17094 case OP_SCALAR: 17095 break; 17096 17097 /* these two consume the stack argument in the scalar case, 17098 * and treat it as a boolean in the non linenumber case */ 17099 case OP_FLIP: 17100 case OP_FLOP: 17101 if ( ((lop->op_flags & OPf_WANT) == OPf_WANT_LIST) 17102 || (lop->op_private & OPpFLIP_LINENUM)) 17103 { 17104 lop = NULL; 17105 break; 17106 } 17107 /* FALLTHROUGH */ 17108 /* these never leave the original value on the stack */ 17109 case OP_NOT: 17110 case OP_XOR: 17111 case OP_COND_EXPR: 17112 case OP_GREPWHILE: 17113 flag = bool_flag; 17114 lop = NULL; 17115 break; 17116 17117 /* OR DOR and AND evaluate their arg as a boolean, but then may 17118 * leave the original scalar value on the stack when following the 17119 * op_next route. If not in void context, we need to ensure 17120 * that whatever follows consumes the arg only in boolean context 17121 * too. 17122 */ 17123 case OP_AND: 17124 if (safe_and) { 17125 flag = bool_flag; 17126 lop = NULL; 17127 break; 17128 } 17129 /* FALLTHROUGH */ 17130 case OP_OR: 17131 case OP_DOR: 17132 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) { 17133 flag = bool_flag; 17134 lop = NULL; 17135 } 17136 else if (!(lop->op_flags & OPf_WANT)) { 17137 /* unknown context - decide at runtime */ 17138 flag = maybe_flag; 17139 lop = NULL; 17140 } 17141 break; 17142 17143 default: 17144 lop = NULL; 17145 break; 17146 } 17147 17148 if (lop) 17149 lop = lop->op_next; 17150 } 17151 17152 o->op_private |= flag; 17153 } 17154 17155 17156 17157 /* mechanism for deferring recursion in rpeep() */ 17158 17159 #define MAX_DEFERRED 4 17160 17161 #define DEFER(o) \ 17162 STMT_START { \ 17163 if (defer_ix == (MAX_DEFERRED-1)) { \ 17164 OP **defer = defer_queue[defer_base]; \ 17165 CALL_RPEEP(*defer); \ 17166 S_prune_chain_head(defer); \ 17167 defer_base = (defer_base + 1) % MAX_DEFERRED; \ 17168 defer_ix--; \ 17169 } \ 17170 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \ 17171 } STMT_END 17172 17173 #define IS_AND_OP(o) (o->op_type == OP_AND) 17174 #define IS_OR_OP(o) (o->op_type == OP_OR) 17175 17176 17177 /* A peephole optimizer. We visit the ops in the order they're to execute. 17178 * See the comments at the top of this file for more details about when 17179 * peep() is called */ 17180 17181 void 17182 Perl_rpeep(pTHX_ OP *o) 17183 { 17184 OP* oldop = NULL; 17185 OP* oldoldop = NULL; 17186 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */ 17187 int defer_base = 0; 17188 int defer_ix = -1; 17189 17190 if (!o || o->op_opt) 17191 return; 17192 17193 assert(o->op_type != OP_FREED); 17194 17195 ENTER; 17196 SAVEOP(); 17197 SAVEVPTR(PL_curcop); 17198 for (;; o = o->op_next) { 17199 if (o && o->op_opt) 17200 o = NULL; 17201 if (!o) { 17202 while (defer_ix >= 0) { 17203 OP **defer = 17204 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED]; 17205 CALL_RPEEP(*defer); 17206 S_prune_chain_head(defer); 17207 } 17208 break; 17209 } 17210 17211 redo: 17212 17213 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */ 17214 assert(!oldoldop || oldoldop->op_next == oldop); 17215 assert(!oldop || oldop->op_next == o); 17216 17217 /* By default, this op has now been optimised. A couple of cases below 17218 clear this again. */ 17219 o->op_opt = 1; 17220 PL_op = o; 17221 17222 /* look for a series of 1 or more aggregate derefs, e.g. 17223 * $a[1]{foo}[$i]{$k} 17224 * and replace with a single OP_MULTIDEREF op. 17225 * Each index must be either a const, or a simple variable, 17226 * 17227 * First, look for likely combinations of starting ops, 17228 * corresponding to (global and lexical variants of) 17229 * $a[...] $h{...} 17230 * $r->[...] $r->{...} 17231 * (preceding expression)->[...] 17232 * (preceding expression)->{...} 17233 * and if so, call maybe_multideref() to do a full inspection 17234 * of the op chain and if appropriate, replace with an 17235 * OP_MULTIDEREF 17236 */ 17237 { 17238 UV action; 17239 OP *o2 = o; 17240 U8 hints = 0; 17241 17242 switch (o2->op_type) { 17243 case OP_GV: 17244 /* $pkg[..] : gv[*pkg] 17245 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */ 17246 17247 /* Fail if there are new op flag combinations that we're 17248 * not aware of, rather than: 17249 * * silently failing to optimise, or 17250 * * silently optimising the flag away. 17251 * If this ASSUME starts failing, examine what new flag 17252 * has been added to the op, and decide whether the 17253 * optimisation should still occur with that flag, then 17254 * update the code accordingly. This applies to all the 17255 * other ASSUMEs in the block of code too. 17256 */ 17257 ASSUME(!(o2->op_flags & 17258 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL))); 17259 ASSUME(!(o2->op_private & ~OPpEARLY_CV)); 17260 17261 o2 = o2->op_next; 17262 17263 if (o2->op_type == OP_RV2AV) { 17264 action = MDEREF_AV_gvav_aelem; 17265 goto do_deref; 17266 } 17267 17268 if (o2->op_type == OP_RV2HV) { 17269 action = MDEREF_HV_gvhv_helem; 17270 goto do_deref; 17271 } 17272 17273 if (o2->op_type != OP_RV2SV) 17274 break; 17275 17276 /* at this point we've seen gv,rv2sv, so the only valid 17277 * construct left is $pkg->[] or $pkg->{} */ 17278 17279 ASSUME(!(o2->op_flags & OPf_STACKED)); 17280 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) 17281 != (OPf_WANT_SCALAR|OPf_MOD)) 17282 break; 17283 17284 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS 17285 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO))); 17286 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO)) 17287 break; 17288 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV 17289 && (o2->op_private & OPpDEREF) != OPpDEREF_HV) 17290 break; 17291 17292 o2 = o2->op_next; 17293 if (o2->op_type == OP_RV2AV) { 17294 action = MDEREF_AV_gvsv_vivify_rv2av_aelem; 17295 goto do_deref; 17296 } 17297 if (o2->op_type == OP_RV2HV) { 17298 action = MDEREF_HV_gvsv_vivify_rv2hv_helem; 17299 goto do_deref; 17300 } 17301 break; 17302 17303 case OP_PADSV: 17304 /* $lex->[...]: padsv[$lex] sM/DREFAV */ 17305 17306 ASSUME(!(o2->op_flags & 17307 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL))); 17308 if ((o2->op_flags & 17309 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) 17310 != (OPf_WANT_SCALAR|OPf_MOD)) 17311 break; 17312 17313 ASSUME(!(o2->op_private & 17314 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO))); 17315 /* skip if state or intro, or not a deref */ 17316 if ( o2->op_private != OPpDEREF_AV 17317 && o2->op_private != OPpDEREF_HV) 17318 break; 17319 17320 o2 = o2->op_next; 17321 if (o2->op_type == OP_RV2AV) { 17322 action = MDEREF_AV_padsv_vivify_rv2av_aelem; 17323 goto do_deref; 17324 } 17325 if (o2->op_type == OP_RV2HV) { 17326 action = MDEREF_HV_padsv_vivify_rv2hv_helem; 17327 goto do_deref; 17328 } 17329 break; 17330 17331 case OP_PADAV: 17332 case OP_PADHV: 17333 /* $lex[..]: padav[@lex:1,2] sR * 17334 * or $lex{..}: padhv[%lex:1,2] sR */ 17335 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS| 17336 OPf_REF|OPf_SPECIAL))); 17337 if ((o2->op_flags & 17338 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) 17339 != (OPf_WANT_SCALAR|OPf_REF)) 17340 break; 17341 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF)) 17342 break; 17343 /* OPf_PARENS isn't currently used in this case; 17344 * if that changes, let us know! */ 17345 ASSUME(!(o2->op_flags & OPf_PARENS)); 17346 17347 /* at this point, we wouldn't expect any of the remaining 17348 * possible private flags: 17349 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL, 17350 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB 17351 * 17352 * OPpSLICEWARNING shouldn't affect runtime 17353 */ 17354 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING))); 17355 17356 action = o2->op_type == OP_PADAV 17357 ? MDEREF_AV_padav_aelem 17358 : MDEREF_HV_padhv_helem; 17359 o2 = o2->op_next; 17360 S_maybe_multideref(aTHX_ o, o2, action, 0); 17361 break; 17362 17363 17364 case OP_RV2AV: 17365 case OP_RV2HV: 17366 action = o2->op_type == OP_RV2AV 17367 ? MDEREF_AV_pop_rv2av_aelem 17368 : MDEREF_HV_pop_rv2hv_helem; 17369 /* FALLTHROUGH */ 17370 do_deref: 17371 /* (expr)->[...]: rv2av sKR/1; 17372 * (expr)->{...}: rv2hv sKR/1; */ 17373 17374 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV); 17375 17376 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 17377 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL))); 17378 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF)) 17379 break; 17380 17381 /* at this point, we wouldn't expect any of these 17382 * possible private flags: 17383 * OPpMAYBE_LVSUB, OPpLVAL_INTRO 17384 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only) 17385 */ 17386 ASSUME(!(o2->op_private & 17387 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING 17388 |OPpOUR_INTRO))); 17389 hints |= (o2->op_private & OPpHINT_STRICT_REFS); 17390 17391 o2 = o2->op_next; 17392 17393 S_maybe_multideref(aTHX_ o, o2, action, hints); 17394 break; 17395 17396 default: 17397 break; 17398 } 17399 } 17400 17401 17402 switch (o->op_type) { 17403 case OP_DBSTATE: 17404 PL_curcop = ((COP*)o); /* for warnings */ 17405 break; 17406 case OP_NEXTSTATE: 17407 PL_curcop = ((COP*)o); /* for warnings */ 17408 17409 /* Optimise a "return ..." at the end of a sub to just be "...". 17410 * This saves 2 ops. Before: 17411 * 1 <;> nextstate(main 1 -e:1) v ->2 17412 * 4 <@> return K ->5 17413 * 2 <0> pushmark s ->3 17414 * - <1> ex-rv2sv sK/1 ->4 17415 * 3 <#> gvsv[*cat] s ->4 17416 * 17417 * After: 17418 * - <@> return K ->- 17419 * - <0> pushmark s ->2 17420 * - <1> ex-rv2sv sK/1 ->- 17421 * 2 <$> gvsv(*cat) s ->3 17422 */ 17423 { 17424 OP *next = o->op_next; 17425 OP *sibling = OpSIBLING(o); 17426 if ( OP_TYPE_IS(next, OP_PUSHMARK) 17427 && OP_TYPE_IS(sibling, OP_RETURN) 17428 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ) 17429 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB) 17430 ||OP_TYPE_IS(sibling->op_next->op_next, 17431 OP_LEAVESUBLV)) 17432 && cUNOPx(sibling)->op_first == next 17433 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next 17434 && next->op_next 17435 ) { 17436 /* Look through the PUSHMARK's siblings for one that 17437 * points to the RETURN */ 17438 OP *top = OpSIBLING(next); 17439 while (top && top->op_next) { 17440 if (top->op_next == sibling) { 17441 top->op_next = sibling->op_next; 17442 o->op_next = next->op_next; 17443 break; 17444 } 17445 top = OpSIBLING(top); 17446 } 17447 } 17448 } 17449 17450 /* Optimise 'my $x; my $y;' into 'my ($x, $y);' 17451 * 17452 * This latter form is then suitable for conversion into padrange 17453 * later on. Convert: 17454 * 17455 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3 17456 * 17457 * into: 17458 * 17459 * nextstate1 -> listop -> nextstate3 17460 * / \ 17461 * pushmark -> padop1 -> padop2 17462 */ 17463 if (o->op_next && ( 17464 o->op_next->op_type == OP_PADSV 17465 || o->op_next->op_type == OP_PADAV 17466 || o->op_next->op_type == OP_PADHV 17467 ) 17468 && !(o->op_next->op_private & ~OPpLVAL_INTRO) 17469 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE 17470 && o->op_next->op_next->op_next && ( 17471 o->op_next->op_next->op_next->op_type == OP_PADSV 17472 || o->op_next->op_next->op_next->op_type == OP_PADAV 17473 || o->op_next->op_next->op_next->op_type == OP_PADHV 17474 ) 17475 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO) 17476 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE 17477 && (!CopLABEL((COP*)o)) /* Don't mess with labels */ 17478 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */ 17479 ) { 17480 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm; 17481 17482 pad1 = o->op_next; 17483 ns2 = pad1->op_next; 17484 pad2 = ns2->op_next; 17485 ns3 = pad2->op_next; 17486 17487 /* we assume here that the op_next chain is the same as 17488 * the op_sibling chain */ 17489 assert(OpSIBLING(o) == pad1); 17490 assert(OpSIBLING(pad1) == ns2); 17491 assert(OpSIBLING(ns2) == pad2); 17492 assert(OpSIBLING(pad2) == ns3); 17493 17494 /* excise and delete ns2 */ 17495 op_sibling_splice(NULL, pad1, 1, NULL); 17496 op_free(ns2); 17497 17498 /* excise pad1 and pad2 */ 17499 op_sibling_splice(NULL, o, 2, NULL); 17500 17501 /* create new listop, with children consisting of: 17502 * a new pushmark, pad1, pad2. */ 17503 newop = newLISTOP(OP_LIST, 0, pad1, pad2); 17504 newop->op_flags |= OPf_PARENS; 17505 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID; 17506 17507 /* insert newop between o and ns3 */ 17508 op_sibling_splice(NULL, o, 0, newop); 17509 17510 /*fixup op_next chain */ 17511 newpm = cUNOPx(newop)->op_first; /* pushmark */ 17512 o ->op_next = newpm; 17513 newpm->op_next = pad1; 17514 pad1 ->op_next = pad2; 17515 pad2 ->op_next = newop; /* listop */ 17516 newop->op_next = ns3; 17517 17518 /* Ensure pushmark has this flag if padops do */ 17519 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) { 17520 newpm->op_flags |= OPf_MOD; 17521 } 17522 17523 break; 17524 } 17525 17526 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen 17527 to carry two labels. For now, take the easier option, and skip 17528 this optimisation if the first NEXTSTATE has a label. */ 17529 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) { 17530 OP *nextop = o->op_next; 17531 while (nextop) { 17532 switch (nextop->op_type) { 17533 case OP_NULL: 17534 case OP_SCALAR: 17535 case OP_LINESEQ: 17536 case OP_SCOPE: 17537 nextop = nextop->op_next; 17538 continue; 17539 } 17540 break; 17541 } 17542 17543 if (nextop && (nextop->op_type == OP_NEXTSTATE)) { 17544 op_null(o); 17545 if (oldop) 17546 oldop->op_next = nextop; 17547 o = nextop; 17548 /* Skip (old)oldop assignment since the current oldop's 17549 op_next already points to the next op. */ 17550 goto redo; 17551 } 17552 } 17553 break; 17554 17555 case OP_CONCAT: 17556 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) { 17557 if (o->op_next->op_private & OPpTARGET_MY) { 17558 if (o->op_flags & OPf_STACKED) /* chained concats */ 17559 break; /* ignore_optimization */ 17560 else { 17561 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */ 17562 o->op_targ = o->op_next->op_targ; 17563 o->op_next->op_targ = 0; 17564 o->op_private |= OPpTARGET_MY; 17565 } 17566 } 17567 op_null(o->op_next); 17568 } 17569 break; 17570 case OP_STUB: 17571 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) { 17572 break; /* Scalar stub must produce undef. List stub is noop */ 17573 } 17574 goto nothin; 17575 case OP_NULL: 17576 if (o->op_targ == OP_NEXTSTATE 17577 || o->op_targ == OP_DBSTATE) 17578 { 17579 PL_curcop = ((COP*)o); 17580 } 17581 /* XXX: We avoid setting op_seq here to prevent later calls 17582 to rpeep() from mistakenly concluding that optimisation 17583 has already occurred. This doesn't fix the real problem, 17584 though (See 20010220.007 (#5874)). AMS 20010719 */ 17585 /* op_seq functionality is now replaced by op_opt */ 17586 o->op_opt = 0; 17587 /* FALLTHROUGH */ 17588 case OP_SCALAR: 17589 case OP_LINESEQ: 17590 case OP_SCOPE: 17591 nothin: 17592 if (oldop) { 17593 oldop->op_next = o->op_next; 17594 o->op_opt = 0; 17595 continue; 17596 } 17597 break; 17598 17599 case OP_PUSHMARK: 17600 17601 /* Given 17602 5 repeat/DOLIST 17603 3 ex-list 17604 1 pushmark 17605 2 scalar or const 17606 4 const[0] 17607 convert repeat into a stub with no kids. 17608 */ 17609 if (o->op_next->op_type == OP_CONST 17610 || ( o->op_next->op_type == OP_PADSV 17611 && !(o->op_next->op_private & OPpLVAL_INTRO)) 17612 || ( o->op_next->op_type == OP_GV 17613 && o->op_next->op_next->op_type == OP_RV2SV 17614 && !(o->op_next->op_next->op_private 17615 & (OPpLVAL_INTRO|OPpOUR_INTRO)))) 17616 { 17617 const OP *kid = o->op_next->op_next; 17618 if (o->op_next->op_type == OP_GV) 17619 kid = kid->op_next; 17620 /* kid is now the ex-list. */ 17621 if (kid->op_type == OP_NULL 17622 && (kid = kid->op_next)->op_type == OP_CONST 17623 /* kid is now the repeat count. */ 17624 && kid->op_next->op_type == OP_REPEAT 17625 && kid->op_next->op_private & OPpREPEAT_DOLIST 17626 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST 17627 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0 17628 && oldop) 17629 { 17630 o = kid->op_next; /* repeat */ 17631 oldop->op_next = o; 17632 op_free(cBINOPo->op_first); 17633 op_free(cBINOPo->op_last ); 17634 o->op_flags &=~ OPf_KIDS; 17635 /* stub is a baseop; repeat is a binop */ 17636 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP)); 17637 OpTYPE_set(o, OP_STUB); 17638 o->op_private = 0; 17639 break; 17640 } 17641 } 17642 17643 /* Convert a series of PAD ops for my vars plus support into a 17644 * single padrange op. Basically 17645 * 17646 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest 17647 * 17648 * becomes, depending on circumstances, one of 17649 * 17650 * padrange ----------------------------------> (list) -> rest 17651 * padrange --------------------------------------------> rest 17652 * 17653 * where all the pad indexes are sequential and of the same type 17654 * (INTRO or not). 17655 * We convert the pushmark into a padrange op, then skip 17656 * any other pad ops, and possibly some trailing ops. 17657 * Note that we don't null() the skipped ops, to make it 17658 * easier for Deparse to undo this optimisation (and none of 17659 * the skipped ops are holding any resourses). It also makes 17660 * it easier for find_uninit_var(), as it can just ignore 17661 * padrange, and examine the original pad ops. 17662 */ 17663 { 17664 OP *p; 17665 OP *followop = NULL; /* the op that will follow the padrange op */ 17666 U8 count = 0; 17667 U8 intro = 0; 17668 PADOFFSET base = 0; /* init only to stop compiler whining */ 17669 bool gvoid = 0; /* init only to stop compiler whining */ 17670 bool defav = 0; /* seen (...) = @_ */ 17671 bool reuse = 0; /* reuse an existing padrange op */ 17672 17673 /* look for a pushmark -> gv[_] -> rv2av */ 17674 17675 { 17676 OP *rv2av, *q; 17677 p = o->op_next; 17678 if ( p->op_type == OP_GV 17679 && cGVOPx_gv(p) == PL_defgv 17680 && (rv2av = p->op_next) 17681 && rv2av->op_type == OP_RV2AV 17682 && !(rv2av->op_flags & OPf_REF) 17683 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB)) 17684 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST) 17685 ) { 17686 q = rv2av->op_next; 17687 if (q->op_type == OP_NULL) 17688 q = q->op_next; 17689 if (q->op_type == OP_PUSHMARK) { 17690 defav = 1; 17691 p = q; 17692 } 17693 } 17694 } 17695 if (!defav) { 17696 p = o; 17697 } 17698 17699 /* scan for PAD ops */ 17700 17701 for (p = p->op_next; p; p = p->op_next) { 17702 if (p->op_type == OP_NULL) 17703 continue; 17704 17705 if (( p->op_type != OP_PADSV 17706 && p->op_type != OP_PADAV 17707 && p->op_type != OP_PADHV 17708 ) 17709 /* any private flag other than INTRO? e.g. STATE */ 17710 || (p->op_private & ~OPpLVAL_INTRO) 17711 ) 17712 break; 17713 17714 /* let $a[N] potentially be optimised into AELEMFAST_LEX 17715 * instead */ 17716 if ( p->op_type == OP_PADAV 17717 && p->op_next 17718 && p->op_next->op_type == OP_CONST 17719 && p->op_next->op_next 17720 && p->op_next->op_next->op_type == OP_AELEM 17721 ) 17722 break; 17723 17724 /* for 1st padop, note what type it is and the range 17725 * start; for the others, check that it's the same type 17726 * and that the targs are contiguous */ 17727 if (count == 0) { 17728 intro = (p->op_private & OPpLVAL_INTRO); 17729 base = p->op_targ; 17730 gvoid = OP_GIMME(p,0) == G_VOID; 17731 } 17732 else { 17733 if ((p->op_private & OPpLVAL_INTRO) != intro) 17734 break; 17735 /* Note that you'd normally expect targs to be 17736 * contiguous in my($a,$b,$c), but that's not the case 17737 * when external modules start doing things, e.g. 17738 * Function::Parameters */ 17739 if (p->op_targ != base + count) 17740 break; 17741 assert(p->op_targ == base + count); 17742 /* Either all the padops or none of the padops should 17743 be in void context. Since we only do the optimisa- 17744 tion for av/hv when the aggregate itself is pushed 17745 on to the stack (one item), there is no need to dis- 17746 tinguish list from scalar context. */ 17747 if (gvoid != (OP_GIMME(p,0) == G_VOID)) 17748 break; 17749 } 17750 17751 /* for AV, HV, only when we're not flattening */ 17752 if ( p->op_type != OP_PADSV 17753 && !gvoid 17754 && !(p->op_flags & OPf_REF) 17755 ) 17756 break; 17757 17758 if (count >= OPpPADRANGE_COUNTMASK) 17759 break; 17760 17761 /* there's a biggest base we can fit into a 17762 * SAVEt_CLEARPADRANGE in pp_padrange. 17763 * (The sizeof() stuff will be constant-folded, and is 17764 * intended to avoid getting "comparison is always false" 17765 * compiler warnings. See the comments above 17766 * MEM_WRAP_CHECK for more explanation on why we do this 17767 * in a weird way to avoid compiler warnings.) 17768 */ 17769 if ( intro 17770 && (8*sizeof(base) > 17771 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT 17772 ? (Size_t)base 17773 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)) 17774 ) > 17775 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)) 17776 ) 17777 break; 17778 17779 /* Success! We've got another valid pad op to optimise away */ 17780 count++; 17781 followop = p->op_next; 17782 } 17783 17784 if (count < 1 || (count == 1 && !defav)) 17785 break; 17786 17787 /* pp_padrange in specifically compile-time void context 17788 * skips pushing a mark and lexicals; in all other contexts 17789 * (including unknown till runtime) it pushes a mark and the 17790 * lexicals. We must be very careful then, that the ops we 17791 * optimise away would have exactly the same effect as the 17792 * padrange. 17793 * In particular in void context, we can only optimise to 17794 * a padrange if we see the complete sequence 17795 * pushmark, pad*v, ...., list 17796 * which has the net effect of leaving the markstack as it 17797 * was. Not pushing onto the stack (whereas padsv does touch 17798 * the stack) makes no difference in void context. 17799 */ 17800 assert(followop); 17801 if (gvoid) { 17802 if (followop->op_type == OP_LIST 17803 && OP_GIMME(followop,0) == G_VOID 17804 ) 17805 { 17806 followop = followop->op_next; /* skip OP_LIST */ 17807 17808 /* consolidate two successive my(...);'s */ 17809 17810 if ( oldoldop 17811 && oldoldop->op_type == OP_PADRANGE 17812 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID 17813 && (oldoldop->op_private & OPpLVAL_INTRO) == intro 17814 && !(oldoldop->op_flags & OPf_SPECIAL) 17815 ) { 17816 U8 old_count; 17817 assert(oldoldop->op_next == oldop); 17818 assert( oldop->op_type == OP_NEXTSTATE 17819 || oldop->op_type == OP_DBSTATE); 17820 assert(oldop->op_next == o); 17821 17822 old_count 17823 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK); 17824 17825 /* Do not assume pad offsets for $c and $d are con- 17826 tiguous in 17827 my ($a,$b,$c); 17828 my ($d,$e,$f); 17829 */ 17830 if ( oldoldop->op_targ + old_count == base 17831 && old_count < OPpPADRANGE_COUNTMASK - count) { 17832 base = oldoldop->op_targ; 17833 count += old_count; 17834 reuse = 1; 17835 } 17836 } 17837 17838 /* if there's any immediately following singleton 17839 * my var's; then swallow them and the associated 17840 * nextstates; i.e. 17841 * my ($a,$b); my $c; my $d; 17842 * is treated as 17843 * my ($a,$b,$c,$d); 17844 */ 17845 17846 while ( ((p = followop->op_next)) 17847 && ( p->op_type == OP_PADSV 17848 || p->op_type == OP_PADAV 17849 || p->op_type == OP_PADHV) 17850 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID 17851 && (p->op_private & OPpLVAL_INTRO) == intro 17852 && !(p->op_private & ~OPpLVAL_INTRO) 17853 && p->op_next 17854 && ( p->op_next->op_type == OP_NEXTSTATE 17855 || p->op_next->op_type == OP_DBSTATE) 17856 && count < OPpPADRANGE_COUNTMASK 17857 && base + count == p->op_targ 17858 ) { 17859 count++; 17860 followop = p->op_next; 17861 } 17862 } 17863 else 17864 break; 17865 } 17866 17867 if (reuse) { 17868 assert(oldoldop->op_type == OP_PADRANGE); 17869 oldoldop->op_next = followop; 17870 oldoldop->op_private = (intro | count); 17871 o = oldoldop; 17872 oldop = NULL; 17873 oldoldop = NULL; 17874 } 17875 else { 17876 /* Convert the pushmark into a padrange. 17877 * To make Deparse easier, we guarantee that a padrange was 17878 * *always* formerly a pushmark */ 17879 assert(o->op_type == OP_PUSHMARK); 17880 o->op_next = followop; 17881 OpTYPE_set(o, OP_PADRANGE); 17882 o->op_targ = base; 17883 /* bit 7: INTRO; bit 6..0: count */ 17884 o->op_private = (intro | count); 17885 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL)) 17886 | gvoid * OPf_WANT_VOID 17887 | (defav ? OPf_SPECIAL : 0)); 17888 } 17889 break; 17890 } 17891 17892 case OP_RV2AV: 17893 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 17894 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0); 17895 break; 17896 17897 case OP_RV2HV: 17898 case OP_PADHV: 17899 /*'keys %h' in void or scalar context: skip the OP_KEYS 17900 * and perform the functionality directly in the RV2HV/PADHV 17901 * op 17902 */ 17903 if (o->op_flags & OPf_REF) { 17904 OP *k = o->op_next; 17905 U8 want = (k->op_flags & OPf_WANT); 17906 if ( k 17907 && k->op_type == OP_KEYS 17908 && ( want == OPf_WANT_VOID 17909 || want == OPf_WANT_SCALAR) 17910 && !(k->op_private & OPpMAYBE_LVSUB) 17911 && !(k->op_flags & OPf_MOD) 17912 ) { 17913 o->op_next = k->op_next; 17914 o->op_flags &= ~(OPf_REF|OPf_WANT); 17915 o->op_flags |= want; 17916 o->op_private |= (o->op_type == OP_PADHV ? 17917 OPpPADHV_ISKEYS : OPpRV2HV_ISKEYS); 17918 /* for keys(%lex), hold onto the OP_KEYS's targ 17919 * since padhv doesn't have its own targ to return 17920 * an int with */ 17921 if (!(o->op_type ==OP_PADHV && want == OPf_WANT_SCALAR)) 17922 op_null(k); 17923 } 17924 } 17925 17926 /* see if %h is used in boolean context */ 17927 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 17928 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL); 17929 17930 17931 if (o->op_type != OP_PADHV) 17932 break; 17933 /* FALLTHROUGH */ 17934 case OP_PADAV: 17935 if ( o->op_type == OP_PADAV 17936 && (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR 17937 ) 17938 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0); 17939 /* FALLTHROUGH */ 17940 case OP_PADSV: 17941 /* Skip over state($x) in void context. */ 17942 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO) 17943 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID) 17944 { 17945 oldop->op_next = o->op_next; 17946 goto redo_nextstate; 17947 } 17948 if (o->op_type != OP_PADAV) 17949 break; 17950 /* FALLTHROUGH */ 17951 case OP_GV: 17952 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) { 17953 OP* const pop = (o->op_type == OP_PADAV) ? 17954 o->op_next : o->op_next->op_next; 17955 IV i; 17956 if (pop && pop->op_type == OP_CONST && 17957 ((PL_op = pop->op_next)) && 17958 pop->op_next->op_type == OP_AELEM && 17959 !(pop->op_next->op_private & 17960 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) && 17961 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127) 17962 { 17963 GV *gv; 17964 if (cSVOPx(pop)->op_private & OPpCONST_STRICT) 17965 no_bareword_allowed(pop); 17966 if (o->op_type == OP_GV) 17967 op_null(o->op_next); 17968 op_null(pop->op_next); 17969 op_null(pop); 17970 o->op_flags |= pop->op_next->op_flags & OPf_MOD; 17971 o->op_next = pop->op_next->op_next; 17972 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST]; 17973 o->op_private = (U8)i; 17974 if (o->op_type == OP_GV) { 17975 gv = cGVOPo_gv; 17976 GvAVn(gv); 17977 o->op_type = OP_AELEMFAST; 17978 } 17979 else 17980 o->op_type = OP_AELEMFAST_LEX; 17981 } 17982 if (o->op_type != OP_GV) 17983 break; 17984 } 17985 17986 /* Remove $foo from the op_next chain in void context. */ 17987 if (oldop 17988 && ( o->op_next->op_type == OP_RV2SV 17989 || o->op_next->op_type == OP_RV2AV 17990 || o->op_next->op_type == OP_RV2HV ) 17991 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID 17992 && !(o->op_next->op_private & OPpLVAL_INTRO)) 17993 { 17994 oldop->op_next = o->op_next->op_next; 17995 /* Reprocess the previous op if it is a nextstate, to 17996 allow double-nextstate optimisation. */ 17997 redo_nextstate: 17998 if (oldop->op_type == OP_NEXTSTATE) { 17999 oldop->op_opt = 0; 18000 o = oldop; 18001 oldop = oldoldop; 18002 oldoldop = NULL; 18003 goto redo; 18004 } 18005 o = oldop->op_next; 18006 goto redo; 18007 } 18008 else if (o->op_next->op_type == OP_RV2SV) { 18009 if (!(o->op_next->op_private & OPpDEREF)) { 18010 op_null(o->op_next); 18011 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO 18012 | OPpOUR_INTRO); 18013 o->op_next = o->op_next->op_next; 18014 OpTYPE_set(o, OP_GVSV); 18015 } 18016 } 18017 else if (o->op_next->op_type == OP_READLINE 18018 && o->op_next->op_next->op_type == OP_CONCAT 18019 && (o->op_next->op_next->op_flags & OPf_STACKED)) 18020 { 18021 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */ 18022 OpTYPE_set(o, OP_RCATLINE); 18023 o->op_flags |= OPf_STACKED; 18024 op_null(o->op_next->op_next); 18025 op_null(o->op_next); 18026 } 18027 18028 break; 18029 18030 case OP_NOT: 18031 break; 18032 18033 case OP_AND: 18034 case OP_OR: 18035 case OP_DOR: 18036 case OP_CMPCHAIN_AND: 18037 case OP_PUSHDEFER: 18038 while (cLOGOP->op_other->op_type == OP_NULL) 18039 cLOGOP->op_other = cLOGOP->op_other->op_next; 18040 while (o->op_next && ( o->op_type == o->op_next->op_type 18041 || o->op_next->op_type == OP_NULL)) 18042 o->op_next = o->op_next->op_next; 18043 18044 /* If we're an OR and our next is an AND in void context, we'll 18045 follow its op_other on short circuit, same for reverse. 18046 We can't do this with OP_DOR since if it's true, its return 18047 value is the underlying value which must be evaluated 18048 by the next op. */ 18049 if (o->op_next && 18050 ( 18051 (IS_AND_OP(o) && IS_OR_OP(o->op_next)) 18052 || (IS_OR_OP(o) && IS_AND_OP(o->op_next)) 18053 ) 18054 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID 18055 ) { 18056 o->op_next = ((LOGOP*)o->op_next)->op_other; 18057 } 18058 DEFER(cLOGOP->op_other); 18059 o->op_opt = 1; 18060 break; 18061 18062 case OP_GREPWHILE: 18063 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 18064 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0); 18065 /* FALLTHROUGH */ 18066 case OP_COND_EXPR: 18067 case OP_MAPWHILE: 18068 case OP_ANDASSIGN: 18069 case OP_ORASSIGN: 18070 case OP_DORASSIGN: 18071 case OP_RANGE: 18072 case OP_ONCE: 18073 case OP_ARGDEFELEM: 18074 while (cLOGOP->op_other->op_type == OP_NULL) 18075 cLOGOP->op_other = cLOGOP->op_other->op_next; 18076 DEFER(cLOGOP->op_other); 18077 break; 18078 18079 case OP_ENTERLOOP: 18080 case OP_ENTERITER: 18081 while (cLOOP->op_redoop->op_type == OP_NULL) 18082 cLOOP->op_redoop = cLOOP->op_redoop->op_next; 18083 while (cLOOP->op_nextop->op_type == OP_NULL) 18084 cLOOP->op_nextop = cLOOP->op_nextop->op_next; 18085 while (cLOOP->op_lastop->op_type == OP_NULL) 18086 cLOOP->op_lastop = cLOOP->op_lastop->op_next; 18087 /* a while(1) loop doesn't have an op_next that escapes the 18088 * loop, so we have to explicitly follow the op_lastop to 18089 * process the rest of the code */ 18090 DEFER(cLOOP->op_lastop); 18091 break; 18092 18093 case OP_ENTERTRY: 18094 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY); 18095 DEFER(cLOGOPo->op_other); 18096 break; 18097 18098 case OP_ENTERTRYCATCH: 18099 assert(cLOGOPo->op_other->op_type == OP_CATCH); 18100 /* catch body is the ->op_other of the OP_CATCH */ 18101 DEFER(cLOGOPx(cLOGOPo->op_other)->op_other); 18102 break; 18103 18104 case OP_SUBST: 18105 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 18106 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0); 18107 assert(!(cPMOP->op_pmflags & PMf_ONCE)); 18108 while (cPMOP->op_pmstashstartu.op_pmreplstart && 18109 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL) 18110 cPMOP->op_pmstashstartu.op_pmreplstart 18111 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next; 18112 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart); 18113 break; 18114 18115 case OP_SORT: { 18116 OP *oright; 18117 18118 if (o->op_flags & OPf_SPECIAL) { 18119 /* first arg is a code block */ 18120 OP * const nullop = OpSIBLING(cLISTOP->op_first); 18121 OP * kid = cUNOPx(nullop)->op_first; 18122 18123 assert(nullop->op_type == OP_NULL); 18124 assert(kid->op_type == OP_SCOPE 18125 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE)); 18126 /* since OP_SORT doesn't have a handy op_other-style 18127 * field that can point directly to the start of the code 18128 * block, store it in the otherwise-unused op_next field 18129 * of the top-level OP_NULL. This will be quicker at 18130 * run-time, and it will also allow us to remove leading 18131 * OP_NULLs by just messing with op_nexts without 18132 * altering the basic op_first/op_sibling layout. */ 18133 kid = kLISTOP->op_first; 18134 assert( 18135 (kid->op_type == OP_NULL 18136 && ( kid->op_targ == OP_NEXTSTATE 18137 || kid->op_targ == OP_DBSTATE )) 18138 || kid->op_type == OP_STUB 18139 || kid->op_type == OP_ENTER 18140 || (PL_parser && PL_parser->error_count)); 18141 nullop->op_next = kid->op_next; 18142 DEFER(nullop->op_next); 18143 } 18144 18145 /* check that RHS of sort is a single plain array */ 18146 oright = cUNOPo->op_first; 18147 if (!oright || oright->op_type != OP_PUSHMARK) 18148 break; 18149 18150 if (o->op_private & OPpSORT_INPLACE) 18151 break; 18152 18153 /* reverse sort ... can be optimised. */ 18154 if (!OpHAS_SIBLING(cUNOPo)) { 18155 /* Nothing follows us on the list. */ 18156 OP * const reverse = o->op_next; 18157 18158 if (reverse->op_type == OP_REVERSE && 18159 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) { 18160 OP * const pushmark = cUNOPx(reverse)->op_first; 18161 if (pushmark && (pushmark->op_type == OP_PUSHMARK) 18162 && (OpSIBLING(cUNOPx(pushmark)) == o)) { 18163 /* reverse -> pushmark -> sort */ 18164 o->op_private |= OPpSORT_REVERSE; 18165 op_null(reverse); 18166 pushmark->op_next = oright->op_next; 18167 op_null(oright); 18168 } 18169 } 18170 } 18171 18172 break; 18173 } 18174 18175 case OP_REVERSE: { 18176 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av; 18177 OP *gvop = NULL; 18178 LISTOP *enter, *exlist; 18179 18180 if (o->op_private & OPpSORT_INPLACE) 18181 break; 18182 18183 enter = (LISTOP *) o->op_next; 18184 if (!enter) 18185 break; 18186 if (enter->op_type == OP_NULL) { 18187 enter = (LISTOP *) enter->op_next; 18188 if (!enter) 18189 break; 18190 } 18191 /* for $a (...) will have OP_GV then OP_RV2GV here. 18192 for (...) just has an OP_GV. */ 18193 if (enter->op_type == OP_GV) { 18194 gvop = (OP *) enter; 18195 enter = (LISTOP *) enter->op_next; 18196 if (!enter) 18197 break; 18198 if (enter->op_type == OP_RV2GV) { 18199 enter = (LISTOP *) enter->op_next; 18200 if (!enter) 18201 break; 18202 } 18203 } 18204 18205 if (enter->op_type != OP_ENTERITER) 18206 break; 18207 18208 iter = enter->op_next; 18209 if (!iter || iter->op_type != OP_ITER) 18210 break; 18211 18212 expushmark = enter->op_first; 18213 if (!expushmark || expushmark->op_type != OP_NULL 18214 || expushmark->op_targ != OP_PUSHMARK) 18215 break; 18216 18217 exlist = (LISTOP *) OpSIBLING(expushmark); 18218 if (!exlist || exlist->op_type != OP_NULL 18219 || exlist->op_targ != OP_LIST) 18220 break; 18221 18222 if (exlist->op_last != o) { 18223 /* Mmm. Was expecting to point back to this op. */ 18224 break; 18225 } 18226 theirmark = exlist->op_first; 18227 if (!theirmark || theirmark->op_type != OP_PUSHMARK) 18228 break; 18229 18230 if (OpSIBLING(theirmark) != o) { 18231 /* There's something between the mark and the reverse, eg 18232 for (1, reverse (...)) 18233 so no go. */ 18234 break; 18235 } 18236 18237 ourmark = ((LISTOP *)o)->op_first; 18238 if (!ourmark || ourmark->op_type != OP_PUSHMARK) 18239 break; 18240 18241 ourlast = ((LISTOP *)o)->op_last; 18242 if (!ourlast || ourlast->op_next != o) 18243 break; 18244 18245 rv2av = OpSIBLING(ourmark); 18246 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av) 18247 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) { 18248 /* We're just reversing a single array. */ 18249 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF; 18250 enter->op_flags |= OPf_STACKED; 18251 } 18252 18253 /* We don't have control over who points to theirmark, so sacrifice 18254 ours. */ 18255 theirmark->op_next = ourmark->op_next; 18256 theirmark->op_flags = ourmark->op_flags; 18257 ourlast->op_next = gvop ? gvop : (OP *) enter; 18258 op_null(ourmark); 18259 op_null(o); 18260 enter->op_private |= OPpITER_REVERSED; 18261 iter->op_private |= OPpITER_REVERSED; 18262 18263 oldoldop = NULL; 18264 oldop = ourlast; 18265 o = oldop->op_next; 18266 goto redo; 18267 NOT_REACHED; /* NOTREACHED */ 18268 break; 18269 } 18270 18271 case OP_QR: 18272 case OP_MATCH: 18273 if (!(cPMOP->op_pmflags & PMf_ONCE)) { 18274 assert (!cPMOP->op_pmstashstartu.op_pmreplstart); 18275 } 18276 break; 18277 18278 case OP_RUNCV: 18279 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv) 18280 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb))) 18281 { 18282 SV *sv; 18283 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef; 18284 else { 18285 sv = newRV((SV *)PL_compcv); 18286 sv_rvweaken(sv); 18287 SvREADONLY_on(sv); 18288 } 18289 OpTYPE_set(o, OP_CONST); 18290 o->op_flags |= OPf_SPECIAL; 18291 cSVOPo->op_sv = sv; 18292 } 18293 break; 18294 18295 case OP_SASSIGN: 18296 if (OP_GIMME(o,0) == G_VOID 18297 || ( o->op_next->op_type == OP_LINESEQ 18298 && ( o->op_next->op_next->op_type == OP_LEAVESUB 18299 || ( o->op_next->op_next->op_type == OP_RETURN 18300 && !CvLVALUE(PL_compcv))))) 18301 { 18302 OP *right = cBINOP->op_first; 18303 if (right) { 18304 /* sassign 18305 * RIGHT 18306 * substr 18307 * pushmark 18308 * arg1 18309 * arg2 18310 * ... 18311 * becomes 18312 * 18313 * ex-sassign 18314 * substr 18315 * pushmark 18316 * RIGHT 18317 * arg1 18318 * arg2 18319 * ... 18320 */ 18321 OP *left = OpSIBLING(right); 18322 if (left->op_type == OP_SUBSTR 18323 && (left->op_private & 7) < 4) { 18324 op_null(o); 18325 /* cut out right */ 18326 op_sibling_splice(o, NULL, 1, NULL); 18327 /* and insert it as second child of OP_SUBSTR */ 18328 op_sibling_splice(left, cBINOPx(left)->op_first, 0, 18329 right); 18330 left->op_private |= OPpSUBSTR_REPL_FIRST; 18331 left->op_flags = 18332 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; 18333 } 18334 } 18335 } 18336 break; 18337 18338 case OP_AASSIGN: { 18339 int l, r, lr, lscalars, rscalars; 18340 18341 /* handle common vars detection, e.g. ($a,$b) = ($b,$a). 18342 Note that we do this now rather than in newASSIGNOP(), 18343 since only by now are aliased lexicals flagged as such 18344 18345 See the essay "Common vars in list assignment" above for 18346 the full details of the rationale behind all the conditions 18347 below. 18348 18349 PL_generation sorcery: 18350 To detect whether there are common vars, the global var 18351 PL_generation is incremented for each assign op we scan. 18352 Then we run through all the lexical variables on the LHS, 18353 of the assignment, setting a spare slot in each of them to 18354 PL_generation. Then we scan the RHS, and if any lexicals 18355 already have that value, we know we've got commonality. 18356 Also, if the generation number is already set to 18357 PERL_INT_MAX, then the variable is involved in aliasing, so 18358 we also have potential commonality in that case. 18359 */ 18360 18361 PL_generation++; 18362 /* scan LHS */ 18363 lscalars = 0; 18364 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, &lscalars); 18365 /* scan RHS */ 18366 rscalars = 0; 18367 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, &rscalars); 18368 lr = (l|r); 18369 18370 18371 /* After looking for things which are *always* safe, this main 18372 * if/else chain selects primarily based on the type of the 18373 * LHS, gradually working its way down from the more dangerous 18374 * to the more restrictive and thus safer cases */ 18375 18376 if ( !l /* () = ....; */ 18377 || !r /* .... = (); */ 18378 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */ 18379 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */ 18380 || (lscalars < 2) /* (undef, $x) = ... */ 18381 ) { 18382 NOOP; /* always safe */ 18383 } 18384 else if (l & AAS_DANGEROUS) { 18385 /* always dangerous */ 18386 o->op_private |= OPpASSIGN_COMMON_SCALAR; 18387 o->op_private |= OPpASSIGN_COMMON_AGG; 18388 } 18389 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) { 18390 /* package vars are always dangerous - too many 18391 * aliasing possibilities */ 18392 if (l & AAS_PKG_SCALAR) 18393 o->op_private |= OPpASSIGN_COMMON_SCALAR; 18394 if (l & AAS_PKG_AGG) 18395 o->op_private |= OPpASSIGN_COMMON_AGG; 18396 } 18397 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG 18398 |AAS_LEX_SCALAR|AAS_LEX_AGG)) 18399 { 18400 /* LHS contains only lexicals and safe ops */ 18401 18402 if (l & (AAS_MY_AGG|AAS_LEX_AGG)) 18403 o->op_private |= OPpASSIGN_COMMON_AGG; 18404 18405 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) { 18406 if (lr & AAS_LEX_SCALAR_COMM) 18407 o->op_private |= OPpASSIGN_COMMON_SCALAR; 18408 else if ( !(l & AAS_LEX_SCALAR) 18409 && (r & AAS_DEFAV)) 18410 { 18411 /* falsely mark 18412 * my (...) = @_ 18413 * as scalar-safe for performance reasons. 18414 * (it will still have been marked _AGG if necessary */ 18415 NOOP; 18416 } 18417 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS)) 18418 /* if there are only lexicals on the LHS and no 18419 * common ones on the RHS, then we assume that the 18420 * only way those lexicals could also get 18421 * on the RHS is via some sort of dereffing or 18422 * closure, e.g. 18423 * $r = \$lex; 18424 * ($lex, $x) = (1, $$r) 18425 * and in this case we assume the var must have 18426 * a bumped ref count. So if its ref count is 1, 18427 * it must only be on the LHS. 18428 */ 18429 o->op_private |= OPpASSIGN_COMMON_RC1; 18430 } 18431 } 18432 18433 /* ... = ($x) 18434 * may have to handle aggregate on LHS, but we can't 18435 * have common scalars. */ 18436 if (rscalars < 2) 18437 o->op_private &= 18438 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1); 18439 18440 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 18441 S_check_for_bool_cxt(o, 1, OPpASSIGN_TRUEBOOL, 0); 18442 break; 18443 } 18444 18445 case OP_REF: 18446 case OP_BLESSED: 18447 /* if the op is used in boolean context, set the TRUEBOOL flag 18448 * which enables an optimisation at runtime which avoids creating 18449 * a stack temporary for known-true package names */ 18450 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 18451 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL); 18452 break; 18453 18454 case OP_LENGTH: 18455 /* see if the op is used in known boolean context, 18456 * but not if OA_TARGLEX optimisation is enabled */ 18457 if ( (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR 18458 && !(o->op_private & OPpTARGET_MY) 18459 ) 18460 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0); 18461 break; 18462 18463 case OP_POS: 18464 /* see if the op is used in known boolean context */ 18465 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR) 18466 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0); 18467 break; 18468 18469 case OP_CUSTOM: { 18470 Perl_cpeep_t cpeep = 18471 XopENTRYCUSTOM(o, xop_peep); 18472 if (cpeep) 18473 cpeep(aTHX_ o, oldop); 18474 break; 18475 } 18476 18477 } 18478 /* did we just null the current op? If so, re-process it to handle 18479 * eliding "empty" ops from the chain */ 18480 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) { 18481 o->op_opt = 0; 18482 o = oldop; 18483 } 18484 else { 18485 oldoldop = oldop; 18486 oldop = o; 18487 } 18488 } 18489 LEAVE; 18490 } 18491 18492 void 18493 Perl_peep(pTHX_ OP *o) 18494 { 18495 CALL_RPEEP(o); 18496 } 18497 18498 /* 18499 =for apidoc_section $custom 18500 18501 =for apidoc Perl_custom_op_xop 18502 Return the XOP structure for a given custom op. This macro should be 18503 considered internal to C<OP_NAME> and the other access macros: use them instead. 18504 This macro does call a function. Prior 18505 to 5.19.6, this was implemented as a 18506 function. 18507 18508 =cut 18509 */ 18510 18511 18512 /* use PERL_MAGIC_ext to call a function to free the xop structure when 18513 * freeing PL_custom_ops */ 18514 18515 static int 18516 custom_op_register_free(pTHX_ SV *sv, MAGIC *mg) 18517 { 18518 XOP *xop; 18519 18520 PERL_UNUSED_ARG(mg); 18521 xop = INT2PTR(XOP *, SvIV(sv)); 18522 Safefree(xop->xop_name); 18523 Safefree(xop->xop_desc); 18524 Safefree(xop); 18525 return 0; 18526 } 18527 18528 18529 static const MGVTBL custom_op_register_vtbl = { 18530 0, /* get */ 18531 0, /* set */ 18532 0, /* len */ 18533 0, /* clear */ 18534 custom_op_register_free, /* free */ 18535 0, /* copy */ 18536 0, /* dup */ 18537 #ifdef MGf_LOCAL 18538 0, /* local */ 18539 #endif 18540 }; 18541 18542 18543 XOPRETANY 18544 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field) 18545 { 18546 SV *keysv; 18547 HE *he = NULL; 18548 XOP *xop; 18549 18550 static const XOP xop_null = { 0, 0, 0, 0, 0 }; 18551 18552 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD; 18553 assert(o->op_type == OP_CUSTOM); 18554 18555 /* This is wrong. It assumes a function pointer can be cast to IV, 18556 * which isn't guaranteed, but this is what the old custom OP code 18557 * did. In principle it should be safer to Copy the bytes of the 18558 * pointer into a PV: since the new interface is hidden behind 18559 * functions, this can be changed later if necessary. */ 18560 /* Change custom_op_xop if this ever happens */ 18561 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr))); 18562 18563 if (PL_custom_ops) 18564 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0); 18565 18566 /* See if the op isn't registered, but its name *is* registered. 18567 * That implies someone is using the pre-5.14 API,where only name and 18568 * description could be registered. If so, fake up a real 18569 * registration. 18570 * We only check for an existing name, and assume no one will have 18571 * just registered a desc */ 18572 if (!he && PL_custom_op_names && 18573 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0)) 18574 ) { 18575 const char *pv; 18576 STRLEN l; 18577 18578 /* XXX does all this need to be shared mem? */ 18579 Newxz(xop, 1, XOP); 18580 pv = SvPV(HeVAL(he), l); 18581 XopENTRY_set(xop, xop_name, savepvn(pv, l)); 18582 if (PL_custom_op_descs && 18583 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0)) 18584 ) { 18585 pv = SvPV(HeVAL(he), l); 18586 XopENTRY_set(xop, xop_desc, savepvn(pv, l)); 18587 } 18588 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop); 18589 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0); 18590 /* add magic to the SV so that the xop struct (pointed to by 18591 * SvIV(sv)) is freed. Normally a static xop is registered, but 18592 * for this backcompat hack, we've alloced one */ 18593 (void)sv_magicext(HeVAL(he), NULL, PERL_MAGIC_ext, 18594 &custom_op_register_vtbl, NULL, 0); 18595 18596 } 18597 else { 18598 if (!he) 18599 xop = (XOP *)&xop_null; 18600 else 18601 xop = INT2PTR(XOP *, SvIV(HeVAL(he))); 18602 } 18603 18604 { 18605 XOPRETANY any; 18606 if(field == XOPe_xop_ptr) { 18607 any.xop_ptr = xop; 18608 } else { 18609 const U32 flags = XopFLAGS(xop); 18610 if(flags & field) { 18611 switch(field) { 18612 case XOPe_xop_name: 18613 any.xop_name = xop->xop_name; 18614 break; 18615 case XOPe_xop_desc: 18616 any.xop_desc = xop->xop_desc; 18617 break; 18618 case XOPe_xop_class: 18619 any.xop_class = xop->xop_class; 18620 break; 18621 case XOPe_xop_peep: 18622 any.xop_peep = xop->xop_peep; 18623 break; 18624 default: 18625 field_panic: 18626 Perl_croak(aTHX_ 18627 "panic: custom_op_get_field(): invalid field %d\n", 18628 (int)field); 18629 break; 18630 } 18631 } else { 18632 switch(field) { 18633 case XOPe_xop_name: 18634 any.xop_name = XOPd_xop_name; 18635 break; 18636 case XOPe_xop_desc: 18637 any.xop_desc = XOPd_xop_desc; 18638 break; 18639 case XOPe_xop_class: 18640 any.xop_class = XOPd_xop_class; 18641 break; 18642 case XOPe_xop_peep: 18643 any.xop_peep = XOPd_xop_peep; 18644 break; 18645 default: 18646 goto field_panic; 18647 break; 18648 } 18649 } 18650 } 18651 return any; 18652 } 18653 } 18654 18655 /* 18656 =for apidoc custom_op_register 18657 Register a custom op. See L<perlguts/"Custom Operators">. 18658 18659 =cut 18660 */ 18661 18662 void 18663 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop) 18664 { 18665 SV *keysv; 18666 18667 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER; 18668 18669 /* see the comment in custom_op_xop */ 18670 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr))); 18671 18672 if (!PL_custom_ops) 18673 PL_custom_ops = newHV(); 18674 18675 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0)) 18676 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name); 18677 } 18678 18679 /* 18680 18681 =for apidoc core_prototype 18682 18683 This function assigns the prototype of the named core function to C<sv>, or 18684 to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or 18685 C<NULL> if the core function has no prototype. C<code> is a code as returned 18686 by C<keyword()>. It must not be equal to 0. 18687 18688 =cut 18689 */ 18690 18691 SV * 18692 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code, 18693 int * const opnum) 18694 { 18695 int i = 0, n = 0, seen_question = 0, defgv = 0; 18696 I32 oa; 18697 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2) 18698 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */ 18699 bool nullret = FALSE; 18700 18701 PERL_ARGS_ASSERT_CORE_PROTOTYPE; 18702 18703 assert (code); 18704 18705 if (!sv) sv = sv_newmortal(); 18706 18707 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv 18708 18709 switch (code < 0 ? -code : code) { 18710 case KEY_and : case KEY_chop: case KEY_chomp: 18711 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec : 18712 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto : 18713 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le : 18714 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next : 18715 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr : 18716 case KEY_redo : case KEY_require: case KEY_return: case KEY_say : 18717 case KEY_select: case KEY_sort : case KEY_split : case KEY_system: 18718 case KEY_x : case KEY_xor : 18719 if (!opnum) return NULL; nullret = TRUE; goto findopnum; 18720 case KEY_glob: retsetpvs("_;", OP_GLOB); 18721 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS); 18722 case KEY_values: retsetpvs("\\[%@]", OP_VALUES); 18723 case KEY_each: retsetpvs("\\[%@]", OP_EACH); 18724 case KEY_pos: retsetpvs(";\\[$*]", OP_POS); 18725 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__: 18726 retsetpvs("", 0); 18727 case KEY_evalbytes: 18728 name = "entereval"; break; 18729 case KEY_readpipe: 18730 name = "backtick"; 18731 } 18732 18733 #undef retsetpvs 18734 18735 findopnum: 18736 while (i < MAXO) { /* The slow way. */ 18737 if (strEQ(name, PL_op_name[i]) 18738 || strEQ(name, PL_op_desc[i])) 18739 { 18740 if (nullret) { assert(opnum); *opnum = i; return NULL; } 18741 goto found; 18742 } 18743 i++; 18744 } 18745 return NULL; 18746 found: 18747 defgv = PL_opargs[i] & OA_DEFGV; 18748 oa = PL_opargs[i] >> OASHIFT; 18749 while (oa) { 18750 if (oa & OA_OPTIONAL && !seen_question && ( 18751 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF 18752 )) { 18753 seen_question = 1; 18754 str[n++] = ';'; 18755 } 18756 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF 18757 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF 18758 /* But globs are already references (kinda) */ 18759 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF 18760 ) { 18761 str[n++] = '\\'; 18762 } 18763 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF 18764 && !scalar_mod_type(NULL, i)) { 18765 str[n++] = '['; 18766 str[n++] = '$'; 18767 str[n++] = '@'; 18768 str[n++] = '%'; 18769 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&'; 18770 str[n++] = '*'; 18771 str[n++] = ']'; 18772 } 18773 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)]; 18774 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') { 18775 str[n-1] = '_'; defgv = 0; 18776 } 18777 oa = oa >> 4; 18778 } 18779 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';'; 18780 str[n++] = '\0'; 18781 sv_setpvn(sv, str, n - 1); 18782 if (opnum) *opnum = i; 18783 return sv; 18784 } 18785 18786 OP * 18787 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code, 18788 const int opnum) 18789 { 18790 OP * const argop = (opnum == OP_SELECT && code) ? NULL : 18791 newSVOP(OP_COREARGS,0,coreargssv); 18792 OP *o; 18793 18794 PERL_ARGS_ASSERT_CORESUB_OP; 18795 18796 switch(opnum) { 18797 case 0: 18798 return op_append_elem(OP_LINESEQ, 18799 argop, 18800 newSLICEOP(0, 18801 newSVOP(OP_CONST, 0, newSViv(-code % 3)), 18802 newOP(OP_CALLER,0) 18803 ) 18804 ); 18805 case OP_EACH: 18806 case OP_KEYS: 18807 case OP_VALUES: 18808 o = newUNOP(OP_AVHVSWITCH,0,argop); 18809 o->op_private = opnum-OP_EACH; 18810 return o; 18811 case OP_SELECT: /* which represents OP_SSELECT as well */ 18812 if (code) 18813 return newCONDOP( 18814 0, 18815 newBINOP(OP_GT, 0, 18816 newAVREF(newGVOP(OP_GV, 0, PL_defgv)), 18817 newSVOP(OP_CONST, 0, newSVuv(1)) 18818 ), 18819 coresub_op(newSVuv((UV)OP_SSELECT), 0, 18820 OP_SSELECT), 18821 coresub_op(coreargssv, 0, OP_SELECT) 18822 ); 18823 /* FALLTHROUGH */ 18824 default: 18825 switch (PL_opargs[opnum] & OA_CLASS_MASK) { 18826 case OA_BASEOP: 18827 return op_append_elem( 18828 OP_LINESEQ, argop, 18829 newOP(opnum, 18830 opnum == OP_WANTARRAY || opnum == OP_RUNCV 18831 ? OPpOFFBYONE << 8 : 0) 18832 ); 18833 case OA_BASEOP_OR_UNOP: 18834 if (opnum == OP_ENTEREVAL) { 18835 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop); 18836 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES; 18837 } 18838 else o = newUNOP(opnum,0,argop); 18839 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE; 18840 else { 18841 onearg: 18842 if (is_handle_constructor(o, 1)) 18843 argop->op_private |= OPpCOREARGS_DEREF1; 18844 if (scalar_mod_type(NULL, opnum)) 18845 argop->op_private |= OPpCOREARGS_SCALARMOD; 18846 } 18847 return o; 18848 default: 18849 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop); 18850 if (is_handle_constructor(o, 2)) 18851 argop->op_private |= OPpCOREARGS_DEREF2; 18852 if (opnum == OP_SUBSTR) { 18853 o->op_private |= OPpMAYBE_LVSUB; 18854 return o; 18855 } 18856 else goto onearg; 18857 } 18858 } 18859 } 18860 18861 void 18862 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv, 18863 SV * const *new_const_svp) 18864 { 18865 const char *hvname; 18866 bool is_const = !!CvCONST(old_cv); 18867 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL; 18868 18869 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV; 18870 18871 if (is_const && new_const_svp && old_const_sv == *new_const_svp) 18872 return; 18873 /* They are 2 constant subroutines generated from 18874 the same constant. This probably means that 18875 they are really the "same" proxy subroutine 18876 instantiated in 2 places. Most likely this is 18877 when a constant is exported twice. Don't warn. 18878 */ 18879 if ( 18880 (ckWARN(WARN_REDEFINE) 18881 && !( 18882 CvGV(old_cv) && GvSTASH(CvGV(old_cv)) 18883 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7 18884 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))), 18885 strEQ(hvname, "autouse")) 18886 ) 18887 ) 18888 || (is_const 18889 && ckWARN_d(WARN_REDEFINE) 18890 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp)) 18891 ) 18892 ) 18893 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 18894 is_const 18895 ? "Constant subroutine %" SVf " redefined" 18896 : "Subroutine %" SVf " redefined", 18897 SVfARG(name)); 18898 } 18899 18900 /* 18901 =for apidoc_section $hook 18902 18903 These functions provide convenient and thread-safe means of manipulating 18904 hook variables. 18905 18906 =cut 18907 */ 18908 18909 /* 18910 =for apidoc wrap_op_checker 18911 18912 Puts a C function into the chain of check functions for a specified op 18913 type. This is the preferred way to manipulate the L</PL_check> array. 18914 C<opcode> specifies which type of op is to be affected. C<new_checker> 18915 is a pointer to the C function that is to be added to that opcode's 18916 check chain, and C<old_checker_p> points to the storage location where a 18917 pointer to the next function in the chain will be stored. The value of 18918 C<new_checker> is written into the L</PL_check> array, while the value 18919 previously stored there is written to C<*old_checker_p>. 18920 18921 L</PL_check> is global to an entire process, and a module wishing to 18922 hook op checking may find itself invoked more than once per process, 18923 typically in different threads. To handle that situation, this function 18924 is idempotent. The location C<*old_checker_p> must initially (once 18925 per process) contain a null pointer. A C variable of static duration 18926 (declared at file scope, typically also marked C<static> to give 18927 it internal linkage) will be implicitly initialised appropriately, 18928 if it does not have an explicit initialiser. This function will only 18929 actually modify the check chain if it finds C<*old_checker_p> to be null. 18930 This function is also thread safe on the small scale. It uses appropriate 18931 locking to avoid race conditions in accessing L</PL_check>. 18932 18933 When this function is called, the function referenced by C<new_checker> 18934 must be ready to be called, except for C<*old_checker_p> being unfilled. 18935 In a threading situation, C<new_checker> may be called immediately, 18936 even before this function has returned. C<*old_checker_p> will always 18937 be appropriately set before C<new_checker> is called. If C<new_checker> 18938 decides not to do anything special with an op that it is given (which 18939 is the usual case for most uses of op check hooking), it must chain the 18940 check function referenced by C<*old_checker_p>. 18941 18942 Taken all together, XS code to hook an op checker should typically look 18943 something like this: 18944 18945 static Perl_check_t nxck_frob; 18946 static OP *myck_frob(pTHX_ OP *op) { 18947 ... 18948 op = nxck_frob(aTHX_ op); 18949 ... 18950 return op; 18951 } 18952 BOOT: 18953 wrap_op_checker(OP_FROB, myck_frob, &nxck_frob); 18954 18955 If you want to influence compilation of calls to a specific subroutine, 18956 then use L</cv_set_call_checker_flags> rather than hooking checking of 18957 all C<entersub> ops. 18958 18959 =cut 18960 */ 18961 18962 void 18963 Perl_wrap_op_checker(pTHX_ Optype opcode, 18964 Perl_check_t new_checker, Perl_check_t *old_checker_p) 18965 { 18966 18967 PERL_UNUSED_CONTEXT; 18968 PERL_ARGS_ASSERT_WRAP_OP_CHECKER; 18969 if (*old_checker_p) return; 18970 OP_CHECK_MUTEX_LOCK; 18971 if (!*old_checker_p) { 18972 *old_checker_p = PL_check[opcode]; 18973 PL_check[opcode] = new_checker; 18974 } 18975 OP_CHECK_MUTEX_UNLOCK; 18976 } 18977 18978 #include "XSUB.h" 18979 18980 /* Efficient sub that returns a constant scalar value. */ 18981 static void 18982 const_sv_xsub(pTHX_ CV* cv) 18983 { 18984 dXSARGS; 18985 SV *const sv = MUTABLE_SV(XSANY.any_ptr); 18986 PERL_UNUSED_ARG(items); 18987 if (!sv) { 18988 XSRETURN(0); 18989 } 18990 EXTEND(sp, 1); 18991 ST(0) = sv; 18992 XSRETURN(1); 18993 } 18994 18995 static void 18996 const_av_xsub(pTHX_ CV* cv) 18997 { 18998 dXSARGS; 18999 AV * const av = MUTABLE_AV(XSANY.any_ptr); 19000 SP -= items; 19001 assert(av); 19002 #ifndef DEBUGGING 19003 if (!av) { 19004 XSRETURN(0); 19005 } 19006 #endif 19007 if (SvRMAGICAL(av)) 19008 Perl_croak(aTHX_ "Magical list constants are not supported"); 19009 if (GIMME_V != G_LIST) { 19010 EXTEND(SP, 1); 19011 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1)); 19012 XSRETURN(1); 19013 } 19014 EXTEND(SP, AvFILLp(av)+1); 19015 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *); 19016 XSRETURN(AvFILLp(av)+1); 19017 } 19018 19019 /* Copy an existing cop->cop_warnings field. 19020 * If it's one of the standard addresses, just re-use the address. 19021 * This is the e implementation for the DUP_WARNINGS() macro 19022 */ 19023 19024 STRLEN* 19025 Perl_dup_warnings(pTHX_ STRLEN* warnings) 19026 { 19027 Size_t size; 19028 STRLEN *new_warnings; 19029 19030 if (warnings == NULL || specialWARN(warnings)) 19031 return warnings; 19032 19033 size = sizeof(*warnings) + *warnings; 19034 19035 new_warnings = (STRLEN*)PerlMemShared_malloc(size); 19036 Copy(warnings, new_warnings, size, char); 19037 return new_warnings; 19038 } 19039 19040 /* 19041 * ex: set ts=8 sts=4 sw=4 et: 19042 */ 19043