3 * Copyright (C) 1991-2022 by Larry Wall and others
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * Aragorn sped on up the hill. Every now and again he bent to the ground.
12 * Hobbits go light, and their footprints are not easy even for a Ranger to
13 * read, but not far from the top a spring crossed the path, and in the wet
14 * earth he saw what he was seeking.
15 * 'I read the signs aright,' he said to himself. 'Frodo ran to the hill-top.
16 * I wonder what he saw there? But he returned by the same way, and went down
21 #define PERL_IN_PEEP_C
25 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
29 S_scalar_slice_warning(pTHX_ const OP *o)
32 const bool is_hash = o->op_type == OP_HSLICE
33 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
36 if (!(o->op_private & OPpSLICEWARNING))
38 if (PL_parser && PL_parser->error_count)
39 /* This warning can be nonsensical when there is a syntax error. */
42 kid = cLISTOPo->op_first;
43 kid = OpSIBLING(kid); /* get past pushmark */
44 /* weed out false positives: any ops that can return lists */
45 switch (kid->op_type) {
71 /* Don't warn if we have a nulled list either. */
72 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
75 assert(OpSIBLING(kid));
76 name = op_varname(OpSIBLING(kid));
77 if (!name) /* XS module fiddling with the op tree */
79 warn_elem_scalar_context(kid, name, is_hash, true);
83 /* info returned by S_sprintf_is_multiconcatable() */
85 struct sprintf_ismc_info {
86 SSize_t nargs; /* num of args to sprintf (not including the format) */
87 char *start; /* start of raw format string */
88 char *end; /* bytes after end of raw format string */
89 STRLEN total_len; /* total length (in bytes) of format string, not
90 including '%s' and half of '%%' */
91 STRLEN variant; /* number of bytes by which total_len_p would grow
92 if upgraded to utf8 */
93 bool utf8; /* whether the format is utf8 */
96 /* is the OP_SPRINTF o suitable for converting into a multiconcat op?
97 * i.e. its format argument is a const string with only '%s' and '%%'
98 * formats, and the number of args is known, e.g.
99 * sprintf "a=%s f=%s", $a[0], scalar(f());
101 * sprintf "i=%d a=%s f=%s", $i, @a, f();
103 * If successful, the sprintf_ismc_info struct pointed to by info will be
108 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info)
110 OP *pm, *constop, *kid;
113 SSize_t nargs, nformats;
114 STRLEN cur, total_len, variant;
117 /* if sprintf's behaviour changes, die here so that someone
118 * can decide whether to enhance this function or skip optimising
119 * under those new circumstances */
120 assert(!(o->op_flags & OPf_STACKED));
121 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX));
122 assert(!(o->op_private & ~OPpARG4_MASK));
124 pm = cUNOPo->op_first;
125 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */
127 constop = OpSIBLING(pm);
128 if (!constop || constop->op_type != OP_CONST)
130 sv = cSVOPx_sv(constop);
131 if (SvMAGICAL(sv) || !SvPOK(sv))
137 /* Scan format for %% and %s and work out how many %s there are.
138 * Abandon if other format types are found.
145 for (p = s; p < e; p++) {
148 if (!UTF8_IS_INVARIANT(*p))
154 return FALSE; /* lone % at end gives "Invalid conversion" */
163 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG)
166 utf8 = cBOOL(SvUTF8(sv));
170 /* scan args; they must all be in scalar cxt */
173 kid = OpSIBLING(constop);
176 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
179 kid = OpSIBLING(kid);
182 if (nargs != nformats)
183 return FALSE; /* e.g. sprintf("%s%s", $a); */
189 info->total_len = total_len;
190 info->variant = variant;
196 /* S_maybe_multiconcat():
198 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly
199 * convert it (and its children) into an OP_MULTICONCAT. See the code
200 * comments just before pp_multiconcat() for the full details of what
201 * OP_MULTICONCAT supports.
203 * Basically we're looking for an optree with a chain of OP_CONCATS down
204 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or
205 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g.
213 * STRINGIFY -- PADSV[$x]
216 * ex-PUSHMARK -- CONCAT/S
218 * CONCAT/S -- PADSV[$d]
220 * CONCAT -- CONST["-"]
222 * PADSV[$a] -- PADSV[$b]
224 * Note that at this stage the OP_SASSIGN may have already been optimised
225 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT.
229 S_maybe_multiconcat(pTHX_ OP *o)
231 OP *lastkidop; /* the right-most of any kids unshifted onto o */
232 OP *topop; /* the top-most op in the concat tree (often equals o,
233 unless there are assign/stringify ops above it */
234 OP *parentop; /* the parent op of topop (or itself if no parent) */
235 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */
236 OP *targetop; /* the op corresponding to target=... or target.=... */
237 OP *stringop; /* the OP_STRINGIFY op, if any */
238 OP *nextop; /* used for recreating the op_next chain without consts */
239 OP *kid; /* general-purpose op pointer */
243 struct sprintf_ismc_info sprintf_info;
245 /* store info about each arg in args[];
246 * toparg is the highest used slot; argp is a general
247 * pointer to args[] slots */
249 void *p; /* initially points to const sv (or null for op);
250 later, set to SvPV(constsv), with ... */
251 STRLEN len; /* ... len set to SvPV(..., len) */
252 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1];
256 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */
259 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op;
260 the last-processed arg will the LHS of one,
261 as args are processed in reverse order */
262 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */
263 STRLEN total_len = 0; /* sum of the lengths of the const segments */
264 U8 flags = 0; /* what will become the op_flags and ... */
265 U8 private_flags = 0; /* ... op_private of the multiconcat op */
266 bool is_sprintf = FALSE; /* we're optimising an sprintf */
267 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */
268 bool prev_was_const = FALSE; /* previous arg was a const */
270 /* -----------------------------------------------------------------
273 * Examine the optree non-destructively to determine whether it's
274 * suitable to be converted into an OP_MULTICONCAT. Accumulate
275 * information about the optree in args[].
285 assert( o->op_type == OP_SASSIGN
286 || o->op_type == OP_CONCAT
287 || o->op_type == OP_SPRINTF
288 || o->op_type == OP_STRINGIFY);
290 Zero(&sprintf_info, 1, struct sprintf_ismc_info);
292 /* first see if, at the top of the tree, there is an assign,
293 * append and/or stringify */
295 if (topop->op_type == OP_SASSIGN) {
297 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN])
299 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
301 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */
304 topop = cBINOPo->op_first;
305 targetop = OpSIBLING(topop);
306 if (!targetop) /* probably some sort of syntax error */
309 /* don't optimise away assign in 'local $foo = ....' */
310 if ( (targetop->op_private & OPpLVAL_INTRO)
311 /* these are the common ops which do 'local', but
313 && ( targetop->op_type == OP_GVSV
314 || targetop->op_type == OP_RV2SV
315 || targetop->op_type == OP_AELEM
316 || targetop->op_type == OP_HELEM
321 else if ( topop->op_type == OP_CONCAT
322 && (topop->op_flags & OPf_STACKED)
323 && (!(topop->op_private & OPpCONCAT_NESTED))
328 /* OPpTARGET_MY shouldn't be able to be set here. If it is,
329 * decide what to do about it */
330 assert(!(o->op_private & OPpTARGET_MY));
332 /* barf on unknown flags */
333 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY)));
334 private_flags |= OPpMULTICONCAT_APPEND;
335 targetop = cBINOPo->op_first;
337 topop = OpSIBLING(targetop);
339 /* $x .= <FOO> gets optimised to rcatline instead */
340 if (topop->op_type == OP_READLINE)
345 /* Can targetop (the LHS) if it's a padsv, be optimised
346 * away and use OPpTARGET_MY instead?
348 if ( (targetop->op_type == OP_PADSV)
349 && !(targetop->op_private & OPpDEREF)
350 && !(targetop->op_private & OPpPAD_STATE)
351 /* we don't support 'my $x .= ...' */
352 && ( o->op_type == OP_SASSIGN
353 || !(targetop->op_private & OPpLVAL_INTRO))
358 if (topop->op_type == OP_STRINGIFY) {
359 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY])
363 /* barf on unknown flags */
364 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY)));
366 if ((topop->op_private & OPpTARGET_MY)) {
367 if (o->op_type == OP_SASSIGN)
368 return; /* can't have two assigns */
372 private_flags |= OPpMULTICONCAT_STRINGIFY;
374 topop = cBINOPx(topop)->op_first;
375 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK));
376 topop = OpSIBLING(topop);
379 if (topop->op_type == OP_SPRINTF) {
380 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF])
382 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) {
383 nargs = sprintf_info.nargs;
384 total_len = sprintf_info.total_len;
385 variant = sprintf_info.variant;
386 utf8 = sprintf_info.utf8;
388 private_flags |= OPpMULTICONCAT_FAKE;
390 /* we have an sprintf op rather than a concat optree.
391 * Skip most of the code below which is associated with
392 * processing that optree. We also skip phase 2, determining
393 * whether its cost effective to optimise, since for sprintf,
394 * multiconcat is *always* faster */
397 /* note that even if the sprintf itself isn't multiconcatable,
398 * the expression as a whole may be, e.g. in
399 * $x .= sprintf("%d",...)
400 * the sprintf op will be left as-is, but the concat/S op may
401 * be upgraded to multiconcat
404 else if (topop->op_type == OP_CONCAT) {
405 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT])
408 if ((topop->op_private & OPpTARGET_MY)) {
409 if (o->op_type == OP_SASSIGN || targmyop)
410 return; /* can't have two assigns */
415 /* Is it safe to convert a sassign/stringify/concat op into
417 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP);
418 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP);
419 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP);
420 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP);
421 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last)
422 == STRUCT_OFFSET(UNOP_AUX, op_aux));
423 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last)
424 == STRUCT_OFFSET(UNOP_AUX, op_aux));
426 /* Now scan the down the tree looking for a series of
427 * CONCAT/OPf_STACKED ops on the LHS (with the last one not
428 * stacked). For example this tree:
433 * CONCAT/STACKED -- EXPR5
435 * CONCAT/STACKED -- EXPR4
441 * corresponds to an expression like
443 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5)
445 * Record info about each EXPR in args[]: in particular, whether it is
446 * a stringifiable OP_CONST and if so what the const sv is.
448 * The reason why the last concat can't be STACKED is the difference
451 * ((($a .= $a) .= $a) .= $a) .= $a
454 * $a . $a . $a . $a . $a
456 * The main difference between the optrees for those two constructs
457 * is the presence of the last STACKED. As well as modifying $a,
458 * the former sees the changed $a between each concat, so if $s is
459 * initially 'a', the first returns 'a' x 16, while the latter returns
460 * 'a' x 5. And pp_multiconcat can't handle that kind of thing.
470 if ( kid->op_type == OP_CONCAT
474 k1 = cUNOPx(kid)->op_first;
476 /* shouldn't happen except maybe after compile err? */
480 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */
481 if (kid->op_private & OPpTARGET_MY)
484 stacked_last = (kid->op_flags & OPf_STACKED);
496 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2
497 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2)
499 /* At least two spare slots are needed to decompose both
500 * concat args. If there are no slots left, continue to
501 * examine the rest of the optree, but don't push new values
502 * on args[]. If the optree as a whole is legal for conversion
503 * (in particular that the last concat isn't STACKED), then
504 * the first PERL_MULTICONCAT_MAXARG elements of the optree
505 * can be converted into an OP_MULTICONCAT now, with the first
506 * child of that op being the remainder of the optree -
507 * which may itself later be converted to a multiconcat op
511 /* the last arg is the rest of the optree */
516 else if ( argop->op_type == OP_CONST
517 && ((sv = cSVOPx_sv(argop)))
518 /* defer stringification until runtime of 'constant'
519 * things that might stringify variantly, e.g. the radix
520 * point of NVs, or overloaded RVs */
521 && (SvPOK(sv) || SvIOK(sv))
524 if (argop->op_private & OPpCONST_STRICT)
525 no_bareword_allowed(argop);
527 utf8 |= cBOOL(SvUTF8(sv));
530 /* this const may be demoted back to a plain arg later;
531 * make sure we have enough arg slots left */
533 prev_was_const = !prev_was_const;
538 prev_was_const = FALSE;
548 return; /* we don't support ((A.=B).=C)...) */
550 /* look for two adjacent consts and don't fold them together:
553 * $o->concat("a")->concat("b")
556 * (but $o .= "a" . "b" should still fold)
559 bool seen_nonconst = FALSE;
560 for (argp = toparg; argp >= args; argp--) {
561 if (argp->p == NULL) {
562 seen_nonconst = TRUE;
568 /* both previous and current arg were constants;
569 * leave the current OP_CONST as-is */
577 /* -----------------------------------------------------------------
580 * At this point we have determined that the optree *can* be converted
581 * into a multiconcat. Having gathered all the evidence, we now decide
582 * whether it *should*.
586 /* we need at least one concat action, e.g.:
592 * otherwise we could be doing something like $x = "foo", which
593 * if treated as a concat, would fail to COW.
595 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2)
598 /* Benchmarking seems to indicate that we gain if:
599 * * we optimise at least two actions into a single multiconcat
600 * (e.g concat+concat, sassign+concat);
601 * * or if we can eliminate at least 1 OP_CONST;
602 * * or if we can eliminate a padsv via OPpTARGET_MY
606 /* eliminated at least one OP_CONST */
608 /* eliminated an OP_SASSIGN */
609 || o->op_type == OP_SASSIGN
610 /* eliminated an OP_PADSV */
611 || (!targmyop && is_targable)
613 /* definitely a net gain to optimise */
616 /* ... if not, what else? */
618 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1):
619 * multiconcat is faster (due to not creating a temporary copy of
620 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is
626 && topop->op_type == OP_CONCAT
628 PADOFFSET t = targmyop->op_targ;
629 OP *k1 = cBINOPx(topop)->op_first;
630 OP *k2 = cBINOPx(topop)->op_last;
631 if ( k2->op_type == OP_PADSV
633 && ( k1->op_type != OP_PADSV
639 /* need at least two concats */
640 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3)
645 /* -----------------------------------------------------------------
648 * At this point the optree has been verified as ok to be optimised
649 * into an OP_MULTICONCAT. Now start changing things.
654 /* stringify all const args and determine utf8ness */
657 for (argp = args; argp <= toparg; argp++) {
658 SV *sv = (SV*)argp->p;
660 continue; /* not a const op */
661 if (utf8 && !SvUTF8(sv))
662 sv_utf8_upgrade_nomg(sv);
663 argp->p = SvPV_nomg(sv, argp->len);
664 total_len += argp->len;
666 /* see if any strings would grow if converted to utf8 */
668 variant += variant_under_utf8_count((U8 *) argp->p,
669 (U8 *) argp->p + argp->len);
673 /* create and populate aux struct */
677 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
678 sizeof(UNOP_AUX_item)
680 PERL_MULTICONCAT_HEADER_SIZE
681 + ((nargs + 1) * (variant ? 2 : 1))
684 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1);
686 /* Extract all the non-const expressions from the concat tree then
687 * dispose of the old tree, e.g. convert the tree from this:
691 * STRINGIFY -- TARGET
693 * ex-PUSHMARK -- CONCAT
708 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET
710 * except that if EXPRi is an OP_CONST, it's discarded.
712 * During the conversion process, EXPR ops are stripped from the tree
713 * and unshifted onto o. Finally, any of o's remaining original
714 * childen are discarded and o is converted into an OP_MULTICONCAT.
716 * In this middle of this, o may contain both: unshifted args on the
717 * left, and some remaining original args on the right. lastkidop
718 * is set to point to the right-most unshifted arg to delineate
719 * between the two sets.
724 /* create a copy of the format with the %'s removed, and record
725 * the sizes of the const string segments in the aux struct */
727 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
729 p = sprintf_info.start;
732 for (; p < sprintf_info.end; p++) {
736 (lenp++)->ssize = q - oldq;
743 lenp->ssize = q - oldq;
744 assert((STRLEN)(q - const_str) == total_len);
746 /* Attach all the args (i.e. the kids of the sprintf) to o (which
747 * may or may not be topop) The pushmark and const ops need to be
748 * kept in case they're an op_next entry point.
750 lastkidop = cLISTOPx(topop)->op_last;
751 kid = cUNOPx(topop)->op_first; /* pushmark */
753 op_null(OpSIBLING(kid)); /* const */
755 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */
756 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */
757 lastkidop->op_next = o;
762 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
766 /* Concatenate all const strings into const_str.
767 * Note that args[] contains the RHS args in reverse order, so
768 * we scan args[] from top to bottom to get constant strings
771 for (argp = toparg; argp >= args; argp--) {
774 (++lenp)->ssize = -1;
776 STRLEN l = argp->len;
777 Copy(argp->p, p, l, char);
779 if (lenp->ssize == -1)
790 for (argp = args; argp <= toparg; argp++) {
791 /* only keep non-const args, except keep the first-in-next-chain
792 * arg no matter what it is (but nulled if OP_CONST), because it
793 * may be the entry point to this subtree from the previous
796 bool last = (argp == toparg);
799 /* set prev to the sibling *before* the arg to be cut out,
800 * e.g. when cutting EXPR:
805 * prev= CONCAT -- EXPR
808 if (argp == args && kid->op_type != OP_CONCAT) {
809 /* in e.g. '$x .= f(1)' there's no RHS concat tree
810 * so the expression to be cut isn't kid->op_last but
813 /* find the op before kid */
815 o2 = cUNOPx(parentop)->op_first;
816 while (o2 && o2 != kid) {
824 else if (kid == o && lastkidop)
825 prev = last ? lastkidop : OpSIBLING(lastkidop);
827 prev = last ? NULL : cUNOPx(kid)->op_first;
829 if (!argp->p || last) {
831 OP *aop = op_sibling_splice(kid, prev, 1, NULL);
832 /* and unshift to front of o */
833 op_sibling_splice(o, NULL, 0, aop);
834 /* record the right-most op added to o: later we will
835 * free anything to the right of it */
838 aop->op_next = nextop;
841 /* null the const at start of op_next chain */
845 nextop = prev->op_next;
848 /* the last two arguments are both attached to the same concat op */
849 if (argp < toparg - 1)
854 /* Populate the aux struct */
856 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs;
857 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str;
858 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len;
859 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str;
860 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len;
862 /* if variant > 0, calculate a variant const string and lengths where
863 * the utf8 version of the string will take 'variant' more bytes than
868 STRLEN ulen = total_len + variant;
869 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
870 UNOP_AUX_item *ulens = lens + (nargs + 1);
871 char *up = (char*)PerlMemShared_malloc(ulen);
874 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up;
875 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen;
877 for (n = 0; n < (nargs + 1); n++) {
880 for (i = (lens++)->ssize; i > 0; i--) {
882 append_utf8_from_native_byte(c, (U8**)&up);
884 (ulens++)->ssize = (i < 0) ? i : up - orig_up;
889 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep
890 * that op's first child - an ex-PUSHMARK - because the op_next of
891 * the previous op may point to it (i.e. it's the entry point for
896 ? op_sibling_splice(o, lastkidop, 1, NULL)
897 : op_sibling_splice(stringop, NULL, 1, NULL);
898 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK));
899 op_sibling_splice(o, NULL, 0, pmop);
912 if (o->op_type == OP_SASSIGN) {
913 /* Move the target subtree from being the last of o's children
914 * to being the last of o's preserved children.
915 * Note the difference between 'target = ...' and 'target .= ...':
916 * for the former, target is executed last; for the latter,
919 kid = OpSIBLING(lastkidop);
920 op_sibling_splice(o, kid, 1, NULL); /* cut target op */
921 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */
922 lastkidop->op_next = kid->op_next;
923 lastkidop = targetop;
926 /* Move the target subtree from being the first of o's
927 * original children to being the first of *all* o's children.
930 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */
931 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/
934 /* if the RHS of .= doesn't contain a concat (e.g.
935 * $x .= "foo"), it gets missed by the "strip ops from the
936 * tree and add to o" loop earlier */
937 assert(topop->op_type != OP_CONCAT);
939 /* in e.g. $x .= "$y", move the $y expression
940 * from being a child of OP_STRINGIFY to being the
941 * second child of the OP_CONCAT
943 assert(cUNOPx(stringop)->op_first == topop);
944 op_sibling_splice(stringop, NULL, 1, NULL);
945 op_sibling_splice(o, cUNOPo->op_first, 0, topop);
947 assert(topop == OpSIBLING(cBINOPo->op_first));
959 * The original padsv op is kept but nulled in case it's the
960 * entry point for the optree (which it will be for
963 private_flags |= OPpTARGET_MY;
964 private_flags |= (targetop->op_private & OPpLVAL_INTRO);
965 o->op_targ = targetop->op_targ;
966 targetop->op_targ = 0;
970 flags |= OPf_STACKED;
973 private_flags |= OPpTARGET_MY;
975 o->op_targ = targmyop->op_targ;
976 targmyop->op_targ = 0;
980 /* detach the emaciated husk of the sprintf/concat optree and free it */
982 kid = op_sibling_splice(o, lastkidop, 1, NULL);
988 /* and convert o into a multiconcat */
990 o->op_flags = (flags|OPf_KIDS|stacked_last
991 |(o->op_flags & (OPf_WANT|OPf_PARENS)));
992 o->op_private = private_flags;
993 o->op_type = OP_MULTICONCAT;
994 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT];
995 cUNOP_AUXo->op_aux = aux;
1000 =for apidoc_section $optree_manipulation
1002 =for apidoc optimize_optree
1004 This function applies some optimisations to the optree in top-down order.
1005 It is called before the peephole optimizer, which processes ops in
1006 execution order. Note that finalize_optree() also does a top-down scan,
1007 but is called *after* the peephole optimizer.
1013 Perl_optimize_optree(pTHX_ OP* o)
1015 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE;
1018 SAVEVPTR(PL_curcop);
1026 #define warn_implicit_snail_cvsig(o) S_warn_implicit_snail_cvsig(aTHX_ o)
1028 S_warn_implicit_snail_cvsig(pTHX_ OP *o)
1031 while(cv && CvEVAL(cv))
1034 if(cv && CvSIGNATURE(cv))
1035 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__ARGS_ARRAY_WITH_SIGNATURES),
1036 "Implicit use of @_ in %s with signatured subroutine is experimental", OP_DESC(o));
1040 #define OP_ZOOM(o) (OP_TYPE_IS(o, OP_NULL) ? cUNOPx(o)->op_first : (o))
1042 /* helper for optimize_optree() which optimises one op then recurses
1043 * to optimise any children.
1047 S_optimize_op(pTHX_ OP* o)
1051 PERL_ARGS_ASSERT_OPTIMIZE_OP;
1054 OP * next_kid = NULL;
1056 assert(o->op_type != OP_FREED);
1058 switch (o->op_type) {
1061 PL_curcop = ((COP*)o); /* for warnings */
1069 S_maybe_multiconcat(aTHX_ o);
1073 if (cPMOPo->op_pmreplrootu.op_pmreplroot) {
1074 /* we can't assume that op_pmreplroot->op_sibparent == o
1075 * and that it is thus possible to walk back up the tree
1076 * past op_pmreplroot. So, although we try to avoid
1077 * recursing through op trees, do it here. After all,
1078 * there are unlikely to be many nested s///e's within
1079 * the replacement part of a s///e.
1081 optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1087 OP *first = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1089 while(cv && CvEVAL(cv))
1092 if(cv && CvSIGNATURE(cv) &&
1093 OP_TYPE_IS(first, OP_GV) && cGVOPx_gv(first) == PL_defgv) {
1094 OP *parent = op_parent(o);
1095 while(OP_TYPE_IS(parent, OP_NULL))
1096 parent = op_parent(parent);
1098 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__ARGS_ARRAY_WITH_SIGNATURES),
1099 "Use of @_ in %s with signatured subroutine is experimental", OP_DESC(parent));
1106 if(!CvUNIQUE(PL_compcv) && !(o->op_flags & OPf_KIDS))
1107 warn_implicit_snail_cvsig(o);
1111 if(!(o->op_flags & OPf_STACKED))
1112 warn_implicit_snail_cvsig(o);
1117 OP *first = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1119 if(OP_TYPE_IS(first, OP_SREFGEN) &&
1120 (ffirst = OP_ZOOM(cUNOPx(first)->op_first)) &&
1121 OP_TYPE_IS(ffirst, OP_RV2CV))
1122 warn_implicit_snail_cvsig(o);
1130 if (o->op_flags & OPf_KIDS)
1131 next_kid = cUNOPo->op_first;
1133 /* if a kid hasn't been nominated to process, continue with the
1134 * next sibling, or if no siblings left, go back to the parent's
1135 * siblings and so on
1139 return; /* at top; no parents/siblings to try */
1140 if (OpHAS_SIBLING(o))
1141 next_kid = o->op_sibparent;
1143 o = o->op_sibparent; /*try parent's next sibling */
1146 /* this label not yet used. Goto here if any code above sets
1155 =for apidoc finalize_optree
1157 This function finalizes the optree. Should be called directly after
1158 the complete optree is built. It does some additional
1159 checking which can't be done in the normal C<ck_>xxx functions and makes
1160 the tree thread-safe.
1166 Perl_finalize_optree(pTHX_ OP* o)
1168 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1171 SAVEVPTR(PL_curcop);
1180 =for apidoc traverse_op_tree
1182 Return the next op in a depth-first traversal of the op tree,
1183 returning NULL when the traversal is complete.
1185 The initial call must supply the root of the tree as both top and o.
1187 For now it's static, but it may be exposed to the API in the future.
1193 S_traverse_op_tree(pTHX_ OP *top, OP *o) {
1196 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;
1198 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {
1199 return cUNOPo->op_first;
1201 else if ((sib = OpSIBLING(o))) {
1205 OP *parent = o->op_sibparent;
1206 assert(!(o->op_moresib));
1207 while (parent && parent != top) {
1208 OP *sib = OpSIBLING(parent);
1211 parent = parent->op_sibparent;
1219 S_finalize_op(pTHX_ OP* o)
1222 PERL_ARGS_ASSERT_FINALIZE_OP;
1225 assert(o->op_type != OP_FREED);
1227 switch (o->op_type) {
1230 PL_curcop = ((COP*)o); /* for warnings */
1233 if (OpHAS_SIBLING(o)) {
1234 OP *sib = OpSIBLING(o);
1235 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
1236 && ckWARN(WARN_EXEC)
1237 && OpHAS_SIBLING(sib))
1239 const OPCODE type = OpSIBLING(sib)->op_type;
1240 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1241 const line_t oldline = CopLINE(PL_curcop);
1242 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
1243 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1244 "Statement unlikely to be reached");
1245 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1246 "\t(Maybe you meant system() when you said exec()?)\n");
1247 CopLINE_set(PL_curcop, oldline);
1254 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1255 GV * const gv = cGVOPo_gv;
1256 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1257 /* XXX could check prototype here instead of just carping */
1258 SV * const sv = sv_newmortal();
1259 gv_efullname3(sv, gv, NULL);
1260 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1261 "%" SVf "() called too early to check prototype",
1268 if (cSVOPo->op_private & OPpCONST_STRICT)
1269 no_bareword_allowed(o);
1273 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
1278 /* Relocate all the METHOP's SVs to the pad for thread safety. */
1279 case OP_METHOD_NAMED:
1280 case OP_METHOD_SUPER:
1281 case OP_METHOD_REDIR:
1282 case OP_METHOD_REDIR_SUPER:
1283 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
1292 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
1295 rop = (UNOP*)((BINOP*)o)->op_first;
1300 S_scalar_slice_warning(aTHX_ o);
1304 kid = OpSIBLING(cLISTOPo->op_first);
1305 if (/* I bet there's always a pushmark... */
1306 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
1307 && OP_TYPE_ISNT_NN(kid, OP_CONST))
1312 key_op = (SVOP*)(kid->op_type == OP_CONST
1314 : OpSIBLING(kLISTOP->op_first));
1316 rop = (UNOP*)((LISTOP*)o)->op_last;
1319 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
1321 check_hash_fields_and_hekify(rop, key_op, 1);
1325 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
1329 S_scalar_slice_warning(aTHX_ o);
1333 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1334 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1342 if (o->op_flags & OPf_KIDS) {
1345 /* check that op_last points to the last sibling, and that
1346 * the last op_sibling/op_sibparent field points back to the
1347 * parent, and that the only ops with KIDS are those which are
1348 * entitled to them */
1349 U32 type = o->op_type;
1353 if (type == OP_NULL) {
1355 /* ck_glob creates a null UNOP with ex-type GLOB
1356 * (which is a list op. So pretend it wasn't a listop */
1357 if (type == OP_GLOB)
1360 family = PL_opargs[type] & OA_CLASS_MASK;
1362 has_last = ( family == OA_BINOP
1363 || family == OA_LISTOP
1364 || family == OA_PMOP
1365 || family == OA_LOOP
1367 assert( has_last /* has op_first and op_last, or ...
1368 ... has (or may have) op_first: */
1369 || family == OA_UNOP
1370 || family == OA_UNOP_AUX
1371 || family == OA_LOGOP
1372 || family == OA_BASEOP_OR_UNOP
1373 || family == OA_FILESTATOP
1374 || family == OA_LOOPEXOP
1375 || family == OA_METHOP
1376 || type == OP_CUSTOM
1377 || type == OP_NULL /* new_logop does this */
1380 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
1381 if (!OpHAS_SIBLING(kid)) {
1383 assert(kid == cLISTOPo->op_last);
1384 assert(kid->op_sibparent == o);
1389 } while (( o = traverse_op_tree(top, o)) != NULL);
1394 ---------------------------------------------------------
1396 Common vars in list assignment
1398 There now follows some enums and static functions for detecting
1399 common variables in list assignments. Here is a little essay I wrote
1400 for myself when trying to get my head around this. DAPM.
1404 First some random observations:
1406 * If a lexical var is an alias of something else, e.g.
1407 for my $x ($lex, $pkg, $a[0]) {...}
1408 then the act of aliasing will increase the reference count of the SV
1410 * If a package var is an alias of something else, it may still have a
1411 reference count of 1, depending on how the alias was created, e.g.
1412 in *a = *b, $a may have a refcount of 1 since the GP is shared
1413 with a single GvSV pointer to the SV. So If it's an alias of another
1414 package var, then RC may be 1; if it's an alias of another scalar, e.g.
1415 a lexical var or an array element, then it will have RC > 1.
1417 * There are many ways to create a package alias; ultimately, XS code
1418 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so
1419 run-time tracing mechanisms are unlikely to be able to catch all cases.
1421 * When the LHS is all my declarations, the same vars can't appear directly
1422 on the RHS, but they can indirectly via closures, aliasing and lvalue
1423 subs. But those techniques all involve an increase in the lexical
1426 * When the LHS is all lexical vars (but not necessarily my declarations),
1427 it is possible for the same lexicals to appear directly on the RHS, and
1428 without an increased ref count, since the stack isn't refcounted.
1429 This case can be detected at compile time by scanning for common lex
1430 vars with PL_generation.
1432 * lvalue subs defeat common var detection, but they do at least
1433 return vars with a temporary ref count increment. Also, you can't
1434 tell at compile time whether a sub call is lvalue.
1439 A: There are a few circumstances where there definitely can't be any
1442 LHS empty: () = (...);
1443 RHS empty: (....) = ();
1444 RHS contains only constants or other 'can't possibly be shared'
1445 elements (e.g. ops that return PADTMPs): (...) = (1,2, length)
1446 i.e. they only contain ops not marked as dangerous, whose children
1447 are also not dangerous;
1449 LHS contains a single scalar element: e.g. ($x) = (....); because
1450 after $x has been modified, it won't be used again on the RHS;
1451 RHS contains a single element with no aggregate on LHS: e.g.
1452 ($a,$b,$c) = ($x); again, once $a has been modified, its value
1453 won't be used again.
1455 B: If LHS are all 'my' lexical var declarations (or safe ops, which
1458 my ($a, $b, @c) = ...;
1460 Due to closure and goto tricks, these vars may already have content.
1461 For the same reason, an element on the RHS may be a lexical or package
1462 alias of one of the vars on the left, or share common elements, for
1465 my ($x,$y) = f(); # $x and $y on both sides
1466 sub f : lvalue { ($x,$y) = (1,2); $y, $x }
1471 my @a = @$ra; # elements of @a on both sides
1472 sub f { @a = 1..4; \@a }
1475 First, just consider scalar vars on LHS:
1477 RHS is safe only if (A), or in addition,
1478 * contains only lexical *scalar* vars, where neither side's
1479 lexicals have been flagged as aliases
1481 If RHS is not safe, then it's always legal to check LHS vars for
1482 RC==1, since the only RHS aliases will always be associated
1485 Note that in particular, RHS is not safe if:
1487 * it contains package scalar vars; e.g.:
1490 my ($x, $y) = (2, $x_alias);
1491 sub f { $x = 1; *x_alias = \$x; }
1493 * It contains other general elements, such as flattened or
1494 * spliced or single array or hash elements, e.g.
1497 my ($x,$y) = @a; # or $a[0] or @a{@b} etc
1501 use feature 'refaliasing';
1502 \($a[0], $a[1]) = \($y,$x);
1505 It doesn't matter if the array/hash is lexical or package.
1507 * it contains a function call that happens to be an lvalue
1508 sub which returns one or more of the above, e.g.
1519 (so a sub call on the RHS should be treated the same
1520 as having a package var on the RHS).
1522 * any other "dangerous" thing, such an op or built-in that
1523 returns one of the above, e.g. pp_preinc
1526 If RHS is not safe, what we can do however is at compile time flag
1527 that the LHS are all my declarations, and at run time check whether
1528 all the LHS have RC == 1, and if so skip the full scan.
1530 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...;
1532 Here the issue is whether there can be elements of @a on the RHS
1533 which will get prematurely freed when @a is cleared prior to
1534 assignment. This is only a problem if the aliasing mechanism
1535 is one which doesn't increase the refcount - only if RC == 1
1536 will the RHS element be prematurely freed.
1538 Because the array/hash is being INTROed, it or its elements
1539 can't directly appear on the RHS:
1541 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE
1543 but can indirectly, e.g.:
1547 sub f { @a = 1..3; \@a }
1549 So if the RHS isn't safe as defined by (A), we must always
1550 mortalise and bump the ref count of any remaining RHS elements
1551 when assigning to a non-empty LHS aggregate.
1553 Lexical scalars on the RHS aren't safe if they've been involved in
1556 use feature 'refaliasing';
1560 my @a = ($lex,3); # equivalent to ($a[0],3)
1567 Similarly with lexical arrays and hashes on the RHS:
1581 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g.
1582 my $a; ($a, my $b) = (....);
1584 The difference between (B) and (C) is that it is now physically
1585 possible for the LHS vars to appear on the RHS too, where they
1586 are not reference counted; but in this case, the compile-time
1587 PL_generation sweep will detect such common vars.
1589 So the rules for (C) differ from (B) in that if common vars are
1590 detected, the runtime "test RC==1" optimisation can no longer be used,
1591 and a full mark and sweep is required
1593 D: As (C), but in addition the LHS may contain package vars.
1595 Since package vars can be aliased without a corresponding refcount
1596 increase, all bets are off. It's only safe if (A). E.g.
1598 my ($x, $y) = (1,2);
1601 ($x_alias, $y) = (3, $x); # whoops
1604 Ditto for LHS aggregate package vars.
1606 E: Any other dangerous ops on LHS, e.g.
1607 (f(), $a[0], @$r) = (...);
1609 this is similar to (E) in that all bets are off. In addition, it's
1610 impossible to determine at compile time whether the LHS
1611 contains a scalar or an aggregate, e.g.
1613 sub f : lvalue { @a }
1616 * ---------------------------------------------------------
1619 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates
1620 * that at least one of the things flagged was seen.
1624 AAS_MY_SCALAR = 0x001, /* my $scalar */
1625 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */
1626 AAS_LEX_SCALAR = 0x004, /* $lexical */
1627 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */
1628 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */
1629 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */
1630 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */
1631 AAS_DANGEROUS = 0x080, /* an op (other than the above)
1632 that's flagged OA_DANGEROUS */
1633 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's
1634 not in any of the categories above */
1635 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */
1638 /* helper function for S_aassign_scan().
1639 * check a PAD-related op for commonality and/or set its generation number.
1640 * Returns a boolean indicating whether its shared */
1643 S_aassign_padcheck(pTHX_ OP* o, bool rhs)
1645 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX)
1646 /* lexical used in aliasing */
1650 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation);
1652 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation);
1658 Helper function for OPpASSIGN_COMMON* detection in rpeep().
1659 It scans the left or right hand subtree of the aassign op, and returns a
1660 set of flags indicating what sorts of things it found there.
1661 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we
1662 set PL_generation on lexical vars; if the latter, we see if
1663 PL_generation matches.
1664 'scalars_p' is a pointer to a counter of the number of scalar SVs seen.
1665 This fn will increment it by the number seen. It's not intended to
1666 be an accurate count (especially as many ops can push a variable
1667 number of SVs onto the stack); rather it's used as to test whether there
1668 can be at most 1 SV pushed; so it's only meanings are "0, 1, many".
1672 S_aassign_scan(pTHX_ OP* o, bool rhs, int *scalars_p)
1675 OP *effective_top_op = o;
1679 bool top = o == effective_top_op;
1681 OP* next_kid = NULL;
1683 /* first, look for a solitary @_ on the RHS */
1686 && (o->op_flags & OPf_KIDS)
1687 && OP_TYPE_IS_OR_WAS(o, OP_LIST)
1689 OP *kid = cUNOPo->op_first;
1690 if ( ( kid->op_type == OP_PUSHMARK
1691 || kid->op_type == OP_PADRANGE) /* ex-pushmark */
1692 && ((kid = OpSIBLING(kid)))
1693 && !OpHAS_SIBLING(kid)
1694 && kid->op_type == OP_RV2AV
1695 && !(kid->op_flags & OPf_REF)
1696 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
1697 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST)
1698 && ((kid = cUNOPx(kid)->op_first))
1699 && kid->op_type == OP_GV
1700 && cGVOPx_gv(kid) == PL_defgv
1705 switch (o->op_type) {
1708 all_flags |= AAS_PKG_SCALAR;
1714 /* if !top, could be e.g. @a[0,1] */
1715 all_flags |= (top && (o->op_flags & OPf_REF))
1716 ? ((o->op_private & OPpLVAL_INTRO)
1717 ? AAS_MY_AGG : AAS_LEX_AGG)
1723 int comm = S_aassign_padcheck(aTHX_ o, rhs)
1724 ? AAS_LEX_SCALAR_COMM : 0;
1726 all_flags |= (o->op_private & OPpLVAL_INTRO)
1727 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm);
1735 if (cUNOPx(o)->op_first->op_type != OP_GV)
1736 all_flags |= AAS_DANGEROUS; /* @{expr}, %{expr} */
1738 /* if !top, could be e.g. @a[0,1] */
1739 else if (top && (o->op_flags & OPf_REF))
1740 all_flags |= AAS_PKG_AGG;
1742 all_flags |= AAS_DANGEROUS;
1747 if (cUNOPx(o)->op_first->op_type != OP_GV) {
1749 all_flags |= AAS_DANGEROUS; /* ${expr} */
1752 all_flags |= AAS_PKG_SCALAR; /* $pkg */
1756 if (o->op_private & OPpSPLIT_ASSIGN) {
1757 /* the assign in @a = split() has been optimised away
1758 * and the @a attached directly to the split op
1759 * Treat the array as appearing on the RHS, i.e.
1760 * ... = (@a = split)
1765 if (o->op_flags & OPf_STACKED) {
1766 /* @{expr} = split() - the array expression is tacked
1767 * on as an extra child to split - process kid */
1768 next_kid = cLISTOPo->op_last;
1772 /* ... else array is directly attached to split op */
1774 all_flags |= (PL_op->op_private & OPpSPLIT_LEX)
1775 ? ((o->op_private & OPpLVAL_INTRO)
1776 ? AAS_MY_AGG : AAS_LEX_AGG)
1781 /* other args of split can't be returned */
1782 all_flags |= AAS_SAFE_SCALAR;
1786 /* undef on LHS following a var is significant, e.g.
1788 * @a = (($x, undef) = (2 => $x));
1789 * # @a shoul be (2,1) not (2,2)
1791 * undef on RHS counts as a scalar:
1792 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe
1794 if ((!rhs && *scalars_p) || rhs)
1796 flags = AAS_SAFE_SCALAR;
1801 /* these are all no-ops; they don't push a potentially common SV
1802 * onto the stack, so they are neither AAS_DANGEROUS nor
1803 * AAS_SAFE_SCALAR */
1806 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */
1811 /* these do nothing, but may have children */
1815 if (PL_opargs[o->op_type] & OA_DANGEROUS) {
1817 flags = AAS_DANGEROUS;
1821 if ( (PL_opargs[o->op_type] & OA_TARGLEX)
1822 && (o->op_private & OPpTARGET_MY))
1825 all_flags |= S_aassign_padcheck(aTHX_ o, rhs)
1826 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR;
1830 /* if its an unrecognised, non-dangerous op, assume that it
1831 * is the cause of at least one safe scalar */
1833 flags = AAS_SAFE_SCALAR;
1839 /* by default, process all kids next
1840 * XXX this assumes that all other ops are "transparent" - i.e. that
1841 * they can return some of their children. While this true for e.g.
1842 * sort and grep, it's not true for e.g. map. We really need a
1843 * 'transparent' flag added to regen/opcodes
1845 if (o->op_flags & OPf_KIDS) {
1846 next_kid = cUNOPo->op_first;
1847 /* these ops do nothing but may have children; but their
1848 * children should also be treated as top-level */
1849 if ( o == effective_top_op
1850 && (o->op_type == OP_NULL || o->op_type == OP_LIST)
1852 effective_top_op = next_kid;
1856 /* If next_kid is set, someone in the code above wanted us to process
1857 * that kid and all its remaining siblings. Otherwise, work our way
1858 * back up the tree */
1862 return all_flags; /* at top; no parents/siblings to try */
1863 if (OpHAS_SIBLING(o)) {
1864 next_kid = o->op_sibparent;
1865 if (o == effective_top_op)
1866 effective_top_op = next_kid;
1868 else if (o == effective_top_op)
1869 effective_top_op = o->op_sibparent;
1870 o = o->op_sibparent; /* try parent's next sibling */
1876 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start'
1877 * that potentially represent a series of one or more aggregate derefs
1878 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert
1879 * the whole chain to a single OP_MULTIDEREF op (maybe with a few
1880 * additional ops left in too).
1882 * The caller will have already verified that the first few ops in the
1883 * chain following 'start' indicate a multideref candidate, and will have
1884 * set 'orig_o' to the point further on in the chain where the first index
1885 * expression (if any) begins. 'orig_action' specifies what type of
1886 * beginning has already been determined by the ops between start..orig_o
1887 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc).
1889 * 'hints' contains any hints flags that need adding (currently just
1890 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller.
1894 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints)
1897 UNOP_AUX_item *arg_buf = NULL;
1898 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */
1899 int index_skip = -1; /* don't output index arg on this action */
1901 /* similar to regex compiling, do two passes; the first pass
1902 * determines whether the op chain is convertible and calculates the
1903 * buffer size; the second pass populates the buffer and makes any
1904 * changes necessary to ops (such as moving consts to the pad on
1907 * NB: for things like Coverity, note that both passes take the same
1908 * path through the logic tree (except for 'if (pass)' bits), since
1909 * both passes are following the same op_next chain; and in
1910 * particular, if it would return early on the second pass, it would
1911 * already have returned early on the first pass.
1913 for (pass = 0; pass < 2; pass++) {
1915 UV action = orig_action;
1916 OP *first_elem_op = NULL; /* first seen aelem/helem */
1917 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */
1918 int action_count = 0; /* number of actions seen so far */
1919 int action_ix = 0; /* action_count % (actions per IV) */
1920 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */
1921 bool is_last = FALSE; /* no more derefs to follow */
1922 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */
1923 UV action_word = 0; /* all actions so far */
1924 UNOP_AUX_item *arg = arg_buf;
1925 UNOP_AUX_item *action_ptr = arg_buf;
1927 arg++; /* reserve slot for first action word */
1930 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1931 case MDEREF_HV_gvhv_helem:
1932 next_is_hash = TRUE;
1934 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1935 case MDEREF_AV_gvav_aelem:
1938 arg->pad_offset = cPADOPx(start)->op_padix;
1939 /* stop it being swiped when nulled */
1940 cPADOPx(start)->op_padix = 0;
1942 arg->sv = cSVOPx(start)->op_sv;
1943 cSVOPx(start)->op_sv = NULL;
1949 case MDEREF_HV_padhv_helem:
1950 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1951 next_is_hash = TRUE;
1953 case MDEREF_AV_padav_aelem:
1954 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1956 arg->pad_offset = start->op_targ;
1957 /* we skip setting op_targ = 0 for now, since the intact
1958 * OP_PADXV is needed by check_hash_fields_and_hekify */
1959 reset_start_targ = TRUE;
1964 case MDEREF_HV_pop_rv2hv_helem:
1965 next_is_hash = TRUE;
1967 case MDEREF_AV_pop_rv2av_aelem:
1971 NOT_REACHED; /* NOTREACHED */
1976 /* look for another (rv2av/hv; get index;
1977 * aelem/helem/exists/delele) sequence */
1982 UV index_type = MDEREF_INDEX_none;
1985 /* if this is not the first lookup, consume the rv2av/hv */
1987 /* for N levels of aggregate lookup, we normally expect
1988 * that the first N-1 [ah]elem ops will be flagged as
1989 * /DEREF (so they autovivifiy if necessary), and the last
1990 * lookup op not to be.
1991 * For other things (like @{$h{k1}{k2}}) extra scope or
1992 * leave ops can appear, so abandon the effort in that
1994 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
1997 /* rv2av or rv2hv sKR/1 */
1999 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2000 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2001 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
2004 /* at this point, we wouldn't expect any of these
2005 * possible private flags:
2006 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO
2007 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only)
2009 ASSUME(!(o->op_private &
2010 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING)));
2012 hints = (o->op_private & OPpHINT_STRICT_REFS);
2014 /* make sure the type of the previous /DEREF matches the
2015 * type of the next lookup */
2016 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV));
2019 action = next_is_hash
2020 ? MDEREF_HV_vivify_rv2hv_helem
2021 : MDEREF_AV_vivify_rv2av_aelem;
2025 /* if this is the second pass, and we're at the depth where
2026 * previously we encountered a non-simple index expression,
2027 * stop processing the index at this point */
2028 if (action_count != index_skip) {
2030 /* look for one or more simple ops that return an array
2031 * index or hash key */
2033 switch (o->op_type) {
2035 /* it may be a lexical var index */
2036 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS
2037 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2038 ASSUME(!(o->op_private &
2039 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
2041 if ( OP_GIMME(o,0) == G_SCALAR
2042 && !(o->op_flags & (OPf_REF|OPf_MOD))
2043 && o->op_private == 0)
2046 arg->pad_offset = o->op_targ;
2048 index_type = MDEREF_INDEX_padsv;
2055 /* it's a constant hash index */
2056 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK)))
2057 /* "use constant foo => FOO; $h{+foo}" for
2058 * some weird FOO, can leave you with constants
2059 * that aren't simple strings. It's not worth
2060 * the extra hassle for those edge cases */
2065 OP * helem_op = o->op_next;
2067 ASSUME( helem_op->op_type == OP_HELEM
2068 || helem_op->op_type == OP_NULL
2070 if (helem_op->op_type == OP_HELEM) {
2071 rop = (UNOP*)(((BINOP*)helem_op)->op_first);
2072 if ( helem_op->op_private & OPpLVAL_INTRO
2073 || rop->op_type != OP_RV2HV
2077 /* on first pass just check; on second pass
2079 check_hash_fields_and_hekify(rop, cSVOPo, pass);
2084 /* Relocate sv to the pad for thread safety */
2085 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2086 arg->pad_offset = o->op_targ;
2089 arg->sv = cSVOPx_sv(o);
2094 /* it's a constant array index */
2096 SV *ix_sv = cSVOPo->op_sv;
2101 if ( action_count == 0
2104 && ( action == MDEREF_AV_padav_aelem
2105 || action == MDEREF_AV_gvav_aelem)
2107 maybe_aelemfast = TRUE;
2111 SvREFCNT_dec_NN(cSVOPo->op_sv);
2115 /* we've taken ownership of the SV */
2116 cSVOPo->op_sv = NULL;
2118 index_type = MDEREF_INDEX_const;
2123 /* it may be a package var index */
2125 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS|OPf_SPECIAL)));
2126 ASSUME(!(o->op_private & ~(OPpEARLY_CV)));
2127 if ( (o->op_flags & ~(OPf_PARENS|OPf_SPECIAL)) != OPf_WANT_SCALAR
2128 || o->op_private != 0
2133 if (kid->op_type != OP_RV2SV)
2136 ASSUME(!(kid->op_flags &
2137 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF
2138 |OPf_SPECIAL|OPf_PARENS)));
2139 ASSUME(!(kid->op_private &
2141 |OPpHINT_STRICT_REFS|OPpOUR_INTRO
2142 |OPpDEREF|OPpLVAL_INTRO)));
2143 if( (kid->op_flags &~ OPf_PARENS)
2144 != (OPf_WANT_SCALAR|OPf_KIDS)
2145 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS))
2151 arg->pad_offset = cPADOPx(o)->op_padix;
2152 /* stop it being swiped when nulled */
2153 cPADOPx(o)->op_padix = 0;
2155 arg->sv = cSVOPx(o)->op_sv;
2156 cSVOPo->op_sv = NULL;
2160 index_type = MDEREF_INDEX_gvsv;
2165 } /* action_count != index_skip */
2167 action |= index_type;
2170 /* at this point we have either:
2171 * * detected what looks like a simple index expression,
2172 * and expect the next op to be an [ah]elem, or
2173 * an nulled [ah]elem followed by a delete or exists;
2174 * * found a more complex expression, so something other
2175 * than the above follows.
2178 /* possibly an optimised away [ah]elem (where op_next is
2179 * exists or delete) */
2180 if (o->op_type == OP_NULL)
2183 /* at this point we're looking for an OP_AELEM, OP_HELEM,
2184 * OP_EXISTS or OP_DELETE */
2186 /* if a custom array/hash access checker is in scope,
2187 * abandon optimisation attempt */
2188 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
2189 && PL_check[o->op_type] != Perl_ck_null)
2191 /* similarly for customised exists and delete */
2192 if ( (o->op_type == OP_EXISTS)
2193 && PL_check[o->op_type] != Perl_ck_exists)
2195 if ( (o->op_type == OP_DELETE)
2196 && PL_check[o->op_type] != Perl_ck_delete)
2199 if ( o->op_type != OP_AELEM
2201 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB))
2203 maybe_aelemfast = FALSE;
2205 /* look for aelem/helem/exists/delete. If it's not the last elem
2206 * lookup, it *must* have OPpDEREF_AV/HV, but not many other
2207 * flags; if it's the last, then it mustn't have
2208 * OPpDEREF_AV/HV, but may have lots of other flags, like
2212 if ( index_type == MDEREF_INDEX_none
2213 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM
2214 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE)
2218 /* we have aelem/helem/exists/delete with valid simple index */
2220 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
2221 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV
2222 || (o->op_private & OPpDEREF) == OPpDEREF_HV);
2224 /* This doesn't make much sense but is legal:
2225 * @{ local $x[0][0] } = 1
2226 * Since scope exit will undo the autovivification,
2227 * don't bother in the first place. The OP_LEAVE
2228 * assertion is in case there are other cases of both
2229 * OPpLVAL_INTRO and OPpDEREF which don't include a scope
2230 * exit that would undo the local - in which case this
2231 * block of code would need rethinking.
2233 if (is_deref && (o->op_private & OPpLVAL_INTRO)) {
2236 while (n && ( n->op_type == OP_NULL
2237 || n->op_type == OP_LIST
2238 || n->op_type == OP_SCALAR))
2240 assert(n && n->op_type == OP_LEAVE);
2242 o->op_private &= ~OPpDEREF;
2247 ASSUME(!(o->op_flags &
2248 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS)));
2249 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF)));
2251 ok = (o->op_flags &~ OPf_PARENS)
2252 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD)
2253 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK));
2255 else if (o->op_type == OP_EXISTS) {
2256 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2257 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2258 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB)));
2259 ok = !(o->op_private & ~OPpARG1_MASK);
2261 else if (o->op_type == OP_DELETE) {
2262 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2263 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2264 ASSUME(!(o->op_private &
2265 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO)));
2266 /* don't handle slices or 'local delete'; the latter
2267 * is fairly rare, and has a complex runtime */
2268 ok = !(o->op_private & ~OPpARG1_MASK);
2269 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM))
2270 /* skip handling run-tome error */
2271 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL));
2274 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM);
2275 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD
2276 |OPf_PARENS|OPf_REF|OPf_SPECIAL)));
2277 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB
2278 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO)));
2279 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV;
2288 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV);
2293 action |= MDEREF_FLAG_last;
2297 /* at this point we have something that started
2298 * promisingly enough (with rv2av or whatever), but failed
2299 * to find a simple index followed by an
2300 * aelem/helem/exists/delete. If this is the first action,
2301 * give up; but if we've already seen at least one
2302 * aelem/helem, then keep them and add a new action with
2303 * MDEREF_INDEX_none, which causes it to do the vivify
2304 * from the end of the previous lookup, and do the deref,
2305 * but stop at that point. So $a[0][expr] will do one
2306 * av_fetch, vivify and deref, then continue executing at
2311 index_skip = action_count;
2312 action |= MDEREF_FLAG_last;
2313 if (index_type != MDEREF_INDEX_none)
2317 action_word |= (action << (action_ix * MDEREF_SHIFT));
2320 /* if there's no space for the next action, reserve a new slot
2321 * for it *before* we start adding args for that action */
2322 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) {
2324 action_ptr->uv = action_word;
2330 } /* while !is_last */
2335 /* slot reserved for next action word not now needed */
2338 action_ptr->uv = action_word;
2344 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf);
2345 if (index_skip == -1) {
2346 mderef->op_flags = o->op_flags
2347 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0));
2348 if (o->op_type == OP_EXISTS)
2349 mderef->op_private = OPpMULTIDEREF_EXISTS;
2350 else if (o->op_type == OP_DELETE)
2351 mderef->op_private = OPpMULTIDEREF_DELETE;
2353 mderef->op_private = o->op_private
2354 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO);
2356 /* accumulate strictness from every level (although I don't think
2357 * they can actually vary) */
2358 mderef->op_private |= hints;
2360 /* integrate the new multideref op into the optree and the
2363 * In general an op like aelem or helem has two child
2364 * sub-trees: the aggregate expression (a_expr) and the
2365 * index expression (i_expr):
2371 * The a_expr returns an AV or HV, while the i-expr returns an
2372 * index. In general a multideref replaces most or all of a
2373 * multi-level tree, e.g.
2389 * With multideref, all the i_exprs will be simple vars or
2390 * constants, except that i_expr1 may be arbitrary in the case
2391 * of MDEREF_INDEX_none.
2393 * The bottom-most a_expr will be either:
2394 * 1) a simple var (so padXv or gv+rv2Xv);
2395 * 2) a simple scalar var dereferenced (e.g. $r->[0]):
2396 * so a simple var with an extra rv2Xv;
2397 * 3) or an arbitrary expression.
2399 * 'start', the first op in the execution chain, will point to
2400 * 1),2): the padXv or gv op;
2401 * 3): the rv2Xv which forms the last op in the a_expr
2402 * execution chain, and the top-most op in the a_expr
2405 * For all cases, the 'start' node is no longer required,
2406 * but we can't free it since one or more external nodes
2407 * may point to it. E.g. consider
2408 * $h{foo} = $a ? $b : $c
2409 * Here, both the op_next and op_other branches of the
2410 * cond_expr point to the gv[*h] of the hash expression, so
2411 * we can't free the 'start' op.
2413 * For expr->[...], we need to save the subtree containing the
2414 * expression; for the other cases, we just need to save the
2416 * So in all cases, we null the start op and keep it around by
2417 * making it the child of the multideref op; for the expr->
2418 * case, the expr will be a subtree of the start node.
2420 * So in the simple 1,2 case the optree above changes to
2426 * ex-gv (or ex-padxv)
2428 * with the op_next chain being
2430 * -> ex-gv -> multideref -> op-following-ex-exists ->
2432 * In the 3 case, we have
2445 * -> rest-of-a_expr subtree ->
2446 * ex-rv2xv -> multideref -> op-following-ex-exists ->
2449 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none,
2450 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the
2451 * multideref attached as the child, e.g.
2457 * ex-rv2av - i_expr1
2465 /* if we free this op, don't free the pad entry */
2466 if (reset_start_targ)
2470 /* Cut the bit we need to save out of the tree and attach to
2471 * the multideref op, then free the rest of the tree */
2473 /* find parent of node to be detached (for use by splice) */
2475 if ( orig_action == MDEREF_AV_pop_rv2av_aelem
2476 || orig_action == MDEREF_HV_pop_rv2hv_helem)
2478 /* there is an arbitrary expression preceding us, e.g.
2479 * expr->[..]? so we need to save the 'expr' subtree */
2480 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE)
2481 p = cUNOPx(p)->op_first;
2482 ASSUME( start->op_type == OP_RV2AV
2483 || start->op_type == OP_RV2HV);
2486 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem
2487 * above for exists/delete. */
2488 while ( (p->op_flags & OPf_KIDS)
2489 && cUNOPx(p)->op_first != start
2491 p = cUNOPx(p)->op_first;
2493 ASSUME(cUNOPx(p)->op_first == start);
2495 /* detach from main tree, and re-attach under the multideref */
2496 op_sibling_splice(mderef, NULL, 0,
2497 op_sibling_splice(p, NULL, 1, NULL));
2500 start->op_next = mderef;
2502 mderef->op_next = index_skip == -1 ? o->op_next : o;
2504 /* excise and free the original tree, and replace with
2505 * the multideref op */
2506 p = op_sibling_splice(top_op, NULL, -1, mderef);
2515 Size_t size = arg - arg_buf;
2517 if (maybe_aelemfast && action_count == 1)
2520 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc(
2521 sizeof(UNOP_AUX_item) * (size + 1));
2522 /* for dumping etc: store the length in a hidden first slot;
2523 * we set the op_aux pointer to the second slot */
2527 } /* for (pass = ...) */
2530 /* See if the ops following o are such that o will always be executed in
2531 * boolean context: that is, the SV which o pushes onto the stack will
2532 * only ever be consumed by later ops via SvTRUE(sv) or similar.
2533 * If so, set a suitable private flag on o. Normally this will be
2534 * bool_flag; but see below why maybe_flag is needed too.
2536 * Typically the two flags you pass will be the generic OPpTRUEBOOL and
2537 * OPpMAYBE_TRUEBOOL, buts it's possible that for some ops those bits may
2538 * already be taken, so you'll have to give that op two different flags.
2540 * More explanation of 'maybe_flag' and 'safe_and' parameters.
2541 * The binary logical ops &&, ||, // (plus 'if' and 'unless' which use
2542 * those underlying ops) short-circuit, which means that rather than
2543 * necessarily returning a truth value, they may return the LH argument,
2544 * which may not be boolean. For example in $x = (keys %h || -1), keys
2545 * should return a key count rather than a boolean, even though its
2546 * sort-of being used in boolean context.
2548 * So we only consider such logical ops to provide boolean context to
2549 * their LH argument if they themselves are in void or boolean context.
2550 * However, sometimes the context isn't known until run-time. In this
2551 * case the op is marked with the maybe_flag flag it.
2553 * Consider the following.
2555 * sub f { ....; if (%h) { .... } }
2557 * This is actually compiled as
2559 * sub f { ....; %h && do { .... } }
2561 * Here we won't know until runtime whether the final statement (and hence
2562 * the &&) is in void context and so is safe to return a boolean value.
2563 * So mark o with maybe_flag rather than the bool_flag.
2564 * Note that there is cost associated with determining context at runtime
2565 * (e.g. a call to block_gimme()), so it may not be worth setting (at
2566 * compile time) and testing (at runtime) maybe_flag if the scalar verses
2567 * boolean costs savings are marginal.
2569 * However, we can do slightly better with && (compared to || and //):
2570 * this op only returns its LH argument when that argument is false. In
2571 * this case, as long as the op promises to return a false value which is
2572 * valid in both boolean and scalar contexts, we can mark an op consumed
2573 * by && with bool_flag rather than maybe_flag.
2574 * For example as long as pp_padhv and pp_rv2hv return &PL_sv_zero rather
2575 * than &PL_sv_no for a false result in boolean context, then it's safe. An
2576 * op which promises to handle this case is indicated by setting safe_and
2581 S_check_for_bool_cxt(OP*o, bool safe_and, U8 bool_flag, U8 maybe_flag)
2586 assert((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR);
2588 /* OPpTARGET_MY and boolean context probably don't mix well.
2589 * If someone finds a valid use case, maybe add an extra flag to this
2590 * function which indicates its safe to do so for this op? */
2591 assert(!( (PL_opargs[o->op_type] & OA_TARGLEX)
2592 && (o->op_private & OPpTARGET_MY)));
2597 switch (lop->op_type) {
2602 /* these two consume the stack argument in the scalar case,
2603 * and treat it as a boolean in the non linenumber case */
2606 if ( ((lop->op_flags & OPf_WANT) == OPf_WANT_LIST)
2607 || (lop->op_private & OPpFLIP_LINENUM))
2613 /* these never leave the original value on the stack */
2622 /* OR DOR and AND evaluate their arg as a boolean, but then may
2623 * leave the original scalar value on the stack when following the
2624 * op_next route. If not in void context, we need to ensure
2625 * that whatever follows consumes the arg only in boolean context
2637 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) {
2641 else if (!(lop->op_flags & OPf_WANT)) {
2642 /* unknown context - decide at runtime */
2657 o->op_private |= flag;
2660 /* mechanism for deferring recursion in rpeep() */
2662 #define MAX_DEFERRED 4
2666 if (defer_ix == (MAX_DEFERRED-1)) { \
2667 OP **defer = defer_queue[defer_base]; \
2668 CALL_RPEEP(*defer); \
2669 op_prune_chain_head(defer); \
2670 defer_base = (defer_base + 1) % MAX_DEFERRED; \
2673 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
2676 #define IS_AND_OP(o) (o->op_type == OP_AND)
2677 #define IS_OR_OP(o) (o->op_type == OP_OR)
2679 /* A peephole optimizer. We visit the ops in the order they're to execute.
2680 * See the comments at the top of this file for more details about when
2681 * peep() is called */
2684 Perl_rpeep(pTHX_ OP *o)
2687 OP* oldoldop = NULL;
2688 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
2692 if (!o || o->op_opt)
2695 assert(o->op_type != OP_FREED);
2699 SAVEVPTR(PL_curcop);
2700 for (;; o = o->op_next) {
2704 while (defer_ix >= 0) {
2706 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
2708 op_prune_chain_head(defer);
2715 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */
2716 assert(!oldoldop || oldoldop->op_next == oldop);
2717 assert(!oldop || oldop->op_next == o);
2719 /* By default, this op has now been optimised. A couple of cases below
2720 clear this again. */
2724 /* look for a series of 1 or more aggregate derefs, e.g.
2725 * $a[1]{foo}[$i]{$k}
2726 * and replace with a single OP_MULTIDEREF op.
2727 * Each index must be either a const, or a simple variable,
2729 * First, look for likely combinations of starting ops,
2730 * corresponding to (global and lexical variants of)
2732 * $r->[...] $r->{...}
2733 * (preceding expression)->[...]
2734 * (preceding expression)->{...}
2735 * and if so, call maybe_multideref() to do a full inspection
2736 * of the op chain and if appropriate, replace with an
2744 switch (o2->op_type) {
2746 /* $pkg[..] : gv[*pkg]
2747 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */
2749 /* Fail if there are new op flag combinations that we're
2750 * not aware of, rather than:
2751 * * silently failing to optimise, or
2752 * * silently optimising the flag away.
2753 * If this ASSUME starts failing, examine what new flag
2754 * has been added to the op, and decide whether the
2755 * optimisation should still occur with that flag, then
2756 * update the code accordingly. This applies to all the
2757 * other ASSUMEs in the block of code too.
2759 ASSUME(!(o2->op_flags &
2760 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL)));
2761 ASSUME(!(o2->op_private & ~OPpEARLY_CV));
2765 if (o2->op_type == OP_RV2AV) {
2766 action = MDEREF_AV_gvav_aelem;
2770 if (o2->op_type == OP_RV2HV) {
2771 action = MDEREF_HV_gvhv_helem;
2775 if (o2->op_type != OP_RV2SV)
2778 /* at this point we've seen gv,rv2sv, so the only valid
2779 * construct left is $pkg->[] or $pkg->{} */
2781 ASSUME(!(o2->op_flags & OPf_STACKED));
2782 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
2783 != (OPf_WANT_SCALAR|OPf_MOD))
2786 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS
2787 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO)));
2788 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO))
2790 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV
2791 && (o2->op_private & OPpDEREF) != OPpDEREF_HV)
2795 if (o2->op_type == OP_RV2AV) {
2796 action = MDEREF_AV_gvsv_vivify_rv2av_aelem;
2799 if (o2->op_type == OP_RV2HV) {
2800 action = MDEREF_HV_gvsv_vivify_rv2hv_helem;
2806 /* $lex->[...]: padsv[$lex] sM/DREFAV */
2808 ASSUME(!(o2->op_flags &
2809 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL)));
2811 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
2812 != (OPf_WANT_SCALAR|OPf_MOD))
2815 ASSUME(!(o2->op_private &
2816 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
2817 /* skip if state or intro, or not a deref */
2818 if ( o2->op_private != OPpDEREF_AV
2819 && o2->op_private != OPpDEREF_HV)
2823 if (o2->op_type == OP_RV2AV) {
2824 action = MDEREF_AV_padsv_vivify_rv2av_aelem;
2827 if (o2->op_type == OP_RV2HV) {
2828 action = MDEREF_HV_padsv_vivify_rv2hv_helem;
2835 /* $lex[..]: padav[@lex:1,2] sR *
2836 * or $lex{..}: padhv[%lex:1,2] sR */
2837 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS|
2838 OPf_REF|OPf_SPECIAL)));
2840 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
2841 != (OPf_WANT_SCALAR|OPf_REF))
2843 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF))
2845 /* OPf_PARENS isn't currently used in this case;
2846 * if that changes, let us know! */
2847 ASSUME(!(o2->op_flags & OPf_PARENS));
2849 /* at this point, we wouldn't expect any of the remaining
2850 * possible private flags:
2851 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL,
2852 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB
2854 * OPpSLICEWARNING shouldn't affect runtime
2856 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING)));
2858 action = o2->op_type == OP_PADAV
2859 ? MDEREF_AV_padav_aelem
2860 : MDEREF_HV_padhv_helem;
2862 S_maybe_multideref(aTHX_ o, o2, action, 0);
2868 action = o2->op_type == OP_RV2AV
2869 ? MDEREF_AV_pop_rv2av_aelem
2870 : MDEREF_HV_pop_rv2hv_helem;
2873 /* (expr)->[...]: rv2av sKR/1;
2874 * (expr)->{...}: rv2hv sKR/1; */
2876 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV);
2878 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2879 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL)));
2880 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
2883 /* at this point, we wouldn't expect any of these
2884 * possible private flags:
2885 * OPpMAYBE_LVSUB, OPpLVAL_INTRO
2886 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only)
2888 ASSUME(!(o2->op_private &
2889 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING
2891 hints |= (o2->op_private & OPpHINT_STRICT_REFS);
2895 S_maybe_multideref(aTHX_ o, o2, action, hints);
2904 switch (o->op_type) {
2906 PL_curcop = ((COP*)o); /* for warnings */
2909 PL_curcop = ((COP*)o); /* for warnings */
2911 /* Optimise a "return ..." at the end of a sub to just be "...".
2912 * This saves 2 ops. Before:
2913 * 1 <;> nextstate(main 1 -e:1) v ->2
2914 * 4 <@> return K ->5
2915 * 2 <0> pushmark s ->3
2916 * - <1> ex-rv2sv sK/1 ->4
2917 * 3 <#> gvsv[*cat] s ->4
2920 * - <@> return K ->-
2921 * - <0> pushmark s ->2
2922 * - <1> ex-rv2sv sK/1 ->-
2923 * 2 <$> gvsv(*cat) s ->3
2926 OP *next = o->op_next;
2927 OP *sibling = OpSIBLING(o);
2928 if ( OP_TYPE_IS(next, OP_PUSHMARK)
2929 && OP_TYPE_IS(sibling, OP_RETURN)
2930 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
2931 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
2932 ||OP_TYPE_IS(sibling->op_next->op_next,
2934 && cUNOPx(sibling)->op_first == next
2935 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next
2938 /* Look through the PUSHMARK's siblings for one that
2939 * points to the RETURN */
2940 OP *top = OpSIBLING(next);
2941 while (top && top->op_next) {
2942 if (top->op_next == sibling) {
2943 top->op_next = sibling->op_next;
2944 o->op_next = next->op_next;
2947 top = OpSIBLING(top);
2952 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
2954 * This latter form is then suitable for conversion into padrange
2955 * later on. Convert:
2957 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
2961 * nextstate1 -> listop -> nextstate3
2963 * pushmark -> padop1 -> padop2
2966 o->op_next->op_type == OP_PADSV
2967 || o->op_next->op_type == OP_PADAV
2968 || o->op_next->op_type == OP_PADHV
2970 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
2971 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
2972 && o->op_next->op_next->op_next && (
2973 o->op_next->op_next->op_next->op_type == OP_PADSV
2974 || o->op_next->op_next->op_next->op_type == OP_PADAV
2975 || o->op_next->op_next->op_next->op_type == OP_PADHV
2977 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
2978 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
2979 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
2980 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
2982 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm;
2985 ns2 = pad1->op_next;
2986 pad2 = ns2->op_next;
2987 ns3 = pad2->op_next;
2989 /* we assume here that the op_next chain is the same as
2990 * the op_sibling chain */
2991 assert(OpSIBLING(o) == pad1);
2992 assert(OpSIBLING(pad1) == ns2);
2993 assert(OpSIBLING(ns2) == pad2);
2994 assert(OpSIBLING(pad2) == ns3);
2996 /* excise and delete ns2 */
2997 op_sibling_splice(NULL, pad1, 1, NULL);
3000 /* excise pad1 and pad2 */
3001 op_sibling_splice(NULL, o, 2, NULL);
3003 /* create new listop, with children consisting of:
3004 * a new pushmark, pad1, pad2. */
3005 newop = newLISTOP(OP_LIST, 0, pad1, pad2);
3006 newop->op_flags |= OPf_PARENS;
3007 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
3009 /* insert newop between o and ns3 */
3010 op_sibling_splice(NULL, o, 0, newop);
3012 /*fixup op_next chain */
3013 newpm = cUNOPx(newop)->op_first; /* pushmark */
3014 o ->op_next = newpm;
3015 newpm->op_next = pad1;
3016 pad1 ->op_next = pad2;
3017 pad2 ->op_next = newop; /* listop */
3018 newop->op_next = ns3;
3020 /* Ensure pushmark has this flag if padops do */
3021 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) {
3022 newpm->op_flags |= OPf_MOD;
3028 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
3029 to carry two labels. For now, take the easier option, and skip
3030 this optimisation if the first NEXTSTATE has a label. */
3031 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
3032 OP *nextop = o->op_next;
3034 switch (nextop->op_type) {
3039 nextop = nextop->op_next;
3045 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
3048 oldop->op_next = nextop;
3050 /* Skip (old)oldop assignment since the current oldop's
3051 op_next already points to the next op. */
3058 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
3059 if (o->op_next->op_private & OPpTARGET_MY) {
3060 if (o->op_flags & OPf_STACKED) /* chained concats */
3061 break; /* ignore_optimization */
3063 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
3064 o->op_targ = o->op_next->op_targ;
3065 o->op_next->op_targ = 0;
3066 o->op_private |= OPpTARGET_MY;
3069 op_null(o->op_next);
3073 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
3074 break; /* Scalar stub must produce undef. List stub is noop */
3078 if (o->op_targ == OP_NEXTSTATE
3079 || o->op_targ == OP_DBSTATE)
3081 PL_curcop = ((COP*)o);
3083 /* XXX: We avoid setting op_seq here to prevent later calls
3084 to rpeep() from mistakenly concluding that optimisation
3085 has already occurred. This doesn't fix the real problem,
3086 though (See 20010220.007 (#5874)). AMS 20010719 */
3087 /* op_seq functionality is now replaced by op_opt */
3095 oldop->op_next = o->op_next;
3109 convert repeat into a stub with no kids.
3111 if (o->op_next->op_type == OP_CONST
3112 || ( o->op_next->op_type == OP_PADSV
3113 && !(o->op_next->op_private & OPpLVAL_INTRO))
3114 || ( o->op_next->op_type == OP_GV
3115 && o->op_next->op_next->op_type == OP_RV2SV
3116 && !(o->op_next->op_next->op_private
3117 & (OPpLVAL_INTRO|OPpOUR_INTRO))))
3119 const OP *kid = o->op_next->op_next;
3120 if (o->op_next->op_type == OP_GV)
3122 /* kid is now the ex-list. */
3123 if (kid->op_type == OP_NULL
3124 && (kid = kid->op_next)->op_type == OP_CONST
3125 /* kid is now the repeat count. */
3126 && kid->op_next->op_type == OP_REPEAT
3127 && kid->op_next->op_private & OPpREPEAT_DOLIST
3128 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST
3129 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0
3132 o = kid->op_next; /* repeat */
3134 op_free(cBINOPo->op_first);
3135 op_free(cBINOPo->op_last );
3136 o->op_flags &=~ OPf_KIDS;
3137 /* stub is a baseop; repeat is a binop */
3138 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP));
3139 OpTYPE_set(o, OP_STUB);
3145 /* Convert a series of PAD ops for my vars plus support into a
3146 * single padrange op. Basically
3148 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
3150 * becomes, depending on circumstances, one of
3152 * padrange ----------------------------------> (list) -> rest
3153 * padrange --------------------------------------------> rest
3155 * where all the pad indexes are sequential and of the same type
3157 * We convert the pushmark into a padrange op, then skip
3158 * any other pad ops, and possibly some trailing ops.
3159 * Note that we don't null() the skipped ops, to make it
3160 * easier for Deparse to undo this optimisation (and none of
3161 * the skipped ops are holding any resourses). It also makes
3162 * it easier for find_uninit_var(), as it can just ignore
3163 * padrange, and examine the original pad ops.
3167 OP *followop = NULL; /* the op that will follow the padrange op */
3170 PADOFFSET base = 0; /* init only to stop compiler whining */
3171 bool gvoid = 0; /* init only to stop compiler whining */
3172 bool defav = 0; /* seen (...) = @_ */
3173 bool reuse = 0; /* reuse an existing padrange op */
3175 /* look for a pushmark -> gv[_] -> rv2av */
3180 if ( p->op_type == OP_GV
3181 && cGVOPx_gv(p) == PL_defgv
3182 && (rv2av = p->op_next)
3183 && rv2av->op_type == OP_RV2AV
3184 && !(rv2av->op_flags & OPf_REF)
3185 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
3186 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
3189 if (q->op_type == OP_NULL)
3191 if (q->op_type == OP_PUSHMARK) {
3201 /* scan for PAD ops */
3203 for (p = p->op_next; p; p = p->op_next) {
3204 if (p->op_type == OP_NULL)
3207 if (( p->op_type != OP_PADSV
3208 && p->op_type != OP_PADAV
3209 && p->op_type != OP_PADHV
3211 /* any private flag other than INTRO? e.g. STATE */
3212 || (p->op_private & ~OPpLVAL_INTRO)
3216 /* let $a[N] potentially be optimised into AELEMFAST_LEX
3218 if ( p->op_type == OP_PADAV
3220 && p->op_next->op_type == OP_CONST
3221 && p->op_next->op_next
3222 && p->op_next->op_next->op_type == OP_AELEM
3226 /* for 1st padop, note what type it is and the range
3227 * start; for the others, check that it's the same type
3228 * and that the targs are contiguous */
3230 intro = (p->op_private & OPpLVAL_INTRO);
3232 gvoid = OP_GIMME(p,0) == G_VOID;
3235 if ((p->op_private & OPpLVAL_INTRO) != intro)
3237 /* Note that you'd normally expect targs to be
3238 * contiguous in my($a,$b,$c), but that's not the case
3239 * when external modules start doing things, e.g.
3240 * Function::Parameters */
3241 if (p->op_targ != base + count)
3243 assert(p->op_targ == base + count);
3244 /* Either all the padops or none of the padops should
3245 be in void context. Since we only do the optimisa-
3246 tion for av/hv when the aggregate itself is pushed
3247 on to the stack (one item), there is no need to dis-
3248 tinguish list from scalar context. */
3249 if (gvoid != (OP_GIMME(p,0) == G_VOID))
3253 /* for AV, HV, only when we're not flattening */
3254 if ( p->op_type != OP_PADSV
3256 && !(p->op_flags & OPf_REF)
3260 if (count >= OPpPADRANGE_COUNTMASK)
3263 /* there's a biggest base we can fit into a
3264 * SAVEt_CLEARPADRANGE in pp_padrange.
3265 * (The sizeof() stuff will be constant-folded, and is
3266 * intended to avoid getting "comparison is always false"
3267 * compiler warnings. See the comments above
3268 * MEM_WRAP_CHECK for more explanation on why we do this
3269 * in a weird way to avoid compiler warnings.)
3272 && (8*sizeof(base) >
3273 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT
3275 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
3277 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
3281 /* Success! We've got another valid pad op to optimise away */
3283 followop = p->op_next;
3286 if (count < 1 || (count == 1 && !defav))
3289 /* pp_padrange in specifically compile-time void context
3290 * skips pushing a mark and lexicals; in all other contexts
3291 * (including unknown till runtime) it pushes a mark and the
3292 * lexicals. We must be very careful then, that the ops we
3293 * optimise away would have exactly the same effect as the
3295 * In particular in void context, we can only optimise to
3296 * a padrange if we see the complete sequence
3297 * pushmark, pad*v, ...., list
3298 * which has the net effect of leaving the markstack as it
3299 * was. Not pushing onto the stack (whereas padsv does touch
3300 * the stack) makes no difference in void context.
3304 if (followop->op_type == OP_LIST
3305 && OP_GIMME(followop,0) == G_VOID
3308 followop = followop->op_next; /* skip OP_LIST */
3310 /* consolidate two successive my(...);'s */
3313 && oldoldop->op_type == OP_PADRANGE
3314 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
3315 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
3316 && !(oldoldop->op_flags & OPf_SPECIAL)
3319 assert(oldoldop->op_next == oldop);
3320 assert( oldop->op_type == OP_NEXTSTATE
3321 || oldop->op_type == OP_DBSTATE);
3322 assert(oldop->op_next == o);
3325 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
3327 /* Do not assume pad offsets for $c and $d are con-
3332 if ( oldoldop->op_targ + old_count == base
3333 && old_count < OPpPADRANGE_COUNTMASK - count) {
3334 base = oldoldop->op_targ;
3340 /* if there's any immediately following singleton
3341 * my var's; then swallow them and the associated
3343 * my ($a,$b); my $c; my $d;
3348 while ( ((p = followop->op_next))
3349 && ( p->op_type == OP_PADSV
3350 || p->op_type == OP_PADAV
3351 || p->op_type == OP_PADHV)
3352 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
3353 && (p->op_private & OPpLVAL_INTRO) == intro
3354 && !(p->op_private & ~OPpLVAL_INTRO)
3356 && ( p->op_next->op_type == OP_NEXTSTATE
3357 || p->op_next->op_type == OP_DBSTATE)
3358 && count < OPpPADRANGE_COUNTMASK
3359 && base + count == p->op_targ
3362 followop = p->op_next;
3370 assert(oldoldop->op_type == OP_PADRANGE);
3371 oldoldop->op_next = followop;
3372 oldoldop->op_private = (intro | count);
3378 /* Convert the pushmark into a padrange.
3379 * To make Deparse easier, we guarantee that a padrange was
3380 * *always* formerly a pushmark */
3381 assert(o->op_type == OP_PUSHMARK);
3382 o->op_next = followop;
3383 OpTYPE_set(o, OP_PADRANGE);
3385 /* bit 7: INTRO; bit 6..0: count */
3386 o->op_private = (intro | count);
3387 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
3388 | gvoid * OPf_WANT_VOID
3389 | (defav ? OPf_SPECIAL : 0));
3395 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3396 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3401 /*'keys %h' in void or scalar context: skip the OP_KEYS
3402 * and perform the functionality directly in the RV2HV/PADHV
3405 if (o->op_flags & OPf_REF) {
3407 U8 want = (k->op_flags & OPf_WANT);
3409 && k->op_type == OP_KEYS
3410 && ( want == OPf_WANT_VOID
3411 || want == OPf_WANT_SCALAR)
3412 && !(k->op_private & OPpMAYBE_LVSUB)
3413 && !(k->op_flags & OPf_MOD)
3415 o->op_next = k->op_next;
3416 o->op_flags &= ~(OPf_REF|OPf_WANT);
3417 o->op_flags |= want;
3418 o->op_private |= (o->op_type == OP_PADHV ?
3419 OPpPADHV_ISKEYS : OPpRV2HV_ISKEYS);
3420 /* for keys(%lex), hold onto the OP_KEYS's targ
3421 * since padhv doesn't have its own targ to return
3423 if (!(o->op_type ==OP_PADHV && want == OPf_WANT_SCALAR))
3428 /* see if %h is used in boolean context */
3429 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3430 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
3433 if (o->op_type != OP_PADHV)
3437 if ( o->op_type == OP_PADAV
3438 && (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR
3440 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3443 /* Skip over state($x) in void context. */
3444 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO)
3445 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID)
3447 oldop->op_next = o->op_next;
3448 goto redo_nextstate;
3450 if (o->op_type != OP_PADAV)
3454 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
3455 OP* const pop = (o->op_type == OP_PADAV) ?
3456 o->op_next : o->op_next->op_next;
3458 if (pop && pop->op_type == OP_CONST &&
3459 ((PL_op = pop->op_next)) &&
3460 pop->op_next->op_type == OP_AELEM &&
3461 !(pop->op_next->op_private &
3462 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
3463 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
3466 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
3467 no_bareword_allowed(pop);
3468 if (o->op_type == OP_GV)
3469 op_null(o->op_next);
3470 op_null(pop->op_next);
3472 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
3473 o->op_next = pop->op_next->op_next;
3474 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
3475 o->op_private = (U8)i;
3476 if (o->op_type == OP_GV) {
3479 o->op_type = OP_AELEMFAST;
3482 o->op_type = OP_AELEMFAST_LEX;
3484 if (o->op_type != OP_GV)
3488 /* Remove $foo from the op_next chain in void context. */
3490 && ( o->op_next->op_type == OP_RV2SV
3491 || o->op_next->op_type == OP_RV2AV
3492 || o->op_next->op_type == OP_RV2HV )
3493 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
3494 && !(o->op_next->op_private & OPpLVAL_INTRO))
3496 oldop->op_next = o->op_next->op_next;
3497 /* Reprocess the previous op if it is a nextstate, to
3498 allow double-nextstate optimisation. */
3500 if (oldop->op_type == OP_NEXTSTATE) {
3510 else if (o->op_next->op_type == OP_RV2SV) {
3511 if (!(o->op_next->op_private & OPpDEREF)) {
3512 op_null(o->op_next);
3513 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
3515 o->op_next = o->op_next->op_next;
3516 OpTYPE_set(o, OP_GVSV);
3519 else if (o->op_next->op_type == OP_READLINE
3520 && o->op_next->op_next->op_type == OP_CONCAT
3521 && (o->op_next->op_next->op_flags & OPf_STACKED))
3523 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
3524 OpTYPE_set(o, OP_RCATLINE);
3525 o->op_flags |= OPf_STACKED;
3526 op_null(o->op_next->op_next);
3527 op_null(o->op_next);
3538 case OP_CMPCHAIN_AND:
3540 while (cLOGOP->op_other->op_type == OP_NULL)
3541 cLOGOP->op_other = cLOGOP->op_other->op_next;
3542 while (o->op_next && ( o->op_type == o->op_next->op_type
3543 || o->op_next->op_type == OP_NULL))
3544 o->op_next = o->op_next->op_next;
3546 /* If we're an OR and our next is an AND in void context, we'll
3547 follow its op_other on short circuit, same for reverse.
3548 We can't do this with OP_DOR since if it's true, its return
3549 value is the underlying value which must be evaluated
3553 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
3554 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
3556 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
3558 o->op_next = ((LOGOP*)o->op_next)->op_other;
3560 DEFER(cLOGOP->op_other);
3565 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3566 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3576 while (cLOGOP->op_other->op_type == OP_NULL)
3577 cLOGOP->op_other = cLOGOP->op_other->op_next;
3578 DEFER(cLOGOP->op_other);
3583 while (cLOOP->op_redoop->op_type == OP_NULL)
3584 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
3585 while (cLOOP->op_nextop->op_type == OP_NULL)
3586 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
3587 while (cLOOP->op_lastop->op_type == OP_NULL)
3588 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
3589 /* a while(1) loop doesn't have an op_next that escapes the
3590 * loop, so we have to explicitly follow the op_lastop to
3591 * process the rest of the code */
3592 DEFER(cLOOP->op_lastop);
3596 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
3597 DEFER(cLOGOPo->op_other);
3600 case OP_ENTERTRYCATCH:
3601 assert(cLOGOPo->op_other->op_type == OP_CATCH);
3602 /* catch body is the ->op_other of the OP_CATCH */
3603 DEFER(cLOGOPx(cLOGOPo->op_other)->op_other);
3607 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3608 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3609 assert(!(cPMOP->op_pmflags & PMf_ONCE));
3610 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
3611 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
3612 cPMOP->op_pmstashstartu.op_pmreplstart
3613 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
3614 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
3620 if (o->op_flags & OPf_SPECIAL) {
3621 /* first arg is a code block */
3622 OP * const nullop = OpSIBLING(cLISTOP->op_first);
3623 OP * kid = cUNOPx(nullop)->op_first;
3625 assert(nullop->op_type == OP_NULL);
3626 assert(kid->op_type == OP_SCOPE
3627 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
3628 /* since OP_SORT doesn't have a handy op_other-style
3629 * field that can point directly to the start of the code
3630 * block, store it in the otherwise-unused op_next field
3631 * of the top-level OP_NULL. This will be quicker at
3632 * run-time, and it will also allow us to remove leading
3633 * OP_NULLs by just messing with op_nexts without
3634 * altering the basic op_first/op_sibling layout. */
3635 kid = kLISTOP->op_first;
3637 (kid->op_type == OP_NULL
3638 && ( kid->op_targ == OP_NEXTSTATE
3639 || kid->op_targ == OP_DBSTATE ))
3640 || kid->op_type == OP_STUB
3641 || kid->op_type == OP_ENTER
3642 || (PL_parser && PL_parser->error_count));
3643 nullop->op_next = kid->op_next;
3644 DEFER(nullop->op_next);
3647 /* check that RHS of sort is a single plain array */
3648 oright = cUNOPo->op_first;
3649 if (!oright || oright->op_type != OP_PUSHMARK)
3652 if (o->op_private & OPpSORT_INPLACE)
3655 /* reverse sort ... can be optimised. */
3656 if (!OpHAS_SIBLING(cUNOPo)) {
3657 /* Nothing follows us on the list. */
3658 OP * const reverse = o->op_next;
3660 if (reverse->op_type == OP_REVERSE &&
3661 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
3662 OP * const pushmark = cUNOPx(reverse)->op_first;
3663 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
3664 && (OpSIBLING(cUNOPx(pushmark)) == o)) {
3665 /* reverse -> pushmark -> sort */
3666 o->op_private |= OPpSORT_REVERSE;
3668 pushmark->op_next = oright->op_next;
3678 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
3680 LISTOP *enter, *exlist;
3682 if (o->op_private & OPpSORT_INPLACE)
3685 enter = (LISTOP *) o->op_next;
3688 if (enter->op_type == OP_NULL) {
3689 enter = (LISTOP *) enter->op_next;
3693 /* for $a (...) will have OP_GV then OP_RV2GV here.
3694 for (...) just has an OP_GV. */
3695 if (enter->op_type == OP_GV) {
3696 gvop = (OP *) enter;
3697 enter = (LISTOP *) enter->op_next;
3700 if (enter->op_type == OP_RV2GV) {
3701 enter = (LISTOP *) enter->op_next;
3707 if (enter->op_type != OP_ENTERITER)
3710 iter = enter->op_next;
3711 if (!iter || iter->op_type != OP_ITER)
3714 expushmark = enter->op_first;
3715 if (!expushmark || expushmark->op_type != OP_NULL
3716 || expushmark->op_targ != OP_PUSHMARK)
3719 exlist = (LISTOP *) OpSIBLING(expushmark);
3720 if (!exlist || exlist->op_type != OP_NULL
3721 || exlist->op_targ != OP_LIST)
3724 if (exlist->op_last != o) {
3725 /* Mmm. Was expecting to point back to this op. */
3728 theirmark = exlist->op_first;
3729 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
3732 if (OpSIBLING(theirmark) != o) {
3733 /* There's something between the mark and the reverse, eg
3734 for (1, reverse (...))
3739 ourmark = ((LISTOP *)o)->op_first;
3740 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
3743 ourlast = ((LISTOP *)o)->op_last;
3744 if (!ourlast || ourlast->op_next != o)
3747 rv2av = OpSIBLING(ourmark);
3748 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av)
3749 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
3750 /* We're just reversing a single array. */
3751 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
3752 enter->op_flags |= OPf_STACKED;
3755 /* We don't have control over who points to theirmark, so sacrifice
3757 theirmark->op_next = ourmark->op_next;
3758 theirmark->op_flags = ourmark->op_flags;
3759 ourlast->op_next = gvop ? gvop : (OP *) enter;
3762 enter->op_private |= OPpITER_REVERSED;
3763 iter->op_private |= OPpITER_REVERSED;
3769 NOT_REACHED; /* NOTREACHED */
3775 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
3776 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
3781 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)
3782 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb)))
3785 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
3787 sv = newRV((SV *)PL_compcv);
3791 OpTYPE_set(o, OP_CONST);
3792 o->op_flags |= OPf_SPECIAL;
3798 if (OP_GIMME(o,0) == G_VOID
3799 || ( o->op_next->op_type == OP_LINESEQ
3800 && ( o->op_next->op_next->op_type == OP_LEAVESUB
3801 || ( o->op_next->op_next->op_type == OP_RETURN
3802 && !CvLVALUE(PL_compcv)))))
3804 OP *right = cBINOP->op_first;
3823 OP *left = OpSIBLING(right);
3824 if (left->op_type == OP_SUBSTR
3825 && (left->op_private & 7) < 4) {
3828 op_sibling_splice(o, NULL, 1, NULL);
3829 /* and insert it as second child of OP_SUBSTR */
3830 op_sibling_splice(left, cBINOPx(left)->op_first, 0,
3832 left->op_private |= OPpSUBSTR_REPL_FIRST;
3834 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
3841 int l, r, lr, lscalars, rscalars;
3843 /* handle common vars detection, e.g. ($a,$b) = ($b,$a).
3844 Note that we do this now rather than in newASSIGNOP(),
3845 since only by now are aliased lexicals flagged as such
3847 See the essay "Common vars in list assignment" above for
3848 the full details of the rationale behind all the conditions
3851 PL_generation sorcery:
3852 To detect whether there are common vars, the global var
3853 PL_generation is incremented for each assign op we scan.
3854 Then we run through all the lexical variables on the LHS,
3855 of the assignment, setting a spare slot in each of them to
3856 PL_generation. Then we scan the RHS, and if any lexicals
3857 already have that value, we know we've got commonality.
3858 Also, if the generation number is already set to
3859 PERL_INT_MAX, then the variable is involved in aliasing, so
3860 we also have potential commonality in that case.
3866 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, &lscalars);
3869 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, &rscalars);
3873 /* After looking for things which are *always* safe, this main
3874 * if/else chain selects primarily based on the type of the
3875 * LHS, gradually working its way down from the more dangerous
3876 * to the more restrictive and thus safer cases */
3878 if ( !l /* () = ....; */
3879 || !r /* .... = (); */
3880 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */
3881 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */
3882 || (lscalars < 2) /* (undef, $x) = ... */
3884 NOOP; /* always safe */
3886 else if (l & AAS_DANGEROUS) {
3887 /* always dangerous */
3888 o->op_private |= OPpASSIGN_COMMON_SCALAR;
3889 o->op_private |= OPpASSIGN_COMMON_AGG;
3891 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) {
3892 /* package vars are always dangerous - too many
3893 * aliasing possibilities */
3894 if (l & AAS_PKG_SCALAR)
3895 o->op_private |= OPpASSIGN_COMMON_SCALAR;
3896 if (l & AAS_PKG_AGG)
3897 o->op_private |= OPpASSIGN_COMMON_AGG;
3899 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG
3900 |AAS_LEX_SCALAR|AAS_LEX_AGG))
3902 /* LHS contains only lexicals and safe ops */
3904 if (l & (AAS_MY_AGG|AAS_LEX_AGG))
3905 o->op_private |= OPpASSIGN_COMMON_AGG;
3907 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) {
3908 if (lr & AAS_LEX_SCALAR_COMM)
3909 o->op_private |= OPpASSIGN_COMMON_SCALAR;
3910 else if ( !(l & AAS_LEX_SCALAR)
3915 * as scalar-safe for performance reasons.
3916 * (it will still have been marked _AGG if necessary */
3919 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS))
3920 /* if there are only lexicals on the LHS and no
3921 * common ones on the RHS, then we assume that the
3922 * only way those lexicals could also get
3923 * on the RHS is via some sort of dereffing or
3926 * ($lex, $x) = (1, $$r)
3927 * and in this case we assume the var must have
3928 * a bumped ref count. So if its ref count is 1,
3929 * it must only be on the LHS.
3931 o->op_private |= OPpASSIGN_COMMON_RC1;
3936 * may have to handle aggregate on LHS, but we can't
3937 * have common scalars. */
3940 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1);
3942 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3943 S_check_for_bool_cxt(o, 1, OPpASSIGN_TRUEBOOL, 0);
3949 /* if the op is used in boolean context, set the TRUEBOOL flag
3950 * which enables an optimisation at runtime which avoids creating
3951 * a stack temporary for known-true package names */
3952 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3953 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
3957 /* see if the op is used in known boolean context,
3958 * but not if OA_TARGLEX optimisation is enabled */
3959 if ( (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR
3960 && !(o->op_private & OPpTARGET_MY)
3962 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3966 /* see if the op is used in known boolean context */
3967 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3968 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3972 Perl_cpeep_t cpeep =
3973 XopENTRYCUSTOM(o, xop_peep);
3975 cpeep(aTHX_ o, oldop);
3980 /* did we just null the current op? If so, re-process it to handle
3981 * eliding "empty" ops from the chain */
3982 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
3995 Perl_peep(pTHX_ OP *o)
4001 * ex: set ts=8 sts=4 sw=4 et: