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
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.
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
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
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.
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):
44 * newBINOP(OP_ADD, flags,
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
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
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:
65 * with the intended execution order being:
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
69 * At this point all the nodes' op_next pointers will have been set,
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
84 * [*] => A; A => B; B => [*]
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.
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.
97 * In summary: given a subtree, its top-level node's op_next will either
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
106 Here's an older description from Larry.
108 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
112 An execution-order pass
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
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.
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.
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.
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.
164 #include "keywords.h"
167 #include "invlist_inline.h"
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)
173 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
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.
181 S_prune_chain_head(OP** 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)
189 *op_p = (*op_p)->op_next;
193 /* See the explanatory comments above struct opslab in op.h. */
195 #ifdef PERL_DEBUG_READONLY_OPS
196 # define PERL_SLAB_SIZE 128
197 # define PERL_MAX_SLAB_SIZE 4096
198 # include <sys/mman.h>
201 #ifndef PERL_SLAB_SIZE
202 # define PERL_SLAB_SIZE 64
204 #ifndef PERL_MAX_SLAB_SIZE
205 # define PERL_MAX_SLAB_SIZE 2048
208 /* rounds up to nearest pointer */
209 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
212 (assert(((char *)(p) - (char *)(o)) % sizeof(I32**) == 0), \
213 ((size_t)((I32 **)(p) - (I32**)(o))))
215 /* requires double parens and aTHX_ */
216 #define DEBUG_S_warn(args) \
218 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
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)))
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))
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 */
232 S_new_slab(pTHX_ OPSLAB *head, size_t sz)
235 size_t sz_bytes = OpSLABSizeBytes(sz);
237 /* opslot_offset is only U16 */
238 assert(sz < U16_MAX);
239 /* room for at least one op */
240 assert(sz >= OPSLOT_SIZE_BASE);
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");
253 slab = (OPSLAB *)PerlMemShared_malloc(sz_bytes);
254 Zero(slab, sz_bytes, char);
256 slab->opslab_size = (U16)sz;
259 /* The context is unused in non-Windows */
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)));
270 #define OPSLOT_SIZE_TO_INDEX(sz) ((sz) - OPSLOT_SIZE_BASE)
272 #define link_freed_op(slab, o) S_link_freed_op(aTHX_ slab, o)
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);
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*));
285 if (!slab->opslab_freed)
288 else if (index >= slab->opslab_freed_size) {
289 /* It's probably not worth doing exponential expansion here, the number of op sizes
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*));
299 Zero(p+slab->opslab_freed_size, newsize - slab->opslab_freed_size, OP *);
301 slab->opslab_freed = p;
302 slab->opslab_freed_size = newsize;
305 o->op_next = slab->opslab_freed[index];
306 slab->opslab_freed[index] = o;
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.
316 Perl_Slab_Alloc(pTHX_ size_t sz)
318 OPSLAB *head_slab; /* first slab in the chain */
322 size_t sz_in_p; /* size in pointer units, including the OPSLOT header */
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)))
333 o = (OP*)PerlMemShared_calloc(1, sz);
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
343 if (!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 */
349 else ++(head_slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
351 sz_in_p = SIZE_TO_PSIZE(sz + OPSLOT_HEADER);
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) {
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];
366 if (base_index < head_slab->opslab_freed_size) {
367 /* found a freed op */
368 o = head_slab->opslab_freed[base_index];
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;
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; \
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
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);
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
405 : slab2->opslab_size * 2);
406 slab2->opslab_next = head_slab->opslab_next;
407 head_slab->opslab_next = slab2;
409 assert(slab2->opslab_size >= sz_in_p);
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));
419 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
420 assert(!o->op_moresib);
421 assert(!o->op_sibparent);
428 #ifdef PERL_DEBUG_READONLY_OPS
430 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
432 PERL_ARGS_ASSERT_SLAB_TO_RO;
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);
446 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
450 PERL_ARGS_ASSERT_SLAB_TO_RW;
452 if (!slab->opslab_readonly) return;
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);
463 slab->opslab_readonly = 0;
467 # define Slab_to_rw(op) NOOP
470 /* This cannot possibly be right, but it was copied from the old slab
471 allocator, to which it was originally added, without explanation, in
474 # define PerlMemShared PerlMem
477 /* make freed ops die if they're inadvertently executed */
482 DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
487 /* Return the block of memory used by an op to the free list of
488 * the OP slab associated with that op.
492 Perl_Slab_Free(pTHX_ void *op)
494 OP * const o = (OP *)op;
497 PERL_ARGS_ASSERT_SLAB_FREE;
500 o->op_ppaddr = S_pp_freed;
503 if (!o->op_slabbed) {
505 PerlMemShared_free(op);
510 /* If this op is already freed, our refcount will get screwy. */
511 assert(o->op_type != OP_FREED);
512 o->op_type = OP_FREED;
513 link_freed_op(slab, o);
514 DEBUG_S_warn((aTHX_ "freeing op at %p, slab %p, head slab %p",
515 (void*)o, (void *)OpMySLAB(o), (void*)slab));
516 OpslabREFCNT_dec_padok(slab);
520 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
522 const bool havepad = !!PL_comppad;
523 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
526 PAD_SAVE_SETNULLPAD();
532 /* Free a chain of OP slabs. Should only be called after all ops contained
533 * in it have been freed. At this point, its reference count should be 1,
534 * because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1,
535 * and just directly calls opslab_free().
536 * (Note that the reference count which PL_compcv held on the slab should
537 * have been removed once compilation of the sub was complete).
543 Perl_opslab_free(pTHX_ OPSLAB *slab)
546 PERL_ARGS_ASSERT_OPSLAB_FREE;
548 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
549 assert(slab->opslab_refcnt == 1);
550 PerlMemShared_free(slab->opslab_freed);
552 slab2 = slab->opslab_next;
554 slab->opslab_refcnt = ~(size_t)0;
556 #ifdef PERL_DEBUG_READONLY_OPS
557 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
559 if (munmap(slab, OpSLABSizeBytes(slab->opslab_size))) {
560 perror("munmap failed");
564 PerlMemShared_free(slab);
570 /* like opslab_free(), but first calls op_free() on any ops in the slab
571 * not marked as OP_FREED
575 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
579 size_t savestack_count = 0;
581 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
584 OPSLOT *slot = OpSLOToff(slab2, slab2->opslab_free_space);
585 OPSLOT *end = OpSLOToff(slab2, slab2->opslab_size);
587 slot = (OPSLOT*) ((I32**)slot + slot->opslot_size) )
589 if (slot->opslot_op.op_type != OP_FREED
590 && !(slot->opslot_op.op_savefree
596 assert(slot->opslot_op.op_slabbed);
597 op_free(&slot->opslot_op);
598 if (slab->opslab_refcnt == 1) goto free;
601 } while ((slab2 = slab2->opslab_next));
602 /* > 1 because the CV still holds a reference count. */
603 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
605 assert(savestack_count == slab->opslab_refcnt-1);
607 /* Remove the CV’s reference count. */
608 slab->opslab_refcnt--;
615 #ifdef PERL_DEBUG_READONLY_OPS
617 Perl_op_refcnt_inc(pTHX_ OP *o)
620 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
621 if (slab && slab->opslab_readonly) {
634 Perl_op_refcnt_dec(pTHX_ OP *o)
637 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
639 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
641 if (slab && slab->opslab_readonly) {
643 result = --o->op_targ;
646 result = --o->op_targ;
652 * In the following definition, the ", (OP*)0" is just to make the compiler
653 * think the expression is of the right type: croak actually does a Siglongjmp.
655 #define CHECKOP(type,o) \
656 ((PL_op_mask && PL_op_mask[type]) \
657 ? ( op_free((OP*)o), \
658 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
660 : PL_check[type](aTHX_ (OP*)o))
662 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
664 #define OpTYPE_set(o,type) \
666 o->op_type = (OPCODE)type; \
667 o->op_ppaddr = PL_ppaddr[type]; \
671 S_no_fh_allowed(pTHX_ OP *o)
673 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
675 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
681 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
683 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
684 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
689 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
691 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
693 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
698 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
700 PERL_ARGS_ASSERT_BAD_TYPE_PV;
702 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
703 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
707 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
709 SV * const namesv = cv_name((CV *)gv, NULL, 0);
710 PERL_ARGS_ASSERT_BAD_TYPE_GV;
712 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
713 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
717 S_no_bareword_allowed(pTHX_ OP *o)
719 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
721 qerror(Perl_mess(aTHX_
722 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
724 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
728 Perl_no_bareword_filehandle(pTHX_ const char *fhname) {
729 PERL_ARGS_ASSERT_NO_BAREWORD_FILEHANDLE;
731 if (strNE(fhname, "STDERR")
732 && strNE(fhname, "STDOUT")
733 && strNE(fhname, "STDIN")
734 && strNE(fhname, "_")
735 && strNE(fhname, "ARGV")
736 && strNE(fhname, "ARGVOUT")
737 && strNE(fhname, "DATA")) {
738 qerror(Perl_mess(aTHX_ "Bareword filehandle \"%s\" not allowed under 'no feature \"bareword_filehandles\"'", fhname));
742 /* "register" allocation */
745 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
748 bool is_idfirst, is_default;
749 const bool is_our = (PL_parser->in_my == KEY_our);
751 PERL_ARGS_ASSERT_ALLOCMY;
753 if (flags & ~SVf_UTF8)
754 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
757 is_idfirst = flags & SVf_UTF8
758 ? isIDFIRST_utf8_safe((U8*)name + 1, name + len)
759 : isIDFIRST_A(name[1]);
762 is_default = len == 2 && name[1] == '_';
764 /* complain about "my $<special_var>" etc etc */
765 if (!is_our && (!is_idfirst || is_default)) {
766 const char * const type =
767 PL_parser->in_my == KEY_sigvar ? "subroutine signature" :
768 PL_parser->in_my == KEY_state ? "\"state\"" : "\"my\"";
770 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
772 && (!isPRINT(name[1]) || memCHRs("\t\n\r\f", name[1]))) {
773 /* diag_listed_as: Can't use global %s in %s */
774 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in %s",
775 name[0], toCTRL(name[1]),
776 (int)(len - 2), name + 2,
779 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in %s",
781 type), flags & SVf_UTF8);
785 /* allocate a spare slot and store the name in that slot */
787 off = pad_add_name_pvn(name, len,
788 (is_our ? padadd_OUR :
789 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
790 PL_parser->in_my_stash,
792 /* $_ is always in main::, even with our */
793 ? (PL_curstash && !memEQs(name,len,"$_")
799 /* anon sub prototypes contains state vars should always be cloned,
800 * otherwise the state var would be shared between anon subs */
802 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
803 CvCLONE_on(PL_compcv);
809 =for apidoc_section $optree_manipulation
811 =for apidoc alloccopstash
813 Available only under threaded builds, this function allocates an entry in
814 C<PL_stashpad> for the stash passed to it.
821 Perl_alloccopstash(pTHX_ HV *hv)
823 PADOFFSET off = 0, o = 1;
824 bool found_slot = FALSE;
826 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
828 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
830 for (; o < PL_stashpadmax; ++o) {
831 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
832 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
833 found_slot = TRUE, off = o;
836 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
837 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
838 off = PL_stashpadmax;
839 PL_stashpadmax += 10;
842 PL_stashpad[PL_stashpadix = off] = hv;
847 /* free the body of an op without examining its contents.
848 * Always use this rather than FreeOp directly */
851 S_op_destroy(pTHX_ OP *o)
861 Free an op and its children. Only use this when an op is no longer linked
868 Perl_op_free(pTHX_ OP *o)
873 bool went_up = FALSE; /* whether we reached the current node by
874 following the parent pointer from a child, and
875 so have already seen this node */
877 if (!o || o->op_type == OP_FREED)
880 if (o->op_private & OPpREFCOUNTED) {
881 /* if base of tree is refcounted, just decrement */
882 switch (o->op_type) {
892 refcnt = OpREFCNT_dec(o);
895 /* Need to find and remove any pattern match ops from
896 * the list we maintain for reset(). */
897 find_and_forget_pmops(o);
910 /* free child ops before ourself, (then free ourself "on the
913 if (!went_up && o->op_flags & OPf_KIDS) {
914 next_op = cUNOPo->op_first;
918 /* find the next node to visit, *then* free the current node
919 * (can't rely on o->op_* fields being valid after o has been
922 /* The next node to visit will be either the sibling, or the
923 * parent if no siblings left, or NULL if we've worked our way
924 * back up to the top node in the tree */
925 next_op = (o == top_op) ? NULL : o->op_sibparent;
926 went_up = cBOOL(!OpHAS_SIBLING(o)); /* parents are already visited */
928 /* Now process the current node */
930 /* Though ops may be freed twice, freeing the op after its slab is a
932 assert(!o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
933 /* During the forced freeing of ops after compilation failure, kidops
934 may be freed before their parents. */
935 if (!o || o->op_type == OP_FREED)
940 /* an op should only ever acquire op_private flags that we know about.
941 * If this fails, you may need to fix something in regen/op_private.
942 * Don't bother testing if:
943 * * the op_ppaddr doesn't match the op; someone may have
944 * overridden the op and be doing strange things with it;
945 * * we've errored, as op flags are often left in an
946 * inconsistent state then. Note that an error when
947 * compiling the main program leaves PL_parser NULL, so
948 * we can't spot faults in the main code, only
949 * evaled/required code */
951 if ( o->op_ppaddr == PL_ppaddr[type]
953 && !PL_parser->error_count)
955 assert(!(o->op_private & ~PL_op_private_valid[type]));
960 /* Call the op_free hook if it has been set. Do it now so that it's called
961 * at the right time for refcounted ops, but still before all of the kids
966 type = (OPCODE)o->op_targ;
969 Slab_to_rw(OpSLAB(o));
971 /* COP* is not cleared by op_clear() so that we may track line
972 * numbers etc even after null() */
973 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
985 /* S_op_clear_gv(): free a GV attached to an OP */
989 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
991 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
995 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
996 || o->op_type == OP_MULTIDEREF)
999 ? ((GV*)PAD_SVl(*ixp)) : NULL;
1001 ? (GV*)(*svp) : NULL;
1003 /* It's possible during global destruction that the GV is freed
1004 before the optree. Whilst the SvREFCNT_inc is happy to bump from
1005 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
1006 will trigger an assertion failure, because the entry to sv_clear
1007 checks that the scalar is not already freed. A check of for
1008 !SvIS_FREED(gv) turns out to be invalid, because during global
1009 destruction the reference count can be forced down to zero
1010 (with SVf_BREAK set). In which case raising to 1 and then
1011 dropping to 0 triggers cleanup before it should happen. I
1012 *think* that this might actually be a general, systematic,
1013 weakness of the whole idea of SVf_BREAK, in that code *is*
1014 allowed to raise and lower references during global destruction,
1015 so any *valid* code that happens to do this during global
1016 destruction might well trigger premature cleanup. */
1017 bool still_valid = gv && SvREFCNT(gv);
1020 SvREFCNT_inc_simple_void(gv);
1023 pad_swipe(*ixp, TRUE);
1031 int try_downgrade = SvREFCNT(gv) == 2;
1032 SvREFCNT_dec_NN(gv);
1034 gv_try_downgrade(gv);
1040 Perl_op_clear(pTHX_ OP *o)
1044 PERL_ARGS_ASSERT_OP_CLEAR;
1046 switch (o->op_type) {
1047 case OP_NULL: /* Was holding old type, if any. */
1050 case OP_ENTEREVAL: /* Was holding hints. */
1051 case OP_ARGDEFELEM: /* Was holding signature index. */
1055 if (!(o->op_flags & OPf_REF) || !OP_IS_STAT(o->op_type))
1062 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
1064 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
1067 case OP_METHOD_REDIR:
1068 case OP_METHOD_REDIR_SUPER:
1070 if (cMETHOPx(o)->op_rclass_targ) {
1071 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
1072 cMETHOPx(o)->op_rclass_targ = 0;
1075 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
1076 cMETHOPx(o)->op_rclass_sv = NULL;
1079 case OP_METHOD_NAMED:
1080 case OP_METHOD_SUPER:
1081 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
1082 cMETHOPx(o)->op_u.op_meth_sv = NULL;
1085 pad_swipe(o->op_targ, 1);
1092 SvREFCNT_dec(cSVOPo->op_sv);
1093 cSVOPo->op_sv = NULL;
1096 Even if op_clear does a pad_free for the target of the op,
1097 pad_free doesn't actually remove the sv that exists in the pad;
1098 instead it lives on. This results in that it could be reused as
1099 a target later on when the pad was reallocated.
1102 pad_swipe(o->op_targ,1);
1112 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
1117 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
1118 && (o->op_private & OPpTRANS_USE_SVOP))
1121 if (cPADOPo->op_padix > 0) {
1122 pad_swipe(cPADOPo->op_padix, TRUE);
1123 cPADOPo->op_padix = 0;
1126 SvREFCNT_dec(cSVOPo->op_sv);
1127 cSVOPo->op_sv = NULL;
1131 PerlMemShared_free(cPVOPo->op_pv);
1132 cPVOPo->op_pv = NULL;
1136 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1140 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1141 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1143 if (o->op_private & OPpSPLIT_LEX)
1144 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1147 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1149 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1156 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1157 op_free(cPMOPo->op_code_list);
1158 cPMOPo->op_code_list = NULL;
1159 forget_pmop(cPMOPo);
1160 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1161 /* we use the same protection as the "SAFE" version of the PM_ macros
1162 * here since sv_clean_all might release some PMOPs
1163 * after PL_regex_padav has been cleared
1164 * and the clearing of PL_regex_padav needs to
1165 * happen before sv_clean_all
1168 if(PL_regex_pad) { /* We could be in destruction */
1169 const IV offset = (cPMOPo)->op_pmoffset;
1170 ReREFCNT_dec(PM_GETRE(cPMOPo));
1171 PL_regex_pad[offset] = &PL_sv_undef;
1172 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1176 ReREFCNT_dec(PM_GETRE(cPMOPo));
1177 PM_SETRE(cPMOPo, NULL);
1183 PerlMemShared_free(cUNOP_AUXo->op_aux);
1186 case OP_MULTICONCAT:
1188 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux;
1189 /* aux[PERL_MULTICONCAT_IX_PLAIN_PV] and/or
1190 * aux[PERL_MULTICONCAT_IX_UTF8_PV] point to plain and/or
1191 * utf8 shared strings */
1192 char *p1 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
1193 char *p2 = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
1195 PerlMemShared_free(p1);
1197 PerlMemShared_free(p2);
1198 PerlMemShared_free(aux);
1204 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1205 UV actions = items->uv;
1207 bool is_hash = FALSE;
1210 switch (actions & MDEREF_ACTION_MASK) {
1213 actions = (++items)->uv;
1216 case MDEREF_HV_padhv_helem:
1219 case MDEREF_AV_padav_aelem:
1220 pad_free((++items)->pad_offset);
1223 case MDEREF_HV_gvhv_helem:
1226 case MDEREF_AV_gvav_aelem:
1228 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1230 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1234 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1237 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1239 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1241 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1243 goto do_vivify_rv2xv_elem;
1245 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1248 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1249 pad_free((++items)->pad_offset);
1250 goto do_vivify_rv2xv_elem;
1252 case MDEREF_HV_pop_rv2hv_helem:
1253 case MDEREF_HV_vivify_rv2hv_helem:
1256 do_vivify_rv2xv_elem:
1257 case MDEREF_AV_pop_rv2av_aelem:
1258 case MDEREF_AV_vivify_rv2av_aelem:
1260 switch (actions & MDEREF_INDEX_MASK) {
1261 case MDEREF_INDEX_none:
1264 case MDEREF_INDEX_const:
1268 pad_swipe((++items)->pad_offset, 1);
1270 SvREFCNT_dec((++items)->sv);
1276 case MDEREF_INDEX_padsv:
1277 pad_free((++items)->pad_offset);
1279 case MDEREF_INDEX_gvsv:
1281 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1283 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1288 if (actions & MDEREF_FLAG_last)
1301 actions >>= MDEREF_SHIFT;
1304 /* start of malloc is at op_aux[-1], where the length is
1306 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1311 if (o->op_targ > 0) {
1312 pad_free(o->op_targ);
1318 S_cop_free(pTHX_ COP* cop)
1320 PERL_ARGS_ASSERT_COP_FREE;
1323 if (! specialWARN(cop->cop_warnings))
1324 PerlMemShared_free(cop->cop_warnings);
1325 cophh_free(CopHINTHASH_get(cop));
1326 if (PL_curcop == cop)
1331 S_forget_pmop(pTHX_ PMOP *const o)
1333 HV * const pmstash = PmopSTASH(o);
1335 PERL_ARGS_ASSERT_FORGET_PMOP;
1337 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1338 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1340 PMOP **const array = (PMOP**) mg->mg_ptr;
1341 U32 count = mg->mg_len / sizeof(PMOP**);
1345 if (array[i] == o) {
1346 /* Found it. Move the entry at the end to overwrite it. */
1347 array[i] = array[--count];
1348 mg->mg_len = count * sizeof(PMOP**);
1349 /* Could realloc smaller at this point always, but probably
1350 not worth it. Probably worth free()ing if we're the
1353 Safefree(mg->mg_ptr);
1367 S_find_and_forget_pmops(pTHX_ OP *o)
1371 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1374 switch (o->op_type) {
1379 forget_pmop((PMOP*)o);
1382 if (o->op_flags & OPf_KIDS) {
1383 o = cUNOPo->op_first;
1389 return; /* at top; no parents/siblings to try */
1390 if (OpHAS_SIBLING(o)) {
1391 o = o->op_sibparent; /* process next sibling */
1394 o = o->op_sibparent; /*try parent's next sibling */
1403 Neutralizes an op when it is no longer needed, but is still linked to from
1410 Perl_op_null(pTHX_ OP *o)
1413 PERL_ARGS_ASSERT_OP_NULL;
1415 if (o->op_type == OP_NULL)
1418 o->op_targ = o->op_type;
1419 OpTYPE_set(o, OP_NULL);
1423 Perl_op_refcnt_lock(pTHX)
1424 PERL_TSA_ACQUIRE(PL_op_mutex)
1426 PERL_UNUSED_CONTEXT;
1431 Perl_op_refcnt_unlock(pTHX)
1432 PERL_TSA_RELEASE(PL_op_mutex)
1434 PERL_UNUSED_CONTEXT;
1440 =for apidoc op_sibling_splice
1442 A general function for editing the structure of an existing chain of
1443 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1444 you to delete zero or more sequential nodes, replacing them with zero or
1445 more different nodes. Performs the necessary op_first/op_last
1446 housekeeping on the parent node and op_sibling manipulation on the
1447 children. The last deleted node will be marked as the last node by
1448 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1450 Note that op_next is not manipulated, and nodes are not freed; that is the
1451 responsibility of the caller. It also won't create a new list op for an
1452 empty list etc; use higher-level functions like op_append_elem() for that.
1454 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1455 the splicing doesn't affect the first or last op in the chain.
1457 C<start> is the node preceding the first node to be spliced. Node(s)
1458 following it will be deleted, and ops will be inserted after it. If it is
1459 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1462 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1463 If -1 or greater than or equal to the number of remaining kids, all
1464 remaining kids are deleted.
1466 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1467 If C<NULL>, no nodes are inserted.
1469 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1474 action before after returns
1475 ------ ----- ----- -------
1478 splice(P, A, 2, X-Y-Z) | | B-C
1482 splice(P, NULL, 1, X-Y) | | A
1486 splice(P, NULL, 3, NULL) | | A-B-C
1490 splice(P, B, 0, X-Y) | | NULL
1494 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1495 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1501 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1505 OP *last_del = NULL;
1506 OP *last_ins = NULL;
1509 first = OpSIBLING(start);
1513 first = cLISTOPx(parent)->op_first;
1515 assert(del_count >= -1);
1517 if (del_count && first) {
1519 while (--del_count && OpHAS_SIBLING(last_del))
1520 last_del = OpSIBLING(last_del);
1521 rest = OpSIBLING(last_del);
1522 OpLASTSIB_set(last_del, NULL);
1529 while (OpHAS_SIBLING(last_ins))
1530 last_ins = OpSIBLING(last_ins);
1531 OpMAYBESIB_set(last_ins, rest, NULL);
1537 OpMAYBESIB_set(start, insert, NULL);
1541 cLISTOPx(parent)->op_first = insert;
1543 parent->op_flags |= OPf_KIDS;
1545 parent->op_flags &= ~OPf_KIDS;
1549 /* update op_last etc */
1556 /* ought to use OP_CLASS(parent) here, but that can't handle
1557 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1559 type = parent->op_type;
1560 if (type == OP_CUSTOM) {
1562 type = XopENTRYCUSTOM(parent, xop_class);
1565 if (type == OP_NULL)
1566 type = parent->op_targ;
1567 type = PL_opargs[type] & OA_CLASS_MASK;
1570 lastop = last_ins ? last_ins : start ? start : NULL;
1571 if ( type == OA_BINOP
1572 || type == OA_LISTOP
1576 cLISTOPx(parent)->op_last = lastop;
1579 OpLASTSIB_set(lastop, parent);
1581 return last_del ? first : NULL;
1584 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1588 =for apidoc op_parent
1590 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1596 Perl_op_parent(OP *o)
1598 PERL_ARGS_ASSERT_OP_PARENT;
1599 while (OpHAS_SIBLING(o))
1601 return o->op_sibparent;
1604 /* replace the sibling following start with a new UNOP, which becomes
1605 * the parent of the original sibling; e.g.
1607 * op_sibling_newUNOP(P, A, unop-args...)
1615 * where U is the new UNOP.
1617 * parent and start args are the same as for op_sibling_splice();
1618 * type and flags args are as newUNOP().
1620 * Returns the new UNOP.
1624 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1628 kid = op_sibling_splice(parent, start, 1, NULL);
1629 newop = newUNOP(type, flags, kid);
1630 op_sibling_splice(parent, start, 0, newop);
1635 /* lowest-level newLOGOP-style function - just allocates and populates
1636 * the struct. Higher-level stuff should be done by S_new_logop() /
1637 * newLOGOP(). This function exists mainly to avoid op_first assignment
1638 * being spread throughout this file.
1642 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1646 NewOp(1101, logop, 1, LOGOP);
1647 OpTYPE_set(logop, type);
1648 logop->op_first = first;
1649 logop->op_other = other;
1651 logop->op_flags = OPf_KIDS;
1652 while (kid && OpHAS_SIBLING(kid))
1653 kid = OpSIBLING(kid);
1655 OpLASTSIB_set(kid, (OP*)logop);
1660 /* Contextualizers */
1663 =for apidoc op_contextualize
1665 Applies a syntactic context to an op tree representing an expression.
1666 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1667 or C<G_VOID> to specify the context to apply. The modified op tree
1674 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1676 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1678 case G_SCALAR: return scalar(o);
1679 case G_ARRAY: return list(o);
1680 case G_VOID: return scalarvoid(o);
1682 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1689 =for apidoc op_linklist
1690 This function is the implementation of the L</LINKLIST> macro. It should
1691 not be called directly.
1698 Perl_op_linklist(pTHX_ OP *o)
1705 PERL_ARGS_ASSERT_OP_LINKLIST;
1708 /* Descend down the tree looking for any unprocessed subtrees to
1711 if (o->op_flags & OPf_KIDS) {
1712 o = cUNOPo->op_first;
1715 o->op_next = o; /* leaf node; link to self initially */
1718 /* if we're at the top level, there either weren't any children
1719 * to process, or we've worked our way back to the top. */
1723 /* o is now processed. Next, process any sibling subtrees */
1725 if (OpHAS_SIBLING(o)) {
1730 /* Done all the subtrees at this level. Go back up a level and
1731 * link the parent in with all its (processed) children.
1734 o = o->op_sibparent;
1735 assert(!o->op_next);
1736 prevp = &(o->op_next);
1737 kid = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1739 *prevp = kid->op_next;
1740 prevp = &(kid->op_next);
1741 kid = OpSIBLING(kid);
1749 S_scalarkids(pTHX_ OP *o)
1751 if (o && o->op_flags & OPf_KIDS) {
1753 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1760 S_scalarboolean(pTHX_ OP *o)
1762 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1764 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1765 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1766 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1767 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1768 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1769 if (ckWARN(WARN_SYNTAX)) {
1770 const line_t oldline = CopLINE(PL_curcop);
1772 if (PL_parser && PL_parser->copline != NOLINE) {
1773 /* This ensures that warnings are reported at the first line
1774 of the conditional, not the last. */
1775 CopLINE_set(PL_curcop, PL_parser->copline);
1777 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1778 CopLINE_set(PL_curcop, oldline);
1785 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1788 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1789 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1791 const char funny = o->op_type == OP_PADAV
1792 || o->op_type == OP_RV2AV ? '@' : '%';
1793 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1795 if (cUNOPo->op_first->op_type != OP_GV
1796 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1798 return varname(gv, funny, 0, NULL, 0, subscript_type);
1801 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1806 S_op_varname(pTHX_ const OP *o)
1808 return S_op_varname_subscript(aTHX_ o, 1);
1812 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1813 { /* or not so pretty :-) */
1814 if (o->op_type == OP_CONST) {
1816 if (SvPOK(*retsv)) {
1818 *retsv = sv_newmortal();
1819 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1820 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1822 else if (!SvOK(*retsv))
1825 else *retpv = "...";
1829 S_scalar_slice_warning(pTHX_ const OP *o)
1832 const bool h = o->op_type == OP_HSLICE
1833 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
1839 SV *keysv = NULL; /* just to silence compiler warnings */
1840 const char *key = NULL;
1842 if (!(o->op_private & OPpSLICEWARNING))
1844 if (PL_parser && PL_parser->error_count)
1845 /* This warning can be nonsensical when there is a syntax error. */
1848 kid = cLISTOPo->op_first;
1849 kid = OpSIBLING(kid); /* get past pushmark */
1850 /* weed out false positives: any ops that can return lists */
1851 switch (kid->op_type) {
1877 /* Don't warn if we have a nulled list either. */
1878 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1881 assert(OpSIBLING(kid));
1882 name = S_op_varname(aTHX_ OpSIBLING(kid));
1883 if (!name) /* XS module fiddling with the op tree */
1885 S_op_pretty(aTHX_ kid, &keysv, &key);
1886 assert(SvPOK(name));
1887 sv_chop(name,SvPVX(name)+1);
1889 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1890 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1891 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1893 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1894 lbrack, key, rbrack);
1896 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1897 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1898 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1900 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1901 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1906 /* apply scalar context to the o subtree */
1909 Perl_scalar(pTHX_ OP *o)
1914 OP *next_kid = NULL; /* what op (if any) to process next */
1917 /* assumes no premature commitment */
1918 if (!o || (PL_parser && PL_parser->error_count)
1919 || (o->op_flags & OPf_WANT)
1920 || o->op_type == OP_RETURN)
1925 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1927 switch (o->op_type) {
1929 scalar(cBINOPo->op_first);
1930 /* convert what initially looked like a list repeat into a
1931 * scalar repeat, e.g. $s = (1) x $n
1933 if (o->op_private & OPpREPEAT_DOLIST) {
1934 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1935 assert(kid->op_type == OP_PUSHMARK);
1936 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1937 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1938 o->op_private &=~ OPpREPEAT_DOLIST;
1946 /* impose scalar context on everything except the condition */
1947 next_kid = OpSIBLING(cUNOPo->op_first);
1951 if (o->op_flags & OPf_KIDS)
1952 next_kid = cUNOPo->op_first; /* do all kids */
1955 /* the children of these ops are usually a list of statements,
1956 * except the leaves, whose first child is a corresponding enter
1961 kid = cLISTOPo->op_first;
1965 kid = cLISTOPo->op_first;
1967 kid = OpSIBLING(kid);
1970 OP *sib = OpSIBLING(kid);
1971 /* Apply void context to all kids except the last, which
1972 * is scalar (ignoring a trailing ex-nextstate in determining
1973 * if it's the last kid). E.g.
1974 * $scalar = do { void; void; scalar }
1975 * Except that 'when's are always scalar, e.g.
1976 * $scalar = do { given(..) {
1977 * when (..) { scalar }
1978 * when (..) { scalar }
1983 || ( !OpHAS_SIBLING(sib)
1984 && sib->op_type == OP_NULL
1985 && ( sib->op_targ == OP_NEXTSTATE
1986 || sib->op_targ == OP_DBSTATE )
1990 /* tail call optimise calling scalar() on the last kid */
1994 else if (kid->op_type == OP_LEAVEWHEN)
2000 NOT_REACHED; /* NOTREACHED */
2004 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
2010 /* Warn about scalar context */
2011 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
2012 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
2015 const char *key = NULL;
2017 /* This warning can be nonsensical when there is a syntax error. */
2018 if (PL_parser && PL_parser->error_count)
2021 if (!ckWARN(WARN_SYNTAX)) break;
2023 kid = cLISTOPo->op_first;
2024 kid = OpSIBLING(kid); /* get past pushmark */
2025 assert(OpSIBLING(kid));
2026 name = S_op_varname(aTHX_ OpSIBLING(kid));
2027 if (!name) /* XS module fiddling with the op tree */
2029 S_op_pretty(aTHX_ kid, &keysv, &key);
2030 assert(SvPOK(name));
2031 sv_chop(name,SvPVX(name)+1);
2033 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
2034 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2035 "%%%" SVf "%c%s%c in scalar context better written "
2036 "as $%" SVf "%c%s%c",
2037 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
2038 lbrack, key, rbrack);
2040 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
2041 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2042 "%%%" SVf "%c%" SVf "%c in scalar context better "
2043 "written as $%" SVf "%c%" SVf "%c",
2044 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
2045 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
2049 /* If next_kid is set, someone in the code above wanted us to process
2050 * that kid and all its remaining siblings. Otherwise, work our way
2051 * back up the tree */
2055 return top_op; /* at top; no parents/siblings to try */
2056 if (OpHAS_SIBLING(o))
2057 next_kid = o->op_sibparent;
2059 o = o->op_sibparent; /*try parent's next sibling */
2060 switch (o->op_type) {
2066 /* should really restore PL_curcop to its old value, but
2067 * setting it to PL_compiling is better than do nothing */
2068 PL_curcop = &PL_compiling;
2077 /* apply void context to the optree arg */
2080 Perl_scalarvoid(pTHX_ OP *arg)
2086 PERL_ARGS_ASSERT_SCALARVOID;
2090 SV *useless_sv = NULL;
2091 const char* useless = NULL;
2092 OP * next_kid = NULL;
2094 if (o->op_type == OP_NEXTSTATE
2095 || o->op_type == OP_DBSTATE
2096 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
2097 || o->op_targ == OP_DBSTATE)))
2098 PL_curcop = (COP*)o; /* for warning below */
2100 /* assumes no premature commitment */
2101 want = o->op_flags & OPf_WANT;
2102 if ((want && want != OPf_WANT_SCALAR)
2103 || (PL_parser && PL_parser->error_count)
2104 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
2109 if ((o->op_private & OPpTARGET_MY)
2110 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2112 /* newASSIGNOP has already applied scalar context, which we
2113 leave, as if this op is inside SASSIGN. */
2117 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
2119 switch (o->op_type) {
2121 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
2125 if (o->op_flags & OPf_STACKED)
2127 if (o->op_type == OP_REPEAT)
2128 scalar(cBINOPo->op_first);
2131 if ((o->op_flags & OPf_STACKED) &&
2132 !(o->op_private & OPpCONCAT_NESTED))
2136 if (o->op_private == 4)
2171 case OP_GETSOCKNAME:
2172 case OP_GETPEERNAME:
2177 case OP_GETPRIORITY:
2202 useless = OP_DESC(o);
2212 case OP_AELEMFAST_LEX:
2216 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
2217 /* Otherwise it's "Useless use of grep iterator" */
2218 useless = OP_DESC(o);
2222 if (!(o->op_private & OPpSPLIT_ASSIGN))
2223 useless = OP_DESC(o);
2227 kid = cUNOPo->op_first;
2228 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2229 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2232 useless = "negative pattern binding (!~)";
2236 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2237 useless = "non-destructive substitution (s///r)";
2241 useless = "non-destructive transliteration (tr///r)";
2248 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2249 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2250 useless = "a variable";
2255 if (cSVOPo->op_private & OPpCONST_STRICT)
2256 no_bareword_allowed(o);
2258 if (ckWARN(WARN_VOID)) {
2260 /* don't warn on optimised away booleans, eg
2261 * use constant Foo, 5; Foo || print; */
2262 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2264 /* the constants 0 and 1 are permitted as they are
2265 conventionally used as dummies in constructs like
2266 1 while some_condition_with_side_effects; */
2267 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2269 else if (SvPOK(sv)) {
2270 SV * const dsv = newSVpvs("");
2272 = Perl_newSVpvf(aTHX_
2274 pv_pretty(dsv, SvPVX_const(sv),
2275 SvCUR(sv), 32, NULL, NULL,
2277 | PERL_PV_ESCAPE_NOCLEAR
2278 | PERL_PV_ESCAPE_UNI_DETECT));
2279 SvREFCNT_dec_NN(dsv);
2281 else if (SvOK(sv)) {
2282 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2285 useless = "a constant (undef)";
2288 op_null(o); /* don't execute or even remember it */
2292 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2296 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2300 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2304 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2309 UNOP *refgen, *rv2cv;
2312 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2315 rv2gv = ((BINOP *)o)->op_last;
2316 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2319 refgen = (UNOP *)((BINOP *)o)->op_first;
2321 if (!refgen || (refgen->op_type != OP_REFGEN
2322 && refgen->op_type != OP_SREFGEN))
2325 exlist = (LISTOP *)refgen->op_first;
2326 if (!exlist || exlist->op_type != OP_NULL
2327 || exlist->op_targ != OP_LIST)
2330 if (exlist->op_first->op_type != OP_PUSHMARK
2331 && exlist->op_first != exlist->op_last)
2334 rv2cv = (UNOP*)exlist->op_last;
2336 if (rv2cv->op_type != OP_RV2CV)
2339 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2340 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2341 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2343 o->op_private |= OPpASSIGN_CV_TO_GV;
2344 rv2gv->op_private |= OPpDONT_INIT_GV;
2345 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2357 kid = cLOGOPo->op_first;
2358 if (kid->op_type == OP_NOT
2359 && (kid->op_flags & OPf_KIDS)) {
2360 if (o->op_type == OP_AND) {
2361 OpTYPE_set(o, OP_OR);
2363 OpTYPE_set(o, OP_AND);
2373 next_kid = OpSIBLING(cUNOPo->op_first);
2377 if (o->op_flags & OPf_STACKED)
2384 if (!(o->op_flags & OPf_KIDS))
2395 next_kid = cLISTOPo->op_first;
2398 /* If the first kid after pushmark is something that the padrange
2399 optimisation would reject, then null the list and the pushmark.
2401 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2402 && ( !(kid = OpSIBLING(kid))
2403 || ( kid->op_type != OP_PADSV
2404 && kid->op_type != OP_PADAV
2405 && kid->op_type != OP_PADHV)
2406 || kid->op_private & ~OPpLVAL_INTRO
2407 || !(kid = OpSIBLING(kid))
2408 || ( kid->op_type != OP_PADSV
2409 && kid->op_type != OP_PADAV
2410 && kid->op_type != OP_PADHV)
2411 || kid->op_private & ~OPpLVAL_INTRO)
2413 op_null(cUNOPo->op_first); /* NULL the pushmark */
2414 op_null(o); /* NULL the list */
2426 /* mortalise it, in case warnings are fatal. */
2427 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2428 "Useless use of %" SVf " in void context",
2429 SVfARG(sv_2mortal(useless_sv)));
2432 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2433 "Useless use of %s in void context",
2438 /* if a kid hasn't been nominated to process, continue with the
2439 * next sibling, or if no siblings left, go back to the parent's
2440 * siblings and so on
2444 return arg; /* at top; no parents/siblings to try */
2445 if (OpHAS_SIBLING(o))
2446 next_kid = o->op_sibparent;
2448 o = o->op_sibparent; /*try parent's next sibling */
2458 S_listkids(pTHX_ OP *o)
2460 if (o && o->op_flags & OPf_KIDS) {
2462 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2469 /* apply list context to the o subtree */
2472 Perl_list(pTHX_ OP *o)
2477 OP *next_kid = NULL; /* what op (if any) to process next */
2481 /* assumes no premature commitment */
2482 if (!o || (o->op_flags & OPf_WANT)
2483 || (PL_parser && PL_parser->error_count)
2484 || o->op_type == OP_RETURN)
2489 if ((o->op_private & OPpTARGET_MY)
2490 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2492 goto do_next; /* As if inside SASSIGN */
2495 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2497 switch (o->op_type) {
2499 if (o->op_private & OPpREPEAT_DOLIST
2500 && !(o->op_flags & OPf_STACKED))
2502 list(cBINOPo->op_first);
2503 kid = cBINOPo->op_last;
2504 /* optimise away (.....) x 1 */
2505 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2506 && SvIVX(kSVOP_sv) == 1)
2508 op_null(o); /* repeat */
2509 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2511 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2519 /* impose list context on everything except the condition */
2520 next_kid = OpSIBLING(cUNOPo->op_first);
2524 if (!(o->op_flags & OPf_KIDS))
2526 /* possibly flatten 1..10 into a constant array */
2527 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2528 list(cBINOPo->op_first);
2529 gen_constant_list(o);
2532 next_kid = cUNOPo->op_first; /* do all kids */
2536 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2537 op_null(cUNOPo->op_first); /* NULL the pushmark */
2538 op_null(o); /* NULL the list */
2540 if (o->op_flags & OPf_KIDS)
2541 next_kid = cUNOPo->op_first; /* do all kids */
2544 /* the children of these ops are usually a list of statements,
2545 * except the leaves, whose first child is a corresponding enter
2549 kid = cLISTOPo->op_first;
2553 kid = cLISTOPo->op_first;
2555 kid = OpSIBLING(kid);
2558 OP *sib = OpSIBLING(kid);
2559 /* Apply void context to all kids except the last, which
2561 * @a = do { void; void; list }
2562 * Except that 'when's are always list context, e.g.
2563 * @a = do { given(..) {
2564 * when (..) { list }
2565 * when (..) { list }
2570 /* tail call optimise calling list() on the last kid */
2574 else if (kid->op_type == OP_LEAVEWHEN)
2580 NOT_REACHED; /* NOTREACHED */
2585 /* If next_kid is set, someone in the code above wanted us to process
2586 * that kid and all its remaining siblings. Otherwise, work our way
2587 * back up the tree */
2591 return top_op; /* at top; no parents/siblings to try */
2592 if (OpHAS_SIBLING(o))
2593 next_kid = o->op_sibparent;
2595 o = o->op_sibparent; /*try parent's next sibling */
2596 switch (o->op_type) {
2602 /* should really restore PL_curcop to its old value, but
2603 * setting it to PL_compiling is better than do nothing */
2604 PL_curcop = &PL_compiling;
2616 S_scalarseq(pTHX_ OP *o)
2619 const OPCODE type = o->op_type;
2621 if (type == OP_LINESEQ || type == OP_SCOPE ||
2622 type == OP_LEAVE || type == OP_LEAVETRY)
2625 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2626 if ((sib = OpSIBLING(kid))
2627 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2628 || ( sib->op_targ != OP_NEXTSTATE
2629 && sib->op_targ != OP_DBSTATE )))
2634 PL_curcop = &PL_compiling;
2636 o->op_flags &= ~OPf_PARENS;
2637 if (PL_hints & HINT_BLOCK_SCOPE)
2638 o->op_flags |= OPf_PARENS;
2641 o = newOP(OP_STUB, 0);
2646 S_modkids(pTHX_ OP *o, I32 type)
2648 if (o && o->op_flags & OPf_KIDS) {
2650 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2651 op_lvalue(kid, type);
2657 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2658 * const fields. Also, convert CONST keys to HEK-in-SVs.
2659 * rop is the op that retrieves the hash;
2660 * key_op is the first key
2661 * real if false, only check (and possibly croak); don't update op
2665 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real)
2671 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2673 if (rop->op_first->op_type == OP_PADSV)
2674 /* @$hash{qw(keys here)} */
2675 rop = (UNOP*)rop->op_first;
2677 /* @{$hash}{qw(keys here)} */
2678 if (rop->op_first->op_type == OP_SCOPE
2679 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2681 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2688 lexname = NULL; /* just to silence compiler warnings */
2689 fields = NULL; /* just to silence compiler warnings */
2693 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2694 SvPAD_TYPED(lexname))
2695 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2696 && isGV(*fields) && GvHV(*fields);
2698 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2700 if (key_op->op_type != OP_CONST)
2702 svp = cSVOPx_svp(key_op);
2704 /* make sure it's not a bareword under strict subs */
2705 if (key_op->op_private & OPpCONST_BARE &&
2706 key_op->op_private & OPpCONST_STRICT)
2708 no_bareword_allowed((OP*)key_op);
2711 /* Make the CONST have a shared SV */
2712 if ( !SvIsCOW_shared_hash(sv = *svp)
2713 && SvTYPE(sv) < SVt_PVMG
2719 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2720 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2721 SvREFCNT_dec_NN(sv);
2726 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2728 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2729 "in variable %" PNf " of type %" HEKf,
2730 SVfARG(*svp), PNfARG(lexname),
2731 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2736 /* info returned by S_sprintf_is_multiconcatable() */
2738 struct sprintf_ismc_info {
2739 SSize_t nargs; /* num of args to sprintf (not including the format) */
2740 char *start; /* start of raw format string */
2741 char *end; /* bytes after end of raw format string */
2742 STRLEN total_len; /* total length (in bytes) of format string, not
2743 including '%s' and half of '%%' */
2744 STRLEN variant; /* number of bytes by which total_len_p would grow
2745 if upgraded to utf8 */
2746 bool utf8; /* whether the format is utf8 */
2750 /* is the OP_SPRINTF o suitable for converting into a multiconcat op?
2751 * i.e. its format argument is a const string with only '%s' and '%%'
2752 * formats, and the number of args is known, e.g.
2753 * sprintf "a=%s f=%s", $a[0], scalar(f());
2755 * sprintf "i=%d a=%s f=%s", $i, @a, f();
2757 * If successful, the sprintf_ismc_info struct pointed to by info will be
2762 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info)
2764 OP *pm, *constop, *kid;
2767 SSize_t nargs, nformats;
2768 STRLEN cur, total_len, variant;
2771 /* if sprintf's behaviour changes, die here so that someone
2772 * can decide whether to enhance this function or skip optimising
2773 * under those new circumstances */
2774 assert(!(o->op_flags & OPf_STACKED));
2775 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX));
2776 assert(!(o->op_private & ~OPpARG4_MASK));
2778 pm = cUNOPo->op_first;
2779 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */
2781 constop = OpSIBLING(pm);
2782 if (!constop || constop->op_type != OP_CONST)
2784 sv = cSVOPx_sv(constop);
2785 if (SvMAGICAL(sv) || !SvPOK(sv))
2791 /* Scan format for %% and %s and work out how many %s there are.
2792 * Abandon if other format types are found.
2799 for (p = s; p < e; p++) {
2802 if (!UTF8_IS_INVARIANT(*p))
2808 return FALSE; /* lone % at end gives "Invalid conversion" */
2817 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG)
2820 utf8 = cBOOL(SvUTF8(sv));
2824 /* scan args; they must all be in scalar cxt */
2827 kid = OpSIBLING(constop);
2830 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
2833 kid = OpSIBLING(kid);
2836 if (nargs != nformats)
2837 return FALSE; /* e.g. sprintf("%s%s", $a); */
2840 info->nargs = nargs;
2843 info->total_len = total_len;
2844 info->variant = variant;
2852 /* S_maybe_multiconcat():
2854 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly
2855 * convert it (and its children) into an OP_MULTICONCAT. See the code
2856 * comments just before pp_multiconcat() for the full details of what
2857 * OP_MULTICONCAT supports.
2859 * Basically we're looking for an optree with a chain of OP_CONCATS down
2860 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or
2861 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g.
2869 * STRINGIFY -- PADSV[$x]
2872 * ex-PUSHMARK -- CONCAT/S
2874 * CONCAT/S -- PADSV[$d]
2876 * CONCAT -- CONST["-"]
2878 * PADSV[$a] -- PADSV[$b]
2880 * Note that at this stage the OP_SASSIGN may have already been optimised
2881 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT.
2885 S_maybe_multiconcat(pTHX_ OP *o)
2887 OP *lastkidop; /* the right-most of any kids unshifted onto o */
2888 OP *topop; /* the top-most op in the concat tree (often equals o,
2889 unless there are assign/stringify ops above it */
2890 OP *parentop; /* the parent op of topop (or itself if no parent) */
2891 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */
2892 OP *targetop; /* the op corresponding to target=... or target.=... */
2893 OP *stringop; /* the OP_STRINGIFY op, if any */
2894 OP *nextop; /* used for recreating the op_next chain without consts */
2895 OP *kid; /* general-purpose op pointer */
2897 UNOP_AUX_item *lenp;
2898 char *const_str, *p;
2899 struct sprintf_ismc_info sprintf_info;
2901 /* store info about each arg in args[];
2902 * toparg is the highest used slot; argp is a general
2903 * pointer to args[] slots */
2905 void *p; /* initially points to const sv (or null for op);
2906 later, set to SvPV(constsv), with ... */
2907 STRLEN len; /* ... len set to SvPV(..., len) */
2908 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1];
2912 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */
2915 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op;
2916 the last-processed arg will the LHS of one,
2917 as args are processed in reverse order */
2918 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */
2919 STRLEN total_len = 0; /* sum of the lengths of the const segments */
2920 U8 flags = 0; /* what will become the op_flags and ... */
2921 U8 private_flags = 0; /* ... op_private of the multiconcat op */
2922 bool is_sprintf = FALSE; /* we're optimising an sprintf */
2923 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */
2924 bool prev_was_const = FALSE; /* previous arg was a const */
2926 /* -----------------------------------------------------------------
2929 * Examine the optree non-destructively to determine whether it's
2930 * suitable to be converted into an OP_MULTICONCAT. Accumulate
2931 * information about the optree in args[].
2941 assert( o->op_type == OP_SASSIGN
2942 || o->op_type == OP_CONCAT
2943 || o->op_type == OP_SPRINTF
2944 || o->op_type == OP_STRINGIFY);
2946 Zero(&sprintf_info, 1, struct sprintf_ismc_info);
2948 /* first see if, at the top of the tree, there is an assign,
2949 * append and/or stringify */
2951 if (topop->op_type == OP_SASSIGN) {
2953 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN])
2955 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
2957 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */
2960 topop = cBINOPo->op_first;
2961 targetop = OpSIBLING(topop);
2962 if (!targetop) /* probably some sort of syntax error */
2965 /* don't optimise away assign in 'local $foo = ....' */
2966 if ( (targetop->op_private & OPpLVAL_INTRO)
2967 /* these are the common ops which do 'local', but
2969 && ( targetop->op_type == OP_GVSV
2970 || targetop->op_type == OP_RV2SV
2971 || targetop->op_type == OP_AELEM
2972 || targetop->op_type == OP_HELEM
2977 else if ( topop->op_type == OP_CONCAT
2978 && (topop->op_flags & OPf_STACKED)
2979 && (!(topop->op_private & OPpCONCAT_NESTED))
2984 /* OPpTARGET_MY shouldn't be able to be set here. If it is,
2985 * decide what to do about it */
2986 assert(!(o->op_private & OPpTARGET_MY));
2988 /* barf on unknown flags */
2989 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY)));
2990 private_flags |= OPpMULTICONCAT_APPEND;
2991 targetop = cBINOPo->op_first;
2993 topop = OpSIBLING(targetop);
2995 /* $x .= <FOO> gets optimised to rcatline instead */
2996 if (topop->op_type == OP_READLINE)
3001 /* Can targetop (the LHS) if it's a padsv, be optimised
3002 * away and use OPpTARGET_MY instead?
3004 if ( (targetop->op_type == OP_PADSV)
3005 && !(targetop->op_private & OPpDEREF)
3006 && !(targetop->op_private & OPpPAD_STATE)
3007 /* we don't support 'my $x .= ...' */
3008 && ( o->op_type == OP_SASSIGN
3009 || !(targetop->op_private & OPpLVAL_INTRO))
3014 if (topop->op_type == OP_STRINGIFY) {
3015 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY])
3019 /* barf on unknown flags */
3020 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY)));
3022 if ((topop->op_private & OPpTARGET_MY)) {
3023 if (o->op_type == OP_SASSIGN)
3024 return; /* can't have two assigns */
3028 private_flags |= OPpMULTICONCAT_STRINGIFY;
3030 topop = cBINOPx(topop)->op_first;
3031 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK));
3032 topop = OpSIBLING(topop);
3035 if (topop->op_type == OP_SPRINTF) {
3036 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF])
3038 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) {
3039 nargs = sprintf_info.nargs;
3040 total_len = sprintf_info.total_len;
3041 variant = sprintf_info.variant;
3042 utf8 = sprintf_info.utf8;
3044 private_flags |= OPpMULTICONCAT_FAKE;
3046 /* we have an sprintf op rather than a concat optree.
3047 * Skip most of the code below which is associated with
3048 * processing that optree. We also skip phase 2, determining
3049 * whether its cost effective to optimise, since for sprintf,
3050 * multiconcat is *always* faster */
3053 /* note that even if the sprintf itself isn't multiconcatable,
3054 * the expression as a whole may be, e.g. in
3055 * $x .= sprintf("%d",...)
3056 * the sprintf op will be left as-is, but the concat/S op may
3057 * be upgraded to multiconcat
3060 else if (topop->op_type == OP_CONCAT) {
3061 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT])
3064 if ((topop->op_private & OPpTARGET_MY)) {
3065 if (o->op_type == OP_SASSIGN || targmyop)
3066 return; /* can't have two assigns */
3071 /* Is it safe to convert a sassign/stringify/concat op into
3073 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP);
3074 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP);
3075 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP);
3076 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP);
3077 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last)
3078 == STRUCT_OFFSET(UNOP_AUX, op_aux));
3079 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last)
3080 == STRUCT_OFFSET(UNOP_AUX, op_aux));
3082 /* Now scan the down the tree looking for a series of
3083 * CONCAT/OPf_STACKED ops on the LHS (with the last one not
3084 * stacked). For example this tree:
3089 * CONCAT/STACKED -- EXPR5
3091 * CONCAT/STACKED -- EXPR4
3097 * corresponds to an expression like
3099 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5)
3101 * Record info about each EXPR in args[]: in particular, whether it is
3102 * a stringifiable OP_CONST and if so what the const sv is.
3104 * The reason why the last concat can't be STACKED is the difference
3107 * ((($a .= $a) .= $a) .= $a) .= $a
3110 * $a . $a . $a . $a . $a
3112 * The main difference between the optrees for those two constructs
3113 * is the presence of the last STACKED. As well as modifying $a,
3114 * the former sees the changed $a between each concat, so if $s is
3115 * initially 'a', the first returns 'a' x 16, while the latter returns
3116 * 'a' x 5. And pp_multiconcat can't handle that kind of thing.
3126 if ( kid->op_type == OP_CONCAT
3130 k1 = cUNOPx(kid)->op_first;
3132 /* shouldn't happen except maybe after compile err? */
3136 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */
3137 if (kid->op_private & OPpTARGET_MY)
3140 stacked_last = (kid->op_flags & OPf_STACKED);
3152 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2
3153 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2)
3155 /* At least two spare slots are needed to decompose both
3156 * concat args. If there are no slots left, continue to
3157 * examine the rest of the optree, but don't push new values
3158 * on args[]. If the optree as a whole is legal for conversion
3159 * (in particular that the last concat isn't STACKED), then
3160 * the first PERL_MULTICONCAT_MAXARG elements of the optree
3161 * can be converted into an OP_MULTICONCAT now, with the first
3162 * child of that op being the remainder of the optree -
3163 * which may itself later be converted to a multiconcat op
3167 /* the last arg is the rest of the optree */
3172 else if ( argop->op_type == OP_CONST
3173 && ((sv = cSVOPx_sv(argop)))
3174 /* defer stringification until runtime of 'constant'
3175 * things that might stringify variantly, e.g. the radix
3176 * point of NVs, or overloaded RVs */
3177 && (SvPOK(sv) || SvIOK(sv))
3178 && (!SvGMAGICAL(sv))
3180 if (argop->op_private & OPpCONST_STRICT)
3181 no_bareword_allowed(argop);
3183 utf8 |= cBOOL(SvUTF8(sv));
3186 /* this const may be demoted back to a plain arg later;
3187 * make sure we have enough arg slots left */
3189 prev_was_const = !prev_was_const;
3194 prev_was_const = FALSE;
3204 return; /* we don't support ((A.=B).=C)...) */
3206 /* look for two adjacent consts and don't fold them together:
3209 * $o->concat("a")->concat("b")
3212 * (but $o .= "a" . "b" should still fold)
3215 bool seen_nonconst = FALSE;
3216 for (argp = toparg; argp >= args; argp--) {
3217 if (argp->p == NULL) {
3218 seen_nonconst = TRUE;
3224 /* both previous and current arg were constants;
3225 * leave the current OP_CONST as-is */
3233 /* -----------------------------------------------------------------
3236 * At this point we have determined that the optree *can* be converted
3237 * into a multiconcat. Having gathered all the evidence, we now decide
3238 * whether it *should*.
3242 /* we need at least one concat action, e.g.:
3248 * otherwise we could be doing something like $x = "foo", which
3249 * if treated as a concat, would fail to COW.
3251 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2)
3254 /* Benchmarking seems to indicate that we gain if:
3255 * * we optimise at least two actions into a single multiconcat
3256 * (e.g concat+concat, sassign+concat);
3257 * * or if we can eliminate at least 1 OP_CONST;
3258 * * or if we can eliminate a padsv via OPpTARGET_MY
3262 /* eliminated at least one OP_CONST */
3264 /* eliminated an OP_SASSIGN */
3265 || o->op_type == OP_SASSIGN
3266 /* eliminated an OP_PADSV */
3267 || (!targmyop && is_targable)
3269 /* definitely a net gain to optimise */
3272 /* ... if not, what else? */
3274 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1):
3275 * multiconcat is faster (due to not creating a temporary copy of
3276 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is
3282 && topop->op_type == OP_CONCAT
3284 PADOFFSET t = targmyop->op_targ;
3285 OP *k1 = cBINOPx(topop)->op_first;
3286 OP *k2 = cBINOPx(topop)->op_last;
3287 if ( k2->op_type == OP_PADSV
3289 && ( k1->op_type != OP_PADSV
3290 || k1->op_targ != t)
3295 /* need at least two concats */
3296 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3)
3301 /* -----------------------------------------------------------------
3304 * At this point the optree has been verified as ok to be optimised
3305 * into an OP_MULTICONCAT. Now start changing things.
3310 /* stringify all const args and determine utf8ness */
3313 for (argp = args; argp <= toparg; argp++) {
3314 SV *sv = (SV*)argp->p;
3316 continue; /* not a const op */
3317 if (utf8 && !SvUTF8(sv))
3318 sv_utf8_upgrade_nomg(sv);
3319 argp->p = SvPV_nomg(sv, argp->len);
3320 total_len += argp->len;
3322 /* see if any strings would grow if converted to utf8 */
3324 variant += variant_under_utf8_count((U8 *) argp->p,
3325 (U8 *) argp->p + argp->len);
3329 /* create and populate aux struct */
3333 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
3334 sizeof(UNOP_AUX_item)
3336 PERL_MULTICONCAT_HEADER_SIZE
3337 + ((nargs + 1) * (variant ? 2 : 1))
3340 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1);
3342 /* Extract all the non-const expressions from the concat tree then
3343 * dispose of the old tree, e.g. convert the tree from this:
3347 * STRINGIFY -- TARGET
3349 * ex-PUSHMARK -- CONCAT
3364 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET
3366 * except that if EXPRi is an OP_CONST, it's discarded.
3368 * During the conversion process, EXPR ops are stripped from the tree
3369 * and unshifted onto o. Finally, any of o's remaining original
3370 * childen are discarded and o is converted into an OP_MULTICONCAT.
3372 * In this middle of this, o may contain both: unshifted args on the
3373 * left, and some remaining original args on the right. lastkidop
3374 * is set to point to the right-most unshifted arg to delineate
3375 * between the two sets.
3380 /* create a copy of the format with the %'s removed, and record
3381 * the sizes of the const string segments in the aux struct */
3383 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3385 p = sprintf_info.start;
3388 for (; p < sprintf_info.end; p++) {
3392 (lenp++)->ssize = q - oldq;
3399 lenp->ssize = q - oldq;
3400 assert((STRLEN)(q - const_str) == total_len);
3402 /* Attach all the args (i.e. the kids of the sprintf) to o (which
3403 * may or may not be topop) The pushmark and const ops need to be
3404 * kept in case they're an op_next entry point.
3406 lastkidop = cLISTOPx(topop)->op_last;
3407 kid = cUNOPx(topop)->op_first; /* pushmark */
3409 op_null(OpSIBLING(kid)); /* const */
3411 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */
3412 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */
3413 lastkidop->op_next = o;
3418 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3422 /* Concatenate all const strings into const_str.
3423 * Note that args[] contains the RHS args in reverse order, so
3424 * we scan args[] from top to bottom to get constant strings
3427 for (argp = toparg; argp >= args; argp--) {
3429 /* not a const op */
3430 (++lenp)->ssize = -1;
3432 STRLEN l = argp->len;
3433 Copy(argp->p, p, l, char);
3435 if (lenp->ssize == -1)
3446 for (argp = args; argp <= toparg; argp++) {
3447 /* only keep non-const args, except keep the first-in-next-chain
3448 * arg no matter what it is (but nulled if OP_CONST), because it
3449 * may be the entry point to this subtree from the previous
3452 bool last = (argp == toparg);
3455 /* set prev to the sibling *before* the arg to be cut out,
3456 * e.g. when cutting EXPR:
3461 * prev= CONCAT -- EXPR
3464 if (argp == args && kid->op_type != OP_CONCAT) {
3465 /* in e.g. '$x .= f(1)' there's no RHS concat tree
3466 * so the expression to be cut isn't kid->op_last but
3469 /* find the op before kid */
3471 o2 = cUNOPx(parentop)->op_first;
3472 while (o2 && o2 != kid) {
3480 else if (kid == o && lastkidop)
3481 prev = last ? lastkidop : OpSIBLING(lastkidop);
3483 prev = last ? NULL : cUNOPx(kid)->op_first;
3485 if (!argp->p || last) {
3487 OP *aop = op_sibling_splice(kid, prev, 1, NULL);
3488 /* and unshift to front of o */
3489 op_sibling_splice(o, NULL, 0, aop);
3490 /* record the right-most op added to o: later we will
3491 * free anything to the right of it */
3494 aop->op_next = nextop;
3497 /* null the const at start of op_next chain */
3501 nextop = prev->op_next;
3504 /* the last two arguments are both attached to the same concat op */
3505 if (argp < toparg - 1)
3510 /* Populate the aux struct */
3512 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs;
3513 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str;
3514 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len;
3515 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str;
3516 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len;
3518 /* if variant > 0, calculate a variant const string and lengths where
3519 * the utf8 version of the string will take 'variant' more bytes than
3523 char *p = const_str;
3524 STRLEN ulen = total_len + variant;
3525 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
3526 UNOP_AUX_item *ulens = lens + (nargs + 1);
3527 char *up = (char*)PerlMemShared_malloc(ulen);
3530 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up;
3531 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen;
3533 for (n = 0; n < (nargs + 1); n++) {
3535 char * orig_up = up;
3536 for (i = (lens++)->ssize; i > 0; i--) {
3538 append_utf8_from_native_byte(c, (U8**)&up);
3540 (ulens++)->ssize = (i < 0) ? i : up - orig_up;
3545 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep
3546 * that op's first child - an ex-PUSHMARK - because the op_next of
3547 * the previous op may point to it (i.e. it's the entry point for
3552 ? op_sibling_splice(o, lastkidop, 1, NULL)
3553 : op_sibling_splice(stringop, NULL, 1, NULL);
3554 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK));
3555 op_sibling_splice(o, NULL, 0, pmop);
3562 * target .= A.B.C...
3568 if (o->op_type == OP_SASSIGN) {
3569 /* Move the target subtree from being the last of o's children
3570 * to being the last of o's preserved children.
3571 * Note the difference between 'target = ...' and 'target .= ...':
3572 * for the former, target is executed last; for the latter,
3575 kid = OpSIBLING(lastkidop);
3576 op_sibling_splice(o, kid, 1, NULL); /* cut target op */
3577 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */
3578 lastkidop->op_next = kid->op_next;
3579 lastkidop = targetop;
3582 /* Move the target subtree from being the first of o's
3583 * original children to being the first of *all* o's children.
3586 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */
3587 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/
3590 /* if the RHS of .= doesn't contain a concat (e.g.
3591 * $x .= "foo"), it gets missed by the "strip ops from the
3592 * tree and add to o" loop earlier */
3593 assert(topop->op_type != OP_CONCAT);
3595 /* in e.g. $x .= "$y", move the $y expression
3596 * from being a child of OP_STRINGIFY to being the
3597 * second child of the OP_CONCAT
3599 assert(cUNOPx(stringop)->op_first == topop);
3600 op_sibling_splice(stringop, NULL, 1, NULL);
3601 op_sibling_splice(o, cUNOPo->op_first, 0, topop);
3603 assert(topop == OpSIBLING(cBINOPo->op_first));
3612 * my $lex = A.B.C...
3615 * The original padsv op is kept but nulled in case it's the
3616 * entry point for the optree (which it will be for
3619 private_flags |= OPpTARGET_MY;
3620 private_flags |= (targetop->op_private & OPpLVAL_INTRO);
3621 o->op_targ = targetop->op_targ;
3622 targetop->op_targ = 0;
3626 flags |= OPf_STACKED;
3628 else if (targmyop) {
3629 private_flags |= OPpTARGET_MY;
3630 if (o != targmyop) {
3631 o->op_targ = targmyop->op_targ;
3632 targmyop->op_targ = 0;
3636 /* detach the emaciated husk of the sprintf/concat optree and free it */
3638 kid = op_sibling_splice(o, lastkidop, 1, NULL);
3644 /* and convert o into a multiconcat */
3646 o->op_flags = (flags|OPf_KIDS|stacked_last
3647 |(o->op_flags & (OPf_WANT|OPf_PARENS)));
3648 o->op_private = private_flags;
3649 o->op_type = OP_MULTICONCAT;
3650 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT];
3651 cUNOP_AUXo->op_aux = aux;
3655 /* do all the final processing on an optree (e.g. running the peephole
3656 * optimiser on it), then attach it to cv (if cv is non-null)
3660 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
3664 /* XXX for some reason, evals, require and main optrees are
3665 * never attached to their CV; instead they just hang off
3666 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
3667 * and get manually freed when appropriate */
3669 startp = &CvSTART(cv);
3671 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
3674 optree->op_private |= OPpREFCOUNTED;
3675 OpREFCNT_set(optree, 1);
3676 optimize_optree(optree);
3678 finalize_optree(optree);
3679 S_prune_chain_head(startp);
3682 /* now that optimizer has done its work, adjust pad values */
3683 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
3684 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
3690 =for apidoc optimize_optree
3692 This function applies some optimisations to the optree in top-down order.
3693 It is called before the peephole optimizer, which processes ops in
3694 execution order. Note that finalize_optree() also does a top-down scan,
3695 but is called *after* the peephole optimizer.
3701 Perl_optimize_optree(pTHX_ OP* o)
3703 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE;
3706 SAVEVPTR(PL_curcop);
3714 /* helper for optimize_optree() which optimises one op then recurses
3715 * to optimise any children.
3719 S_optimize_op(pTHX_ OP* o)
3723 PERL_ARGS_ASSERT_OPTIMIZE_OP;
3726 OP * next_kid = NULL;
3728 assert(o->op_type != OP_FREED);
3730 switch (o->op_type) {
3733 PL_curcop = ((COP*)o); /* for warnings */
3741 S_maybe_multiconcat(aTHX_ o);
3745 if (cPMOPo->op_pmreplrootu.op_pmreplroot) {
3746 /* we can't assume that op_pmreplroot->op_sibparent == o
3747 * and that it is thus possible to walk back up the tree
3748 * past op_pmreplroot. So, although we try to avoid
3749 * recursing through op trees, do it here. After all,
3750 * there are unlikely to be many nested s///e's within
3751 * the replacement part of a s///e.
3753 optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3761 if (o->op_flags & OPf_KIDS)
3762 next_kid = cUNOPo->op_first;
3764 /* if a kid hasn't been nominated to process, continue with the
3765 * next sibling, or if no siblings left, go back to the parent's
3766 * siblings and so on
3770 return; /* at top; no parents/siblings to try */
3771 if (OpHAS_SIBLING(o))
3772 next_kid = o->op_sibparent;
3774 o = o->op_sibparent; /*try parent's next sibling */
3777 /* this label not yet used. Goto here if any code above sets
3787 =for apidoc finalize_optree
3789 This function finalizes the optree. Should be called directly after
3790 the complete optree is built. It does some additional
3791 checking which can't be done in the normal C<ck_>xxx functions and makes
3792 the tree thread-safe.
3797 Perl_finalize_optree(pTHX_ OP* o)
3799 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
3802 SAVEVPTR(PL_curcop);
3810 /* Relocate sv to the pad for thread safety.
3811 * Despite being a "constant", the SV is written to,
3812 * for reference counts, sv_upgrade() etc. */
3813 PERL_STATIC_INLINE void
3814 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
3817 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
3819 ix = pad_alloc(OP_CONST, SVf_READONLY);
3820 SvREFCNT_dec(PAD_SVl(ix));
3821 PAD_SETSV(ix, *svp);
3822 /* XXX I don't know how this isn't readonly already. */
3823 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
3830 =for apidoc traverse_op_tree
3832 Return the next op in a depth-first traversal of the op tree,
3833 returning NULL when the traversal is complete.
3835 The initial call must supply the root of the tree as both top and o.
3837 For now it's static, but it may be exposed to the API in the future.
3843 S_traverse_op_tree(pTHX_ OP *top, OP *o) {
3846 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;
3848 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {
3849 return cUNOPo->op_first;
3851 else if ((sib = OpSIBLING(o))) {
3855 OP *parent = o->op_sibparent;
3856 assert(!(o->op_moresib));
3857 while (parent && parent != top) {
3858 OP *sib = OpSIBLING(parent);
3861 parent = parent->op_sibparent;
3869 S_finalize_op(pTHX_ OP* o)
3872 PERL_ARGS_ASSERT_FINALIZE_OP;
3875 assert(o->op_type != OP_FREED);
3877 switch (o->op_type) {
3880 PL_curcop = ((COP*)o); /* for warnings */
3883 if (OpHAS_SIBLING(o)) {
3884 OP *sib = OpSIBLING(o);
3885 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
3886 && ckWARN(WARN_EXEC)
3887 && OpHAS_SIBLING(sib))
3889 const OPCODE type = OpSIBLING(sib)->op_type;
3890 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
3891 const line_t oldline = CopLINE(PL_curcop);
3892 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
3893 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3894 "Statement unlikely to be reached");
3895 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3896 "\t(Maybe you meant system() when you said exec()?)\n");
3897 CopLINE_set(PL_curcop, oldline);
3904 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
3905 GV * const gv = cGVOPo_gv;
3906 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
3907 /* XXX could check prototype here instead of just carping */
3908 SV * const sv = sv_newmortal();
3909 gv_efullname3(sv, gv, NULL);
3910 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3911 "%" SVf "() called too early to check prototype",
3918 if (cSVOPo->op_private & OPpCONST_STRICT)
3919 no_bareword_allowed(o);
3923 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
3928 /* Relocate all the METHOP's SVs to the pad for thread safety. */
3929 case OP_METHOD_NAMED:
3930 case OP_METHOD_SUPER:
3931 case OP_METHOD_REDIR:
3932 case OP_METHOD_REDIR_SUPER:
3933 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
3942 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
3945 rop = (UNOP*)((BINOP*)o)->op_first;
3950 S_scalar_slice_warning(aTHX_ o);
3954 kid = OpSIBLING(cLISTOPo->op_first);
3955 if (/* I bet there's always a pushmark... */
3956 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
3957 && OP_TYPE_ISNT_NN(kid, OP_CONST))
3962 key_op = (SVOP*)(kid->op_type == OP_CONST
3964 : OpSIBLING(kLISTOP->op_first));
3966 rop = (UNOP*)((LISTOP*)o)->op_last;
3969 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
3971 S_check_hash_fields_and_hekify(aTHX_ rop, key_op, 1);
3975 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
3979 S_scalar_slice_warning(aTHX_ o);
3983 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
3984 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3992 if (o->op_flags & OPf_KIDS) {
3995 /* check that op_last points to the last sibling, and that
3996 * the last op_sibling/op_sibparent field points back to the
3997 * parent, and that the only ops with KIDS are those which are
3998 * entitled to them */
3999 U32 type = o->op_type;
4003 if (type == OP_NULL) {
4005 /* ck_glob creates a null UNOP with ex-type GLOB
4006 * (which is a list op. So pretend it wasn't a listop */
4007 if (type == OP_GLOB)
4010 family = PL_opargs[type] & OA_CLASS_MASK;
4012 has_last = ( family == OA_BINOP
4013 || family == OA_LISTOP
4014 || family == OA_PMOP
4015 || family == OA_LOOP
4017 assert( has_last /* has op_first and op_last, or ...
4018 ... has (or may have) op_first: */
4019 || family == OA_UNOP
4020 || family == OA_UNOP_AUX
4021 || family == OA_LOGOP
4022 || family == OA_BASEOP_OR_UNOP
4023 || family == OA_FILESTATOP
4024 || family == OA_LOOPEXOP
4025 || family == OA_METHOP
4026 || type == OP_CUSTOM
4027 || type == OP_NULL /* new_logop does this */
4030 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
4031 if (!OpHAS_SIBLING(kid)) {
4033 assert(kid == cLISTOPo->op_last);
4034 assert(kid->op_sibparent == o);
4039 } while (( o = traverse_op_tree(top, o)) != NULL);
4043 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
4046 PadnameLVALUE_on(pn);
4047 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
4049 /* RT #127786: cv can be NULL due to an eval within the DB package
4050 * called from an anon sub - anon subs don't have CvOUTSIDE() set
4051 * unless they contain an eval, but calling eval within DB
4052 * pretends the eval was done in the caller's scope.
4056 assert(CvPADLIST(cv));
4058 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
4059 assert(PadnameLEN(pn));
4060 PadnameLVALUE_on(pn);
4065 S_vivifies(const OPCODE type)
4068 case OP_RV2AV: case OP_ASLICE:
4069 case OP_RV2HV: case OP_KVASLICE:
4070 case OP_RV2SV: case OP_HSLICE:
4071 case OP_AELEMFAST: case OP_KVHSLICE:
4080 /* apply lvalue reference (aliasing) context to the optree o.
4083 * o would be the list ($x,$y) and type would be OP_AASSIGN.
4084 * It may descend and apply this to children too, for example in
4085 * \( $cond ? $x, $y) = (...)
4089 S_lvref(pTHX_ OP *o, I32 type)
4095 switch (o->op_type) {
4097 o = OpSIBLING(cUNOPo->op_first);
4104 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
4105 o->op_flags |= OPf_STACKED;
4106 if (o->op_flags & OPf_PARENS) {
4107 if (o->op_private & OPpLVAL_INTRO) {
4108 yyerror(Perl_form(aTHX_ "Can't modify reference to "
4109 "localized parenthesized array in list assignment"));
4113 OpTYPE_set(o, OP_LVAVREF);
4114 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
4115 o->op_flags |= OPf_MOD|OPf_REF;
4118 o->op_private |= OPpLVREF_AV;
4122 kid = cUNOPo->op_first;
4123 if (kid->op_type == OP_NULL)
4124 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
4126 o->op_private = OPpLVREF_CV;
4127 if (kid->op_type == OP_GV)
4128 o->op_flags |= OPf_STACKED;
4129 else if (kid->op_type == OP_PADCV) {
4130 o->op_targ = kid->op_targ;
4132 op_free(cUNOPo->op_first);
4133 cUNOPo->op_first = NULL;
4134 o->op_flags &=~ OPf_KIDS;
4140 if (o->op_flags & OPf_PARENS) {
4142 yyerror(Perl_form(aTHX_ "Can't modify reference to "
4143 "parenthesized hash in list assignment"));
4146 o->op_private |= OPpLVREF_HV;
4150 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
4151 o->op_flags |= OPf_STACKED;
4155 if (o->op_flags & OPf_PARENS) goto parenhash;
4156 o->op_private |= OPpLVREF_HV;
4159 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
4163 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
4164 if (o->op_flags & OPf_PARENS) goto slurpy;
4165 o->op_private |= OPpLVREF_AV;
4170 o->op_private |= OPpLVREF_ELEM;
4171 o->op_flags |= OPf_STACKED;
4176 OpTYPE_set(o, OP_LVREFSLICE);
4177 o->op_private &= OPpLVAL_INTRO;
4181 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4183 else if (!(o->op_flags & OPf_KIDS))
4186 /* the code formerly only recursed into the first child of
4187 * a non ex-list OP_NULL. if we ever encounter such a null op with
4188 * more than one child, need to decide whether its ok to process
4189 * *all* its kids or not */
4190 assert(o->op_targ == OP_LIST
4191 || !(OpHAS_SIBLING(cBINOPo->op_first)));
4194 o = cLISTOPo->op_first;
4198 if (o->op_flags & OPf_PARENS)
4203 /* diag_listed_as: Can't modify reference to %s in %s assignment */
4204 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
4205 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
4212 OpTYPE_set(o, OP_LVREF);
4214 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
4215 if (type == OP_ENTERLOOP)
4216 o->op_private |= OPpLVREF_ITER;
4221 return; /* at top; no parents/siblings to try */
4222 if (OpHAS_SIBLING(o)) {
4223 o = o->op_sibparent;
4226 o = o->op_sibparent; /*try parent's next sibling */
4232 PERL_STATIC_INLINE bool
4233 S_potential_mod_type(I32 type)
4235 /* Types that only potentially result in modification. */
4236 return type == OP_GREPSTART || type == OP_ENTERSUB
4237 || type == OP_REFGEN || type == OP_LEAVESUBLV;
4242 =for apidoc op_lvalue
4244 Propagate lvalue ("modifiable") context to an op and its children.
4245 C<type> represents the context type, roughly based on the type of op that
4246 would do the modifying, although C<local()> is represented by C<OP_NULL>,
4247 because it has no op type of its own (it is signalled by a flag on
4250 This function detects things that can't be modified, such as C<$x+1>, and
4251 generates errors for them. For example, C<$x+1 = 2> would cause it to be
4252 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
4254 It also flags things that need to behave specially in an lvalue context,
4255 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
4259 Perl_op_lvalue_flags() is a non-API lower-level interface to
4260 op_lvalue(). The flags param has these bits:
4261 OP_LVALUE_NO_CROAK: return rather than croaking on error
4266 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
4270 if (!o || (PL_parser && PL_parser->error_count))
4275 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
4277 OP *next_kid = NULL;
4279 if ((o->op_private & OPpTARGET_MY)
4280 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
4285 /* elements of a list might be in void context because the list is
4286 in scalar context or because they are attribute sub calls */
4287 if ((o->op_flags & OPf_WANT) == OPf_WANT_VOID)
4290 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
4292 switch (o->op_type) {
4298 if ((o->op_flags & OPf_PARENS))
4303 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
4304 !(o->op_flags & OPf_STACKED)) {
4305 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
4306 assert(cUNOPo->op_first->op_type == OP_NULL);
4307 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
4310 else { /* lvalue subroutine call */
4311 o->op_private |= OPpLVAL_INTRO;
4312 PL_modcount = RETURN_UNLIMITED_NUMBER;
4313 if (S_potential_mod_type(type)) {
4314 o->op_private |= OPpENTERSUB_INARGS;
4317 else { /* Compile-time error message: */
4318 OP *kid = cUNOPo->op_first;
4323 if (kid->op_type != OP_PUSHMARK) {
4324 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
4326 "panic: unexpected lvalue entersub "
4327 "args: type/targ %ld:%" UVuf,
4328 (long)kid->op_type, (UV)kid->op_targ);
4329 kid = kLISTOP->op_first;
4331 while (OpHAS_SIBLING(kid))
4332 kid = OpSIBLING(kid);
4333 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
4334 break; /* Postpone until runtime */
4337 kid = kUNOP->op_first;
4338 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
4339 kid = kUNOP->op_first;
4340 if (kid->op_type == OP_NULL)
4342 "Unexpected constant lvalue entersub "
4343 "entry via type/targ %ld:%" UVuf,
4344 (long)kid->op_type, (UV)kid->op_targ);
4345 if (kid->op_type != OP_GV) {
4352 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
4353 ? MUTABLE_CV(SvRV(gv))
4359 if (flags & OP_LVALUE_NO_CROAK)
4362 namesv = cv_name(cv, NULL, 0);
4363 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
4364 "subroutine call of &%" SVf " in %s",
4365 SVfARG(namesv), PL_op_desc[type]),
4373 if (flags & OP_LVALUE_NO_CROAK) return NULL;
4374 /* grep, foreach, subcalls, refgen */
4375 if (S_potential_mod_type(type))
4377 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
4378 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
4381 type ? PL_op_desc[type] : "local"));
4394 case OP_RIGHT_SHIFT:
4403 if (!(o->op_flags & OPf_STACKED))
4409 if (o->op_flags & OPf_STACKED) {
4413 if (!(o->op_private & OPpREPEAT_DOLIST))
4416 const I32 mods = PL_modcount;
4417 /* we recurse rather than iterate here because we need to
4418 * calculate and use the delta applied to PL_modcount by the
4419 * first child. So in something like
4420 * ($x, ($y) x 3) = split;
4421 * split knows that 4 elements are wanted
4423 modkids(cBINOPo->op_first, type);
4424 if (type != OP_AASSIGN)
4426 kid = cBINOPo->op_last;
4427 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
4428 const IV iv = SvIV(kSVOP_sv);
4429 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
4431 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
4434 PL_modcount = RETURN_UNLIMITED_NUMBER;
4440 next_kid = OpSIBLING(cUNOPo->op_first);
4445 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
4446 PL_modcount = RETURN_UNLIMITED_NUMBER;
4447 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4448 fiable since some contexts need to know. */
4449 o->op_flags |= OPf_MOD;
4454 if (scalar_mod_type(o, type))
4456 ref(cUNOPo->op_first, o->op_type);
4463 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
4464 if (type == OP_LEAVESUBLV && (
4465 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
4466 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4468 o->op_private |= OPpMAYBE_LVSUB;
4472 PL_modcount = RETURN_UNLIMITED_NUMBER;
4478 if (type == OP_LEAVESUBLV)
4479 o->op_private |= OPpMAYBE_LVSUB;
4483 if (type == OP_LEAVESUBLV
4484 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS)
4485 o->op_private |= OPpMAYBE_LVSUB;
4489 PL_hints |= HINT_BLOCK_SCOPE;
4490 if (type == OP_LEAVESUBLV)
4491 o->op_private |= OPpMAYBE_LVSUB;
4496 ref(cUNOPo->op_first, o->op_type);
4500 PL_hints |= HINT_BLOCK_SCOPE;
4510 case OP_AELEMFAST_LEX:
4517 PL_modcount = RETURN_UNLIMITED_NUMBER;
4518 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
4520 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4521 fiable since some contexts need to know. */
4522 o->op_flags |= OPf_MOD;
4525 if (scalar_mod_type(o, type))
4527 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4528 && type == OP_LEAVESUBLV)
4529 o->op_private |= OPpMAYBE_LVSUB;
4533 if (!type) /* local() */
4534 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
4535 PNfARG(PAD_COMPNAME(o->op_targ)));
4536 if (!(o->op_private & OPpLVAL_INTRO)
4537 || ( type != OP_SASSIGN && type != OP_AASSIGN
4538 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
4539 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
4547 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
4551 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
4557 if (type == OP_LEAVESUBLV)
4558 o->op_private |= OPpMAYBE_LVSUB;
4559 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
4560 /* we recurse rather than iterate here because the child
4561 * needs to be processed with a different 'type' parameter */
4563 /* substr and vec */
4564 /* If this op is in merely potential (non-fatal) modifiable
4565 context, then apply OP_ENTERSUB context to
4566 the kid op (to avoid croaking). Other-
4567 wise pass this op’s own type so the correct op is mentioned
4568 in error messages. */
4569 op_lvalue(OpSIBLING(cBINOPo->op_first),
4570 S_potential_mod_type(type)
4578 ref(cBINOPo->op_first, o->op_type);
4579 if (type == OP_ENTERSUB &&
4580 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
4581 o->op_private |= OPpLVAL_DEFER;
4582 if (type == OP_LEAVESUBLV)
4583 o->op_private |= OPpMAYBE_LVSUB;
4590 o->op_private |= OPpLVALUE;
4596 if (o->op_flags & OPf_KIDS)
4597 next_kid = cLISTOPo->op_last;
4602 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4604 else if (!(o->op_flags & OPf_KIDS))
4607 if (o->op_targ != OP_LIST) {
4608 OP *sib = OpSIBLING(cLISTOPo->op_first);
4609 /* OP_TRANS and OP_TRANSR with argument have a weird optree
4616 * compared with things like OP_MATCH which have the argument
4622 * so handle specially to correctly get "Can't modify" croaks etc
4625 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
4627 /* this should trigger a "Can't modify transliteration" err */
4628 op_lvalue(sib, type);
4630 next_kid = cBINOPo->op_first;
4631 /* we assume OP_NULLs which aren't ex-list have no more than 2
4632 * children. If this assumption is wrong, increase the scan
4634 assert( !OpHAS_SIBLING(next_kid)
4635 || !OpHAS_SIBLING(OpSIBLING(next_kid)));
4641 next_kid = cLISTOPo->op_first;
4649 if (type == OP_LEAVESUBLV
4650 || !S_vivifies(cLOGOPo->op_first->op_type))
4651 next_kid = cLOGOPo->op_first;
4652 else if (type == OP_LEAVESUBLV
4653 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
4654 next_kid = OpSIBLING(cLOGOPo->op_first);
4658 if (type == OP_NULL) { /* local */
4660 if (!FEATURE_MYREF_IS_ENABLED)
4661 Perl_croak(aTHX_ "The experimental declared_refs "
4662 "feature is not enabled");
4663 Perl_ck_warner_d(aTHX_
4664 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
4665 "Declaring references is experimental");
4666 next_kid = cUNOPo->op_first;
4669 if (type != OP_AASSIGN && type != OP_SASSIGN
4670 && type != OP_ENTERLOOP)
4672 /* Don’t bother applying lvalue context to the ex-list. */
4673 kid = cUNOPx(cUNOPo->op_first)->op_first;
4674 assert (!OpHAS_SIBLING(kid));
4677 if (type == OP_NULL) /* local */
4679 if (type != OP_AASSIGN) goto nomod;
4680 kid = cUNOPo->op_first;
4683 const U8 ec = PL_parser ? PL_parser->error_count : 0;
4684 S_lvref(aTHX_ kid, type);