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"
168 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
169 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
170 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
172 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
174 /* Used to avoid recursion through the op tree in scalarvoid() and
179 SSize_t defer_stack_alloc = 0; \
180 SSize_t defer_ix = -1; \
181 OP **defer_stack = NULL;
182 #define DEFER_OP_CLEANUP Safefree(defer_stack)
183 #define DEFERRED_OP_STEP 100
184 #define DEFER_OP(o) \
186 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \
187 defer_stack_alloc += DEFERRED_OP_STEP; \
188 assert(defer_stack_alloc > 0); \
189 Renew(defer_stack, defer_stack_alloc, OP *); \
191 defer_stack[++defer_ix] = o; \
193 #define DEFER_REVERSE(count) \
197 OP **top = defer_stack + defer_ix; \
198 /* top - (cnt) + 1 isn't safe here */ \
199 OP **bottom = top - (cnt - 1); \
201 assert(bottom >= defer_stack); \
202 while (top > bottom) { \
210 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
212 /* remove any leading "empty" ops from the op_next chain whose first
213 * node's address is stored in op_p. Store the updated address of the
214 * first node in op_p.
218 S_prune_chain_head(OP** op_p)
221 && ( (*op_p)->op_type == OP_NULL
222 || (*op_p)->op_type == OP_SCOPE
223 || (*op_p)->op_type == OP_SCALAR
224 || (*op_p)->op_type == OP_LINESEQ)
226 *op_p = (*op_p)->op_next;
230 /* See the explanatory comments above struct opslab in op.h. */
232 #ifdef PERL_DEBUG_READONLY_OPS
233 # define PERL_SLAB_SIZE 128
234 # define PERL_MAX_SLAB_SIZE 4096
235 # include <sys/mman.h>
238 #ifndef PERL_SLAB_SIZE
239 # define PERL_SLAB_SIZE 64
241 #ifndef PERL_MAX_SLAB_SIZE
242 # define PERL_MAX_SLAB_SIZE 2048
245 /* rounds up to nearest pointer */
246 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
247 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
249 /* malloc a new op slab (suitable for attaching to PL_compcv) */
252 S_new_slab(pTHX_ size_t sz)
254 #ifdef PERL_DEBUG_READONLY_OPS
255 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
256 PROT_READ|PROT_WRITE,
257 MAP_ANON|MAP_PRIVATE, -1, 0);
258 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
259 (unsigned long) sz, slab));
260 if (slab == MAP_FAILED) {
261 perror("mmap failed");
264 slab->opslab_size = (U16)sz;
266 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
269 /* The context is unused in non-Windows */
272 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
276 /* requires double parens and aTHX_ */
277 #define DEBUG_S_warn(args) \
279 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
282 /* Returns a sz-sized block of memory (suitable for holding an op) from
283 * a free slot in the chain of op slabs attached to PL_compcv.
284 * Allocates a new slab if necessary.
285 * if PL_compcv isn't compiling, malloc() instead.
289 Perl_Slab_Alloc(pTHX_ size_t sz)
297 /* We only allocate ops from the slab during subroutine compilation.
298 We find the slab via PL_compcv, hence that must be non-NULL. It could
299 also be pointing to a subroutine which is now fully set up (CvROOT()
300 pointing to the top of the optree for that sub), or a subroutine
301 which isn't using the slab allocator. If our sanity checks aren't met,
302 don't use a slab, but allocate the OP directly from the heap. */
303 if (!PL_compcv || CvROOT(PL_compcv)
304 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
306 o = (OP*)PerlMemShared_calloc(1, sz);
310 /* While the subroutine is under construction, the slabs are accessed via
311 CvSTART(), to avoid needing to expand PVCV by one pointer for something
312 unneeded at runtime. Once a subroutine is constructed, the slabs are
313 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
314 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
316 if (!CvSTART(PL_compcv)) {
318 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
319 CvSLABBED_on(PL_compcv);
320 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
322 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
324 opsz = SIZE_TO_PSIZE(sz);
325 sz = opsz + OPSLOT_HEADER_P;
327 /* The slabs maintain a free list of OPs. In particular, constant folding
328 will free up OPs, so it makes sense to re-use them where possible. A
329 freed up slot is used in preference to a new allocation. */
330 if (slab->opslab_freed) {
331 OP **too = &slab->opslab_freed;
333 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
334 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
335 DEBUG_S_warn((aTHX_ "Alas! too small"));
336 o = *(too = &o->op_next);
337 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
341 Zero(o, opsz, I32 *);
347 #define INIT_OPSLOT \
348 slot->opslot_slab = slab; \
349 slot->opslot_next = slab2->opslab_first; \
350 slab2->opslab_first = slot; \
351 o = &slot->opslot_op; \
354 /* The partially-filled slab is next in the chain. */
355 slab2 = slab->opslab_next ? slab->opslab_next : slab;
356 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
357 /* Remaining space is too small. */
359 /* If we can fit a BASEOP, add it to the free chain, so as not
361 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
362 slot = &slab2->opslab_slots;
364 o->op_type = OP_FREED;
365 o->op_next = slab->opslab_freed;
366 slab->opslab_freed = o;
369 /* Create a new slab. Make this one twice as big. */
370 slot = slab2->opslab_first;
371 while (slot->opslot_next) slot = slot->opslot_next;
372 slab2 = S_new_slab(aTHX_
373 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
375 : (DIFF(slab2, slot)+1)*2);
376 slab2->opslab_next = slab->opslab_next;
377 slab->opslab_next = slab2;
379 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
381 /* Create a new op slot */
382 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
383 assert(slot >= &slab2->opslab_slots);
384 if (DIFF(&slab2->opslab_slots, slot)
385 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
386 slot = &slab2->opslab_slots;
388 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
391 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
392 assert(!o->op_moresib);
393 assert(!o->op_sibparent);
400 #ifdef PERL_DEBUG_READONLY_OPS
402 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
404 PERL_ARGS_ASSERT_SLAB_TO_RO;
406 if (slab->opslab_readonly) return;
407 slab->opslab_readonly = 1;
408 for (; slab; slab = slab->opslab_next) {
409 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
410 (unsigned long) slab->opslab_size, slab));*/
411 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
412 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
413 (unsigned long)slab->opslab_size, errno);
418 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
422 PERL_ARGS_ASSERT_SLAB_TO_RW;
424 if (!slab->opslab_readonly) return;
426 for (; slab2; slab2 = slab2->opslab_next) {
427 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
428 (unsigned long) size, slab2));*/
429 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
430 PROT_READ|PROT_WRITE)) {
431 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
432 (unsigned long)slab2->opslab_size, errno);
435 slab->opslab_readonly = 0;
439 # define Slab_to_rw(op) NOOP
442 /* This cannot possibly be right, but it was copied from the old slab
443 allocator, to which it was originally added, without explanation, in
446 # define PerlMemShared PerlMem
449 /* make freed ops die if they're inadvertently executed */
454 DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
459 /* Return the block of memory used by an op to the free list of
460 * the OP slab associated with that op.
464 Perl_Slab_Free(pTHX_ void *op)
466 OP * const o = (OP *)op;
469 PERL_ARGS_ASSERT_SLAB_FREE;
472 o->op_ppaddr = S_pp_freed;
475 if (!o->op_slabbed) {
477 PerlMemShared_free(op);
482 /* If this op is already freed, our refcount will get screwy. */
483 assert(o->op_type != OP_FREED);
484 o->op_type = OP_FREED;
485 o->op_next = slab->opslab_freed;
486 slab->opslab_freed = o;
487 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
488 OpslabREFCNT_dec_padok(slab);
492 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
494 const bool havepad = !!PL_comppad;
495 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
498 PAD_SAVE_SETNULLPAD();
504 /* Free a chain of OP slabs. Should only be called after all ops contained
505 * in it have been freed. At this point, its reference count should be 1,
506 * because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1,
507 * and just directly calls opslab_free().
508 * (Note that the reference count which PL_compcv held on the slab should
509 * have been removed once compilation of the sub was complete).
515 Perl_opslab_free(pTHX_ OPSLAB *slab)
518 PERL_ARGS_ASSERT_OPSLAB_FREE;
520 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
521 assert(slab->opslab_refcnt == 1);
523 slab2 = slab->opslab_next;
525 slab->opslab_refcnt = ~(size_t)0;
527 #ifdef PERL_DEBUG_READONLY_OPS
528 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
530 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
531 perror("munmap failed");
535 PerlMemShared_free(slab);
541 /* like opslab_free(), but first calls op_free() on any ops in the slab
542 * not marked as OP_FREED
546 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
550 size_t savestack_count = 0;
552 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
556 for (slot = slab2->opslab_first;
558 slot = slot->opslot_next) {
559 if (slot->opslot_op.op_type != OP_FREED
560 && !(slot->opslot_op.op_savefree
566 assert(slot->opslot_op.op_slabbed);
567 op_free(&slot->opslot_op);
568 if (slab->opslab_refcnt == 1) goto free;
571 } while ((slab2 = slab2->opslab_next));
572 /* > 1 because the CV still holds a reference count. */
573 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
575 assert(savestack_count == slab->opslab_refcnt-1);
577 /* Remove the CV’s reference count. */
578 slab->opslab_refcnt--;
585 #ifdef PERL_DEBUG_READONLY_OPS
587 Perl_op_refcnt_inc(pTHX_ OP *o)
590 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
591 if (slab && slab->opslab_readonly) {
604 Perl_op_refcnt_dec(pTHX_ OP *o)
607 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
609 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
611 if (slab && slab->opslab_readonly) {
613 result = --o->op_targ;
616 result = --o->op_targ;
622 * In the following definition, the ", (OP*)0" is just to make the compiler
623 * think the expression is of the right type: croak actually does a Siglongjmp.
625 #define CHECKOP(type,o) \
626 ((PL_op_mask && PL_op_mask[type]) \
627 ? ( op_free((OP*)o), \
628 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
630 : PL_check[type](aTHX_ (OP*)o))
632 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
634 #define OpTYPE_set(o,type) \
636 o->op_type = (OPCODE)type; \
637 o->op_ppaddr = PL_ppaddr[type]; \
641 S_no_fh_allowed(pTHX_ OP *o)
643 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
645 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
651 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
653 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
654 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
659 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
661 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
663 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
668 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
670 PERL_ARGS_ASSERT_BAD_TYPE_PV;
672 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
673 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
676 /* remove flags var, its unused in all callers, move to to right end since gv
677 and kid are always the same */
679 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
681 SV * const namesv = cv_name((CV *)gv, NULL, 0);
682 PERL_ARGS_ASSERT_BAD_TYPE_GV;
684 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
685 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
689 S_no_bareword_allowed(pTHX_ OP *o)
691 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
693 qerror(Perl_mess(aTHX_
694 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
696 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
699 /* "register" allocation */
702 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
705 const bool is_our = (PL_parser->in_my == KEY_our);
707 PERL_ARGS_ASSERT_ALLOCMY;
709 if (flags & ~SVf_UTF8)
710 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
713 /* complain about "my $<special_var>" etc etc */
717 || ( (flags & SVf_UTF8)
718 && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
719 || (name[1] == '_' && len > 2)))
721 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
723 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
724 /* diag_listed_as: Can't use global %s in "%s" */
725 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
726 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
727 PL_parser->in_my == KEY_state ? "state" : "my"));
729 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
730 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
734 /* allocate a spare slot and store the name in that slot */
736 off = pad_add_name_pvn(name, len,
737 (is_our ? padadd_OUR :
738 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
739 PL_parser->in_my_stash,
741 /* $_ is always in main::, even with our */
742 ? (PL_curstash && !memEQs(name,len,"$_")
748 /* anon sub prototypes contains state vars should always be cloned,
749 * otherwise the state var would be shared between anon subs */
751 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
752 CvCLONE_on(PL_compcv);
758 =head1 Optree Manipulation Functions
760 =for apidoc alloccopstash
762 Available only under threaded builds, this function allocates an entry in
763 C<PL_stashpad> for the stash passed to it.
770 Perl_alloccopstash(pTHX_ HV *hv)
772 PADOFFSET off = 0, o = 1;
773 bool found_slot = FALSE;
775 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
777 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
779 for (; o < PL_stashpadmax; ++o) {
780 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
781 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
782 found_slot = TRUE, off = o;
785 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
786 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
787 off = PL_stashpadmax;
788 PL_stashpadmax += 10;
791 PL_stashpad[PL_stashpadix = off] = hv;
796 /* free the body of an op without examining its contents.
797 * Always use this rather than FreeOp directly */
800 S_op_destroy(pTHX_ OP *o)
808 =for apidoc Am|void|op_free|OP *o
810 Free an op. Only use this when an op is no longer linked to from any
817 Perl_op_free(pTHX_ OP *o)
825 /* Though ops may be freed twice, freeing the op after its slab is a
827 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
828 /* During the forced freeing of ops after compilation failure, kidops
829 may be freed before their parents. */
830 if (!o || o->op_type == OP_FREED)
835 /* an op should only ever acquire op_private flags that we know about.
836 * If this fails, you may need to fix something in regen/op_private.
837 * Don't bother testing if:
838 * * the op_ppaddr doesn't match the op; someone may have
839 * overridden the op and be doing strange things with it;
840 * * we've errored, as op flags are often left in an
841 * inconsistent state then. Note that an error when
842 * compiling the main program leaves PL_parser NULL, so
843 * we can't spot faults in the main code, only
844 * evaled/required code */
846 if ( o->op_ppaddr == PL_ppaddr[o->op_type]
848 && !PL_parser->error_count)
850 assert(!(o->op_private & ~PL_op_private_valid[type]));
854 if (o->op_private & OPpREFCOUNTED) {
865 refcnt = OpREFCNT_dec(o);
868 /* Need to find and remove any pattern match ops from the list
869 we maintain for reset(). */
870 find_and_forget_pmops(o);
880 /* Call the op_free hook if it has been set. Do it now so that it's called
881 * at the right time for refcounted ops, but still before all of the kids
885 if (o->op_flags & OPf_KIDS) {
887 assert(cUNOPo->op_first); /* OPf_KIDS implies op_first non-null */
888 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
889 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
890 if (kid->op_type == OP_FREED)
891 /* During the forced freeing of ops after
892 compilation failure, kidops may be freed before
895 if (!(kid->op_flags & OPf_KIDS))
896 /* If it has no kids, just free it now */
903 type = (OPCODE)o->op_targ;
906 Slab_to_rw(OpSLAB(o));
908 /* COP* is not cleared by op_clear() so that we may track line
909 * numbers etc even after null() */
910 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
918 } while ( (o = POP_DEFERRED_OP()) );
923 /* S_op_clear_gv(): free a GV attached to an OP */
927 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
929 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
933 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
934 || o->op_type == OP_MULTIDEREF)
937 ? ((GV*)PAD_SVl(*ixp)) : NULL;
939 ? (GV*)(*svp) : NULL;
941 /* It's possible during global destruction that the GV is freed
942 before the optree. Whilst the SvREFCNT_inc is happy to bump from
943 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
944 will trigger an assertion failure, because the entry to sv_clear
945 checks that the scalar is not already freed. A check of for
946 !SvIS_FREED(gv) turns out to be invalid, because during global
947 destruction the reference count can be forced down to zero
948 (with SVf_BREAK set). In which case raising to 1 and then
949 dropping to 0 triggers cleanup before it should happen. I
950 *think* that this might actually be a general, systematic,
951 weakness of the whole idea of SVf_BREAK, in that code *is*
952 allowed to raise and lower references during global destruction,
953 so any *valid* code that happens to do this during global
954 destruction might well trigger premature cleanup. */
955 bool still_valid = gv && SvREFCNT(gv);
958 SvREFCNT_inc_simple_void(gv);
961 pad_swipe(*ixp, TRUE);
969 int try_downgrade = SvREFCNT(gv) == 2;
972 gv_try_downgrade(gv);
978 Perl_op_clear(pTHX_ OP *o)
983 PERL_ARGS_ASSERT_OP_CLEAR;
985 switch (o->op_type) {
986 case OP_NULL: /* Was holding old type, if any. */
989 case OP_ENTEREVAL: /* Was holding hints. */
990 case OP_ARGDEFELEM: /* Was holding signature index. */
994 if (!(o->op_flags & OPf_REF)
995 || (PL_check[o->op_type] != Perl_ck_ftst))
1002 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
1004 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
1007 case OP_METHOD_REDIR:
1008 case OP_METHOD_REDIR_SUPER:
1010 if (cMETHOPx(o)->op_rclass_targ) {
1011 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
1012 cMETHOPx(o)->op_rclass_targ = 0;
1015 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
1016 cMETHOPx(o)->op_rclass_sv = NULL;
1019 case OP_METHOD_NAMED:
1020 case OP_METHOD_SUPER:
1021 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
1022 cMETHOPx(o)->op_u.op_meth_sv = NULL;
1025 pad_swipe(o->op_targ, 1);
1032 SvREFCNT_dec(cSVOPo->op_sv);
1033 cSVOPo->op_sv = NULL;
1036 Even if op_clear does a pad_free for the target of the op,
1037 pad_free doesn't actually remove the sv that exists in the pad;
1038 instead it lives on. This results in that it could be reused as
1039 a target later on when the pad was reallocated.
1042 pad_swipe(o->op_targ,1);
1052 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
1057 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
1058 && (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)))
1061 if (cPADOPo->op_padix > 0) {
1062 pad_swipe(cPADOPo->op_padix, TRUE);
1063 cPADOPo->op_padix = 0;
1066 SvREFCNT_dec(cSVOPo->op_sv);
1067 cSVOPo->op_sv = NULL;
1071 PerlMemShared_free(cPVOPo->op_pv);
1072 cPVOPo->op_pv = NULL;
1076 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1080 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1081 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1083 if (o->op_private & OPpSPLIT_LEX)
1084 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1087 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1089 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1096 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1097 op_free(cPMOPo->op_code_list);
1098 cPMOPo->op_code_list = NULL;
1099 forget_pmop(cPMOPo);
1100 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1101 /* we use the same protection as the "SAFE" version of the PM_ macros
1102 * here since sv_clean_all might release some PMOPs
1103 * after PL_regex_padav has been cleared
1104 * and the clearing of PL_regex_padav needs to
1105 * happen before sv_clean_all
1108 if(PL_regex_pad) { /* We could be in destruction */
1109 const IV offset = (cPMOPo)->op_pmoffset;
1110 ReREFCNT_dec(PM_GETRE(cPMOPo));
1111 PL_regex_pad[offset] = &PL_sv_undef;
1112 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1116 ReREFCNT_dec(PM_GETRE(cPMOPo));
1117 PM_SETRE(cPMOPo, NULL);
1123 PerlMemShared_free(cUNOP_AUXo->op_aux);
1126 case OP_MULTICONCAT:
1128 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux;
1129 /* aux[PERL_MULTICONCAT_IX_PLAIN_PV] and/or
1130 * aux[PERL_MULTICONCAT_IX_UTF8_PV] point to plain and/or
1131 * utf8 shared strings */
1132 char *p1 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
1133 char *p2 = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
1135 PerlMemShared_free(p1);
1137 PerlMemShared_free(p2);
1138 PerlMemShared_free(aux);
1144 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1145 UV actions = items->uv;
1147 bool is_hash = FALSE;
1150 switch (actions & MDEREF_ACTION_MASK) {
1153 actions = (++items)->uv;
1156 case MDEREF_HV_padhv_helem:
1159 case MDEREF_AV_padav_aelem:
1160 pad_free((++items)->pad_offset);
1163 case MDEREF_HV_gvhv_helem:
1166 case MDEREF_AV_gvav_aelem:
1168 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1170 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1174 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1177 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1179 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1181 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1183 goto do_vivify_rv2xv_elem;
1185 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1188 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1189 pad_free((++items)->pad_offset);
1190 goto do_vivify_rv2xv_elem;
1192 case MDEREF_HV_pop_rv2hv_helem:
1193 case MDEREF_HV_vivify_rv2hv_helem:
1196 do_vivify_rv2xv_elem:
1197 case MDEREF_AV_pop_rv2av_aelem:
1198 case MDEREF_AV_vivify_rv2av_aelem:
1200 switch (actions & MDEREF_INDEX_MASK) {
1201 case MDEREF_INDEX_none:
1204 case MDEREF_INDEX_const:
1208 pad_swipe((++items)->pad_offset, 1);
1210 SvREFCNT_dec((++items)->sv);
1216 case MDEREF_INDEX_padsv:
1217 pad_free((++items)->pad_offset);
1219 case MDEREF_INDEX_gvsv:
1221 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1223 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1228 if (actions & MDEREF_FLAG_last)
1241 actions >>= MDEREF_SHIFT;
1244 /* start of malloc is at op_aux[-1], where the length is
1246 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1251 if (o->op_targ > 0) {
1252 pad_free(o->op_targ);
1258 S_cop_free(pTHX_ COP* cop)
1260 PERL_ARGS_ASSERT_COP_FREE;
1263 if (! specialWARN(cop->cop_warnings))
1264 PerlMemShared_free(cop->cop_warnings);
1265 cophh_free(CopHINTHASH_get(cop));
1266 if (PL_curcop == cop)
1271 S_forget_pmop(pTHX_ PMOP *const o)
1273 HV * const pmstash = PmopSTASH(o);
1275 PERL_ARGS_ASSERT_FORGET_PMOP;
1277 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1278 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1280 PMOP **const array = (PMOP**) mg->mg_ptr;
1281 U32 count = mg->mg_len / sizeof(PMOP**);
1285 if (array[i] == o) {
1286 /* Found it. Move the entry at the end to overwrite it. */
1287 array[i] = array[--count];
1288 mg->mg_len = count * sizeof(PMOP**);
1289 /* Could realloc smaller at this point always, but probably
1290 not worth it. Probably worth free()ing if we're the
1293 Safefree(mg->mg_ptr);
1306 S_find_and_forget_pmops(pTHX_ OP *o)
1308 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1310 if (o->op_flags & OPf_KIDS) {
1311 OP *kid = cUNOPo->op_first;
1313 switch (kid->op_type) {
1318 forget_pmop((PMOP*)kid);
1320 find_and_forget_pmops(kid);
1321 kid = OpSIBLING(kid);
1327 =for apidoc Am|void|op_null|OP *o
1329 Neutralizes an op when it is no longer needed, but is still linked to from
1336 Perl_op_null(pTHX_ OP *o)
1340 PERL_ARGS_ASSERT_OP_NULL;
1342 if (o->op_type == OP_NULL)
1345 o->op_targ = o->op_type;
1346 OpTYPE_set(o, OP_NULL);
1350 Perl_op_refcnt_lock(pTHX)
1351 PERL_TSA_ACQUIRE(PL_op_mutex)
1356 PERL_UNUSED_CONTEXT;
1361 Perl_op_refcnt_unlock(pTHX)
1362 PERL_TSA_RELEASE(PL_op_mutex)
1367 PERL_UNUSED_CONTEXT;
1373 =for apidoc op_sibling_splice
1375 A general function for editing the structure of an existing chain of
1376 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1377 you to delete zero or more sequential nodes, replacing them with zero or
1378 more different nodes. Performs the necessary op_first/op_last
1379 housekeeping on the parent node and op_sibling manipulation on the
1380 children. The last deleted node will be marked as as the last node by
1381 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1383 Note that op_next is not manipulated, and nodes are not freed; that is the
1384 responsibility of the caller. It also won't create a new list op for an
1385 empty list etc; use higher-level functions like op_append_elem() for that.
1387 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1388 the splicing doesn't affect the first or last op in the chain.
1390 C<start> is the node preceding the first node to be spliced. Node(s)
1391 following it will be deleted, and ops will be inserted after it. If it is
1392 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1395 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1396 If -1 or greater than or equal to the number of remaining kids, all
1397 remaining kids are deleted.
1399 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1400 If C<NULL>, no nodes are inserted.
1402 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1407 action before after returns
1408 ------ ----- ----- -------
1411 splice(P, A, 2, X-Y-Z) | | B-C
1415 splice(P, NULL, 1, X-Y) | | A
1419 splice(P, NULL, 3, NULL) | | A-B-C
1423 splice(P, B, 0, X-Y) | | NULL
1427 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1428 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1434 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1438 OP *last_del = NULL;
1439 OP *last_ins = NULL;
1442 first = OpSIBLING(start);
1446 first = cLISTOPx(parent)->op_first;
1448 assert(del_count >= -1);
1450 if (del_count && first) {
1452 while (--del_count && OpHAS_SIBLING(last_del))
1453 last_del = OpSIBLING(last_del);
1454 rest = OpSIBLING(last_del);
1455 OpLASTSIB_set(last_del, NULL);
1462 while (OpHAS_SIBLING(last_ins))
1463 last_ins = OpSIBLING(last_ins);
1464 OpMAYBESIB_set(last_ins, rest, NULL);
1470 OpMAYBESIB_set(start, insert, NULL);
1474 cLISTOPx(parent)->op_first = insert;
1476 parent->op_flags |= OPf_KIDS;
1478 parent->op_flags &= ~OPf_KIDS;
1482 /* update op_last etc */
1489 /* ought to use OP_CLASS(parent) here, but that can't handle
1490 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1492 type = parent->op_type;
1493 if (type == OP_CUSTOM) {
1495 type = XopENTRYCUSTOM(parent, xop_class);
1498 if (type == OP_NULL)
1499 type = parent->op_targ;
1500 type = PL_opargs[type] & OA_CLASS_MASK;
1503 lastop = last_ins ? last_ins : start ? start : NULL;
1504 if ( type == OA_BINOP
1505 || type == OA_LISTOP
1509 cLISTOPx(parent)->op_last = lastop;
1512 OpLASTSIB_set(lastop, parent);
1514 return last_del ? first : NULL;
1517 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1521 =for apidoc op_parent
1523 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1529 Perl_op_parent(OP *o)
1531 PERL_ARGS_ASSERT_OP_PARENT;
1532 while (OpHAS_SIBLING(o))
1534 return o->op_sibparent;
1537 /* replace the sibling following start with a new UNOP, which becomes
1538 * the parent of the original sibling; e.g.
1540 * op_sibling_newUNOP(P, A, unop-args...)
1548 * where U is the new UNOP.
1550 * parent and start args are the same as for op_sibling_splice();
1551 * type and flags args are as newUNOP().
1553 * Returns the new UNOP.
1557 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1561 kid = op_sibling_splice(parent, start, 1, NULL);
1562 newop = newUNOP(type, flags, kid);
1563 op_sibling_splice(parent, start, 0, newop);
1568 /* lowest-level newLOGOP-style function - just allocates and populates
1569 * the struct. Higher-level stuff should be done by S_new_logop() /
1570 * newLOGOP(). This function exists mainly to avoid op_first assignment
1571 * being spread throughout this file.
1575 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1580 NewOp(1101, logop, 1, LOGOP);
1581 OpTYPE_set(logop, type);
1582 logop->op_first = first;
1583 logop->op_other = other;
1585 logop->op_flags = OPf_KIDS;
1586 while (kid && OpHAS_SIBLING(kid))
1587 kid = OpSIBLING(kid);
1589 OpLASTSIB_set(kid, (OP*)logop);
1594 /* Contextualizers */
1597 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1599 Applies a syntactic context to an op tree representing an expression.
1600 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1601 or C<G_VOID> to specify the context to apply. The modified op tree
1608 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1610 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1612 case G_SCALAR: return scalar(o);
1613 case G_ARRAY: return list(o);
1614 case G_VOID: return scalarvoid(o);
1616 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1623 =for apidoc Am|OP*|op_linklist|OP *o
1624 This function is the implementation of the L</LINKLIST> macro. It should
1625 not be called directly.
1631 Perl_op_linklist(pTHX_ OP *o)
1635 PERL_ARGS_ASSERT_OP_LINKLIST;
1640 /* establish postfix order */
1641 first = cUNOPo->op_first;
1644 o->op_next = LINKLIST(first);
1647 OP *sibl = OpSIBLING(kid);
1649 kid->op_next = LINKLIST(sibl);
1664 S_scalarkids(pTHX_ OP *o)
1666 if (o && o->op_flags & OPf_KIDS) {
1668 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1675 S_scalarboolean(pTHX_ OP *o)
1677 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1679 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1680 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1681 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1682 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1683 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1684 if (ckWARN(WARN_SYNTAX)) {
1685 const line_t oldline = CopLINE(PL_curcop);
1687 if (PL_parser && PL_parser->copline != NOLINE) {
1688 /* This ensures that warnings are reported at the first line
1689 of the conditional, not the last. */
1690 CopLINE_set(PL_curcop, PL_parser->copline);
1692 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1693 CopLINE_set(PL_curcop, oldline);
1700 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1703 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1704 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1706 const char funny = o->op_type == OP_PADAV
1707 || o->op_type == OP_RV2AV ? '@' : '%';
1708 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1710 if (cUNOPo->op_first->op_type != OP_GV
1711 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1713 return varname(gv, funny, 0, NULL, 0, subscript_type);
1716 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1721 S_op_varname(pTHX_ const OP *o)
1723 return S_op_varname_subscript(aTHX_ o, 1);
1727 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1728 { /* or not so pretty :-) */
1729 if (o->op_type == OP_CONST) {
1731 if (SvPOK(*retsv)) {
1733 *retsv = sv_newmortal();
1734 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1735 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1737 else if (!SvOK(*retsv))
1740 else *retpv = "...";
1744 S_scalar_slice_warning(pTHX_ const OP *o)
1747 const bool h = o->op_type == OP_HSLICE
1748 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
1754 SV *keysv = NULL; /* just to silence compiler warnings */
1755 const char *key = NULL;
1757 if (!(o->op_private & OPpSLICEWARNING))
1759 if (PL_parser && PL_parser->error_count)
1760 /* This warning can be nonsensical when there is a syntax error. */
1763 kid = cLISTOPo->op_first;
1764 kid = OpSIBLING(kid); /* get past pushmark */
1765 /* weed out false positives: any ops that can return lists */
1766 switch (kid->op_type) {
1792 /* Don't warn if we have a nulled list either. */
1793 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1796 assert(OpSIBLING(kid));
1797 name = S_op_varname(aTHX_ OpSIBLING(kid));
1798 if (!name) /* XS module fiddling with the op tree */
1800 S_op_pretty(aTHX_ kid, &keysv, &key);
1801 assert(SvPOK(name));
1802 sv_chop(name,SvPVX(name)+1);
1804 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1805 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1806 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1808 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1809 lbrack, key, rbrack);
1811 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1812 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1813 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1815 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1816 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1820 Perl_scalar(pTHX_ OP *o)
1824 /* assumes no premature commitment */
1825 if (!o || (PL_parser && PL_parser->error_count)
1826 || (o->op_flags & OPf_WANT)
1827 || o->op_type == OP_RETURN)
1832 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1834 switch (o->op_type) {
1836 scalar(cBINOPo->op_first);
1837 if (o->op_private & OPpREPEAT_DOLIST) {
1838 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1839 assert(kid->op_type == OP_PUSHMARK);
1840 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1841 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1842 o->op_private &=~ OPpREPEAT_DOLIST;
1849 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1859 if (o->op_flags & OPf_KIDS) {
1860 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1866 kid = cLISTOPo->op_first;
1868 kid = OpSIBLING(kid);
1871 OP *sib = OpSIBLING(kid);
1872 if (sib && kid->op_type != OP_LEAVEWHEN
1873 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1874 || ( sib->op_targ != OP_NEXTSTATE
1875 && sib->op_targ != OP_DBSTATE )))
1881 PL_curcop = &PL_compiling;
1886 kid = cLISTOPo->op_first;
1889 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1894 /* Warn about scalar context */
1895 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1896 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1899 const char *key = NULL;
1901 /* This warning can be nonsensical when there is a syntax error. */
1902 if (PL_parser && PL_parser->error_count)
1905 if (!ckWARN(WARN_SYNTAX)) break;
1907 kid = cLISTOPo->op_first;
1908 kid = OpSIBLING(kid); /* get past pushmark */
1909 assert(OpSIBLING(kid));
1910 name = S_op_varname(aTHX_ OpSIBLING(kid));
1911 if (!name) /* XS module fiddling with the op tree */
1913 S_op_pretty(aTHX_ kid, &keysv, &key);
1914 assert(SvPOK(name));
1915 sv_chop(name,SvPVX(name)+1);
1917 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1918 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1919 "%%%" SVf "%c%s%c in scalar context better written "
1920 "as $%" SVf "%c%s%c",
1921 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1922 lbrack, key, rbrack);
1924 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1925 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1926 "%%%" SVf "%c%" SVf "%c in scalar context better "
1927 "written as $%" SVf "%c%" SVf "%c",
1928 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1929 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1936 Perl_scalarvoid(pTHX_ OP *arg)
1944 PERL_ARGS_ASSERT_SCALARVOID;
1948 SV *useless_sv = NULL;
1949 const char* useless = NULL;
1951 if (o->op_type == OP_NEXTSTATE
1952 || o->op_type == OP_DBSTATE
1953 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1954 || o->op_targ == OP_DBSTATE)))
1955 PL_curcop = (COP*)o; /* for warning below */
1957 /* assumes no premature commitment */
1958 want = o->op_flags & OPf_WANT;
1959 if ((want && want != OPf_WANT_SCALAR)
1960 || (PL_parser && PL_parser->error_count)
1961 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1966 if ((o->op_private & OPpTARGET_MY)
1967 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1969 /* newASSIGNOP has already applied scalar context, which we
1970 leave, as if this op is inside SASSIGN. */
1974 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1976 switch (o->op_type) {
1978 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1982 if (o->op_flags & OPf_STACKED)
1984 if (o->op_type == OP_REPEAT)
1985 scalar(cBINOPo->op_first);
1988 if ((o->op_flags & OPf_STACKED) &&
1989 !(o->op_private & OPpCONCAT_NESTED))
1993 if (o->op_private == 4)
2028 case OP_GETSOCKNAME:
2029 case OP_GETPEERNAME:
2034 case OP_GETPRIORITY:
2059 useless = OP_DESC(o);
2069 case OP_AELEMFAST_LEX:
2073 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
2074 /* Otherwise it's "Useless use of grep iterator" */
2075 useless = OP_DESC(o);
2079 if (!(o->op_private & OPpSPLIT_ASSIGN))
2080 useless = OP_DESC(o);
2084 kid = cUNOPo->op_first;
2085 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2086 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2089 useless = "negative pattern binding (!~)";
2093 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2094 useless = "non-destructive substitution (s///r)";
2098 useless = "non-destructive transliteration (tr///r)";
2105 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2106 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2107 useless = "a variable";
2112 if (cSVOPo->op_private & OPpCONST_STRICT)
2113 no_bareword_allowed(o);
2115 if (ckWARN(WARN_VOID)) {
2117 /* don't warn on optimised away booleans, eg
2118 * use constant Foo, 5; Foo || print; */
2119 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2121 /* the constants 0 and 1 are permitted as they are
2122 conventionally used as dummies in constructs like
2123 1 while some_condition_with_side_effects; */
2124 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2126 else if (SvPOK(sv)) {
2127 SV * const dsv = newSVpvs("");
2129 = Perl_newSVpvf(aTHX_
2131 pv_pretty(dsv, SvPVX_const(sv),
2132 SvCUR(sv), 32, NULL, NULL,
2134 | PERL_PV_ESCAPE_NOCLEAR
2135 | PERL_PV_ESCAPE_UNI_DETECT));
2136 SvREFCNT_dec_NN(dsv);
2138 else if (SvOK(sv)) {
2139 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2142 useless = "a constant (undef)";
2145 op_null(o); /* don't execute or even remember it */
2149 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2153 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2157 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2161 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2166 UNOP *refgen, *rv2cv;
2169 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2172 rv2gv = ((BINOP *)o)->op_last;
2173 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2176 refgen = (UNOP *)((BINOP *)o)->op_first;
2178 if (!refgen || (refgen->op_type != OP_REFGEN
2179 && refgen->op_type != OP_SREFGEN))
2182 exlist = (LISTOP *)refgen->op_first;
2183 if (!exlist || exlist->op_type != OP_NULL
2184 || exlist->op_targ != OP_LIST)
2187 if (exlist->op_first->op_type != OP_PUSHMARK
2188 && exlist->op_first != exlist->op_last)
2191 rv2cv = (UNOP*)exlist->op_last;
2193 if (rv2cv->op_type != OP_RV2CV)
2196 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2197 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2198 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2200 o->op_private |= OPpASSIGN_CV_TO_GV;
2201 rv2gv->op_private |= OPpDONT_INIT_GV;
2202 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2214 kid = cLOGOPo->op_first;
2215 if (kid->op_type == OP_NOT
2216 && (kid->op_flags & OPf_KIDS)) {
2217 if (o->op_type == OP_AND) {
2218 OpTYPE_set(o, OP_OR);
2220 OpTYPE_set(o, OP_AND);
2230 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2231 if (!(kid->op_flags & OPf_KIDS))
2238 if (o->op_flags & OPf_STACKED)
2245 if (!(o->op_flags & OPf_KIDS))
2256 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2257 if (!(kid->op_flags & OPf_KIDS))
2263 /* If the first kid after pushmark is something that the padrange
2264 optimisation would reject, then null the list and the pushmark.
2266 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2267 && ( !(kid = OpSIBLING(kid))
2268 || ( kid->op_type != OP_PADSV
2269 && kid->op_type != OP_PADAV
2270 && kid->op_type != OP_PADHV)
2271 || kid->op_private & ~OPpLVAL_INTRO
2272 || !(kid = OpSIBLING(kid))
2273 || ( kid->op_type != OP_PADSV
2274 && kid->op_type != OP_PADAV
2275 && kid->op_type != OP_PADHV)
2276 || kid->op_private & ~OPpLVAL_INTRO)
2278 op_null(cUNOPo->op_first); /* NULL the pushmark */
2279 op_null(o); /* NULL the list */
2291 /* mortalise it, in case warnings are fatal. */
2292 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2293 "Useless use of %" SVf " in void context",
2294 SVfARG(sv_2mortal(useless_sv)));
2297 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2298 "Useless use of %s in void context",
2301 } while ( (o = POP_DEFERRED_OP()) );
2309 S_listkids(pTHX_ OP *o)
2311 if (o && o->op_flags & OPf_KIDS) {
2313 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2320 Perl_list(pTHX_ OP *o)
2324 /* assumes no premature commitment */
2325 if (!o || (o->op_flags & OPf_WANT)
2326 || (PL_parser && PL_parser->error_count)
2327 || o->op_type == OP_RETURN)
2332 if ((o->op_private & OPpTARGET_MY)
2333 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2335 return o; /* As if inside SASSIGN */
2338 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2340 switch (o->op_type) {
2342 list(cBINOPo->op_first);
2345 if (o->op_private & OPpREPEAT_DOLIST
2346 && !(o->op_flags & OPf_STACKED))
2348 list(cBINOPo->op_first);
2349 kid = cBINOPo->op_last;
2350 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2351 && SvIVX(kSVOP_sv) == 1)
2353 op_null(o); /* repeat */
2354 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2356 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2363 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2371 if (!(o->op_flags & OPf_KIDS))
2373 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2374 list(cBINOPo->op_first);
2375 return gen_constant_list(o);
2381 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2382 op_null(cUNOPo->op_first); /* NULL the pushmark */
2383 op_null(o); /* NULL the list */
2388 kid = cLISTOPo->op_first;
2390 kid = OpSIBLING(kid);
2393 OP *sib = OpSIBLING(kid);
2394 if (sib && kid->op_type != OP_LEAVEWHEN)
2400 PL_curcop = &PL_compiling;
2404 kid = cLISTOPo->op_first;
2411 S_scalarseq(pTHX_ OP *o)
2414 const OPCODE type = o->op_type;
2416 if (type == OP_LINESEQ || type == OP_SCOPE ||
2417 type == OP_LEAVE || type == OP_LEAVETRY)
2420 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2421 if ((sib = OpSIBLING(kid))
2422 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2423 || ( sib->op_targ != OP_NEXTSTATE
2424 && sib->op_targ != OP_DBSTATE )))
2429 PL_curcop = &PL_compiling;
2431 o->op_flags &= ~OPf_PARENS;
2432 if (PL_hints & HINT_BLOCK_SCOPE)
2433 o->op_flags |= OPf_PARENS;
2436 o = newOP(OP_STUB, 0);
2441 S_modkids(pTHX_ OP *o, I32 type)
2443 if (o && o->op_flags & OPf_KIDS) {
2445 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2446 op_lvalue(kid, type);
2452 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2453 * const fields. Also, convert CONST keys to HEK-in-SVs.
2454 * rop is the op that retrieves the hash;
2455 * key_op is the first key
2456 * real if false, only check (and possibly croak); don't update op
2460 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real)
2466 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2468 if (rop->op_first->op_type == OP_PADSV)
2469 /* @$hash{qw(keys here)} */
2470 rop = (UNOP*)rop->op_first;
2472 /* @{$hash}{qw(keys here)} */
2473 if (rop->op_first->op_type == OP_SCOPE
2474 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2476 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2483 lexname = NULL; /* just to silence compiler warnings */
2484 fields = NULL; /* just to silence compiler warnings */
2488 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2489 SvPAD_TYPED(lexname))
2490 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2491 && isGV(*fields) && GvHV(*fields);
2493 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2495 if (key_op->op_type != OP_CONST)
2497 svp = cSVOPx_svp(key_op);
2499 /* make sure it's not a bareword under strict subs */
2500 if (key_op->op_private & OPpCONST_BARE &&
2501 key_op->op_private & OPpCONST_STRICT)
2503 no_bareword_allowed((OP*)key_op);
2506 /* Make the CONST have a shared SV */
2507 if ( !SvIsCOW_shared_hash(sv = *svp)
2508 && SvTYPE(sv) < SVt_PVMG
2514 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2515 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2516 SvREFCNT_dec_NN(sv);
2521 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2523 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2524 "in variable %" PNf " of type %" HEKf,
2525 SVfARG(*svp), PNfARG(lexname),
2526 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2531 /* info returned by S_sprintf_is_multiconcatable() */
2533 struct sprintf_ismc_info {
2534 SSize_t nargs; /* num of args to sprintf (not including the format) */
2535 char *start; /* start of raw format string */
2536 char *end; /* bytes after end of raw format string */
2537 STRLEN total_len; /* total length (in bytes) of format string, not
2538 including '%s' and half of '%%' */
2539 STRLEN variant; /* number of bytes by which total_len_p would grow
2540 if upgraded to utf8 */
2541 bool utf8; /* whether the format is utf8 */
2545 /* is the OP_SPRINTF o suitable for converting into a multiconcat op?
2546 * i.e. its format argument is a const string with only '%s' and '%%'
2547 * formats, and the number of args is known, e.g.
2548 * sprintf "a=%s f=%s", $a[0], scalar(f());
2550 * sprintf "i=%d a=%s f=%s", $i, @a, f();
2552 * If successful, the sprintf_ismc_info struct pointed to by info will be
2557 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info)
2559 OP *pm, *constop, *kid;
2562 SSize_t nargs, nformats;
2563 STRLEN cur, total_len, variant;
2566 /* if sprintf's behaviour changes, die here so that someone
2567 * can decide whether to enhance this function or skip optimising
2568 * under those new circumstances */
2569 assert(!(o->op_flags & OPf_STACKED));
2570 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX));
2571 assert(!(o->op_private & ~OPpARG4_MASK));
2573 pm = cUNOPo->op_first;
2574 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */
2576 constop = OpSIBLING(pm);
2577 if (!constop || constop->op_type != OP_CONST)
2579 sv = cSVOPx_sv(constop);
2580 if (SvMAGICAL(sv) || !SvPOK(sv))
2586 /* Scan format for %% and %s and work out how many %s there are.
2587 * Abandon if other format types are found.
2594 for (p = s; p < e; p++) {
2597 if (!UTF8_IS_INVARIANT(*p))
2603 return FALSE; /* lone % at end gives "Invalid conversion" */
2612 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG)
2615 utf8 = cBOOL(SvUTF8(sv));
2619 /* scan args; they must all be in scalar cxt */
2622 kid = OpSIBLING(constop);
2625 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
2628 kid = OpSIBLING(kid);
2631 if (nargs != nformats)
2632 return FALSE; /* e.g. sprintf("%s%s", $a); */
2635 info->nargs = nargs;
2638 info->total_len = total_len;
2639 info->variant = variant;
2647 /* S_maybe_multiconcat():
2649 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly
2650 * convert it (and its children) into an OP_MULTICONCAT. See the code
2651 * comments just before pp_multiconcat() for the full details of what
2652 * OP_MULTICONCAT supports.
2654 * Basically we're looking for an optree with a chain of OP_CONCATS down
2655 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or
2656 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g.
2664 * STRINGIFY -- PADSV[$x]
2667 * ex-PUSHMARK -- CONCAT/S
2669 * CONCAT/S -- PADSV[$d]
2671 * CONCAT -- CONST["-"]
2673 * PADSV[$a] -- PADSV[$b]
2675 * Note that at this stage the OP_SASSIGN may have already been optimised
2676 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT.
2680 S_maybe_multiconcat(pTHX_ OP *o)
2683 OP *lastkidop; /* the right-most of any kids unshifted onto o */
2684 OP *topop; /* the top-most op in the concat tree (often equals o,
2685 unless there are assign/stringify ops above it */
2686 OP *parentop; /* the parent op of topop (or itself if no parent) */
2687 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */
2688 OP *targetop; /* the op corresponding to target=... or target.=... */
2689 OP *stringop; /* the OP_STRINGIFY op, if any */
2690 OP *nextop; /* used for recreating the op_next chain without consts */
2691 OP *kid; /* general-purpose op pointer */
2693 UNOP_AUX_item *lenp;
2694 char *const_str, *p;
2695 struct sprintf_ismc_info sprintf_info;
2697 /* store info about each arg in args[];
2698 * toparg is the highest used slot; argp is a general
2699 * pointer to args[] slots */
2701 void *p; /* initially points to const sv (or null for op);
2702 later, set to SvPV(constsv), with ... */
2703 STRLEN len; /* ... len set to SvPV(..., len) */
2704 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1];
2708 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */
2711 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op;
2712 the last-processed arg will the LHS of one,
2713 as args are processed in reverse order */
2714 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */
2715 STRLEN total_len = 0; /* sum of the lengths of the const segments */
2716 U8 flags = 0; /* what will become the op_flags and ... */
2717 U8 private_flags = 0; /* ... op_private of the multiconcat op */
2718 bool is_sprintf = FALSE; /* we're optimising an sprintf */
2719 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */
2720 bool prev_was_const = FALSE; /* previous arg was a const */
2722 /* -----------------------------------------------------------------
2725 * Examine the optree non-destructively to determine whether it's
2726 * suitable to be converted into an OP_MULTICONCAT. Accumulate
2727 * information about the optree in args[].
2737 assert( o->op_type == OP_SASSIGN
2738 || o->op_type == OP_CONCAT
2739 || o->op_type == OP_SPRINTF
2740 || o->op_type == OP_STRINGIFY);
2742 Zero(&sprintf_info, 1, struct sprintf_ismc_info);
2744 /* first see if, at the top of the tree, there is an assign,
2745 * append and/or stringify */
2747 if (topop->op_type == OP_SASSIGN) {
2749 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN])
2751 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
2753 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */
2756 topop = cBINOPo->op_first;
2757 targetop = OpSIBLING(topop);
2758 if (!targetop) /* probably some sort of syntax error */
2761 else if ( topop->op_type == OP_CONCAT
2762 && (topop->op_flags & OPf_STACKED)
2763 && (!(topop->op_private & OPpCONCAT_NESTED))
2768 /* OPpTARGET_MY shouldn't be able to be set here. If it is,
2769 * decide what to do about it */
2770 assert(!(o->op_private & OPpTARGET_MY));
2772 /* barf on unknown flags */
2773 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY)));
2774 private_flags |= OPpMULTICONCAT_APPEND;
2775 targetop = cBINOPo->op_first;
2777 topop = OpSIBLING(targetop);
2779 /* $x .= <FOO> gets optimised to rcatline instead */
2780 if (topop->op_type == OP_READLINE)
2785 /* Can targetop (the LHS) if it's a padsv, be be optimised
2786 * away and use OPpTARGET_MY instead?
2788 if ( (targetop->op_type == OP_PADSV)
2789 && !(targetop->op_private & OPpDEREF)
2790 && !(targetop->op_private & OPpPAD_STATE)
2791 /* we don't support 'my $x .= ...' */
2792 && ( o->op_type == OP_SASSIGN
2793 || !(targetop->op_private & OPpLVAL_INTRO))
2798 if (topop->op_type == OP_STRINGIFY) {
2799 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY])
2803 /* barf on unknown flags */
2804 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY)));
2806 if ((topop->op_private & OPpTARGET_MY)) {
2807 if (o->op_type == OP_SASSIGN)
2808 return; /* can't have two assigns */
2812 private_flags |= OPpMULTICONCAT_STRINGIFY;
2814 topop = cBINOPx(topop)->op_first;
2815 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK));
2816 topop = OpSIBLING(topop);
2819 if (topop->op_type == OP_SPRINTF) {
2820 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF])
2822 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) {
2823 nargs = sprintf_info.nargs;
2824 total_len = sprintf_info.total_len;
2825 variant = sprintf_info.variant;
2826 utf8 = sprintf_info.utf8;
2828 private_flags |= OPpMULTICONCAT_FAKE;
2830 /* we have an sprintf op rather than a concat optree.
2831 * Skip most of the code below which is associated with
2832 * processing that optree. We also skip phase 2, determining
2833 * whether its cost effective to optimise, since for sprintf,
2834 * multiconcat is *always* faster */
2837 /* note that even if the sprintf itself isn't multiconcatable,
2838 * the expression as a whole may be, e.g. in
2839 * $x .= sprintf("%d",...)
2840 * the sprintf op will be left as-is, but the concat/S op may
2841 * be upgraded to multiconcat
2844 else if (topop->op_type == OP_CONCAT) {
2845 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT])
2848 if ((topop->op_private & OPpTARGET_MY)) {
2849 if (o->op_type == OP_SASSIGN || targmyop)
2850 return; /* can't have two assigns */
2855 /* Is it safe to convert a sassign/stringify/concat op into
2857 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP);
2858 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP);
2859 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP);
2860 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP);
2861 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last)
2862 == STRUCT_OFFSET(UNOP_AUX, op_aux));
2863 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last)
2864 == STRUCT_OFFSET(UNOP_AUX, op_aux));
2866 /* Now scan the down the tree looking for a series of
2867 * CONCAT/OPf_STACKED ops on the LHS (with the last one not
2868 * stacked). For example this tree:
2873 * CONCAT/STACKED -- EXPR5
2875 * CONCAT/STACKED -- EXPR4
2881 * corresponds to an expression like
2883 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5)
2885 * Record info about each EXPR in args[]: in particular, whether it is
2886 * a stringifiable OP_CONST and if so what the const sv is.
2888 * The reason why the last concat can't be STACKED is the difference
2891 * ((($a .= $a) .= $a) .= $a) .= $a
2894 * $a . $a . $a . $a . $a
2896 * The main difference between the optrees for those two constructs
2897 * is the presence of the last STACKED. As well as modifying $a,
2898 * the former sees the changed $a between each concat, so if $s is
2899 * initially 'a', the first returns 'a' x 16, while the latter returns
2900 * 'a' x 5. And pp_multiconcat can't handle that kind of thing.
2910 if ( kid->op_type == OP_CONCAT
2914 k1 = cUNOPx(kid)->op_first;
2916 /* shouldn't happen except maybe after compile err? */
2920 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */
2921 if (kid->op_private & OPpTARGET_MY)
2924 stacked_last = (kid->op_flags & OPf_STACKED);
2936 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2
2937 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2)
2939 /* At least two spare slots are needed to decompose both
2940 * concat args. If there are no slots left, continue to
2941 * examine the rest of the optree, but don't push new values
2942 * on args[]. If the optree as a whole is legal for conversion
2943 * (in particular that the last concat isn't STACKED), then
2944 * the first PERL_MULTICONCAT_MAXARG elements of the optree
2945 * can be converted into an OP_MULTICONCAT now, with the first
2946 * child of that op being the remainder of the optree -
2947 * which may itself later be converted to a multiconcat op
2951 /* the last arg is the rest of the optree */
2956 else if ( argop->op_type == OP_CONST
2957 && ((sv = cSVOPx_sv(argop)))
2958 /* defer stringification until runtime of 'constant'
2959 * things that might stringify variantly, e.g. the radix
2960 * point of NVs, or overloaded RVs */
2961 && (SvPOK(sv) || SvIOK(sv))
2962 && (!SvGMAGICAL(sv))
2965 utf8 |= cBOOL(SvUTF8(sv));
2968 /* this const may be demoted back to a plain arg later;
2969 * make sure we have enough arg slots left */
2971 prev_was_const = !prev_was_const;
2976 prev_was_const = FALSE;
2986 return; /* we don't support ((A.=B).=C)...) */
2988 /* look for two adjacent consts and don't fold them together:
2991 * $o->concat("a")->concat("b")
2994 * (but $o .= "a" . "b" should still fold)
2997 bool seen_nonconst = FALSE;
2998 for (argp = toparg; argp >= args; argp--) {
2999 if (argp->p == NULL) {
3000 seen_nonconst = TRUE;
3006 /* both previous and current arg were constants;
3007 * leave the current OP_CONST as-is */
3015 /* -----------------------------------------------------------------
3018 * At this point we have determined that the optree *can* be converted
3019 * into a multiconcat. Having gathered all the evidence, we now decide
3020 * whether it *should*.
3024 /* we need at least one concat action, e.g.:
3030 * otherwise we could be doing something like $x = "foo", which
3031 * if treated as as a concat, would fail to COW.
3033 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2)
3036 /* Benchmarking seems to indicate that we gain if:
3037 * * we optimise at least two actions into a single multiconcat
3038 * (e.g concat+concat, sassign+concat);
3039 * * or if we can eliminate at least 1 OP_CONST;
3040 * * or if we can eliminate a padsv via OPpTARGET_MY
3044 /* eliminated at least one OP_CONST */
3046 /* eliminated an OP_SASSIGN */
3047 || o->op_type == OP_SASSIGN
3048 /* eliminated an OP_PADSV */
3049 || (!targmyop && is_targable)
3051 /* definitely a net gain to optimise */
3054 /* ... if not, what else? */
3056 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1):
3057 * multiconcat is faster (due to not creating a temporary copy of
3058 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is
3064 && topop->op_type == OP_CONCAT
3066 PADOFFSET t = targmyop->op_targ;
3067 OP *k1 = cBINOPx(topop)->op_first;
3068 OP *k2 = cBINOPx(topop)->op_last;
3069 if ( k2->op_type == OP_PADSV
3071 && ( k1->op_type != OP_PADSV
3072 || k1->op_targ != t)
3077 /* need at least two concats */
3078 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3)
3083 /* -----------------------------------------------------------------
3086 * At this point the optree has been verified as ok to be optimised
3087 * into an OP_MULTICONCAT. Now start changing things.
3092 /* stringify all const args and determine utf8ness */
3095 for (argp = args; argp <= toparg; argp++) {
3096 SV *sv = (SV*)argp->p;
3098 continue; /* not a const op */
3099 if (utf8 && !SvUTF8(sv))
3100 sv_utf8_upgrade_nomg(sv);
3101 argp->p = SvPV_nomg(sv, argp->len);
3102 total_len += argp->len;
3104 /* see if any strings would grow if converted to utf8 */
3106 variant += variant_under_utf8_count((U8 *) argp->p,
3107 (U8 *) argp->p + argp->len);
3111 /* create and populate aux struct */
3115 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
3116 sizeof(UNOP_AUX_item)
3118 PERL_MULTICONCAT_HEADER_SIZE
3119 + ((nargs + 1) * (variant ? 2 : 1))
3122 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1);
3124 /* Extract all the non-const expressions from the concat tree then
3125 * dispose of the old tree, e.g. convert the tree from this:
3129 * STRINGIFY -- TARGET
3131 * ex-PUSHMARK -- CONCAT
3146 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET
3148 * except that if EXPRi is an OP_CONST, it's discarded.
3150 * During the conversion process, EXPR ops are stripped from the tree
3151 * and unshifted onto o. Finally, any of o's remaining original
3152 * childen are discarded and o is converted into an OP_MULTICONCAT.
3154 * In this middle of this, o may contain both: unshifted args on the
3155 * left, and some remaining original args on the right. lastkidop
3156 * is set to point to the right-most unshifted arg to delineate
3157 * between the two sets.
3162 /* create a copy of the format with the %'s removed, and record
3163 * the sizes of the const string segments in the aux struct */
3165 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3167 p = sprintf_info.start;
3170 for (; p < sprintf_info.end; p++) {
3174 (lenp++)->ssize = q - oldq;
3181 lenp->ssize = q - oldq;
3182 assert((STRLEN)(q - const_str) == total_len);
3184 /* Attach all the args (i.e. the kids of the sprintf) to o (which
3185 * may or may not be topop) The pushmark and const ops need to be
3186 * kept in case they're an op_next entry point.
3188 lastkidop = cLISTOPx(topop)->op_last;
3189 kid = cUNOPx(topop)->op_first; /* pushmark */
3191 op_null(OpSIBLING(kid)); /* const */
3193 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */
3194 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */
3195 lastkidop->op_next = o;
3200 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
3204 /* Concatenate all const strings into const_str.
3205 * Note that args[] contains the RHS args in reverse order, so
3206 * we scan args[] from top to bottom to get constant strings
3209 for (argp = toparg; argp >= args; argp--) {
3211 /* not a const op */
3212 (++lenp)->ssize = -1;
3214 STRLEN l = argp->len;
3215 Copy(argp->p, p, l, char);
3217 if (lenp->ssize == -1)
3228 for (argp = args; argp <= toparg; argp++) {
3229 /* only keep non-const args, except keep the first-in-next-chain
3230 * arg no matter what it is (but nulled if OP_CONST), because it
3231 * may be the entry point to this subtree from the previous
3234 bool last = (argp == toparg);
3237 /* set prev to the sibling *before* the arg to be cut out,
3238 * e.g. when cutting EXPR:
3243 * prev= CONCAT -- EXPR
3246 if (argp == args && kid->op_type != OP_CONCAT) {
3247 /* in e.g. '$x .= f(1)' there's no RHS concat tree
3248 * so the expression to be cut isn't kid->op_last but
3251 /* find the op before kid */
3253 o2 = cUNOPx(parentop)->op_first;
3254 while (o2 && o2 != kid) {
3262 else if (kid == o && lastkidop)
3263 prev = last ? lastkidop : OpSIBLING(lastkidop);
3265 prev = last ? NULL : cUNOPx(kid)->op_first;
3267 if (!argp->p || last) {
3269 OP *aop = op_sibling_splice(kid, prev, 1, NULL);
3270 /* and unshift to front of o */
3271 op_sibling_splice(o, NULL, 0, aop);
3272 /* record the right-most op added to o: later we will
3273 * free anything to the right of it */
3276 aop->op_next = nextop;
3279 /* null the const at start of op_next chain */
3283 nextop = prev->op_next;
3286 /* the last two arguments are both attached to the same concat op */
3287 if (argp < toparg - 1)
3292 /* Populate the aux struct */
3294 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs;
3295 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str;
3296 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len;
3297 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str;
3298 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len;
3300 /* if variant > 0, calculate a variant const string and lengths where
3301 * the utf8 version of the string will take 'variant' more bytes than
3305 char *p = const_str;
3306 STRLEN ulen = total_len + variant;
3307 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
3308 UNOP_AUX_item *ulens = lens + (nargs + 1);
3309 char *up = (char*)PerlMemShared_malloc(ulen);
3312 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up;
3313 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen;
3315 for (n = 0; n < (nargs + 1); n++) {
3317 char * orig_up = up;
3318 for (i = (lens++)->ssize; i > 0; i--) {
3320 append_utf8_from_native_byte(c, (U8**)&up);
3322 (ulens++)->ssize = (i < 0) ? i : up - orig_up;
3327 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep
3328 * that op's first child - an ex-PUSHMARK - because the op_next of
3329 * the previous op may point to it (i.e. it's the entry point for
3334 ? op_sibling_splice(o, lastkidop, 1, NULL)
3335 : op_sibling_splice(stringop, NULL, 1, NULL);
3336 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK));
3337 op_sibling_splice(o, NULL, 0, pmop);
3344 * target .= A.B.C...
3350 if (o->op_type == OP_SASSIGN) {
3351 /* Move the target subtree from being the last of o's children
3352 * to being the last of o's preserved children.
3353 * Note the difference between 'target = ...' and 'target .= ...':
3354 * for the former, target is executed last; for the latter,
3357 kid = OpSIBLING(lastkidop);
3358 op_sibling_splice(o, kid, 1, NULL); /* cut target op */
3359 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */
3360 lastkidop->op_next = kid->op_next;
3361 lastkidop = targetop;
3364 /* Move the target subtree from being the first of o's
3365 * original children to being the first of *all* o's children.
3368 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */
3369 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/
3372 /* if the RHS of .= doesn't contain a concat (e.g.
3373 * $x .= "foo"), it gets missed by the "strip ops from the
3374 * tree and add to o" loop earlier */
3375 assert(topop->op_type != OP_CONCAT);
3377 /* in e.g. $x .= "$y", move the $y expression
3378 * from being a child of OP_STRINGIFY to being the
3379 * second child of the OP_CONCAT
3381 assert(cUNOPx(stringop)->op_first == topop);
3382 op_sibling_splice(stringop, NULL, 1, NULL);
3383 op_sibling_splice(o, cUNOPo->op_first, 0, topop);
3385 assert(topop == OpSIBLING(cBINOPo->op_first));
3394 * my $lex = A.B.C...
3397 * The original padsv op is kept but nulled in case it's the
3398 * entry point for the optree (which it will be for
3401 private_flags |= OPpTARGET_MY;
3402 private_flags |= (targetop->op_private & OPpLVAL_INTRO);
3403 o->op_targ = targetop->op_targ;
3404 targetop->op_targ = 0;
3408 flags |= OPf_STACKED;
3410 else if (targmyop) {
3411 private_flags |= OPpTARGET_MY;
3412 if (o != targmyop) {
3413 o->op_targ = targmyop->op_targ;
3414 targmyop->op_targ = 0;
3418 /* detach the emaciated husk of the sprintf/concat optree and free it */
3420 kid = op_sibling_splice(o, lastkidop, 1, NULL);
3426 /* and convert o into a multiconcat */
3428 o->op_flags = (flags|OPf_KIDS|stacked_last
3429 |(o->op_flags & (OPf_WANT|OPf_PARENS)));
3430 o->op_private = private_flags;
3431 o->op_type = OP_MULTICONCAT;
3432 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT];
3433 cUNOP_AUXo->op_aux = aux;
3437 /* do all the final processing on an optree (e.g. running the peephole
3438 * optimiser on it), then attach it to cv (if cv is non-null)
3442 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
3446 /* XXX for some reason, evals, require and main optrees are
3447 * never attached to their CV; instead they just hang off
3448 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
3449 * and get manually freed when appropriate */
3451 startp = &CvSTART(cv);
3453 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
3456 optree->op_private |= OPpREFCOUNTED;
3457 OpREFCNT_set(optree, 1);
3458 optimize_optree(optree);
3460 finalize_optree(optree);
3461 S_prune_chain_head(startp);
3464 /* now that optimizer has done its work, adjust pad values */
3465 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
3466 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
3472 =for apidoc optimize_optree
3474 This function applies some optimisations to the optree in top-down order.
3475 It is called before the peephole optimizer, which processes ops in
3476 execution order. Note that finalize_optree() also does a top-down scan,
3477 but is called *after* the peephole optimizer.
3483 Perl_optimize_optree(pTHX_ OP* o)
3485 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE;
3488 SAVEVPTR(PL_curcop);
3496 /* helper for optimize_optree() which optimises on op then recurses
3497 * to optimise any children.
3501 S_optimize_op(pTHX_ OP* o)
3505 PERL_ARGS_ASSERT_OPTIMIZE_OP;
3507 assert(o->op_type != OP_FREED);
3509 switch (o->op_type) {
3512 PL_curcop = ((COP*)o); /* for warnings */
3520 S_maybe_multiconcat(aTHX_ o);
3524 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
3525 DEFER_OP(cPMOPo->op_pmreplrootu.op_pmreplroot);
3532 if (o->op_flags & OPf_KIDS) {
3535 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
3539 DEFER_REVERSE(child_count);
3541 } while ( ( o = POP_DEFERRED_OP() ) );
3548 =for apidoc finalize_optree
3550 This function finalizes the optree. Should be called directly after
3551 the complete optree is built. It does some additional
3552 checking which can't be done in the normal C<ck_>xxx functions and makes
3553 the tree thread-safe.
3558 Perl_finalize_optree(pTHX_ OP* o)
3560 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
3563 SAVEVPTR(PL_curcop);
3571 /* Relocate sv to the pad for thread safety.
3572 * Despite being a "constant", the SV is written to,
3573 * for reference counts, sv_upgrade() etc. */
3574 PERL_STATIC_INLINE void
3575 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
3578 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
3580 ix = pad_alloc(OP_CONST, SVf_READONLY);
3581 SvREFCNT_dec(PAD_SVl(ix));
3582 PAD_SETSV(ix, *svp);
3583 /* XXX I don't know how this isn't readonly already. */
3584 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
3591 =for apidoc s|OP*|traverse_op_tree|OP* top|OP* o
3593 Return the next op in a depth-first traversal of the op tree,
3594 returning NULL when the traversal is complete.
3596 The initial call must supply the root of the tree as both top and o.
3598 For now it's static, but it may be exposed to the API in the future.
3604 S_traverse_op_tree(pTHX_ OP *top, OP *o) {
3607 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;
3609 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {
3610 return cUNOPo->op_first;
3612 else if ((sib = OpSIBLING(o))) {
3616 OP *parent = o->op_sibparent;
3617 assert(!(o->op_moresib));
3618 while (parent && parent != top) {
3619 OP *sib = OpSIBLING(parent);
3622 parent = parent->op_sibparent;
3630 S_finalize_op(pTHX_ OP* o)
3633 PERL_ARGS_ASSERT_FINALIZE_OP;
3636 assert(o->op_type != OP_FREED);
3638 switch (o->op_type) {
3641 PL_curcop = ((COP*)o); /* for warnings */
3644 if (OpHAS_SIBLING(o)) {
3645 OP *sib = OpSIBLING(o);
3646 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
3647 && ckWARN(WARN_EXEC)
3648 && OpHAS_SIBLING(sib))
3650 const OPCODE type = OpSIBLING(sib)->op_type;
3651 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
3652 const line_t oldline = CopLINE(PL_curcop);
3653 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
3654 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3655 "Statement unlikely to be reached");
3656 Perl_warner(aTHX_ packWARN(WARN_EXEC),
3657 "\t(Maybe you meant system() when you said exec()?)\n");
3658 CopLINE_set(PL_curcop, oldline);
3665 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
3666 GV * const gv = cGVOPo_gv;
3667 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
3668 /* XXX could check prototype here instead of just carping */
3669 SV * const sv = sv_newmortal();
3670 gv_efullname3(sv, gv, NULL);
3671 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3672 "%" SVf "() called too early to check prototype",
3679 if (cSVOPo->op_private & OPpCONST_STRICT)
3680 no_bareword_allowed(o);
3684 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
3689 /* Relocate all the METHOP's SVs to the pad for thread safety. */
3690 case OP_METHOD_NAMED:
3691 case OP_METHOD_SUPER:
3692 case OP_METHOD_REDIR:
3693 case OP_METHOD_REDIR_SUPER:
3694 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
3703 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
3706 rop = (UNOP*)((BINOP*)o)->op_first;
3711 S_scalar_slice_warning(aTHX_ o);
3715 kid = OpSIBLING(cLISTOPo->op_first);
3716 if (/* I bet there's always a pushmark... */
3717 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
3718 && OP_TYPE_ISNT_NN(kid, OP_CONST))
3723 key_op = (SVOP*)(kid->op_type == OP_CONST
3725 : OpSIBLING(kLISTOP->op_first));
3727 rop = (UNOP*)((LISTOP*)o)->op_last;
3730 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
3732 S_check_hash_fields_and_hekify(aTHX_ rop, key_op, 1);
3736 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
3740 S_scalar_slice_warning(aTHX_ o);
3744 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
3745 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
3753 if (o->op_flags & OPf_KIDS) {
3756 /* check that op_last points to the last sibling, and that
3757 * the last op_sibling/op_sibparent field points back to the
3758 * parent, and that the only ops with KIDS are those which are
3759 * entitled to them */
3760 U32 type = o->op_type;
3764 if (type == OP_NULL) {
3766 /* ck_glob creates a null UNOP with ex-type GLOB
3767 * (which is a list op. So pretend it wasn't a listop */
3768 if (type == OP_GLOB)
3771 family = PL_opargs[type] & OA_CLASS_MASK;
3773 has_last = ( family == OA_BINOP
3774 || family == OA_LISTOP
3775 || family == OA_PMOP
3776 || family == OA_LOOP
3778 assert( has_last /* has op_first and op_last, or ...
3779 ... has (or may have) op_first: */
3780 || family == OA_UNOP
3781 || family == OA_UNOP_AUX
3782 || family == OA_LOGOP
3783 || family == OA_BASEOP_OR_UNOP
3784 || family == OA_FILESTATOP
3785 || family == OA_LOOPEXOP
3786 || family == OA_METHOP
3787 || type == OP_CUSTOM
3788 || type == OP_NULL /* new_logop does this */
3791 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
3792 if (!OpHAS_SIBLING(kid)) {
3794 assert(kid == cLISTOPo->op_last);
3795 assert(kid->op_sibparent == o);
3800 } while (( o = traverse_op_tree(top, o)) != NULL);
3804 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
3806 Propagate lvalue ("modifiable") context to an op and its children.
3807 C<type> represents the context type, roughly based on the type of op that
3808 would do the modifying, although C<local()> is represented by C<OP_NULL>,
3809 because it has no op type of its own (it is signalled by a flag on
3812 This function detects things that can't be modified, such as C<$x+1>, and
3813 generates errors for them. For example, C<$x+1 = 2> would cause it to be
3814 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
3816 It also flags things that need to behave specially in an lvalue context,
3817 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
3823 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
3826 PadnameLVALUE_on(pn);
3827 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
3829 /* RT #127786: cv can be NULL due to an eval within the DB package
3830 * called from an anon sub - anon subs don't have CvOUTSIDE() set
3831 * unless they contain an eval, but calling eval within DB
3832 * pretends the eval was done in the caller's scope.
3836 assert(CvPADLIST(cv));
3838 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
3839 assert(PadnameLEN(pn));
3840 PadnameLVALUE_on(pn);
3845 S_vivifies(const OPCODE type)
3848 case OP_RV2AV: case OP_ASLICE:
3849 case OP_RV2HV: case OP_KVASLICE:
3850 case OP_RV2SV: case OP_HSLICE:
3851 case OP_AELEMFAST: case OP_KVHSLICE:
3860 S_lvref(pTHX_ OP *o, I32 type)
3864 switch (o->op_type) {
3866 for (kid = OpSIBLING(cUNOPo->op_first); kid;
3867 kid = OpSIBLING(kid))
3868 S_lvref(aTHX_ kid, type);
3873 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
3874 o->op_flags |= OPf_STACKED;
3875 if (o->op_flags & OPf_PARENS) {
3876 if (o->op_private & OPpLVAL_INTRO) {
3877 yyerror(Perl_form(aTHX_ "Can't modify reference to "
3878 "localized parenthesized array in list assignment"));
3882 OpTYPE_set(o, OP_LVAVREF);
3883 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
3884 o->op_flags |= OPf_MOD|OPf_REF;
3887 o->op_private |= OPpLVREF_AV;
3890 kid = cUNOPo->op_first;
3891 if (kid->op_type == OP_NULL)
3892 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
3894 o->op_private = OPpLVREF_CV;
3895 if (kid->op_type == OP_GV)
3896 o->op_flags |= OPf_STACKED;
3897 else if (kid->op_type == OP_PADCV) {
3898 o->op_targ = kid->op_targ;
3900 op_free(cUNOPo->op_first);
3901 cUNOPo->op_first = NULL;
3902 o->op_flags &=~ OPf_KIDS;
3907 if (o->op_flags & OPf_PARENS) {
3909 yyerror(Perl_form(aTHX_ "Can't modify reference to "
3910 "parenthesized hash in list assignment"));
3913 o->op_private |= OPpLVREF_HV;
3917 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
3918 o->op_flags |= OPf_STACKED;
3921 if (o->op_flags & OPf_PARENS) goto parenhash;
3922 o->op_private |= OPpLVREF_HV;
3925 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
3928 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
3929 if (o->op_flags & OPf_PARENS) goto slurpy;
3930 o->op_private |= OPpLVREF_AV;
3934 o->op_private |= OPpLVREF_ELEM;
3935 o->op_flags |= OPf_STACKED;
3939 OpTYPE_set(o, OP_LVREFSLICE);
3940 o->op_private &= OPpLVAL_INTRO;
3943 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3945 else if (!(o->op_flags & OPf_KIDS))
3947 if (o->op_targ != OP_LIST) {
3948 S_lvref(aTHX_ cBINOPo->op_first, type);
3953 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
3954 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
3955 S_lvref(aTHX_ kid, type);
3959 if (o->op_flags & OPf_PARENS)
3964 /* diag_listed_as: Can't modify reference to %s in %s assignment */
3965 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
3966 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
3972 OpTYPE_set(o, OP_LVREF);
3974 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
3975 if (type == OP_ENTERLOOP)
3976 o->op_private |= OPpLVREF_ITER;
3979 PERL_STATIC_INLINE bool
3980 S_potential_mod_type(I32 type)
3982 /* Types that only potentially result in modification. */
3983 return type == OP_GREPSTART || type == OP_ENTERSUB
3984 || type == OP_REFGEN || type == OP_LEAVESUBLV;
3988 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
3992 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
3995 if (!o || (PL_parser && PL_parser->error_count))
3998 if ((o->op_private & OPpTARGET_MY)
3999 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
4004 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
4006 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
4008 switch (o->op_type) {
4013 if ((o->op_flags & OPf_PARENS))
4017 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
4018 !(o->op_flags & OPf_STACKED)) {
4019 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
4020 assert(cUNOPo->op_first->op_type == OP_NULL);
4021 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
4024 else { /* lvalue subroutine call */
4025 o->op_private |= OPpLVAL_INTRO;
4026 PL_modcount = RETURN_UNLIMITED_NUMBER;
4027 if (S_potential_mod_type(type)) {
4028 o->op_private |= OPpENTERSUB_INARGS;
4031 else { /* Compile-time error message: */
4032 OP *kid = cUNOPo->op_first;
4037 if (kid->op_type != OP_PUSHMARK) {
4038 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
4040 "panic: unexpected lvalue entersub "
4041 "args: type/targ %ld:%" UVuf,
4042 (long)kid->op_type, (UV)kid->op_targ);
4043 kid = kLISTOP->op_first;
4045 while (OpHAS_SIBLING(kid))
4046 kid = OpSIBLING(kid);
4047 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
4048 break; /* Postpone until runtime */
4051 kid = kUNOP->op_first;
4052 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
4053 kid = kUNOP->op_first;
4054 if (kid->op_type == OP_NULL)
4056 "Unexpected constant lvalue entersub "
4057 "entry via type/targ %ld:%" UVuf,
4058 (long)kid->op_type, (UV)kid->op_targ);
4059 if (kid->op_type != OP_GV) {
4066 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
4067 ? MUTABLE_CV(SvRV(gv))
4073 if (flags & OP_LVALUE_NO_CROAK)
4076 namesv = cv_name(cv, NULL, 0);
4077 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
4078 "subroutine call of &%" SVf " in %s",
4079 SVfARG(namesv), PL_op_desc[type]),
4087 if (flags & OP_LVALUE_NO_CROAK) return NULL;
4088 /* grep, foreach, subcalls, refgen */
4089 if (S_potential_mod_type(type))
4091 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
4092 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
4095 type ? PL_op_desc[type] : "local"));
4108 case OP_RIGHT_SHIFT:
4117 if (!(o->op_flags & OPf_STACKED))
4123 if (o->op_flags & OPf_STACKED) {
4127 if (!(o->op_private & OPpREPEAT_DOLIST))
4130 const I32 mods = PL_modcount;
4131 modkids(cBINOPo->op_first, type);
4132 if (type != OP_AASSIGN)
4134 kid = cBINOPo->op_last;
4135 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
4136 const IV iv = SvIV(kSVOP_sv);
4137 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
4139 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
4142 PL_modcount = RETURN_UNLIMITED_NUMBER;
4148 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
4149 op_lvalue(kid, type);
4154 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
4155 PL_modcount = RETURN_UNLIMITED_NUMBER;
4156 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4157 fiable since some contexts need to know. */
4158 o->op_flags |= OPf_MOD;
4163 if (scalar_mod_type(o, type))
4165 ref(cUNOPo->op_first, o->op_type);
4172 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
4173 if (type == OP_LEAVESUBLV && (
4174 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
4175 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4177 o->op_private |= OPpMAYBE_LVSUB;
4181 PL_modcount = RETURN_UNLIMITED_NUMBER;
4186 if (type == OP_LEAVESUBLV)
4187 o->op_private |= OPpMAYBE_LVSUB;
4190 if (type == OP_LEAVESUBLV
4191 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS)
4192 o->op_private |= OPpMAYBE_LVSUB;
4195 PL_hints |= HINT_BLOCK_SCOPE;
4196 if (type == OP_LEAVESUBLV)
4197 o->op_private |= OPpMAYBE_LVSUB;
4201 ref(cUNOPo->op_first, o->op_type);
4205 PL_hints |= HINT_BLOCK_SCOPE;
4215 case OP_AELEMFAST_LEX:
4222 PL_modcount = RETURN_UNLIMITED_NUMBER;
4223 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
4225 /* Treat \(@foo) like ordinary list, but still mark it as modi-
4226 fiable since some contexts need to know. */
4227 o->op_flags |= OPf_MOD;
4230 if (scalar_mod_type(o, type))
4232 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
4233 && type == OP_LEAVESUBLV)
4234 o->op_private |= OPpMAYBE_LVSUB;
4238 if (!type) /* local() */
4239 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
4240 PNfARG(PAD_COMPNAME(o->op_targ)));
4241 if (!(o->op_private & OPpLVAL_INTRO)
4242 || ( type != OP_SASSIGN && type != OP_AASSIGN
4243 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
4244 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
4252 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
4256 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
4262 if (type == OP_LEAVESUBLV)
4263 o->op_private |= OPpMAYBE_LVSUB;
4264 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
4265 /* substr and vec */
4266 /* If this op is in merely potential (non-fatal) modifiable
4267 context, then apply OP_ENTERSUB context to
4268 the kid op (to avoid croaking). Other-
4269 wise pass this op’s own type so the correct op is mentioned
4270 in error messages. */
4271 op_lvalue(OpSIBLING(cBINOPo->op_first),
4272 S_potential_mod_type(type)
4280 ref(cBINOPo->op_first, o->op_type);
4281 if (type == OP_ENTERSUB &&
4282 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
4283 o->op_private |= OPpLVAL_DEFER;
4284 if (type == OP_LEAVESUBLV)
4285 o->op_private |= OPpMAYBE_LVSUB;
4292 o->op_private |= OPpLVALUE;
4298 if (o->op_flags & OPf_KIDS)
4299 op_lvalue(cLISTOPo->op_last, type);
4304 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
4306 else if (!(o->op_flags & OPf_KIDS))
4309 if (o->op_targ != OP_LIST) {
4310 OP *sib = OpSIBLING(cLISTOPo->op_first);
4311 /* OP_TRANS and OP_TRANSR with argument have a weird optree
4318 * compared with things like OP_MATCH which have the argument
4324 * so handle specially to correctly get "Can't modify" croaks etc
4327 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
4329 /* this should trigger a "Can't modify transliteration" err */
4330 op_lvalue(sib, type);
4332 op_lvalue(cBINOPo->op_first, type);
4338 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
4339 /* elements might be in void context because the list is
4340 in scalar context or because they are attribute sub calls */
4341 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
4342 op_lvalue(kid, type);
4350 if (type == OP_LEAVESUBLV
4351 || !S_vivifies(cLOGOPo->op_first->op_type))
4352 op_lvalue(cLOGOPo->op_first, type);
4353 if (type == OP_LEAVESUBLV
4354 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
4355 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
4359 if (type == OP_NULL) { /* local */
4361 if (!FEATURE_MYREF_IS_ENABLED)
4362 Perl_croak(aTHX_ "The experimental declared_refs "
4363 "feature is not enabled");
4364 Perl_ck_warner_d(aTHX_
4365 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
4366 "Declaring references is experimental");
4367 op_lvalue(cUNOPo->op_first, OP_NULL);
4370 if (type != OP_AASSIGN && type != OP_SASSIGN
4371 && type != OP_ENTERLOOP)
4373 /* Don’t bother applying lvalue context to the ex-list. */
4374 kid = cUNOPx(cUNOPo->op_first)->op_first;
4375 assert (!OpHAS_SIBLING(kid));
4378 if (type == OP_NULL) /* local */
4380 if (type != OP_AASSIGN) goto nomod;
4381 kid = cUNOPo->op_first;
4384 const U8 ec = PL_parser ? PL_parser->error_count : 0;
4385 S_lvref(aTHX_ kid, type);
4386 if (!PL_parser || PL_parser->error_count == ec) {
4387 if (!FEATURE_REFALIASING_IS_ENABLED)
4389 "Experimental aliasing via reference not enabled");
4390 Perl_ck_warner_d(aTHX_
4391 packWARN(WARN_EXPERIMENTAL__REFALIASING),
4392 "Aliasing via reference is experimental");
4395 if (o->op_type == OP_REFGEN)
4396 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
4401 if ((o->op_private & OPpSPLIT_ASSIGN)) {
4402 /* This is actually @array = split. */
4403 PL_modcount = RETURN_UNLIMITED_NUMBER;
4409 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
4413 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that
4414 their argument is a filehandle; thus \stat(".") should not set
4416 if (type == OP_REFGEN &&
4417 PL_check[o->op_type] == Perl_ck_ftst)
4420 if (type != OP_LEAVESUBLV)
4421 o->op_flags |= OPf_MOD;
4423 if (type == OP_AASSIGN || type == OP_SASSIGN)
4424 o->op_flags |= OPf_SPECIAL
4425 |(o->op_type == OP_ENTERSUB ? 0 : OPf_REF);
4426 else if (!type) { /* local() */
4429 o->op_private |= OPpLVAL_INTRO;
4430 o->op_flags &= ~OPf_SPECIAL;
4431 PL_hints |= HINT_BLOCK_SCOPE;
4436 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
4437 "Useless localization of %s", OP_DESC(o));
4440 else if (type != OP_GREPSTART && type != OP_ENTERSUB
4441 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB)
4442 o->op_flags |= OPf_REF;
4447 S_scalar_mod_type(const OP *o, I32 type)
4452 if (o && o->op_type == OP_RV2GV)
4476 case OP_RIGHT_SHIFT:
4505 S_is_handle_constructor(const OP *o, I32 numargs)
4507 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
4509 switch (o->op_type) {
4517 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
4530 S_refkids(pTHX_ OP *o, I32 type)
4532 if (o && o->op_flags & OPf_KIDS) {
4534 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
4541 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
4546 PERL_ARGS_ASSERT_DOREF;
4548 if (PL_parser && PL_parser->error_count)
4551 switch (o->op_type) {
4553 if ((type == OP_EXISTS || type == OP_DEFINED) &&
4554 !(o->op_flags & OPf_STACKED)) {
4555 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
4556 assert(cUNOPo->op_first->op_type == OP_NULL);
4557 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
4558 o->op_flags |= OPf_SPECIAL;
4560 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
4561 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
4562 : type == OP_RV2HV ? OPpDEREF_HV
4564 o->op_flags |= OPf_MOD;
4570 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
4571 doref(kid, type, set_op_ref);
4574 if (type == OP_DEFINED)
4575 o->op_flags |= OPf_SPECIAL; /* don't create GV */
4576 doref(cUNOPo->op_first, o->op_type, set_op_ref);
4579 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
4580 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
4581 : type == OP_RV2HV ? OPpDEREF_HV
4583 o->op_flags |= OPf_MOD;
4590 o->op_flags |= OPf_REF;
4593 if (type == OP_DEFINED)
4594 o->op_flags |= OPf_SPECIAL; /* don't create GV */
4595 doref(cUNOPo->op_first, o->op_type, set_op_ref);
4601 o->op_flags |= OPf_REF;
4606 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
4608 doref(cBINOPo->op_first, type, set_op_ref);
4612 doref(cBINOPo->op_first, o->op_type, set_op_ref);
4613 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
4614 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
4615 : type == OP_RV2HV ? OPpDEREF_HV
4617 o->op_flags |= OPf_MOD;
4627 if (!(o->op_flags & OPf_KIDS))
4629 doref(cLISTOPo->op_last, type, set_op_ref);
4639 S_dup_attrlist(pTHX_ OP *o)
4643 PERL_ARGS_ASSERT_DUP_ATTRLIST;
4645 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
4646 * where the first kid is OP_PUSHMARK and the remaining ones
4647 * are OP_CONST. We need to push the OP_CONST values.
4649 if (o->op_type == OP_CONST)
4650 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
4652 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
4654 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
4655 if (o->op_type == OP_CONST)
4656 rop = op_append_elem(OP_LIST, rop,
4657 newSVOP(OP_CONST, o->op_flags,
4658 SvREFCNT_inc_NN(cSVOPo->op_sv)));
4665 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
4667 PERL_ARGS_ASSERT_APPLY_ATTRS;
4669 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
4671 /* fake up C<use attributes $pkg,$rv,@attrs> */
4673 #define ATTRSMODULE "attributes"
4674 #define ATTRSMODULE_PM "attributes.pm"
4677 aTHX_ PERL_LOADMOD_IMPORT_OPS,
4678 newSVpvs(ATTRSMODULE),
4680 op_prepend_elem(OP_LIST,
4681 newSVOP(OP_CONST, 0, stashsv),
4682 op_prepend_elem(OP_LIST,
4683 newSVOP(OP_CONST, 0,
4685 dup_attrlist(attrs))));
4690 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
4692 OP *pack, *imop, *arg;
4693 SV *meth, *stashsv, **svp;
4695 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
4700 assert(target->op_type == OP_PADSV ||
4701 target->op_type == OP_PADHV ||
4702 target->op_type == OP_PADAV);
4704 /* Ensure that attributes.pm is loaded. */
4705 /* Don't force the C<use> if we don't need it. */
4706 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
4707 if (svp && *svp != &PL_sv_undef)
4708 NOOP; /* already in %INC */
4710 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
4711 newSVpvs(ATTRSMODULE), NULL);
4713 /* Need package name for method call. */
4714 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
4716 /* Build up the real arg-list. */
4717 stashsv = newSVhek(HvNAME_HEK(stash));
4719 arg = newOP(OP_PADSV, 0);
4720 arg->op_targ = target->op_targ;
4721 arg = op_prepend_elem(OP_LIST,
4722 newSVOP(OP_CONST, 0, stashsv),
4723 op_prepend_elem(OP_LIST,
4724 newUNOP(OP_REFGEN, 0,
4726 dup_attrlist(attrs)));
4728 /* Fake up a method call to import */
4729 meth = newSVpvs_share("import");
4730 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
4731 op_append_elem(OP_LIST,
4732 op_prepend_elem(OP_LIST, pack, arg),