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 *))
210 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
212 /* requires double parens and aTHX_ */
213 #define DEBUG_S_warn(args) \
215 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
219 /* malloc a new op slab (suitable for attaching to PL_compcv).
220 * sz is in units of pointers */
223 S_new_slab(pTHX_ OPSLAB *head, size_t sz)
227 /* opslot_offset is only U16 */
228 assert(sz < U16_MAX);
230 #ifdef PERL_DEBUG_READONLY_OPS
231 slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
232 PROT_READ|PROT_WRITE,
233 MAP_ANON|MAP_PRIVATE, -1, 0);
234 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
235 (unsigned long) sz, slab));
236 if (slab == MAP_FAILED) {
237 perror("mmap failed");
241 slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
243 slab->opslab_size = (U16)sz;
246 /* The context is unused in non-Windows */
249 slab->opslab_free_space = sz - DIFF(slab, &slab->opslab_slots);
250 slab->opslab_head = head ? head : slab;
251 DEBUG_S_warn((aTHX_ "allocated new op slab sz 0x%x, %p, head slab %p",
252 (unsigned int)slab->opslab_size, (void*)slab,
253 (void*)(slab->opslab_head)));
257 /* opslot_size includes the size of the slot header, and an op can't be smaller than BASEOP */
258 #define OPSLOT_SIZE_BASE (SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
259 #define OPSLOT_SIZE_TO_INDEX(sz) ((sz) - OPSLOT_SIZE_BASE)
261 #define link_freed_op(slab, o) S_link_freed_op(aTHX_ slab, o)
263 S_link_freed_op(pTHX_ OPSLAB *slab, OP *o) {
264 U16 sz = OpSLOT(o)->opslot_size;
265 U16 index = OPSLOT_SIZE_TO_INDEX(sz);
267 assert(sz >= OPSLOT_SIZE_BASE);
268 /* make sure the array is large enough to include ops this large */
269 if (!slab->opslab_freed) {
270 /* we don't have a free list array yet, make a new one */
271 slab->opslab_freed_size = index+1;
272 slab->opslab_freed = (OP**)PerlMemShared_calloc((slab->opslab_freed_size), sizeof(OP*));
274 if (!slab->opslab_freed)
277 else if (index >= slab->opslab_freed_size) {
278 /* It's probably not worth doing exponential expansion here, the number of op sizes
281 /* We already have a list that isn't large enough, expand it */
282 size_t newsize = index+1;
283 OP **p = (OP **)PerlMemShared_realloc(slab->opslab_freed, newsize * sizeof(OP*));
288 Zero(p+slab->opslab_freed_size, newsize - slab->opslab_freed_size, OP *);
290 slab->opslab_freed = p;
291 slab->opslab_freed_size = newsize;
294 o->op_next = slab->opslab_freed[index];
295 slab->opslab_freed[index] = o;
298 /* Returns a sz-sized block of memory (suitable for holding an op) from
299 * a free slot in the chain of op slabs attached to PL_compcv.
300 * Allocates a new slab if necessary.
301 * if PL_compcv isn't compiling, malloc() instead.
305 Perl_Slab_Alloc(pTHX_ size_t sz)
307 OPSLAB *head_slab; /* first slab in the chain */
313 /* We only allocate ops from the slab during subroutine compilation.
314 We find the slab via PL_compcv, hence that must be non-NULL. It could
315 also be pointing to a subroutine which is now fully set up (CvROOT()
316 pointing to the top of the optree for that sub), or a subroutine
317 which isn't using the slab allocator. If our sanity checks aren't met,
318 don't use a slab, but allocate the OP directly from the heap. */
319 if (!PL_compcv || CvROOT(PL_compcv)
320 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
322 o = (OP*)PerlMemShared_calloc(1, sz);
326 /* While the subroutine is under construction, the slabs are accessed via
327 CvSTART(), to avoid needing to expand PVCV by one pointer for something
328 unneeded at runtime. Once a subroutine is constructed, the slabs are
329 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
330 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
332 if (!CvSTART(PL_compcv)) {
334 (OP *)(head_slab = S_new_slab(aTHX_ NULL, PERL_SLAB_SIZE));
335 CvSLABBED_on(PL_compcv);
336 head_slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
338 else ++(head_slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
340 opsz = SIZE_TO_PSIZE(sz);
341 sz = opsz + OPSLOT_HEADER_P;
343 /* The head slab for each CV maintains a free list of OPs. In particular, constant folding
344 will free up OPs, so it makes sense to re-use them where possible. A
345 freed up slot is used in preference to a new allocation. */
346 if (head_slab->opslab_freed &&
347 OPSLOT_SIZE_TO_INDEX(sz) < head_slab->opslab_freed_size) {
350 /* look for a large enough size with any freed ops */
351 for (base_index = OPSLOT_SIZE_TO_INDEX(sz);
352 base_index < head_slab->opslab_freed_size && !head_slab->opslab_freed[base_index];
356 if (base_index < head_slab->opslab_freed_size) {
357 /* found a freed op */
358 o = head_slab->opslab_freed[base_index];
360 DEBUG_S_warn((aTHX_ "realloced op at %p, slab %p, head slab %p",
362 (I32**)OpSLOT(o) - OpSLOT(o)->opslot_offset,
364 head_slab->opslab_freed[base_index] = o->op_next;
365 Zero(o, opsz, I32 *);
371 #define INIT_OPSLOT(s) \
372 slot->opslot_offset = DIFF(slab2, slot) ; \
373 slot->opslot_size = s; \
374 slab2->opslab_free_space -= s; \
375 o = &slot->opslot_op; \
378 /* The partially-filled slab is next in the chain. */
379 slab2 = head_slab->opslab_next ? head_slab->opslab_next : head_slab;
380 if (slab2->opslab_free_space < sz) {
381 /* Remaining space is too small. */
382 /* If we can fit a BASEOP, add it to the free chain, so as not
384 if (slab2->opslab_free_space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
385 slot = &slab2->opslab_slots;
386 INIT_OPSLOT(slab2->opslab_free_space);
387 o->op_type = OP_FREED;
388 link_freed_op(head_slab, o);
391 /* Create a new slab. Make this one twice as big. */
392 slab2 = S_new_slab(aTHX_ head_slab,
393 slab2->opslab_size > PERL_MAX_SLAB_SIZE / 2
395 : slab2->opslab_size * 2);
396 slab2->opslab_next = head_slab->opslab_next;
397 head_slab->opslab_next = slab2;
399 assert(slab2->opslab_size >= sz);
401 /* Create a new op slot */
403 ((I32 **)&slab2->opslab_slots
404 + slab2->opslab_free_space - sz);
405 assert(slot >= &slab2->opslab_slots);
407 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p, head slab %p",
408 (void*)o, (void*)slab2, (void*)head_slab));
411 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
412 assert(!o->op_moresib);
413 assert(!o->op_sibparent);
420 #ifdef PERL_DEBUG_READONLY_OPS
422 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
424 PERL_ARGS_ASSERT_SLAB_TO_RO;
426 if (slab->opslab_readonly) return;
427 slab->opslab_readonly = 1;
428 for (; slab; slab = slab->opslab_next) {
429 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
430 (unsigned long) slab->opslab_size, slab));*/
431 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
432 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
433 (unsigned long)slab->opslab_size, errno);
438 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
442 PERL_ARGS_ASSERT_SLAB_TO_RW;
444 if (!slab->opslab_readonly) return;
446 for (; slab2; slab2 = slab2->opslab_next) {
447 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
448 (unsigned long) size, slab2));*/
449 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
450 PROT_READ|PROT_WRITE)) {
451 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
452 (unsigned long)slab2->opslab_size, errno);
455 slab->opslab_readonly = 0;
459 # define Slab_to_rw(op) NOOP
462 /* This cannot possibly be right, but it was copied from the old slab
463 allocator, to which it was originally added, without explanation, in
466 # define PerlMemShared PerlMem
469 /* make freed ops die if they're inadvertently executed */
474 DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
479 /* Return the block of memory used by an op to the free list of
480 * the OP slab associated with that op.
484 Perl_Slab_Free(pTHX_ void *op)
486 OP * const o = (OP *)op;
489 PERL_ARGS_ASSERT_SLAB_FREE;
492 o->op_ppaddr = S_pp_freed;
495 if (!o->op_slabbed) {
497 PerlMemShared_free(op);
502 /* If this op is already freed, our refcount will get screwy. */
503 assert(o->op_type != OP_FREED);
504 o->op_type = OP_FREED;
505 link_freed_op(slab, o);
506 DEBUG_S_warn((aTHX_ "freeing op at %p, slab %p, head slab %p",
508 (I32**)OpSLOT(o) - OpSLOT(o)->opslot_offset,
510 OpslabREFCNT_dec_padok(slab);
514 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
516 const bool havepad = !!PL_comppad;
517 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
520 PAD_SAVE_SETNULLPAD();
526 /* Free a chain of OP slabs. Should only be called after all ops contained
527 * in it have been freed. At this point, its reference count should be 1,
528 * because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1,
529 * and just directly calls opslab_free().
530 * (Note that the reference count which PL_compcv held on the slab should
531 * have been removed once compilation of the sub was complete).
537 Perl_opslab_free(pTHX_ OPSLAB *slab)
540 PERL_ARGS_ASSERT_OPSLAB_FREE;
542 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
543 assert(slab->opslab_refcnt == 1);
544 PerlMemShared_free(slab->opslab_freed);
546 slab2 = slab->opslab_next;
548 slab->opslab_refcnt = ~(size_t)0;
550 #ifdef PERL_DEBUG_READONLY_OPS
551 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
553 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
554 perror("munmap failed");
558 PerlMemShared_free(slab);
564 /* like opslab_free(), but first calls op_free() on any ops in the slab
565 * not marked as OP_FREED
569 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
573 size_t savestack_count = 0;
575 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
578 OPSLOT *slot = (OPSLOT*)
579 ((I32**)&slab2->opslab_slots + slab2->opslab_free_space);
580 OPSLOT *end = (OPSLOT*)
581 ((I32**)slab2 + slab2->opslab_size);
583 slot = (OPSLOT*) ((I32**)slot + slot->opslot_size) )
585 if (slot->opslot_op.op_type != OP_FREED
586 && !(slot->opslot_op.op_savefree
592 assert(slot->opslot_op.op_slabbed);
593 op_free(&slot->opslot_op);
594 if (slab->opslab_refcnt == 1) goto free;
597 } while ((slab2 = slab2->opslab_next));
598 /* > 1 because the CV still holds a reference count. */
599 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
601 assert(savestack_count == slab->opslab_refcnt-1);
603 /* Remove the CV’s reference count. */
604 slab->opslab_refcnt--;
611 #ifdef PERL_DEBUG_READONLY_OPS
613 Perl_op_refcnt_inc(pTHX_ OP *o)
616 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
617 if (slab && slab->opslab_readonly) {
630 Perl_op_refcnt_dec(pTHX_ OP *o)
633 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
635 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
637 if (slab && slab->opslab_readonly) {
639 result = --o->op_targ;
642 result = --o->op_targ;
648 * In the following definition, the ", (OP*)0" is just to make the compiler
649 * think the expression is of the right type: croak actually does a Siglongjmp.
651 #define CHECKOP(type,o) \
652 ((PL_op_mask && PL_op_mask[type]) \
653 ? ( op_free((OP*)o), \
654 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
656 : PL_check[type](aTHX_ (OP*)o))
658 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
660 #define OpTYPE_set(o,type) \
662 o->op_type = (OPCODE)type; \
663 o->op_ppaddr = PL_ppaddr[type]; \
667 S_no_fh_allowed(pTHX_ OP *o)
669 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
671 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
677 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
679 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
680 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
685 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
687 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
689 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
694 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
696 PERL_ARGS_ASSERT_BAD_TYPE_PV;
698 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
699 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
702 /* remove flags var, its unused in all callers, move to to right end since gv
703 and kid are always the same */
705 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
707 SV * const namesv = cv_name((CV *)gv, NULL, 0);
708 PERL_ARGS_ASSERT_BAD_TYPE_GV;
710 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
711 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
715 S_no_bareword_allowed(pTHX_ OP *o)
717 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
719 qerror(Perl_mess(aTHX_
720 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
722 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
725 /* "register" allocation */
728 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
731 const bool is_our = (PL_parser->in_my == KEY_our);
733 PERL_ARGS_ASSERT_ALLOCMY;
735 if (flags & ~SVf_UTF8)
736 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
739 /* complain about "my $<special_var>" etc etc */
743 || ( (flags & SVf_UTF8)
744 && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
745 || (name[1] == '_' && len > 2)))
747 const char * const type =
748 PL_parser->in_my == KEY_sigvar ? "subroutine signature" :
749 PL_parser->in_my == KEY_state ? "\"state\"" : "\"my\"";
751 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
753 && (!isPRINT(name[1]) || memCHRs("\t\n\r\f", name[1]))) {
754 /* diag_listed_as: Can't use global %s in %s */
755 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in %s",
756 name[0], toCTRL(name[1]),
757 (int)(len - 2), name + 2,
760 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in %s",
762 type), flags & SVf_UTF8);
766 /* allocate a spare slot and store the name in that slot */
768 off = pad_add_name_pvn(name, len,
769 (is_our ? padadd_OUR :
770 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
771 PL_parser->in_my_stash,
773 /* $_ is always in main::, even with our */
774 ? (PL_curstash && !memEQs(name,len,"$_")
780 /* anon sub prototypes contains state vars should always be cloned,
781 * otherwise the state var would be shared between anon subs */
783 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
784 CvCLONE_on(PL_compcv);
790 =head1 Optree Manipulation Functions
792 =for apidoc alloccopstash
794 Available only under threaded builds, this function allocates an entry in
795 C<PL_stashpad> for the stash passed to it.
802 Perl_alloccopstash(pTHX_ HV *hv)
804 PADOFFSET off = 0, o = 1;
805 bool found_slot = FALSE;
807 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
809 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
811 for (; o < PL_stashpadmax; ++o) {
812 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
813 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
814 found_slot = TRUE, off = o;
817 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
818 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
819 off = PL_stashpadmax;
820 PL_stashpadmax += 10;
823 PL_stashpad[PL_stashpadix = off] = hv;
828 /* free the body of an op without examining its contents.
829 * Always use this rather than FreeOp directly */
832 S_op_destroy(pTHX_ OP *o)
842 Free an op and its children. Only use this when an op is no longer linked
849 Perl_op_free(pTHX_ OP *o)
855 bool went_up = FALSE; /* whether we reached the current node by
856 following the parent pointer from a child, and
857 so have already seen this node */
859 if (!o || o->op_type == OP_FREED)
862 if (o->op_private & OPpREFCOUNTED) {
863 /* if base of tree is refcounted, just decrement */
864 switch (o->op_type) {
874 refcnt = OpREFCNT_dec(o);
877 /* Need to find and remove any pattern match ops from
878 * the list we maintain for reset(). */
879 find_and_forget_pmops(o);
892 /* free child ops before ourself, (then free ourself "on the
895 if (!went_up && o->op_flags & OPf_KIDS) {
896 next_op = cUNOPo->op_first;
900 /* find the next node to visit, *then* free the current node
901 * (can't rely on o->op_* fields being valid after o has been
904 /* The next node to visit will be either the sibling, or the
905 * parent if no siblings left, or NULL if we've worked our way
906 * back up to the top node in the tree */
907 next_op = (o == top_op) ? NULL : o->op_sibparent;
908 went_up = cBOOL(!OpHAS_SIBLING(o)); /* parents are already visited */
910 /* Now process the current node */
912 /* Though ops may be freed twice, freeing the op after its slab is a
914 assert(!o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
915 /* During the forced freeing of ops after compilation failure, kidops
916 may be freed before their parents. */
917 if (!o || o->op_type == OP_FREED)
922 /* an op should only ever acquire op_private flags that we know about.
923 * If this fails, you may need to fix something in regen/op_private.
924 * Don't bother testing if:
925 * * the op_ppaddr doesn't match the op; someone may have
926 * overridden the op and be doing strange things with it;
927 * * we've errored, as op flags are often left in an
928 * inconsistent state then. Note that an error when
929 * compiling the main program leaves PL_parser NULL, so
930 * we can't spot faults in the main code, only
931 * evaled/required code */
933 if ( o->op_ppaddr == PL_ppaddr[type]
935 && !PL_parser->error_count)
937 assert(!(o->op_private & ~PL_op_private_valid[type]));
942 /* Call the op_free hook if it has been set. Do it now so that it's called
943 * at the right time for refcounted ops, but still before all of the kids
948 type = (OPCODE)o->op_targ;
951 Slab_to_rw(OpSLAB(o));
953 /* COP* is not cleared by op_clear() so that we may track line
954 * numbers etc even after null() */
955 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
967 /* S_op_clear_gv(): free a GV attached to an OP */
971 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
973 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
977 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
978 || o->op_type == OP_MULTIDEREF)
981 ? ((GV*)PAD_SVl(*ixp)) : NULL;
983 ? (GV*)(*svp) : NULL;
985 /* It's possible during global destruction that the GV is freed
986 before the optree. Whilst the SvREFCNT_inc is happy to bump from
987 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
988 will trigger an assertion failure, because the entry to sv_clear
989 checks that the scalar is not already freed. A check of for
990 !SvIS_FREED(gv) turns out to be invalid, because during global
991 destruction the reference count can be forced down to zero
992 (with SVf_BREAK set). In which case raising to 1 and then
993 dropping to 0 triggers cleanup before it should happen. I
994 *think* that this might actually be a general, systematic,
995 weakness of the whole idea of SVf_BREAK, in that code *is*
996 allowed to raise and lower references during global destruction,
997 so any *valid* code that happens to do this during global
998 destruction might well trigger premature cleanup. */
999 bool still_valid = gv && SvREFCNT(gv);
1002 SvREFCNT_inc_simple_void(gv);
1005 pad_swipe(*ixp, TRUE);
1013 int try_downgrade = SvREFCNT(gv) == 2;
1014 SvREFCNT_dec_NN(gv);
1016 gv_try_downgrade(gv);
1022 Perl_op_clear(pTHX_ OP *o)
1027 PERL_ARGS_ASSERT_OP_CLEAR;
1029 switch (o->op_type) {
1030 case OP_NULL: /* Was holding old type, if any. */
1033 case OP_ENTEREVAL: /* Was holding hints. */
1034 case OP_ARGDEFELEM: /* Was holding signature index. */
1038 if (!(o->op_flags & OPf_REF) || !OP_IS_STAT(o->op_type))
1045 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
1047 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
1050 case OP_METHOD_REDIR:
1051 case OP_METHOD_REDIR_SUPER:
1053 if (cMETHOPx(o)->op_rclass_targ) {
1054 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
1055 cMETHOPx(o)->op_rclass_targ = 0;
1058 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
1059 cMETHOPx(o)->op_rclass_sv = NULL;
1062 case OP_METHOD_NAMED:
1063 case OP_METHOD_SUPER:
1064 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
1065 cMETHOPx(o)->op_u.op_meth_sv = NULL;
1068 pad_swipe(o->op_targ, 1);
1075 SvREFCNT_dec(cSVOPo->op_sv);
1076 cSVOPo->op_sv = NULL;
1079 Even if op_clear does a pad_free for the target of the op,
1080 pad_free doesn't actually remove the sv that exists in the pad;
1081 instead it lives on. This results in that it could be reused as
1082 a target later on when the pad was reallocated.
1085 pad_swipe(o->op_targ,1);
1095 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
1100 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
1101 && (o->op_private & OPpTRANS_USE_SVOP))
1104 if (cPADOPo->op_padix > 0) {
1105 pad_swipe(cPADOPo->op_padix, TRUE);
1106 cPADOPo->op_padix = 0;
1109 SvREFCNT_dec(cSVOPo->op_sv);
1110 cSVOPo->op_sv = NULL;
1114 PerlMemShared_free(cPVOPo->op_pv);
1115 cPVOPo->op_pv = NULL;
1119 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1123 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1124 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1126 if (o->op_private & OPpSPLIT_LEX)
1127 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1130 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1132 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1139 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1140 op_free(cPMOPo->op_code_list);
1141 cPMOPo->op_code_list = NULL;
1142 forget_pmop(cPMOPo);
1143 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1144 /* we use the same protection as the "SAFE" version of the PM_ macros
1145 * here since sv_clean_all might release some PMOPs
1146 * after PL_regex_padav has been cleared
1147 * and the clearing of PL_regex_padav needs to
1148 * happen before sv_clean_all
1151 if(PL_regex_pad) { /* We could be in destruction */
1152 const IV offset = (cPMOPo)->op_pmoffset;
1153 ReREFCNT_dec(PM_GETRE(cPMOPo));
1154 PL_regex_pad[offset] = &PL_sv_undef;
1155 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1159 ReREFCNT_dec(PM_GETRE(cPMOPo));
1160 PM_SETRE(cPMOPo, NULL);
1166 PerlMemShared_free(cUNOP_AUXo->op_aux);
1169 case OP_MULTICONCAT:
1171 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux;
1172 /* aux[PERL_MULTICONCAT_IX_PLAIN_PV] and/or
1173 * aux[PERL_MULTICONCAT_IX_UTF8_PV] point to plain and/or
1174 * utf8 shared strings */
1175 char *p1 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
1176 char *p2 = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
1178 PerlMemShared_free(p1);
1180 PerlMemShared_free(p2);
1181 PerlMemShared_free(aux);
1187 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1188 UV actions = items->uv;
1190 bool is_hash = FALSE;
1193 switch (actions & MDEREF_ACTION_MASK) {
1196 actions = (++items)->uv;
1199 case MDEREF_HV_padhv_helem:
1202 case MDEREF_AV_padav_aelem:
1203 pad_free((++items)->pad_offset);
1206 case MDEREF_HV_gvhv_helem:
1209 case MDEREF_AV_gvav_aelem:
1211 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1213 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1217 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1220 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1222 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1224 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1226 goto do_vivify_rv2xv_elem;
1228 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1231 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1232 pad_free((++items)->pad_offset);
1233 goto do_vivify_rv2xv_elem;
1235 case MDEREF_HV_pop_rv2hv_helem:
1236 case MDEREF_HV_vivify_rv2hv_helem:
1239 do_vivify_rv2xv_elem:
1240 case MDEREF_AV_pop_rv2av_aelem:
1241 case MDEREF_AV_vivify_rv2av_aelem:
1243 switch (actions & MDEREF_INDEX_MASK) {
1244 case MDEREF_INDEX_none:
1247 case MDEREF_INDEX_const:
1251 pad_swipe((++items)->pad_offset, 1);
1253 SvREFCNT_dec((++items)->sv);
1259 case MDEREF_INDEX_padsv:
1260 pad_free((++items)->pad_offset);
1262 case MDEREF_INDEX_gvsv:
1264 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1266 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1271 if (actions & MDEREF_FLAG_last)
1284 actions >>= MDEREF_SHIFT;
1287 /* start of malloc is at op_aux[-1], where the length is
1289 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1294 if (o->op_targ > 0) {
1295 pad_free(o->op_targ);
1301 S_cop_free(pTHX_ COP* cop)
1303 PERL_ARGS_ASSERT_COP_FREE;
1306 if (! specialWARN(cop->cop_warnings))
1307 PerlMemShared_free(cop->cop_warnings);
1308 cophh_free(CopHINTHASH_get(cop));
1309 if (PL_curcop == cop)
1314 S_forget_pmop(pTHX_ PMOP *const o)
1316 HV * const pmstash = PmopSTASH(o);
1318 PERL_ARGS_ASSERT_FORGET_PMOP;
1320 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1321 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1323 PMOP **const array = (PMOP**) mg->mg_ptr;
1324 U32 count = mg->mg_len / sizeof(PMOP**);
1328 if (array[i] == o) {
1329 /* Found it. Move the entry at the end to overwrite it. */
1330 array[i] = array[--count];
1331 mg->mg_len = count * sizeof(PMOP**);
1332 /* Could realloc smaller at this point always, but probably
1333 not worth it. Probably worth free()ing if we're the
1336 Safefree(mg->mg_ptr);
1350 S_find_and_forget_pmops(pTHX_ OP *o)
1354 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1357 switch (o->op_type) {
1362 forget_pmop((PMOP*)o);
1365 if (o->op_flags & OPf_KIDS) {
1366 o = cUNOPo->op_first;
1372 return; /* at top; no parents/siblings to try */
1373 if (OpHAS_SIBLING(o)) {
1374 o = o->op_sibparent; /* process next sibling */
1377 o = o->op_sibparent; /*try parent's next sibling */
1386 Neutralizes an op when it is no longer needed, but is still linked to from
1393 Perl_op_null(pTHX_ OP *o)
1397 PERL_ARGS_ASSERT_OP_NULL;
1399 if (o->op_type == OP_NULL)
1402 o->op_targ = o->op_type;
1403 OpTYPE_set(o, OP_NULL);
1407 Perl_op_refcnt_lock(pTHX)
1408 PERL_TSA_ACQUIRE(PL_op_mutex)
1413 PERL_UNUSED_CONTEXT;
1418 Perl_op_refcnt_unlock(pTHX)
1419 PERL_TSA_RELEASE(PL_op_mutex)
1424 PERL_UNUSED_CONTEXT;
1430 =for apidoc op_sibling_splice
1432 A general function for editing the structure of an existing chain of
1433 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1434 you to delete zero or more sequential nodes, replacing them with zero or
1435 more different nodes. Performs the necessary op_first/op_last
1436 housekeeping on the parent node and op_sibling manipulation on the
1437 children. The last deleted node will be marked as as the last node by
1438 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1440 Note that op_next is not manipulated, and nodes are not freed; that is the
1441 responsibility of the caller. It also won't create a new list op for an
1442 empty list etc; use higher-level functions like op_append_elem() for that.
1444 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1445 the splicing doesn't affect the first or last op in the chain.
1447 C<start> is the node preceding the first node to be spliced. Node(s)
1448 following it will be deleted, and ops will be inserted after it. If it is
1449 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1452 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1453 If -1 or greater than or equal to the number of remaining kids, all
1454 remaining kids are deleted.
1456 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1457 If C<NULL>, no nodes are inserted.
1459 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1464 action before after returns
1465 ------ ----- ----- -------
1468 splice(P, A, 2, X-Y-Z) | | B-C
1472 splice(P, NULL, 1, X-Y) | | A
1476 splice(P, NULL, 3, NULL) | | A-B-C
1480 splice(P, B, 0, X-Y) | | NULL
1484 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1485 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1491 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1495 OP *last_del = NULL;
1496 OP *last_ins = NULL;
1499 first = OpSIBLING(start);
1503 first = cLISTOPx(parent)->op_first;
1505 assert(del_count >= -1);
1507 if (del_count && first) {
1509 while (--del_count && OpHAS_SIBLING(last_del))
1510 last_del = OpSIBLING(last_del);
1511 rest = OpSIBLING(last_del);
1512 OpLASTSIB_set(last_del, NULL);
1519 while (OpHAS_SIBLING(last_ins))
1520 last_ins = OpSIBLING(last_ins);
1521 OpMAYBESIB_set(last_ins, rest, NULL);
1527 OpMAYBESIB_set(start, insert, NULL);
1531 cLISTOPx(parent)->op_first = insert;
1533 parent->op_flags |= OPf_KIDS;
1535 parent->op_flags &= ~OPf_KIDS;
1539 /* update op_last etc */
1546 /* ought to use OP_CLASS(parent) here, but that can't handle
1547 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1549 type = parent->op_type;
1550 if (type == OP_CUSTOM) {
1552 type = XopENTRYCUSTOM(parent, xop_class);
1555 if (type == OP_NULL)
1556 type = parent->op_targ;
1557 type = PL_opargs[type] & OA_CLASS_MASK;
1560 lastop = last_ins ? last_ins : start ? start : NULL;
1561 if ( type == OA_BINOP
1562 || type == OA_LISTOP
1566 cLISTOPx(parent)->op_last = lastop;
1569 OpLASTSIB_set(lastop, parent);
1571 return last_del ? first : NULL;
1574 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1578 =for apidoc op_parent
1580 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1586 Perl_op_parent(OP *o)
1588 PERL_ARGS_ASSERT_OP_PARENT;
1589 while (OpHAS_SIBLING(o))
1591 return o->op_sibparent;
1594 /* replace the sibling following start with a new UNOP, which becomes
1595 * the parent of the original sibling; e.g.
1597 * op_sibling_newUNOP(P, A, unop-args...)
1605 * where U is the new UNOP.
1607 * parent and start args are the same as for op_sibling_splice();
1608 * type and flags args are as newUNOP().
1610 * Returns the new UNOP.
1614 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1618 kid = op_sibling_splice(parent, start, 1, NULL);
1619 newop = newUNOP(type, flags, kid);
1620 op_sibling_splice(parent, start, 0, newop);
1625 /* lowest-level newLOGOP-style function - just allocates and populates
1626 * the struct. Higher-level stuff should be done by S_new_logop() /
1627 * newLOGOP(). This function exists mainly to avoid op_first assignment
1628 * being spread throughout this file.
1632 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1637 NewOp(1101, logop, 1, LOGOP);
1638 OpTYPE_set(logop, type);
1639 logop->op_first = first;
1640 logop->op_other = other;
1642 logop->op_flags = OPf_KIDS;
1643 while (kid && OpHAS_SIBLING(kid))
1644 kid = OpSIBLING(kid);
1646 OpLASTSIB_set(kid, (OP*)logop);
1651 /* Contextualizers */
1654 =for apidoc op_contextualize
1656 Applies a syntactic context to an op tree representing an expression.
1657 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1658 or C<G_VOID> to specify the context to apply. The modified op tree
1665 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1667 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1669 case G_SCALAR: return scalar(o);
1670 case G_ARRAY: return list(o);
1671 case G_VOID: return scalarvoid(o);
1673 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1680 =for apidoc op_linklist
1681 This function is the implementation of the L</LINKLIST> macro. It should
1682 not be called directly.
1689 Perl_op_linklist(pTHX_ OP *o)
1696 PERL_ARGS_ASSERT_OP_LINKLIST;
1699 /* Descend down the tree looking for any unprocessed subtrees to
1702 if (o->op_flags & OPf_KIDS) {
1703 o = cUNOPo->op_first;
1706 o->op_next = o; /* leaf node; link to self initially */
1709 /* if we're at the top level, there either weren't any children
1710 * to process, or we've worked our way back to the top. */
1714 /* o is now processed. Next, process any sibling subtrees */
1716 if (OpHAS_SIBLING(o)) {
1721 /* Done all the subtrees at this level. Go back up a level and
1722 * link the parent in with all its (processed) children.
1725 o = o->op_sibparent;
1726 assert(!o->op_next);
1727 prevp = &(o->op_next);
1728 kid = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1730 *prevp = kid->op_next;
1731 prevp = &(kid->op_next);
1732 kid = OpSIBLING(kid);
1740 S_scalarkids(pTHX_ OP *o)
1742 if (o && o->op_flags & OPf_KIDS) {
1744 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1751 S_scalarboolean(pTHX_ OP *o)
1753 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1755 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1756 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1757 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1758 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1759 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1760 if (ckWARN(WARN_SYNTAX)) {
1761 const line_t oldline = CopLINE(PL_curcop);
1763 if (PL_parser && PL_parser->copline != NOLINE) {
1764 /* This ensures that warnings are reported at the first line
1765 of the conditional, not the last. */
1766 CopLINE_set(PL_curcop, PL_parser->copline);
1768 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1769 CopLINE_set(PL_curcop, oldline);
1776 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1779 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1780 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1782 const char funny = o->op_type == OP_PADAV
1783 || o->op_type == OP_RV2AV ? '@' : '%';
1784 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1786 if (cUNOPo->op_first->op_type != OP_GV
1787 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1789 return varname(gv, funny, 0, NULL, 0, subscript_type);
1792 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1797 S_op_varname(pTHX_ const OP *o)
1799 return S_op_varname_subscript(aTHX_ o, 1);
1803 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1804 { /* or not so pretty :-) */
1805 if (o->op_type == OP_CONST) {
1807 if (SvPOK(*retsv)) {
1809 *retsv = sv_newmortal();
1810 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1811 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1813 else if (!SvOK(*retsv))
1816 else *retpv = "...";
1820 S_scalar_slice_warning(pTHX_ const OP *o)
1823 const bool h = o->op_type == OP_HSLICE
1824 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
1830 SV *keysv = NULL; /* just to silence compiler warnings */
1831 const char *key = NULL;
1833 if (!(o->op_private & OPpSLICEWARNING))
1835 if (PL_parser && PL_parser->error_count)
1836 /* This warning can be nonsensical when there is a syntax error. */
1839 kid = cLISTOPo->op_first;
1840 kid = OpSIBLING(kid); /* get past pushmark */
1841 /* weed out false positives: any ops that can return lists */
1842 switch (kid->op_type) {
1868 /* Don't warn if we have a nulled list either. */
1869 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1872 assert(OpSIBLING(kid));
1873 name = S_op_varname(aTHX_ OpSIBLING(kid));
1874 if (!name) /* XS module fiddling with the op tree */
1876 S_op_pretty(aTHX_ kid, &keysv, &key);
1877 assert(SvPOK(name));
1878 sv_chop(name,SvPVX(name)+1);
1880 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1881 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1882 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1884 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1885 lbrack, key, rbrack);
1887 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1888 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1889 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1891 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1892 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1897 /* apply scalar context to the o subtree */
1900 Perl_scalar(pTHX_ OP *o)
1905 OP *next_kid = NULL; /* what op (if any) to process next */
1908 /* assumes no premature commitment */
1909 if (!o || (PL_parser && PL_parser->error_count)
1910 || (o->op_flags & OPf_WANT)
1911 || o->op_type == OP_RETURN)
1916 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1918 switch (o->op_type) {
1920 scalar(cBINOPo->op_first);
1921 /* convert what initially looked like a list repeat into a
1922 * scalar repeat, e.g. $s = (1) x $n
1924 if (o->op_private & OPpREPEAT_DOLIST) {
1925 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1926 assert(kid->op_type == OP_PUSHMARK);
1927 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1928 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1929 o->op_private &=~ OPpREPEAT_DOLIST;
1937 /* impose scalar context on everything except the condition */
1938 next_kid = OpSIBLING(cUNOPo->op_first);
1942 if (o->op_flags & OPf_KIDS)
1943 next_kid = cUNOPo->op_first; /* do all kids */
1946 /* the children of these ops are usually a list of statements,
1947 * except the leaves, whose first child is a corresponding enter
1952 kid = cLISTOPo->op_first;
1956 kid = cLISTOPo->op_first;
1958 kid = OpSIBLING(kid);
1961 OP *sib = OpSIBLING(kid);
1962 /* Apply void context to all kids except the last, which
1963 * is scalar (ignoring a trailing ex-nextstate in determining
1964 * if it's the last kid). E.g.
1965 * $scalar = do { void; void; scalar }
1966 * Except that 'when's are always scalar, e.g.
1967 * $scalar = do { given(..) {
1968 * when (..) { scalar }
1969 * when (..) { scalar }
1974 || ( !OpHAS_SIBLING(sib)
1975 && sib->op_type == OP_NULL
1976 && ( sib->op_targ == OP_NEXTSTATE
1977 || sib->op_targ == OP_DBSTATE )
1981 /* tail call optimise calling scalar() on the last kid */
1985 else if (kid->op_type == OP_LEAVEWHEN)
1991 NOT_REACHED; /* NOTREACHED */
1995 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
2001 /* Warn about scalar context */
2002 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
2003 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
2006 const char *key = NULL;
2008 /* This warning can be nonsensical when there is a syntax error. */
2009 if (PL_parser && PL_parser->error_count)
2012 if (!ckWARN(WARN_SYNTAX)) break;
2014 kid = cLISTOPo->op_first;
2015 kid = OpSIBLING(kid); /* get past pushmark */
2016 assert(OpSIBLING(kid));
2017 name = S_op_varname(aTHX_ OpSIBLING(kid));
2018 if (!name) /* XS module fiddling with the op tree */
2020 S_op_pretty(aTHX_ kid, &keysv, &key);
2021 assert(SvPOK(name));
2022 sv_chop(name,SvPVX(name)+1);
2024 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
2025 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2026 "%%%" SVf "%c%s%c in scalar context better written "
2027 "as $%" SVf "%c%s%c",
2028 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
2029 lbrack, key, rbrack);
2031 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
2032 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2033 "%%%" SVf "%c%" SVf "%c in scalar context better "
2034 "written as $%" SVf "%c%" SVf "%c",
2035 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
2036 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
2040 /* If next_kid is set, someone in the code above wanted us to process
2041 * that kid and all its remaining siblings. Otherwise, work our way
2042 * back up the tree */
2046 return top_op; /* at top; no parents/siblings to try */
2047 if (OpHAS_SIBLING(o))
2048 next_kid = o->op_sibparent;
2050 o = o->op_sibparent; /*try parent's next sibling */
2051 switch (o->op_type) {
2057 /* should really restore PL_curcop to its old value, but
2058 * setting it to PL_compiling is better than do nothing */
2059 PL_curcop = &PL_compiling;
2068 /* apply void context to the optree arg */
2071 Perl_scalarvoid(pTHX_ OP *arg)
2078 PERL_ARGS_ASSERT_SCALARVOID;
2082 SV *useless_sv = NULL;
2083 const char* useless = NULL;
2084 OP * next_kid = NULL;
2086 if (o->op_type == OP_NEXTSTATE
2087 || o->op_type == OP_DBSTATE
2088 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
2089 || o->op_targ == OP_DBSTATE)))
2090 PL_curcop = (COP*)o; /* for warning below */
2092 /* assumes no premature commitment */
2093 want = o->op_flags & OPf_WANT;
2094 if ((want && want != OPf_WANT_SCALAR)
2095 || (PL_parser && PL_parser->error_count)
2096 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
2101 if ((o->op_private & OPpTARGET_MY)
2102 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2104 /* newASSIGNOP has already applied scalar context, which we
2105 leave, as if this op is inside SASSIGN. */
2109 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
2111 switch (o->op_type) {
2113 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
2117 if (o->op_flags & OPf_STACKED)
2119 if (o->op_type == OP_REPEAT)
2120 scalar(cBINOPo->op_first);
2123 if ((o->op_flags & OPf_STACKED) &&
2124 !(o->op_private & OPpCONCAT_NESTED))
2128 if (o->op_private == 4)
2163 case OP_GETSOCKNAME:
2164 case OP_GETPEERNAME:
2169 case OP_GETPRIORITY:
2194 useless = OP_DESC(o);
2204 case OP_AELEMFAST_LEX:
2208 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
2209 /* Otherwise it's "Useless use of grep iterator" */
2210 useless = OP_DESC(o);
2214 if (!(o->op_private & OPpSPLIT_ASSIGN))
2215 useless = OP_DESC(o);
2219 kid = cUNOPo->op_first;
2220 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2221 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2224 useless = "negative pattern binding (!~)";
2228 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2229 useless = "non-destructive substitution (s///r)";
2233 useless = "non-destructive transliteration (tr///r)";
2240 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2241 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2242 useless = "a variable";
2247 if (cSVOPo->op_private & OPpCONST_STRICT)
2248 no_bareword_allowed(o);
2250 if (ckWARN(WARN_VOID)) {
2252 /* don't warn on optimised away booleans, eg
2253 * use constant Foo, 5; Foo || print; */
2254 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2256 /* the constants 0 and 1 are permitted as they are
2257 conventionally used as dummies in constructs like
2258 1 while some_condition_with_side_effects; */
2259 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2261 else if (SvPOK(sv)) {
2262 SV * const dsv = newSVpvs("");
2264 = Perl_newSVpvf(aTHX_
2266 pv_pretty(dsv, SvPVX_const(sv),
2267 SvCUR(sv), 32, NULL, NULL,
2269 | PERL_PV_ESCAPE_NOCLEAR
2270 | PERL_PV_ESCAPE_UNI_DETECT));
2271 SvREFCNT_dec_NN(dsv);
2273 else if (SvOK(sv)) {
2274 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2277 useless = "a constant (undef)";
2280 op_null(o); /* don't execute or even remember it */
2284 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2288 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2292 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2296 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2301 UNOP *refgen, *rv2cv;
2304 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2307 rv2gv = ((BINOP *)o)->op_last;
2308 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2311 refgen = (UNOP *)((BINOP *)o)->op_first;
2313 if (!refgen || (refgen->op_type != OP_REFGEN
2314 && refgen->op_type != OP_SREFGEN))
2317 exlist = (LISTOP *)refgen->op_first;
2318 if (!exlist || exlist->op_type != OP_NULL
2319 || exlist->op_targ != OP_LIST)
2322 if (exlist->op_first->op_type != OP_PUSHMARK
2323 && exlist->op_first != exlist->op_last)
2326 rv2cv = (UNOP*)exlist->op_last;
2328 if (rv2cv->op_type != OP_RV2CV)
2331 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2332 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2333 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2335 o->op_private |= OPpASSIGN_CV_TO_GV;
2336 rv2gv->op_private |= OPpDONT_INIT_GV;
2337 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2349 kid = cLOGOPo->op_first;
2350 if (kid->op_type == OP_NOT
2351 && (kid->op_flags & OPf_KIDS)) {
2352 if (o->op_type == OP_AND) {
2353 OpTYPE_set(o, OP_OR);
2355 OpTYPE_set(o, OP_AND);
2365 next_kid = OpSIBLING(cUNOPo->op_first);
2369 if (o->op_flags & OPf_STACKED)
2376 if (!(o->op_flags & OPf_KIDS))
2387 next_kid = cLISTOPo->op_first;
2390 /* If the first kid after pushmark is something that the padrange
2391 optimisation would reject, then null the list and the pushmark.
2393 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2394 && ( !(kid = OpSIBLING(kid))
2395 || ( kid->op_type != OP_PADSV
2396 && kid->op_type != OP_PADAV
2397 && kid->op_type != OP_PADHV)
2398 || kid->op_private & ~OPpLVAL_INTRO
2399 || !(kid = OpSIBLING(kid))
2400 || ( kid->op_type != OP_PADSV
2401 && kid->op_type != OP_PADAV
2402 && kid->op_type != OP_PADHV)
2403 || kid->op_private & ~OPpLVAL_INTRO)
2405 op_null(cUNOPo->op_first); /* NULL the pushmark */
2406 op_null(o); /* NULL the list */
2418 /* mortalise it, in case warnings are fatal. */
2419 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2420 "Useless use of %" SVf " in void context",
2421 SVfARG(sv_2mortal(useless_sv)));
2424 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2425 "Useless use of %s in void context",
2430 /* if a kid hasn't been nominated to process, continue with the
2431 * next sibling, or if no siblings left, go back to the parent's
2432 * siblings and so on
2436 return arg; /* at top; no parents/siblings to try */
2437 if (OpHAS_SIBLING(o))
2438 next_kid = o->op_sibparent;
2440 o = o->op_sibparent; /*try parent's next sibling */
2450 S_listkids(pTHX_ OP *o)
2452 if (o && o->op_flags & OPf_KIDS) {
2454 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2461 /* apply list context to the o subtree */
2464 Perl_list(pTHX_ OP *o)
2469 OP *next_kid = NULL; /* what op (if any) to process next */
2473 /* assumes no premature commitment */
2474 if (!o || (o->op_flags & OPf_WANT)
2475 || (PL_parser && PL_parser->error_count)
2476 || o->op_type == OP_RETURN)
2481 if ((o->op_private & OPpTARGET_MY)
2482 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2484 goto do_next; /* As if inside SASSIGN */
2487 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2489 switch (o->op_type) {
2491 if (o->op_private & OPpREPEAT_DOLIST
2492 && !(o->op_flags & OPf_STACKED))
2494 list(cBINOPo->op_first);
2495 kid = cBINOPo->op_last;
2496 /* optimise away (.....) x 1 */
2497 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2498 && SvIVX(kSVOP_sv) == 1)
2500 op_null(o); /* repeat */
2501 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2503 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2511 /* impose list context on everything except the condition */
2512 next_kid = OpSIBLING(cUNOPo->op_first);
2516 if (!(o->op_flags & OPf_KIDS))
2518 /* possibly flatten 1..10 into a constant array */
2519 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2520 list(cBINOPo->op_first);
2521 gen_constant_list(o);
2524 next_kid = cUNOPo->op_first; /* do all kids */
2528 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2529 op_null(cUNOPo->op_first); /* NULL the pushmark */
2530 op_null(o); /* NULL the list */
2532 if (o->op_flags & OPf_KIDS)
2533 next_kid = cUNOPo->op_first; /* do all kids */
2536 /* the children of these ops are usually a list of statements,
2537 * except the leaves, whose first child is a corresponding enter
2541 kid = cLISTOPo->op_first;
2545 kid = cLISTOPo->op_first;
2547 kid = OpSIBLING(kid);
2550 OP *sib = OpSIBLING(kid);
2551 /* Apply void context to all kids except the last, which
2553 * @a = do { void; void; list }
2554 * Except that 'when's are always list context, e.g.
2555 * @a = do { given(..) {
2556 * when (..) { list }
2557 * when (..) { list }
2562 /* tail call optimise calling list() on the last kid */
2566 else if (kid->op_type == OP_LEAVEWHEN)
2572 NOT_REACHED; /* NOTREACHED */
2577 /* If next_kid is set, someone in the code above wanted us to process
2578 * that kid and all its remaining siblings. Otherwise, work our way
2579 * back up the tree */
2583 return top_op; /* at top; no parents/siblings to try */
2584 if (OpHAS_SIBLING(o))
2585 next_kid = o->op_sibparent;
2587 o = o->op_sibparent; /*try parent's next sibling */
2588 switch (o->op_type) {
2594 /* should really restore PL_curcop to its old value, but
2595 * setting it to PL_compiling is better than do nothing */
2596 PL_curcop = &PL_compiling;
2608 S_scalarseq(pTHX_ OP *o)
2611 const OPCODE type = o->op_type;
2613 if (type == OP_LINESEQ || type == OP_SCOPE ||
2614 type == OP_LEAVE || type == OP_LEAVETRY)
2617 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2618 if ((sib = OpSIBLING(kid))
2619 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2620 || ( sib->op_targ != OP_NEXTSTATE
2621 && sib->op_targ != OP_DBSTATE )))
2626 PL_curcop = &PL_compiling;
2628 o->op_flags &= ~OPf_PARENS;
2629 if (PL_hints & HINT_BLOCK_SCOPE)
2630 o->op_flags |= OPf_PARENS;
2633 o = newOP(OP_STUB, 0);
2638 S_modkids(pTHX_ OP *o, I32 type)
2640 if (o && o->op_flags & OPf_KIDS) {
2642 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2643 op_lvalue(kid, type);
2649 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2650 * const fields. Also, convert CONST keys to HEK-in-SVs.
2651 * rop is the op that retrieves the hash;
2652 * key_op is the first key
2653 * real if false, only check (and possibly croak); don't update op
2657 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real)
2663 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2665 if (rop->op_first->op_type == OP_PADSV)
2666 /* @$hash{qw(keys here)} */
2667 rop = (UNOP*)rop->op_first;
2669 /* @{$hash}{qw(keys here)} */
2670 if (rop->op_first->op_type == OP_SCOPE
2671 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2673 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2680 lexname = NULL; /* just to silence compiler warnings */
2681 fields = NULL; /* just to silence compiler warnings */
2685 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2686 SvPAD_TYPED(lexname))
2687 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2688 && isGV(*fields) && GvHV(*fields);
2690 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2692 if (key_op->op_type != OP_CONST)
2694 svp = cSVOPx_svp(key_op);
2696 /* make sure it's not a bareword under strict subs */
2697 if (key_op->op_private & OPpCONST_BARE &&
2698 key_op->op_private & OPpCONST_STRICT)
2700 no_bareword_allowed((OP*)key_op);
2703 /* Make the CONST have a shared SV */
2704 if ( !SvIsCOW_shared_hash(sv = *svp)
2705 && SvTYPE(sv) < SVt_PVMG
2711 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2712 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2713 SvREFCNT_dec_NN(sv);
2718 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2720 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2721 "in variable %" PNf " of type %" HEKf,
2722 SVfARG(*svp), PNfARG(lexname),
2723 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2728 /* info returned by S_sprintf_is_multiconcatable() */
2730 struct sprintf_ismc_info {
2731 SSize_t nargs; /* num of args to sprintf (not including the format) */
2732 char *start; /* start of raw format string */
2733 char *end; /* bytes after end of raw format string */
2734 STRLEN total_len; /* total length (in bytes) of format string, not
2735 including '%s' and half of '%%' */
2736 STRLEN variant; /* number of bytes by which total_len_p would grow
2737 if upgraded to utf8 */
2738 bool utf8; /* whether the format is utf8 */
2742 /* is the OP_SPRINTF o suitable for converting into a multiconcat op?
2743 * i.e. its format argument is a const string with only '%s' and '%%'
2744 * formats, and the number of args is known, e.g.
2745 * sprintf "a=%s f=%s", $a[0], scalar(f());
2747 * sprintf "i=%d a=%s f=%s", $i, @a, f();
2749 * If successful, the sprintf_ismc_info struct pointed to by info will be
2754 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info)
2756 OP *pm, *constop, *kid;
2759 SSize_t nargs, nformats;
2760 STRLEN cur, total_len, variant;
2763 /* if sprintf's behaviour changes, die here so that someone
2764 * can decide whether to enhance this function or skip optimising
2765 * under those new circumstances */
2766 assert(!(o->op_flags & OPf_STACKED));
2767 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX));
2768 assert(!(o->op_private & ~OPpARG4_MASK));
2770 pm = cUNOPo->op_first;
2771 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */
2773 constop = OpSIBLING(pm);
2774 if (!constop || constop->op_type != OP_CONST)
2776 sv = cSVOPx_sv(constop);
2777 if (SvMAGICAL(sv) || !SvPOK(sv))
2783 /* Scan format for %% and %s and work out how many %s there are.
2784 * Abandon if other format types are found.
2791 for (p = s; p < e; p++) {
2794 if (!UTF8_IS_INVARIANT(*p))
2800 return FALSE; /* lone % at end gives "Invalid conversion" */
2809 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG)
2812 utf8 = cBOOL(SvUTF8(sv));
2816 /* scan args; they must all be in scalar cxt */
2819 kid = OpSIBLING(constop);
2822 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
2825 kid = OpSIBLING(kid);
2828 if (nargs != nformats)
2829 return FALSE; /* e.g. sprintf("%s%s", $a); */
2832 info->nargs = nargs;
2835 info->total_len = total_len;
2836 info->variant = variant;
2844 /* S_maybe_multiconcat():
2846 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly
2847 * convert it (and its children) into an OP_MULTICONCAT. See the code
2848 * comments just before pp_multiconcat() for the full details of what
2849 * OP_MULTICONCAT supports.
2851 * Basically we're looking for an optree with a chain of OP_CONCATS down
2852 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or
2853 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g.
2861 * STRINGIFY -- PADSV[$x]
2864 * ex-PUSHMARK -- CONCAT/S
2866 * CONCAT/S -- PADSV[$d]
2868 * CONCAT -- CONST["-"]
2870 * PADSV[$a] -- PADSV[$b]
2872 * Note that at this stage the OP_SASSIGN may have already been optimised
2873 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT.
2877 S_maybe_multiconcat(pTHX_ OP *o)
2880 OP *lastkidop; /* the right-most of any kids unshifted onto o */
2881 OP *topop; /* the top-most op in the concat tree (often equals o,
2882 unless there are assign/stringify ops above it */
2883 OP *parentop; /* the parent op of topop (or itself if no parent) */
2884 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */
2885 OP *targetop; /* the op corresponding to target=... or target.=... */
2886 OP *stringop; /* the OP_STRINGIFY op, if any */
2887 OP *nextop; /* used for recreating the op_next chain without consts */
2888 OP *kid; /* general-purpose op pointer */
2890 UNOP_AUX_item *lenp;
2891 char *const_str, *p;
2892 struct sprintf_ismc_info sprintf_info;
2894 /* store info about each arg in args[];
2895 * toparg is the highest used slot; argp is a general
2896 * pointer to args[] slots */
2898 void *p; /* initially points to const sv (or null for op);
2899 later, set to SvPV(constsv), with ... */
2900 STRLEN len; /* ... len set to SvPV(..., len) */
2901 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1];
2905 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */
2908 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op;
2909 the last-processed arg will the LHS of one,
2910 as args are processed in reverse order */
2911 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */
2912 STRLEN total_len = 0; /* sum of the lengths of the const segments */
2913 U8 flags = 0; /* what will become the op_flags and ... */
2914 U8 private_flags = 0; /* ... op_private of the multiconcat op */
2915 bool is_sprintf = FALSE; /* we're optimising an sprintf */
2916 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */
2917 bool prev_was_const = FALSE; /* previous arg was a const */
2919 /* -----------------------------------------------------------------
2922 * Examine the optree non-destructively to determine whether it's
2923 * suitable to be converted into an OP_MULTICONCAT. Accumulate
2924 * information about the optree in args[].
2934 assert( o->op_type == OP_SASSIGN
2935 || o->op_type == OP_CONCAT
2936 || o->op_type == OP_SPRINTF
2937 || o->op_type == OP_STRINGIFY);
2939 Zero(&sprintf_info, 1, struct sprintf_ismc_info);
2941 /* first see if, at the top of the tree, there is an assign,
2942 * append and/or stringify */
2944 if (topop->op_type == OP_SASSIGN) {
2946 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN])
2948 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
2950 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */
2953 topop = cBINOPo->op_first;
2954 targetop = OpSIBLING(topop);
2955 if (!targetop) /* probably some sort of syntax error */
2958 /* don't optimise away assign in 'local $foo = ....' */
2959 if ( (targetop->op_private & OPpLVAL_INTRO)
2960 /* these are the common ops which do 'local', but
2962 && ( targetop->op_type == OP_GVSV
2963 || targetop->op_type == OP_RV2SV
2964 || targetop->op_type == OP_AELEM
2965 || targetop->op_type == OP_HELEM
2970 else if ( topop->op_type == OP_CONCAT
2971 && (topop->op_flags & OPf_STACKED)
2972 && (!(topop->op_private & OPpCONCAT_NESTED))
2977 /* OPpTARGET_MY shouldn't be able to be set here. If it is,
2978 * decide what to do about it */
2979 assert(!(o->op_private & OPpTARGET_MY));
2981 /* barf on unknown flags */
2982 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY)));
2983 private_flags |= OPpMULTICONCAT_APPEND;
2984 targetop = cBINOPo->op_first;
2986 topop = OpSIBLING(targetop);
2988 /* $x .= <FOO> gets optimised to rcatline instead */
2989 if (topop->op_type == OP_READLINE)
2994 /* Can targetop (the LHS) if it's a padsv, be be optimised
2995 * away and use OPpTARGET_MY instead?
2997 if ( (targetop->op_type == OP_PADSV)
2998 && !(targetop->op_private & OPpDEREF)
2999 && !(targetop->op_private & OPpPAD_STATE)
3000 /* we don't support 'my $x .= ...' */
3001 && ( o->op_type == OP_SASSIGN
3002 || !(targetop->op_private & OPpLVAL_INTRO))
3007 if (topop->op_type == OP_STRINGIFY) {
3008 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY])
3012 /* barf on unknown flags */
3013 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY)));
3015 if ((topop->op_private & OPpTARGET_MY)) {
3016 if (o->op_type == OP_SASSIGN)
3017 return; /* can't have two assigns */
3021 private_flags |= OPpMULTICONCAT_STRINGIFY;
3023 topop = cBINOPx(topop)->op_first;
3024 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK));
3025 topop = OpSIBLING(topop);
3028 if (topop->op_type == OP_SPRINTF) {
3029 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF])
3031 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) {
3032 nargs = sprintf_info.nargs;
3033 total_len = sprintf_info.total_len;
3034 variant = sprintf_info.variant;
3035 utf8 = sprintf_info.utf8;
3037 private_flags |= OPpMULTICONCAT_FAKE;
3039 /* we have an sprintf op rather than a concat optree.
3040 * Skip most of the code below which is associated with
3041 * processing that optree. We also skip phase 2, determining
3042 * whether its cost effective to optimise, since for sprintf,
3043 * multiconcat is *always* faster */
3046 /* note that even if the sprintf itself isn't multiconcatable,
3047 * the expression as a whole may be, e.g. in
3048 * $x .= sprintf("%d",...)
3049 * the sprintf op will be left as-is, but the concat/S op may
3050 * be upgraded to multiconcat
3053 else if (topop->op_type == OP_CONCAT) {
3054 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT])
3057 if ((topop->op_private & OPpTARGET_MY)) {
3058 if (o->op_type == OP_SASSIGN || targmyop)
3059 return; /* can't have two assigns */
3064 /* Is it safe to convert a sassign/stringify/concat op into
3066 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP);
3067 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP);
3068 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP);
3069 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP);
3070 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last)
3071 == STRUCT_OFFSET(UNOP_AUX, op_aux));
3072 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last)
3073 == STRUCT_OFFSET(UNOP_AUX, op_aux));
3075 /* Now scan the down the tree looking for a series of
3076 * CONCAT/OPf_STACKED ops on the LHS (with the last one not
3077 * stacked). For example this tree:
3082 * CONCAT/STACKED -- EXPR5
3084 * CONCAT/STACKED -- EXPR4
3090 * corresponds to an expression like
3092 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5)
3094 * Record info about each EXPR in args[]: in particular, whether it is
3095 * a stringifiable OP_CONST and if so what the const sv is.
3097 * The reason why the last concat can't be STACKED is the difference
3100 * ((($a .= $a) .= $a) .= $a) .= $a
3103 * $a . $a . $a . $a . $a
3105 * The main difference between the optrees for those two constructs
3106 * is the presence of the last STACKED. As well as modifying $a,
3107 * the former sees the changed $a between each concat, so if $s is
3108 * initially 'a', the first returns 'a' x 16, while the latter returns
3109 * 'a' x 5. And pp_multiconcat can't handle that kind of thing.
3119 if ( kid->op_type == OP_CONCAT
3123 k1 = cUNOPx(kid)->op_first;
3125 /* shouldn't happen except maybe after compile err? */
3129 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */
3130 if (kid->op_private & OPpTARGET_MY)
3133 stacked_last = (kid->op_flags & OPf_STACKED);
3145 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2
3146 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2)
3148 /* At least two spare slots are needed to decompose both
3149 * concat args. If there are no slots left, continue to
3150 * examine the rest of the optree, but don't push new values
3151 * on args[]. If the optree as a whole is legal for conversion
3152 * (in particular that the last concat isn't STACKED), then
3153 * the first PERL_MULTICONCAT_MAXARG elements of the optree
3154 * can be converted into an OP_MULTICONCAT now, with the first
3155 * child of that op being the remainder of the optree -
3156 * which may itself later be converted to a multiconcat op
3160 /* the last arg is the rest of the optree */
3165 else if ( argop->op_type == OP_CONST
3166 && ((sv = cSVOPx_sv(argop)))
3167 /* defer stringification until runtime of 'constant'
3168 * things that might stringify variantly, e.g. the radix
3169 * point of NVs, or overloaded RVs */
3170 && (SvPOK(sv) || SvIOK(sv))
3171 && (!SvGMAGICAL(sv))
3173 if (argop->op_private & OPpCONST_STRICT)
3174 no_bareword_allowed(argop);
3176 utf8 |= cBOOL(SvUTF8(sv));
3179 /* this const may be demoted back to a plain arg later;
3180 * make sure we have enough arg slots left */
3182 prev_was_const = !prev_was_const;
3187 prev_was_const = FALSE;
3197 return; /* we don't support ((A.=B).=C)...) */
3199 /* look for two adjacent consts and don't fold them together:
3202 * $o->concat("a")->concat("b")
3205 * (but $o .= "a" . "b" should still fold)
3208 bool seen_nonconst = FALSE;
3209 for (argp = toparg; argp >= args; argp--) {
3210 if (argp->p == NULL) {
3211 seen_nonconst = TRUE;
3217 /* both previous and current arg were constants;
3218 * leave the current OP_CONST as-is */
3226 /* -----------------------------------------------------------------
3229 * At this point we have determined that the optree *can* be converted
3230 * into a multiconcat. Having gathered all the evidence, we now decide
3231 * whether it *should*.
3235 /* we need at least one concat action, e.g.:
3241 * otherwise we could be doing something like $x = "foo", which
3242 * if treated as as a concat, would fail to COW.
3244 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2)
3247 /* Benchmarking seems to indicate that we gain if:
3248 * * we optimise at least two actions into a single multiconcat
3249 * (e.g concat+concat, sassign+concat);
3250 * * or if we can eliminate at least 1 OP_CONST;
3251 * * or if we can eliminate a padsv via OPpTARGET_MY
3255 /* eliminated at least one OP_CONST */
3257 /* eliminated an OP_SASSIGN */
3258 || o->op_type == OP_SASSIGN
3259 /* eliminated an OP_PADSV */
3260 || (!targmyop && is_targable)
3262 /* definitely a net gain to optimise */
3265 /* ... if not, what else? */
3267 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1):
3268 * multiconcat is faster (due to not creating a temporary copy of
3269 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is
3275 && topop->op_type == OP_CONCAT
3277 PADOFFSET t = targmyop->op_targ;
3278 OP *k1 = cBINOPx(topop)->op_first;
3279 OP *k2 = cBINOPx(topop)->op_last;
3280 if ( k2->op_type == OP_PADSV
3282 && ( k1->op_type != OP_PADSV
3283 || k1->op_targ != t)
3288 /* need at least two concats */
3289 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3)
3294 /* -----------------------------------------------------------------
3297 * At this point the optree has been verified as ok to be optimised
3298 * into an OP_MULTICONCAT. Now start changing things.
3303 /* stringify all const args and determine utf8ness */
3306 for (argp = args; argp <= toparg; argp++) {
3307 SV *sv = (SV*)argp->p;
3309 continue; /* not a const op */
3310 if (utf8 && !SvUTF8(sv))
3311 sv_utf8_upgrade_nomg(sv);
3312 argp->p = SvPV_nomg(sv, argp->len);
3313 total_len += argp->len;
3315 /* see if any strings would grow if converted to utf8 */
3317 variant += variant_under_utf8_count((U8 *) argp->p,
3318 (U8 *) argp->p + argp->len);
3322 /* create and populate aux struct */
3326 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
3327 sizeof(UNOP_AUX_item)
3329 PERL_MULTICONCAT_HEADER_SIZE
3330 + ((nargs + 1) * (variant ? 2 : 1))
3333 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1);
3335 /* Extract all the non-const expressions from the concat tree then
3336 * dispose of the old tree, e.g. convert the tree from this:
3340 * STRINGIFY -- TARGET
3342 * ex-PUSHMARK -- CONCAT
3357 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET
3359 * except that if EXPRi is an OP_CONST, it's discarded.
3361 * During the conversion process, EXPR ops are stripped from the tree
3362 * and unshifted onto o. Finally, any of o's remaining original
3363 * childen are discarded and o is converted into an OP_MULTICONCAT.
3365 * In this middle of this, o may contain both: unshifted args on the
3366 * left, and some remaining original args on the right. lastkidop
3367 * is set to point to the right-most unshifted arg to delineate
3368 * between the two sets.
3373 /* create a copy of the format with the %'s removed, and record
3374 * the sizes of the const string segments in the aux struct */
3376 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3378 p = sprintf_info.start;
3381 for (; p < sprintf_info.end; p++) {
3385 (lenp++)->ssize = q - oldq;
3392 lenp->ssize = q - oldq;
3393 assert((STRLEN)(q - const_str) == total_len);
3395 /* Attach all the args (i.e. the kids of the sprintf) to o (which
3396 * may or may not be topop) The pushmark and const ops need to be
3397 * kept in case they're an op_next entry point.
3399 lastkidop = cLISTOPx(topop)->op_last;
3400 kid = cUNOPx(topop)->op_first; /* pushmark */
3402 op_null(OpSIBLING(kid)); /* const */
3404 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */
3405 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */
3406 lastkidop->op_next = o;
3411 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3415 /* Concatenate all const strings into const_str.
3416 * Note that args[] contains the RHS args in reverse order, so
3417 * we scan args[] from top to bottom to get constant strings
3420 for (argp = toparg; argp >= args; argp--) {
3422 /* not a const op */
3423 (++lenp)->ssize = -1;
3425 STRLEN l = argp->len;
3426 Copy(argp->p, p, l, char);
3428 if (lenp->ssize == -1)
3439 for (argp = args; argp <= toparg; argp++) {
3440 /* only keep non-const args, except keep the first-in-next-chain
3441 * arg no matter what it is (but nulled if OP_CONST), because it
3442 * may be the entry point to this subtree from the previous
3445 bool last = (argp == toparg);
3448 /* set prev to the sibling *before* the arg to be cut out,
3449 * e.g. when cutting EXPR:
3454 * prev= CONCAT -- EXPR
3457 if (argp == args && kid->op_type != OP_CONCAT) {
3458 /* in e.g. '$x .= f(1)' there's no RHS concat tree
3459 * so the expression to be cut isn't kid->op_last but
3462 /* find the op before kid */
3464 o2 = cUNOPx(parentop)->op_first;
3465 while (o2 && o2 != kid) {
3473 else if (kid == o && lastkidop)
3474 prev = last ? lastkidop : OpSIBLING(lastkidop);
3476 prev = last ? NULL : cUNOPx(kid)->op_first;
3478 if (!argp->p || last) {
3480 OP *aop = op_sibling_splice(kid, prev, 1, NULL);
3481 /* and unshift to front of o */
3482 op_sibling_splice(o, NULL, 0, aop);
3483 /* record the right-most op added to o: later we will
3484 * free anything to the right of it */
3487 aop->op_next = nextop;
3490 /* null the const at start of op_next chain */
3494 nextop = prev->op_next;
3497 /* the last two arguments are both attached to the same concat op */
3498 if (argp < toparg - 1)
3503 /* Populate the aux struct */
3505 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs;
3506 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str;
3507 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len;
3508 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str;
3509 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len;
3511 /* if variant > 0, calculate a variant const string and lengths where
3512 * the utf8 version of the string will take 'variant' more bytes than
3516 char *p = const_str;
3517 STRLEN ulen = total_len + variant;
3518 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
3519 UNOP_AUX_item *ulens = lens + (nargs + 1);
3520 char *up = (char*)PerlMemShared_malloc(ulen);
3523 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up;
3524 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen;
3526 for (n = 0; n < (nargs + 1); n++) {
3528 char * orig_up = up;
3529 for (i = (lens++)->ssize; i > 0; i--) {
3531 append_utf8_from_native_byte(c, (U8**)&up);
3533 (ulens++)->ssize = (i < 0) ? i : up - orig_up;
3538 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep
3539 * that op's first child - an ex-PUSHMARK - because the op_next of
3540 * the previous op may point to it (i.e. it's the entry point for
3545 ? op_sibling_splice(o, lastkidop, 1, NULL)
3546 : op_sibling_splice(stringop, NULL, 1, NULL);
3547 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK));
3548 op_sibling_splice(o, NULL, 0, pmop);
3555 * target .= A.B.C...
3561 if (o->op_type == OP_SASSIGN) {
3562 /* Move the target subtree from being the last of o's children
3563 * to being the last of o's preserved children.
3564 * Note the difference between 'target = ...' and 'target .= ...':
3565 * for the former, target is executed last; for the latter,
3568 kid = OpSIBLING(lastkidop);
3569 op_sibling_splice(o, kid, 1, NULL); /* cut target op */
3570 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */
3571 lastkidop->op_next = kid->op_next;
3572 lastkidop = targetop;
3575 /* Move the target subtree from being the first of o's
3576 * original children to being the first of *all* o's children.
3579 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */
3580 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/
3583 /* if the RHS of .= doesn't contain a concat (e.g.
3584 * $x .= "foo"), it gets missed by the "strip ops from the
3585 * tree and add to o" loop earlier */
3586 assert(topop->op_type != OP_CONCAT);
3588 /* in e.g. $x .= "$y", move the $y expression
3589 * from being a child of OP_STRINGIFY to being the
3590 * second child of the OP_CONCAT
3592 assert(cUNOPx(stringop)->op_first == topop);
3593 op_sibling_splice(stringop, NULL, 1, NULL);
3594 op_sibling_splice(o, cUNOPo->op_first, 0, topop);
3596 assert(topop == OpSIBLING(cBINOPo->op_first));
3605 * my $lex = A.B.C...
3608 * The original padsv op is kept but nulled in case it's the
3609 * entry point for the optree (which it will be for
3612 private_flags |= OPpTARGET_MY;
3613 private_flags |= (targetop->op_private & OPpLVAL_INTRO);
3614 o->op_targ = targetop->op_targ;
3615 targetop->op_targ = 0;
3619 flags |= OPf_STACKED;
3621 else if (targmyop) {
3622 private_flags |= OPpTARGET_MY;
3623 if (o != targmyop) {
3624 o->op_targ = targmyop->op_targ;
3625 targmyop->op_targ = 0;
3629 /* detach the emaciated husk of the sprintf/concat optree and free it */
3631 kid = op_sibling_splice(o, lastkidop, 1, NULL);
3637 /* and convert o into a multiconcat */
3639 o->op_flags = (flags|OPf_KIDS|stacked_last
3640 |(o->op_flags & (OPf_WANT|OPf_PARENS)));
3641 o->op_private = private_flags;
3642 o->op_type = OP_MULTICONCAT;
3643 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT];
3644 cUNOP_AUXo->op_aux = aux;
3648 /* do all the final processing on an optree (e.g. running the peephole
3649 * optimiser on it), then attach it to cv (if cv is non-null)
3653 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
3657 /* XXX for some reason, evals, require and main optrees are
3658 * never attached to their CV; instead they just hang off
3659 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
3660 * and get manually freed when appropriate */
3662 startp = &CvSTART(cv);
3664 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
3667 optree->op_private |= OPpREFCOUNTED;
3668 OpREFCNT_set(optree, 1);
3669 optimize_optree(optree);
3671 finalize_optree(optree);
3672 S_prune_chain_head(startp);
3675 /* now that optimizer has done its work, adjust pad values */
3676 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
3677 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
3683 =for apidoc optimize_optree
3685 This function applies some optimisations to the optree in top-down order.
3686 It is called before the peephole optimizer, which processes ops in
3687 execution order. Note that finalize_optree() also does a top-down scan,
3688 but is called *after* the peephole optimizer.
3694 Perl_optimize_optree(pTHX_ OP* o)
3696 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE;
3699 SAVEVPTR(PL_curcop);
3707 /* helper for optimize_optree() which optimises one op then recurses
3708 * to optimise any children.
3712 S_optimize_op(pTHX_ OP* o)
3716 PERL_ARGS_ASSERT_OPTIMIZE_OP;
3719 OP * next_kid = NULL;
3721 assert(o->op_type != OP_FREED);
3723 switch (o->op_type) {
3726 PL_curcop = ((COP*)o); /* for warnings */
3734 S_maybe_multiconcat(aTHX_ o);
3738 if (cPMOPo->op_pmreplrootu.op_pmreplroot) {
3739 /* we can't assume that op_pmreplroot->op_sibparent == o
3740 * and that it is thus possible to walk back up the tree
3741 * past op_pmreplroot. So, although we try to avoid
3742 * recursing through op trees, do it here. After all,
3743 * there are unlikely to be many nested s///e's within
3744 * the replacement part of a s///e.
3746 optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3754 if (o->op_flags & OPf_KIDS)
3755 next_kid = cUNOPo->op_first;
3757 /* if a kid hasn't been nominated to process, continue with the
3758 * next sibling, or if no siblings left, go back to the parent's
3759 * siblings and so on
3763 return; /* at top; no parents/siblings to try */
3764 if (OpHAS_SIBLING(o))
3765 next_kid = o->op_sibparent;
3767 o = o->op_sibparent; /*try parent's next sibling */
3770 /* this label not yet used. Goto here if any code above sets
3780 =for apidoc finalize_optree
3782 This function finalizes the optree. Should be called directly after
3783 the complete optree is built. It does some additional
3784 checking which can't be done in the normal C<ck_>xxx functions and makes
3785 the tree thread-safe.
3790 Perl_finalize_optree(pTHX_ OP* o)
3792 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
3795 SAVEVPTR(PL_curcop);
3803 /* Relocate sv to the pad for thread safety.
3804 * Despite being a "constant", the SV is written to,
3805 * for reference counts, sv_upgrade() etc. */
3806 PERL_STATIC_INLINE void
3807 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
3810 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
3812 ix = pad_alloc(OP_CONST, SVf_READONLY);
3813 SvREFCNT_dec(PAD_SVl(ix));
3814 PAD_SETSV(ix, *svp);
3815 /* XXX I don't know how this isn't readonly already. */
3816 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
3823 =for apidoc traverse_op_tree
3825 Return the next op in a depth-first traversal of the op tree,
3826 returning NULL when the traversal is complete.
3828 The initial call must supply the root of the tree as both top and o.
3830 For now it's static, but it may be exposed to the API in the future.
3836 S_traverse_op_tree(pTHX_ OP *top, OP *o) {
3839 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;
3841 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {
3842 return cUNOPo->op_first;
3844 else if ((sib = OpSIBLING(o))) {
3848 OP *parent = o->op_sibparent;
3849 assert(!(o->op_moresib));
3850 while (parent && parent != top) {
3851 OP *sib = OpSIBLING(parent);
3854 parent = parent->op_sibparent;
3862 S_finalize_op(pTHX_ OP* o)
3865 PERL_ARGS_ASSERT_FINALIZE_OP;
3868 assert(o->op_type != OP_FREED);
3870 switch (o->op_type) {
3873 PL_curcop = ((COP*)o); /* for warnings */
3876 if (OpHAS_SIBLING(o)) {
3877 OP *sib = OpSIBLING(o);
3878 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
3879 && ckWARN(WARN_EXEC)
3880 && OpHAS_SIBLING(sib))
3882 const OPCODE type = OpSIBLING(sib)->op_type;
3883 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
3884 const line_t oldline = CopLINE(PL_curcop);
3885 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
3886 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3887 "Statement unlikely to be reached");
3888 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3889 "\t(Maybe you meant system() when you said exec()?)\n");
3890 CopLINE_set(PL_curcop, oldline);
3897 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
3898 GV * const gv = cGVOPo_gv;
3899 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
3900 /* XXX could check prototype here instead of just carping */
3901 SV * const sv = sv_newmortal();
3902 gv_efullname3(sv, gv, NULL);
3903 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3904 "%" SVf "() called too early to check prototype",
3911 if (cSVOPo->op_private & OPpCONST_STRICT)
3912 no_bareword_allowed(o);
3916 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
3921 /* Relocate all the METHOP's SVs to the pad for thread safety. */
3922 case OP_METHOD_NAMED:
3923 case OP_METHOD_SUPER:
3924 case OP_METHOD_REDIR:
3925 case OP_METHOD_REDIR_SUPER:
3926 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
3935 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
3938 rop = (UNOP*)((BINOP*)o)->op_first;
3943 S_scalar_slice_warning(aTHX_ o);
3947 kid = OpSIBLING(cLISTOPo->op_first);
3948 if (/* I bet there's always a pushmark... */
3949 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
3950 && OP_TYPE_ISNT_NN(kid, OP_CONST))
3955 key_op = (SVOP*)(kid->op_type == OP_CONST
3957 : OpSIBLING(kLISTOP->op_first));
3959 rop = (UNOP*)((LISTOP*)o)->op_last;
3962 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
3964 S_check_hash_fields_and_hekify(aTHX_ rop, key_op, 1);
3968 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
3972 S_scalar_slice_warning(aTHX_ o);
3976 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
3977 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3985 if (o->op_flags & OPf_KIDS) {
3988 /* check that op_last points to the last sibling, and that
3989 * the last op_sibling/op_sibparent field points back to the
3990 * parent, and that the only ops with KIDS are those which are
3991 * entitled to them */
3992 U32 type = o->op_type;
3996 if (type == OP_NULL) {
3998 /* ck_glob creates a null UNOP with ex-type GLOB
3999 * (which is a list op. So pretend it wasn't a listop */
4000 if (type == OP_GLOB)
4003 family = PL_opargs[type] & OA_CLASS_MASK;
4005 has_last = ( family == OA_BINOP
4006 || family == OA_LISTOP
4007 || family == OA_PMOP
4008 || family == OA_LOOP
4010 assert( has_last /* has op_first and op_last, or ...
4011 ... has (or may have) op_first: */
4012 || family == OA_UNOP
4013 || family == OA_UNOP_AUX
4014 || family == OA_LOGOP
4015 || family == OA_BASEOP_OR_UNOP
4016 || family == OA_FILESTATOP
4017 || family == OA_LOOPEXOP
4018 || family == OA_METHOP
4019 || type == OP_CUSTOM
4020 || type == OP_NULL /* new_logop does this */
4023 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
4024 if (!OpHAS_SIBLING(kid)) {
4026 assert(kid == cLISTOPo->op_last);
4027 assert(kid->op_sibparent == o);
4032 } while (( o = traverse_op_tree(top, o)) != NULL);
4036 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
4039 PadnameLVALUE_on(pn);
4040 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
4042 /* RT #127786: cv can be NULL due to an eval within the DB package
4043 * called from an anon sub - anon subs don't have CvOUTSIDE() set
4044 * unless they contain an eval, but calling eval within DB
4045 * pretends the eval was done in the caller's scope.
4049 assert(CvPADLIST(cv));
4051 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
4052 assert(PadnameLEN(pn));
4053 PadnameLVALUE_on(pn);
4058 S_vivifies(const OPCODE type)
4061 case OP_RV2AV: case OP_ASLICE:
4062 case OP_RV2HV: case OP_KVASLICE:
4063 case OP_RV2SV: case OP_HSLICE:
4064 case OP_AELEMFAST: case OP_KVHSLICE:
4073 /* apply lvalue reference (aliasing) context to the optree o.
4076 * o would be the list ($x,$y) and type would be OP_AASSIGN.
4077 * It may descend and apply this to children too, for example in
4078 * \( $cond ? $x, $y) = (...)
4082 S_lvref(pTHX_ OP *o, I32 type)
4089 switch (o->op_type) {
4091 o = OpSIBLING(cUNOPo->op_first);
4098 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
4099 o->op_flags |= OPf_STACKED;
4100 if (o->op_flags & OPf_PARENS) {
4101 if (o->op_private & OPpLVAL_INTRO) {
4102 yyerror(Perl_form(aTHX_ "Can't modify reference to "
4103 "localized parenthesized array in list assignment"));
4107 OpTYPE_set(o, OP_LVAVREF);
4108 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
4109 o->op_flags |= OPf_MOD|OPf_REF;
4112 o->op_private |= OPpLVREF_AV;
4116 kid = cUNOPo->op_first;
4117 if (kid->op_type == OP_NULL)
4118 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
4120 o->op_private = OPpLVREF_CV;
4121 if (kid->op_type == OP_GV)
4122 o->op_flags |= OPf_STACKED;
4123 else if (kid->op_type == OP_PADCV) {
4124 o->op_targ = kid->op_targ;
4126 op_free(cUNOPo->op_first);
4127 cUNOPo->op_first = NULL;
4128 o->op_flags &=~ OPf_KIDS;
4134 if (o->op_flags & OPf_PARENS) {
4136 yyerror(Perl_form(aTHX_ "Can't modify reference to "
4137 "parenthesized hash in list assignment"));
4140 o->op_private |= OPpLVREF_HV;
4144 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
4145 o->op_flags |= OPf_STACKED;
4149 if (o->op_flags & OPf_PARENS) goto parenhash;
4150 o->op_private |= OPpLVREF_HV;
4153 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
4157 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
4158 if (o->op_flags & OPf_PARENS) goto slurpy;
4159 o->op_private |= OPpLVREF_AV;
4164 o->op_private |= OPpLVREF_ELEM;
4165 o->op_flags |= OPf_STACKED;
4170 OpTYPE_set(o, OP_LVREFSLICE);
4171 o->op_private &= OPpLVAL_INTRO;
4175 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4177 else if (!(o->op_flags & OPf_KIDS))
4180 /* the code formerly only recursed into the first child of
4181 * a non ex-list OP_NULL. if we ever encounter such a null op with
4182 * more than one child, need to decide whether its ok to process
4183 * *all* its kids or not */
4184 assert(o->op_targ == OP_LIST
4185 || !(OpHAS_SIBLING(cBINOPo->op_first)));
4188 o = cLISTOPo->op_first;
4192 if (o->op_flags & OPf_PARENS)
4197 /* diag_listed_as: Can't modify reference to %s in %s assignment */
4198 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
4199 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
4206 OpTYPE_set(o, OP_LVREF);
4208 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
4209 if (type == OP_ENTERLOOP)
4210 o->op_private |= OPpLVREF_ITER;
4215 return; /* at top; no parents/siblings to try */
4216 if (OpHAS_SIBLING(o)) {
4217 o = o->op_sibparent;
4220 o = o->op_sibparent; /*try parent's next sibling */
4226 PERL_STATIC_INLINE bool
4227 S_potential_mod_type(I32 type)
4229 /* Types that only potentially result in modification. */
4230 return type == OP_GREPSTART || type == OP_ENTERSUB
4231 || type == OP_REFGEN || type == OP_LEAVESUBLV;
4236 =for apidoc op_lvalue
4238 Propagate lvalue ("modifiable") context to an op and its children.
4239 C<type> represents the context type, roughly based on the type of op that
4240 would do the modifying, although C<local()> is represented by C<OP_NULL>,
4241 because it has no op type of its own (it is signalled by a flag on
4244 This function detects things that can't be modified, such as C<$x+1>, and
4245 generates errors for them. For example, C<$x+1 = 2> would cause it to be
4246 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
4248 It also flags things that need to behave specially in an lvalue context,
4249 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
4253 Perl_op_lvalue_flags() is a non-API lower-level interface to
4254 op_lvalue(). The flags param has these bits:
4255 OP_LVALUE_NO_CROAK: return rather than croaking on error
4260 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
4265 if (!o || (PL_parser && PL_parser->error_count))
4270 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
4272 OP *next_kid = NULL;
4274 if ((o->op_private & OPpTARGET_MY)
4275 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
4280 /* elements of a list might be in void context because the list is
4281 in scalar context or because they are attribute sub calls */
4282 if ((o->op_flags & OPf_WANT) == OPf_WANT_VOID)
4285 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
4287 switch (o->op_type) {
4293 if ((o->op_flags & OPf_PARENS))
4298 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
4299 !(o->op_flags & OPf_STACKED)) {
4300 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
4301 assert(cUNOPo->op_first->op_type == OP_NULL);
4302 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
4305 else { /* lvalue subroutine call */
4306 o->op_private |= OPpLVAL_INTRO;
4307 PL_modcount = RETURN_UNLIMITED_NUMBER;
4308 if (S_potential_mod_type(type)) {
4309 o->op_private |= OPpENTERSUB_INARGS;
4312 else { /* Compile-time error message: */
4313 OP *kid = cUNOPo->op_first;
4318 if (kid->op_type != OP_PUSHMARK) {
4319 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
4321 "panic: unexpected lvalue entersub "
4322 "args: type/targ %ld:%" UVuf,
4323 (long)kid->op_type, (UV)kid->op_targ);
4324 kid = kLISTOP->op_first;
4326 while (OpHAS_SIBLING(kid))
4327 kid = OpSIBLING(kid);
4328 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
4329 break; /* Postpone until runtime */
4332 kid = kUNOP->op_first;
4333 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
4334 kid = kUNOP->op_first;
4335 if (kid->op_type == OP_NULL)
4337 "Unexpected constant lvalue entersub "
4338 "entry via type/targ %ld:%" UVuf,
4339 (long)kid->op_type, (UV)kid->op_targ);
4340 if (kid->op_type != OP_GV) {
4347 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
4348 ? MUTABLE_CV(SvRV(gv))
4354 if (flags & OP_LVALUE_NO_CROAK)
4357 namesv = cv_name(cv, NULL, 0);
4358 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
4359 "subroutine call of &%" SVf " in %s",
4360 SVfARG(namesv), PL_op_desc[type]),
4368 if (flags & OP_LVALUE_NO_CROAK) return NULL;
4369 /* grep, foreach, subcalls, refgen */
4370 if (S_potential_mod_type(type))
4372 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
4373 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
4376 type ? PL_op_desc[type] : "local"));
4389 case OP_RIGHT_SHIFT:
4398 if (!(o->op_flags & OPf_STACKED))
4404 if (o->op_flags & OPf_STACKED) {
4408 if (!(o->op_private & OPpREPEAT_DOLIST))
4411 const I32 mods = PL_modcount;
4412 /* we recurse rather than iterate here because we need to
4413 * calculate and use the delta applied to PL_modcount by the
4414 * first child. So in something like
4415 * ($x, ($y) x 3) = split;
4416 * split knows that 4 elements are wanted
4418 modkids(cBINOPo->op_first, type);
4419 if (type != OP_AASSIGN)
4421 kid = cBINOPo->op_last;
4422 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
4423 const IV iv = SvIV(kSVOP_sv);
4424 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
4426 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
4429 PL_modcount = RETURN_UNLIMITED_NUMBER;
4435 next_kid = OpSIBLING(cUNOPo->op_first);
4440 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
4441 PL_modcount = RETURN_UNLIMITED_NUMBER;
4442 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4443 fiable since some contexts need to know. */
4444 o->op_flags |= OPf_MOD;
4449 if (scalar_mod_type(o, type))
4451 ref(cUNOPo->op_first, o->op_type);
4458 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
4459 if (type == OP_LEAVESUBLV && (
4460 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
4461 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4463 o->op_private |= OPpMAYBE_LVSUB;
4467 PL_modcount = RETURN_UNLIMITED_NUMBER;
4473 if (type == OP_LEAVESUBLV)
4474 o->op_private |= OPpMAYBE_LVSUB;
4478 if (type == OP_LEAVESUBLV
4479 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS)
4480 o->op_private |= OPpMAYBE_LVSUB;
4484 PL_hints |= HINT_BLOCK_SCOPE;
4485 if (type == OP_LEAVESUBLV)
4486 o->op_private |= OPpMAYBE_LVSUB;
4491 ref(cUNOPo->op_first, o->op_type);
4495 PL_hints |= HINT_BLOCK_SCOPE;
4505 case OP_AELEMFAST_LEX:
4512 PL_modcount = RETURN_UNLIMITED_NUMBER;
4513 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
4515 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4516 fiable since some contexts need to know. */
4517 o->op_flags |= OPf_MOD;
4520 if (scalar_mod_type(o, type))
4522 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4523 && type == OP_LEAVESUBLV)
4524 o->op_private |= OPpMAYBE_LVSUB;
4528 if (!type) /* local() */
4529 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
4530 PNfARG(PAD_COMPNAME(o->op_targ)));
4531 if (!(o->op_private & OPpLVAL_INTRO)
4532 || ( type != OP_SASSIGN && type != OP_AASSIGN
4533 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
4534 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
4542 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
4546 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
4552 if (type == OP_LEAVESUBLV)
4553 o->op_private |= OPpMAYBE_LVSUB;
4554 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
4555 /* we recurse rather than iterate here because the child
4556 * needs to be processed with a different 'type' parameter */
4558 /* substr and vec */
4559 /* If this op is in merely potential (non-fatal) modifiable
4560 context, then apply OP_ENTERSUB context to
4561 the kid op (to avoid croaking). Other-
4562 wise pass this op’s own type so the correct op is mentioned
4563 in error messages. */
4564 op_lvalue(OpSIBLING(cBINOPo->op_first),
4565 S_potential_mod_type(type)
4573 ref(cBINOPo->op_first, o->op_type);
4574 if (type == OP_ENTERSUB &&
4575 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
4576 o->op_private |= OPpLVAL_DEFER;
4577 if (type == OP_LEAVESUBLV)
4578 o->op_private |= OPpMAYBE_LVSUB;
4585 o->op_private |= OPpLVALUE;
4591 if (o->op_flags & OPf_KIDS)
4592 next_kid = cLISTOPo->op_last;
4597 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4599 else if (!(o->op_flags & OPf_KIDS))
4602 if (o->op_targ != OP_LIST) {
4603 OP *sib = OpSIBLING(cLISTOPo->op_first);
4604 /* OP_TRANS and OP_TRANSR with argument have a weird optree
4611 * compared with things like OP_MATCH which have the argument
4617 * so handle specially to correctly get "Can't modify" croaks etc
4620 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
4622 /* this should trigger a "Can't modify transliteration" err */
4623 op_lvalue(sib, type);
4625 next_kid = cBINOPo->op_first;
4626 /* we assume OP_NULLs which aren't ex-list have no more than 2
4627 * children. If this assumption is wrong, increase the scan
4629 assert( !OpHAS_SIBLING(next_kid)
4630 || !OpHAS_SIBLING(OpSIBLING(next_kid)));
4636 next_kid = cLISTOPo->op_first;
4644 if (type == OP_LEAVESUBLV
4645 || !S_vivifies(cLOGOPo->op_first->op_type))
4646 next_kid = cLOGOPo->op_first;
4647 else if (type == OP_LEAVESUBLV
4648 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
4649 next_kid = OpSIBLING(cLOGOPo->op_first);
4653 if (type == OP_NULL) { /* local */
4655 if (!FEATURE_MYREF_IS_ENABLED)
4656 Perl_croak(aTHX_ "The experimental declared_refs "
4657 "feature is not enabled");
4658 Perl_ck_warner_d(aTHX_
4659 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
4660 "Declaring references is experimental");
4661 next_kid = cUNOPo->op_first;
4664 if (type != OP_AASSIGN && type != OP_SASSIGN
4665 && type != OP_ENTERLOOP)
4667 /* Don’t bother applying lvalue context to the ex-list. */
4668 kid = cUNOPx(cUNOPo->op_first)->op_first;
4669 assert (!OpHAS_SIBLING(kid));
4672 if (type == OP_NULL) /* local */
4674 if (type != OP_AASSIGN) goto nomod;
4675 kid = cUNOPo->op_first;
4678 const U8 ec = PL_parser ? PL_parser->error_count : 0;
4679 S_lvref(aTHX_ kid, type);
4680 if (!PL_parser || PL_parser->error_count == ec) {
4681 if (!FEATURE_REFALIASING_IS_ENABLED)
4683 "Experimental aliasing via reference not enabled");
4684 Perl_ck_warner_d(aTHX_