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
20 /* This file contains functions for optimizing and finalizing the OP
21 * structures that hold a compiled perl program
25 #define PERL_IN_PEEP_C
29 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
33 S_scalar_slice_warning(pTHX_ const OP *o)
36 const bool is_hash = o->op_type == OP_HSLICE
37 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
40 if (!(o->op_private & OPpSLICEWARNING))
42 if (PL_parser && PL_parser->error_count)
43 /* This warning can be nonsensical when there is a syntax error. */
46 kid = cLISTOPo->op_first;
47 kid = OpSIBLING(kid); /* get past pushmark */
48 /* weed out false positives: any ops that can return lists */
49 switch (kid->op_type) {
75 /* Don't warn if we have a nulled list either. */
76 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
79 assert(OpSIBLING(kid));
80 name = op_varname(OpSIBLING(kid));
81 if (!name) /* XS module fiddling with the op tree */
83 warn_elem_scalar_context(kid, name, is_hash, true);
87 /* info returned by S_sprintf_is_multiconcatable() */
89 struct sprintf_ismc_info {
90 SSize_t nargs; /* num of args to sprintf (not including the format) */
91 char *start; /* start of raw format string */
92 char *end; /* bytes after end of raw format string */
93 STRLEN total_len; /* total length (in bytes) of format string, not
94 including '%s' and half of '%%' */
95 STRLEN variant; /* number of bytes by which total_len_p would grow
96 if upgraded to utf8 */
97 bool utf8; /* whether the format is utf8 */
100 /* is the OP_SPRINTF o suitable for converting into a multiconcat op?
101 * i.e. its format argument is a const string with only '%s' and '%%'
102 * formats, and the number of args is known, e.g.
103 * sprintf "a=%s f=%s", $a[0], scalar(f());
105 * sprintf "i=%d a=%s f=%s", $i, @a, f();
107 * If successful, the sprintf_ismc_info struct pointed to by info will be
112 S_sprintf_is_multiconcatable(pTHX_ OP *o,struct sprintf_ismc_info *info)
114 OP *pm, *constop, *kid;
117 SSize_t nargs, nformats;
118 STRLEN cur, total_len, variant;
121 /* if sprintf's behaviour changes, die here so that someone
122 * can decide whether to enhance this function or skip optimising
123 * under those new circumstances */
124 assert(!(o->op_flags & OPf_STACKED));
125 assert(!(PL_opargs[OP_SPRINTF] & OA_TARGLEX));
126 assert(!(o->op_private & ~OPpARG4_MASK));
128 pm = cUNOPo->op_first;
129 if (pm->op_type != OP_PUSHMARK) /* weird coreargs stuff */
131 constop = OpSIBLING(pm);
132 if (!constop || constop->op_type != OP_CONST)
134 sv = cSVOPx_sv(constop);
135 if (SvMAGICAL(sv) || !SvPOK(sv))
141 /* Scan format for %% and %s and work out how many %s there are.
142 * Abandon if other format types are found.
149 for (p = s; p < e; p++) {
152 if (!UTF8_IS_INVARIANT(*p))
158 return FALSE; /* lone % at end gives "Invalid conversion" */
167 if (!nformats || nformats > PERL_MULTICONCAT_MAXARG)
170 utf8 = cBOOL(SvUTF8(sv));
174 /* scan args; they must all be in scalar cxt */
177 kid = OpSIBLING(constop);
180 if ((kid->op_flags & OPf_WANT) != OPf_WANT_SCALAR)
183 kid = OpSIBLING(kid);
186 if (nargs != nformats)
187 return FALSE; /* e.g. sprintf("%s%s", $a); */
193 info->total_len = total_len;
194 info->variant = variant;
200 /* S_maybe_multiconcat():
202 * given an OP_STRINGIFY, OP_SASSIGN, OP_CONCAT or OP_SPRINTF op, possibly
203 * convert it (and its children) into an OP_MULTICONCAT. See the code
204 * comments just before pp_multiconcat() for the full details of what
205 * OP_MULTICONCAT supports.
207 * Basically we're looking for an optree with a chain of OP_CONCATS down
208 * the LHS (or an OP_SPRINTF), with possibly an OP_SASSIGN, and/or
209 * OP_STRINGIFY, and/or OP_CONCAT acting as '.=' at its head, e.g.
217 * STRINGIFY -- PADSV[$x]
220 * ex-PUSHMARK -- CONCAT/S
222 * CONCAT/S -- PADSV[$d]
224 * CONCAT -- CONST["-"]
226 * PADSV[$a] -- PADSV[$b]
228 * Note that at this stage the OP_SASSIGN may have already been optimised
229 * away with OPpTARGET_MY set on the OP_STRINGIFY or OP_CONCAT.
233 S_maybe_multiconcat(pTHX_ OP *o)
235 OP *lastkidop; /* the right-most of any kids unshifted onto o */
236 OP *topop; /* the top-most op in the concat tree (often equals o,
237 unless there are assign/stringify ops above it */
238 OP *parentop; /* the parent op of topop (or itself if no parent) */
239 OP *targmyop; /* the op (if any) with the OPpTARGET_MY flag */
240 OP *targetop; /* the op corresponding to target=... or target.=... */
241 OP *stringop; /* the OP_STRINGIFY op, if any */
242 OP *nextop; /* used for recreating the op_next chain without consts */
243 OP *kid; /* general-purpose op pointer */
247 struct sprintf_ismc_info sprintf_info;
249 /* store info about each arg in args[];
250 * toparg is the highest used slot; argp is a general
251 * pointer to args[] slots */
253 void *p; /* initially points to const sv (or null for op);
254 later, set to SvPV(constsv), with ... */
255 STRLEN len; /* ... len set to SvPV(..., len) */
256 } *argp, *toparg, args[PERL_MULTICONCAT_MAXARG*2 + 1];
260 SSize_t nadjconst = 0; /* adjacent consts - may be demoted to args */
263 bool kid_is_last = FALSE; /* most args will be the RHS kid of a concat op;
264 the last-processed arg will the LHS of one,
265 as args are processed in reverse order */
266 U8 stacked_last = 0; /* whether the last seen concat op was STACKED */
267 STRLEN total_len = 0; /* sum of the lengths of the const segments */
268 U8 flags = 0; /* what will become the op_flags and ... */
269 U8 private_flags = 0; /* ... op_private of the multiconcat op */
270 bool is_sprintf = FALSE; /* we're optimising an sprintf */
271 bool is_targable = FALSE; /* targetop is an OPpTARGET_MY candidate */
272 bool prev_was_const = FALSE; /* previous arg was a const */
274 /* -----------------------------------------------------------------
277 * Examine the optree non-destructively to determine whether it's
278 * suitable to be converted into an OP_MULTICONCAT. Accumulate
279 * information about the optree in args[].
289 assert( o->op_type == OP_SASSIGN
290 || o->op_type == OP_CONCAT
291 || o->op_type == OP_SPRINTF
292 || o->op_type == OP_STRINGIFY);
294 Zero(&sprintf_info, 1, struct sprintf_ismc_info);
296 /* first see if, at the top of the tree, there is an assign,
297 * append and/or stringify */
299 if (topop->op_type == OP_SASSIGN) {
301 if (o->op_ppaddr != PL_ppaddr[OP_SASSIGN])
303 if (o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
305 assert(!(o->op_private & ~OPpARG2_MASK)); /* barf on unknown flags */
308 topop = cBINOPo->op_first;
309 targetop = OpSIBLING(topop);
310 if (!targetop) /* probably some sort of syntax error */
313 /* don't optimise away assign in 'local $foo = ....' */
314 if ( (targetop->op_private & OPpLVAL_INTRO)
315 /* these are the common ops which do 'local', but
317 && ( targetop->op_type == OP_GVSV
318 || targetop->op_type == OP_RV2SV
319 || targetop->op_type == OP_AELEM
320 || targetop->op_type == OP_HELEM
325 else if ( topop->op_type == OP_CONCAT
326 && (topop->op_flags & OPf_STACKED)
327 && (!(topop->op_private & OPpCONCAT_NESTED))
332 /* OPpTARGET_MY shouldn't be able to be set here. If it is,
333 * decide what to do about it */
334 assert(!(o->op_private & OPpTARGET_MY));
336 /* barf on unknown flags */
337 assert(!(o->op_private & ~(OPpARG2_MASK|OPpTARGET_MY)));
338 private_flags |= OPpMULTICONCAT_APPEND;
339 targetop = cBINOPo->op_first;
341 topop = OpSIBLING(targetop);
343 /* $x .= <FOO> gets optimised to rcatline instead */
344 if (topop->op_type == OP_READLINE)
349 /* Can targetop (the LHS) if it's a padsv, be optimised
350 * away and use OPpTARGET_MY instead?
352 if ( (targetop->op_type == OP_PADSV)
353 && !(targetop->op_private & OPpDEREF)
354 && !(targetop->op_private & OPpPAD_STATE)
355 /* we don't support 'my $x .= ...' */
356 && ( o->op_type == OP_SASSIGN
357 || !(targetop->op_private & OPpLVAL_INTRO))
362 if (topop->op_type == OP_STRINGIFY) {
363 if (topop->op_ppaddr != PL_ppaddr[OP_STRINGIFY])
367 /* barf on unknown flags */
368 assert(!(o->op_private & ~(OPpARG4_MASK|OPpTARGET_MY)));
370 if ((topop->op_private & OPpTARGET_MY)) {
371 if (o->op_type == OP_SASSIGN)
372 return; /* can't have two assigns */
376 private_flags |= OPpMULTICONCAT_STRINGIFY;
378 topop = cBINOPx(topop)->op_first;
379 assert(OP_TYPE_IS_OR_WAS_NN(topop, OP_PUSHMARK));
380 topop = OpSIBLING(topop);
383 if (topop->op_type == OP_SPRINTF) {
384 if (topop->op_ppaddr != PL_ppaddr[OP_SPRINTF])
386 if (S_sprintf_is_multiconcatable(aTHX_ topop, &sprintf_info)) {
387 nargs = sprintf_info.nargs;
388 total_len = sprintf_info.total_len;
389 variant = sprintf_info.variant;
390 utf8 = sprintf_info.utf8;
392 private_flags |= OPpMULTICONCAT_FAKE;
394 /* we have an sprintf op rather than a concat optree.
395 * Skip most of the code below which is associated with
396 * processing that optree. We also skip phase 2, determining
397 * whether its cost effective to optimise, since for sprintf,
398 * multiconcat is *always* faster */
401 /* note that even if the sprintf itself isn't multiconcatable,
402 * the expression as a whole may be, e.g. in
403 * $x .= sprintf("%d",...)
404 * the sprintf op will be left as-is, but the concat/S op may
405 * be upgraded to multiconcat
408 else if (topop->op_type == OP_CONCAT) {
409 if (topop->op_ppaddr != PL_ppaddr[OP_CONCAT])
412 if ((topop->op_private & OPpTARGET_MY)) {
413 if (o->op_type == OP_SASSIGN || targmyop)
414 return; /* can't have two assigns */
419 /* Is it safe to convert a sassign/stringify/concat op into
421 assert((PL_opargs[OP_SASSIGN] & OA_CLASS_MASK) == OA_BINOP);
422 assert((PL_opargs[OP_CONCAT] & OA_CLASS_MASK) == OA_BINOP);
423 assert((PL_opargs[OP_STRINGIFY] & OA_CLASS_MASK) == OA_LISTOP);
424 assert((PL_opargs[OP_SPRINTF] & OA_CLASS_MASK) == OA_LISTOP);
425 STATIC_ASSERT_STMT( STRUCT_OFFSET(BINOP, op_last)
426 == STRUCT_OFFSET(UNOP_AUX, op_aux));
427 STATIC_ASSERT_STMT( STRUCT_OFFSET(LISTOP, op_last)
428 == STRUCT_OFFSET(UNOP_AUX, op_aux));
430 /* Now scan the down the tree looking for a series of
431 * CONCAT/OPf_STACKED ops on the LHS (with the last one not
432 * stacked). For example this tree:
437 * CONCAT/STACKED -- EXPR5
439 * CONCAT/STACKED -- EXPR4
445 * corresponds to an expression like
447 * (EXPR1 . EXPR2 . EXPR3 . EXPR4 . EXPR5)
449 * Record info about each EXPR in args[]: in particular, whether it is
450 * a stringifiable OP_CONST and if so what the const sv is.
452 * The reason why the last concat can't be STACKED is the difference
455 * ((($a .= $a) .= $a) .= $a) .= $a
458 * $a . $a . $a . $a . $a
460 * The main difference between the optrees for those two constructs
461 * is the presence of the last STACKED. As well as modifying $a,
462 * the former sees the changed $a between each concat, so if $s is
463 * initially 'a', the first returns 'a' x 16, while the latter returns
464 * 'a' x 5. And pp_multiconcat can't handle that kind of thing.
474 if ( kid->op_type == OP_CONCAT
478 k1 = cUNOPx(kid)->op_first;
480 /* shouldn't happen except maybe after compile err? */
484 /* avoid turning (A . B . ($lex = C) ...) into (A . B . C ...) */
485 if (kid->op_private & OPpTARGET_MY)
488 stacked_last = (kid->op_flags & OPf_STACKED);
500 if ( nargs + nadjconst > PERL_MULTICONCAT_MAXARG - 2
501 || (argp - args + 1) > (PERL_MULTICONCAT_MAXARG*2 + 1) - 2)
503 /* At least two spare slots are needed to decompose both
504 * concat args. If there are no slots left, continue to
505 * examine the rest of the optree, but don't push new values
506 * on args[]. If the optree as a whole is legal for conversion
507 * (in particular that the last concat isn't STACKED), then
508 * the first PERL_MULTICONCAT_MAXARG elements of the optree
509 * can be converted into an OP_MULTICONCAT now, with the first
510 * child of that op being the remainder of the optree -
511 * which may itself later be converted to a multiconcat op
515 /* the last arg is the rest of the optree */
520 else if ( argop->op_type == OP_CONST
521 && ((sv = cSVOPx_sv(argop)))
522 /* defer stringification until runtime of 'constant'
523 * things that might stringify variantly, e.g. the radix
524 * point of NVs, or overloaded RVs */
525 && (SvPOK(sv) || SvIOK(sv))
528 if (argop->op_private & OPpCONST_STRICT)
529 no_bareword_allowed(argop);
531 utf8 |= cBOOL(SvUTF8(sv));
534 /* this const may be demoted back to a plain arg later;
535 * make sure we have enough arg slots left */
537 prev_was_const = !prev_was_const;
542 prev_was_const = FALSE;
552 return; /* we don't support ((A.=B).=C)...) */
554 /* look for two adjacent consts and don't fold them together:
557 * $o->concat("a")->concat("b")
560 * (but $o .= "a" . "b" should still fold)
563 bool seen_nonconst = FALSE;
564 for (argp = toparg; argp >= args; argp--) {
565 if (argp->p == NULL) {
566 seen_nonconst = TRUE;
572 /* both previous and current arg were constants;
573 * leave the current OP_CONST as-is */
581 /* -----------------------------------------------------------------
584 * At this point we have determined that the optree *can* be converted
585 * into a multiconcat. Having gathered all the evidence, we now decide
586 * whether it *should*.
590 /* we need at least one concat action, e.g.:
596 * otherwise we could be doing something like $x = "foo", which
597 * if treated as a concat, would fail to COW.
599 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 2)
602 /* Benchmarking seems to indicate that we gain if:
603 * * we optimise at least two actions into a single multiconcat
604 * (e.g concat+concat, sassign+concat);
605 * * or if we can eliminate at least 1 OP_CONST;
606 * * or if we can eliminate a padsv via OPpTARGET_MY
610 /* eliminated at least one OP_CONST */
612 /* eliminated an OP_SASSIGN */
613 || o->op_type == OP_SASSIGN
614 /* eliminated an OP_PADSV */
615 || (!targmyop && is_targable)
617 /* definitely a net gain to optimise */
620 /* ... if not, what else? */
622 /* special-case '$lex1 = expr . $lex1' (where expr isn't lex1):
623 * multiconcat is faster (due to not creating a temporary copy of
624 * $lex1), whereas for a general $lex1 = $lex2 . $lex3, concat is
630 && topop->op_type == OP_CONCAT
632 PADOFFSET t = targmyop->op_targ;
633 OP *k1 = cBINOPx(topop)->op_first;
634 OP *k2 = cBINOPx(topop)->op_last;
635 if ( k2->op_type == OP_PADSV
637 && ( k1->op_type != OP_PADSV
643 /* need at least two concats */
644 if (nargs + nconst + cBOOL(private_flags & OPpMULTICONCAT_APPEND) < 3)
649 /* -----------------------------------------------------------------
652 * At this point the optree has been verified as ok to be optimised
653 * into an OP_MULTICONCAT. Now start changing things.
658 /* stringify all const args and determine utf8ness */
661 for (argp = args; argp <= toparg; argp++) {
662 SV *sv = (SV*)argp->p;
664 continue; /* not a const op */
665 if (utf8 && !SvUTF8(sv))
666 sv_utf8_upgrade_nomg(sv);
667 argp->p = SvPV_nomg(sv, argp->len);
668 total_len += argp->len;
670 /* see if any strings would grow if converted to utf8 */
672 variant += variant_under_utf8_count((U8 *) argp->p,
673 (U8 *) argp->p + argp->len);
677 /* create and populate aux struct */
681 aux = (UNOP_AUX_item*)PerlMemShared_malloc(
682 sizeof(UNOP_AUX_item)
684 PERL_MULTICONCAT_HEADER_SIZE
685 + ((nargs + 1) * (variant ? 2 : 1))
688 const_str = (char *)PerlMemShared_malloc(total_len ? total_len : 1);
690 /* Extract all the non-const expressions from the concat tree then
691 * dispose of the old tree, e.g. convert the tree from this:
695 * STRINGIFY -- TARGET
697 * ex-PUSHMARK -- CONCAT
712 * ex-PUSHMARK -- EXPR1 -- EXPR2 -- EXPR3 -- EXPR4 -- EXPR5 -- TARGET
714 * except that if EXPRi is an OP_CONST, it's discarded.
716 * During the conversion process, EXPR ops are stripped from the tree
717 * and unshifted onto o. Finally, any of o's remaining original
718 * childen are discarded and o is converted into an OP_MULTICONCAT.
720 * In this middle of this, o may contain both: unshifted args on the
721 * left, and some remaining original args on the right. lastkidop
722 * is set to point to the right-most unshifted arg to delineate
723 * between the two sets.
728 /* create a copy of the format with the %'s removed, and record
729 * the sizes of the const string segments in the aux struct */
731 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
733 p = sprintf_info.start;
736 for (; p < sprintf_info.end; p++) {
740 (lenp++)->ssize = q - oldq;
747 lenp->ssize = q - oldq;
748 assert((STRLEN)(q - const_str) == total_len);
750 /* Attach all the args (i.e. the kids of the sprintf) to o (which
751 * may or may not be topop) The pushmark and const ops need to be
752 * kept in case they're an op_next entry point.
754 lastkidop = cLISTOPx(topop)->op_last;
755 kid = cUNOPx(topop)->op_first; /* pushmark */
757 op_null(OpSIBLING(kid)); /* const */
759 kid = op_sibling_splice(topop, NULL, -1, NULL); /* cut all args */
760 op_sibling_splice(o, NULL, 0, kid); /* and attach to o */
761 lastkidop->op_next = o;
766 lenp = aux + PERL_MULTICONCAT_IX_LENGTHS;
770 /* Concatenate all const strings into const_str.
771 * Note that args[] contains the RHS args in reverse order, so
772 * we scan args[] from top to bottom to get constant strings
775 for (argp = toparg; argp >= args; argp--) {
778 (++lenp)->ssize = -1;
780 STRLEN l = argp->len;
781 Copy(argp->p, p, l, char);
783 if (lenp->ssize == -1)
794 for (argp = args; argp <= toparg; argp++) {
795 /* only keep non-const args, except keep the first-in-next-chain
796 * arg no matter what it is (but nulled if OP_CONST), because it
797 * may be the entry point to this subtree from the previous
800 bool last = (argp == toparg);
803 /* set prev to the sibling *before* the arg to be cut out,
804 * e.g. when cutting EXPR:
809 * prev= CONCAT -- EXPR
812 if (argp == args && kid->op_type != OP_CONCAT) {
813 /* in e.g. '$x .= f(1)' there's no RHS concat tree
814 * so the expression to be cut isn't kid->op_last but
817 /* find the op before kid */
819 o2 = cUNOPx(parentop)->op_first;
820 while (o2 && o2 != kid) {
828 else if (kid == o && lastkidop)
829 prev = last ? lastkidop : OpSIBLING(lastkidop);
831 prev = last ? NULL : cUNOPx(kid)->op_first;
833 if (!argp->p || last) {
835 OP *aop = op_sibling_splice(kid, prev, 1, NULL);
836 /* and unshift to front of o */
837 op_sibling_splice(o, NULL, 0, aop);
838 /* record the right-most op added to o: later we will
839 * free anything to the right of it */
842 aop->op_next = nextop;
845 /* null the const at start of op_next chain */
849 nextop = prev->op_next;
852 /* the last two arguments are both attached to the same concat op */
853 if (argp < toparg - 1)
858 /* Populate the aux struct */
860 aux[PERL_MULTICONCAT_IX_NARGS].ssize = nargs;
861 aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv = utf8 ? NULL : const_str;
862 aux[PERL_MULTICONCAT_IX_PLAIN_LEN].ssize = utf8 ? 0 : total_len;
863 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = const_str;
864 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = total_len;
866 /* if variant > 0, calculate a variant const string and lengths where
867 * the utf8 version of the string will take 'variant' more bytes than
872 STRLEN ulen = total_len + variant;
873 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
874 UNOP_AUX_item *ulens = lens + (nargs + 1);
875 char *up = (char*)PerlMemShared_malloc(ulen);
878 aux[PERL_MULTICONCAT_IX_UTF8_PV].pv = up;
879 aux[PERL_MULTICONCAT_IX_UTF8_LEN].ssize = ulen;
881 for (n = 0; n < (nargs + 1); n++) {
884 for (i = (lens++)->ssize; i > 0; i--) {
886 append_utf8_from_native_byte(c, (U8**)&up);
888 (ulens++)->ssize = (i < 0) ? i : up - orig_up;
893 /* if there was a top(ish)-level OP_STRINGIFY, we need to keep
894 * that op's first child - an ex-PUSHMARK - because the op_next of
895 * the previous op may point to it (i.e. it's the entry point for
900 ? op_sibling_splice(o, lastkidop, 1, NULL)
901 : op_sibling_splice(stringop, NULL, 1, NULL);
902 assert(OP_TYPE_IS_OR_WAS_NN(pmop, OP_PUSHMARK));
903 op_sibling_splice(o, NULL, 0, pmop);
916 if (o->op_type == OP_SASSIGN) {
917 /* Move the target subtree from being the last of o's children
918 * to being the last of o's preserved children.
919 * Note the difference between 'target = ...' and 'target .= ...':
920 * for the former, target is executed last; for the latter,
923 kid = OpSIBLING(lastkidop);
924 op_sibling_splice(o, kid, 1, NULL); /* cut target op */
925 op_sibling_splice(o, lastkidop, 0, targetop); /* and paste */
926 lastkidop->op_next = kid->op_next;
927 lastkidop = targetop;
930 /* Move the target subtree from being the first of o's
931 * original children to being the first of *all* o's children.
934 op_sibling_splice(o, lastkidop, 1, NULL); /* cut target op */
935 op_sibling_splice(o, NULL, 0, targetop); /* and paste*/
938 /* if the RHS of .= doesn't contain a concat (e.g.
939 * $x .= "foo"), it gets missed by the "strip ops from the
940 * tree and add to o" loop earlier */
941 assert(topop->op_type != OP_CONCAT);
943 /* in e.g. $x .= "$y", move the $y expression
944 * from being a child of OP_STRINGIFY to being the
945 * second child of the OP_CONCAT
947 assert(cUNOPx(stringop)->op_first == topop);
948 op_sibling_splice(stringop, NULL, 1, NULL);
949 op_sibling_splice(o, cUNOPo->op_first, 0, topop);
951 assert(topop == OpSIBLING(cBINOPo->op_first));
963 * The original padsv op is kept but nulled in case it's the
964 * entry point for the optree (which it will be for
967 private_flags |= OPpTARGET_MY;
968 private_flags |= (targetop->op_private & OPpLVAL_INTRO);
969 o->op_targ = targetop->op_targ;
970 targetop->op_targ = 0;
974 flags |= OPf_STACKED;
977 private_flags |= OPpTARGET_MY;
979 o->op_targ = targmyop->op_targ;
980 targmyop->op_targ = 0;
984 /* detach the emaciated husk of the sprintf/concat optree and free it */
986 kid = op_sibling_splice(o, lastkidop, 1, NULL);
992 /* and convert o into a multiconcat */
994 o->op_flags = (flags|OPf_KIDS|stacked_last
995 |(o->op_flags & (OPf_WANT|OPf_PARENS)));
996 o->op_private = private_flags;
997 o->op_type = OP_MULTICONCAT;
998 o->op_ppaddr = PL_ppaddr[OP_MULTICONCAT];
999 cUNOP_AUXo->op_aux = aux;
1004 =for apidoc_section $optree_manipulation
1006 =for apidoc optimize_optree
1008 This function applies some optimisations to the optree in top-down order.
1009 It is called before the peephole optimizer, which processes ops in
1010 execution order. Note that finalize_optree() also does a top-down scan,
1011 but is called *after* the peephole optimizer.
1017 Perl_optimize_optree(pTHX_ OP* o)
1019 PERL_ARGS_ASSERT_OPTIMIZE_OPTREE;
1022 SAVEVPTR(PL_curcop);
1030 #define warn_implicit_snail_cvsig(o) S_warn_implicit_snail_cvsig(aTHX_ o)
1032 S_warn_implicit_snail_cvsig(pTHX_ OP *o)
1035 while(cv && CvEVAL(cv))
1038 if(cv && CvSIGNATURE(cv))
1039 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__ARGS_ARRAY_WITH_SIGNATURES),
1040 "Implicit use of @_ in %s with signatured subroutine is experimental", OP_DESC(o));
1044 #define OP_ZOOM(o) (OP_TYPE_IS(o, OP_NULL) ? cUNOPx(o)->op_first : (o))
1046 /* helper for optimize_optree() which optimises one op then recurses
1047 * to optimise any children.
1051 S_optimize_op(pTHX_ OP* o)
1055 PERL_ARGS_ASSERT_OPTIMIZE_OP;
1058 OP * next_kid = NULL;
1060 assert(o->op_type != OP_FREED);
1062 switch (o->op_type) {
1065 PL_curcop = ((COP*)o); /* for warnings */
1073 S_maybe_multiconcat(aTHX_ o);
1077 if (cPMOPo->op_pmreplrootu.op_pmreplroot) {
1078 /* we can't assume that op_pmreplroot->op_sibparent == o
1079 * and that it is thus possible to walk back up the tree
1080 * past op_pmreplroot. So, although we try to avoid
1081 * recursing through op trees, do it here. After all,
1082 * there are unlikely to be many nested s///e's within
1083 * the replacement part of a s///e.
1085 optimize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1091 OP *first = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1093 while(cv && CvEVAL(cv))
1096 if(cv && CvSIGNATURE(cv) &&
1097 OP_TYPE_IS(first, OP_GV) && cGVOPx_gv(first) == PL_defgv) {
1098 OP *parent = op_parent(o);
1099 while(OP_TYPE_IS(parent, OP_NULL))
1100 parent = op_parent(parent);
1102 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__ARGS_ARRAY_WITH_SIGNATURES),
1103 "Use of @_ in %s with signatured subroutine is experimental", OP_DESC(parent));
1110 if(!CvUNIQUE(PL_compcv) && !(o->op_flags & OPf_KIDS))
1111 warn_implicit_snail_cvsig(o);
1115 if(!(o->op_flags & OPf_STACKED))
1116 warn_implicit_snail_cvsig(o);
1121 OP *first = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1123 if(OP_TYPE_IS(first, OP_SREFGEN) &&
1124 (ffirst = OP_ZOOM(cUNOPx(first)->op_first)) &&
1125 OP_TYPE_IS(ffirst, OP_RV2CV))
1126 warn_implicit_snail_cvsig(o);
1134 if (o->op_flags & OPf_KIDS)
1135 next_kid = cUNOPo->op_first;
1137 /* if a kid hasn't been nominated to process, continue with the
1138 * next sibling, or if no siblings left, go back to the parent's
1139 * siblings and so on
1143 return; /* at top; no parents/siblings to try */
1144 if (OpHAS_SIBLING(o))
1145 next_kid = o->op_sibparent;
1147 o = o->op_sibparent; /*try parent's next sibling */
1150 /* this label not yet used. Goto here if any code above sets
1159 =for apidoc finalize_optree
1161 This function finalizes the optree. Should be called directly after
1162 the complete optree is built. It does some additional
1163 checking which can't be done in the normal C<ck_>xxx functions and makes
1164 the tree thread-safe.
1170 Perl_finalize_optree(pTHX_ OP* o)
1172 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1175 SAVEVPTR(PL_curcop);
1184 =for apidoc traverse_op_tree
1186 Return the next op in a depth-first traversal of the op tree,
1187 returning NULL when the traversal is complete.
1189 The initial call must supply the root of the tree as both top and o.
1191 For now it's static, but it may be exposed to the API in the future.
1197 S_traverse_op_tree(pTHX_ OP *top, OP *o) {
1200 PERL_ARGS_ASSERT_TRAVERSE_OP_TREE;
1202 if ((o->op_flags & OPf_KIDS) && cUNOPo->op_first) {
1203 return cUNOPo->op_first;
1205 else if ((sib = OpSIBLING(o))) {
1209 OP *parent = o->op_sibparent;
1210 assert(!(o->op_moresib));
1211 while (parent && parent != top) {
1212 OP *sib = OpSIBLING(parent);
1215 parent = parent->op_sibparent;
1223 S_finalize_op(pTHX_ OP* o)
1226 PERL_ARGS_ASSERT_FINALIZE_OP;
1229 assert(o->op_type != OP_FREED);
1231 switch (o->op_type) {
1234 PL_curcop = ((COP*)o); /* for warnings */
1237 if (OpHAS_SIBLING(o)) {
1238 OP *sib = OpSIBLING(o);
1239 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
1240 && ckWARN(WARN_EXEC)
1241 && OpHAS_SIBLING(sib))
1243 const OPCODE type = OpSIBLING(sib)->op_type;
1244 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1245 const line_t oldline = CopLINE(PL_curcop);
1246 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
1247 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1248 "Statement unlikely to be reached");
1249 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1250 "\t(Maybe you meant system() when you said exec()?)\n");
1251 CopLINE_set(PL_curcop, oldline);
1258 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1259 GV * const gv = cGVOPo_gv;
1260 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1261 /* XXX could check prototype here instead of just carping */
1262 SV * const sv = sv_newmortal();
1263 gv_efullname3(sv, gv, NULL);
1264 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1265 "%" SVf "() called too early to check prototype",
1272 if (cSVOPo->op_private & OPpCONST_STRICT)
1273 no_bareword_allowed(o);
1277 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
1282 /* Relocate all the METHOP's SVs to the pad for thread safety. */
1283 case OP_METHOD_NAMED:
1284 case OP_METHOD_SUPER:
1285 case OP_METHOD_REDIR:
1286 case OP_METHOD_REDIR_SUPER:
1287 op_relocate_sv(&cMETHOPo->op_u.op_meth_sv, &o->op_targ);
1296 if ((key_op = cSVOPx(cBINOPo->op_last))->op_type != OP_CONST)
1299 rop = cUNOPx(cBINOPo->op_first);
1304 S_scalar_slice_warning(aTHX_ o);
1308 kid = OpSIBLING(cLISTOPo->op_first);
1309 if (/* I bet there's always a pushmark... */
1310 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
1311 && OP_TYPE_ISNT_NN(kid, OP_CONST))
1316 key_op = cSVOPx(kid->op_type == OP_CONST
1318 : OpSIBLING(kLISTOP->op_first));
1320 rop = cUNOPx(cLISTOPo->op_last);
1323 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
1325 check_hash_fields_and_hekify(rop, key_op, 1);
1329 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
1333 S_scalar_slice_warning(aTHX_ o);
1337 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1338 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1346 if (o->op_flags & OPf_KIDS) {
1349 /* check that op_last points to the last sibling, and that
1350 * the last op_sibling/op_sibparent field points back to the
1351 * parent, and that the only ops with KIDS are those which are
1352 * entitled to them */
1353 U32 type = o->op_type;
1357 if (type == OP_NULL) {
1359 /* ck_glob creates a null UNOP with ex-type GLOB
1360 * (which is a list op. So pretend it wasn't a listop */
1361 if (type == OP_GLOB)
1364 family = PL_opargs[type] & OA_CLASS_MASK;
1366 has_last = ( family == OA_BINOP
1367 || family == OA_LISTOP
1368 || family == OA_PMOP
1369 || family == OA_LOOP
1371 assert( has_last /* has op_first and op_last, or ...
1372 ... has (or may have) op_first: */
1373 || family == OA_UNOP
1374 || family == OA_UNOP_AUX
1375 || family == OA_LOGOP
1376 || family == OA_BASEOP_OR_UNOP
1377 || family == OA_FILESTATOP
1378 || family == OA_LOOPEXOP
1379 || family == OA_METHOP
1380 || type == OP_CUSTOM
1381 || type == OP_NULL /* new_logop does this */
1384 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
1385 if (!OpHAS_SIBLING(kid)) {
1387 assert(kid == cLISTOPo->op_last);
1388 assert(kid->op_sibparent == o);
1393 } while (( o = traverse_op_tree(top, o)) != NULL);
1398 ---------------------------------------------------------
1400 Common vars in list assignment
1402 There now follows some enums and static functions for detecting
1403 common variables in list assignments. Here is a little essay I wrote
1404 for myself when trying to get my head around this. DAPM.
1408 First some random observations:
1410 * If a lexical var is an alias of something else, e.g.
1411 for my $x ($lex, $pkg, $a[0]) {...}
1412 then the act of aliasing will increase the reference count of the SV
1414 * If a package var is an alias of something else, it may still have a
1415 reference count of 1, depending on how the alias was created, e.g.
1416 in *a = *b, $a may have a refcount of 1 since the GP is shared
1417 with a single GvSV pointer to the SV. So If it's an alias of another
1418 package var, then RC may be 1; if it's an alias of another scalar, e.g.
1419 a lexical var or an array element, then it will have RC > 1.
1421 * There are many ways to create a package alias; ultimately, XS code
1422 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so
1423 run-time tracing mechanisms are unlikely to be able to catch all cases.
1425 * When the LHS is all my declarations, the same vars can't appear directly
1426 on the RHS, but they can indirectly via closures, aliasing and lvalue
1427 subs. But those techniques all involve an increase in the lexical
1430 * When the LHS is all lexical vars (but not necessarily my declarations),
1431 it is possible for the same lexicals to appear directly on the RHS, and
1432 without an increased ref count, since the stack isn't refcounted.
1433 This case can be detected at compile time by scanning for common lex
1434 vars with PL_generation.
1436 * lvalue subs defeat common var detection, but they do at least
1437 return vars with a temporary ref count increment. Also, you can't
1438 tell at compile time whether a sub call is lvalue.
1443 A: There are a few circumstances where there definitely can't be any
1446 LHS empty: () = (...);
1447 RHS empty: (....) = ();
1448 RHS contains only constants or other 'can't possibly be shared'
1449 elements (e.g. ops that return PADTMPs): (...) = (1,2, length)
1450 i.e. they only contain ops not marked as dangerous, whose children
1451 are also not dangerous;
1453 LHS contains a single scalar element: e.g. ($x) = (....); because
1454 after $x has been modified, it won't be used again on the RHS;
1455 RHS contains a single element with no aggregate on LHS: e.g.
1456 ($a,$b,$c) = ($x); again, once $a has been modified, its value
1457 won't be used again.
1459 B: If LHS are all 'my' lexical var declarations (or safe ops, which
1462 my ($a, $b, @c) = ...;
1464 Due to closure and goto tricks, these vars may already have content.
1465 For the same reason, an element on the RHS may be a lexical or package
1466 alias of one of the vars on the left, or share common elements, for
1469 my ($x,$y) = f(); # $x and $y on both sides
1470 sub f : lvalue { ($x,$y) = (1,2); $y, $x }
1475 my @a = @$ra; # elements of @a on both sides
1476 sub f { @a = 1..4; \@a }
1479 First, just consider scalar vars on LHS:
1481 RHS is safe only if (A), or in addition,
1482 * contains only lexical *scalar* vars, where neither side's
1483 lexicals have been flagged as aliases
1485 If RHS is not safe, then it's always legal to check LHS vars for
1486 RC==1, since the only RHS aliases will always be associated
1489 Note that in particular, RHS is not safe if:
1491 * it contains package scalar vars; e.g.:
1494 my ($x, $y) = (2, $x_alias);
1495 sub f { $x = 1; *x_alias = \$x; }
1497 * It contains other general elements, such as flattened or
1498 * spliced or single array or hash elements, e.g.
1501 my ($x,$y) = @a; # or $a[0] or @a{@b} etc
1505 use feature 'refaliasing';
1506 \($a[0], $a[1]) = \($y,$x);
1509 It doesn't matter if the array/hash is lexical or package.
1511 * it contains a function call that happens to be an lvalue
1512 sub which returns one or more of the above, e.g.
1523 (so a sub call on the RHS should be treated the same
1524 as having a package var on the RHS).
1526 * any other "dangerous" thing, such an op or built-in that
1527 returns one of the above, e.g. pp_preinc
1530 If RHS is not safe, what we can do however is at compile time flag
1531 that the LHS are all my declarations, and at run time check whether
1532 all the LHS have RC == 1, and if so skip the full scan.
1534 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...;
1536 Here the issue is whether there can be elements of @a on the RHS
1537 which will get prematurely freed when @a is cleared prior to
1538 assignment. This is only a problem if the aliasing mechanism
1539 is one which doesn't increase the refcount - only if RC == 1
1540 will the RHS element be prematurely freed.
1542 Because the array/hash is being INTROed, it or its elements
1543 can't directly appear on the RHS:
1545 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE
1547 but can indirectly, e.g.:
1551 sub f { @a = 1..3; \@a }
1553 So if the RHS isn't safe as defined by (A), we must always
1554 mortalise and bump the ref count of any remaining RHS elements
1555 when assigning to a non-empty LHS aggregate.
1557 Lexical scalars on the RHS aren't safe if they've been involved in
1560 use feature 'refaliasing';
1564 my @a = ($lex,3); # equivalent to ($a[0],3)
1571 Similarly with lexical arrays and hashes on the RHS:
1585 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g.
1586 my $a; ($a, my $b) = (....);
1588 The difference between (B) and (C) is that it is now physically
1589 possible for the LHS vars to appear on the RHS too, where they
1590 are not reference counted; but in this case, the compile-time
1591 PL_generation sweep will detect such common vars.
1593 So the rules for (C) differ from (B) in that if common vars are
1594 detected, the runtime "test RC==1" optimisation can no longer be used,
1595 and a full mark and sweep is required
1597 D: As (C), but in addition the LHS may contain package vars.
1599 Since package vars can be aliased without a corresponding refcount
1600 increase, all bets are off. It's only safe if (A). E.g.
1602 my ($x, $y) = (1,2);
1605 ($x_alias, $y) = (3, $x); # whoops
1608 Ditto for LHS aggregate package vars.
1610 E: Any other dangerous ops on LHS, e.g.
1611 (f(), $a[0], @$r) = (...);
1613 this is similar to (E) in that all bets are off. In addition, it's
1614 impossible to determine at compile time whether the LHS
1615 contains a scalar or an aggregate, e.g.
1617 sub f : lvalue { @a }
1620 * ---------------------------------------------------------
1623 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates
1624 * that at least one of the things flagged was seen.
1628 AAS_MY_SCALAR = 0x001, /* my $scalar */
1629 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */
1630 AAS_LEX_SCALAR = 0x004, /* $lexical */
1631 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */
1632 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */
1633 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */
1634 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */
1635 AAS_DANGEROUS = 0x080, /* an op (other than the above)
1636 that's flagged OA_DANGEROUS */
1637 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's
1638 not in any of the categories above */
1639 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */
1642 /* helper function for S_aassign_scan().
1643 * check a PAD-related op for commonality and/or set its generation number.
1644 * Returns a boolean indicating whether its shared */
1647 S_aassign_padcheck(pTHX_ OP* o, bool rhs)
1649 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX)
1650 /* lexical used in aliasing */
1654 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation);
1656 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation);
1662 Helper function for OPpASSIGN_COMMON* detection in rpeep().
1663 It scans the left or right hand subtree of the aassign op, and returns a
1664 set of flags indicating what sorts of things it found there.
1665 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we
1666 set PL_generation on lexical vars; if the latter, we see if
1667 PL_generation matches.
1668 'scalars_p' is a pointer to a counter of the number of scalar SVs seen.
1669 This fn will increment it by the number seen. It's not intended to
1670 be an accurate count (especially as many ops can push a variable
1671 number of SVs onto the stack); rather it's used as to test whether there
1672 can be at most 1 SV pushed; so it's only meanings are "0, 1, many".
1676 S_aassign_scan(pTHX_ OP* o, bool rhs, int *scalars_p)
1679 OP *effective_top_op = o;
1683 bool top = o == effective_top_op;
1685 OP* next_kid = NULL;
1687 /* first, look for a solitary @_ on the RHS */
1690 && (o->op_flags & OPf_KIDS)
1691 && OP_TYPE_IS_OR_WAS(o, OP_LIST)
1693 OP *kid = cUNOPo->op_first;
1694 if ( ( kid->op_type == OP_PUSHMARK
1695 || kid->op_type == OP_PADRANGE) /* ex-pushmark */
1696 && ((kid = OpSIBLING(kid)))
1697 && !OpHAS_SIBLING(kid)
1698 && kid->op_type == OP_RV2AV
1699 && !(kid->op_flags & OPf_REF)
1700 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
1701 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST)
1702 && ((kid = cUNOPx(kid)->op_first))
1703 && kid->op_type == OP_GV
1704 && cGVOPx_gv(kid) == PL_defgv
1709 switch (o->op_type) {
1712 all_flags |= AAS_PKG_SCALAR;
1718 /* if !top, could be e.g. @a[0,1] */
1719 all_flags |= (top && (o->op_flags & OPf_REF))
1720 ? ((o->op_private & OPpLVAL_INTRO)
1721 ? AAS_MY_AGG : AAS_LEX_AGG)
1727 int comm = S_aassign_padcheck(aTHX_ o, rhs)
1728 ? AAS_LEX_SCALAR_COMM : 0;
1730 all_flags |= (o->op_private & OPpLVAL_INTRO)
1731 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm);
1739 if (cUNOPx(o)->op_first->op_type != OP_GV)
1740 all_flags |= AAS_DANGEROUS; /* @{expr}, %{expr} */
1742 /* if !top, could be e.g. @a[0,1] */
1743 else if (top && (o->op_flags & OPf_REF))
1744 all_flags |= AAS_PKG_AGG;
1746 all_flags |= AAS_DANGEROUS;
1751 if (cUNOPx(o)->op_first->op_type != OP_GV) {
1753 all_flags |= AAS_DANGEROUS; /* ${expr} */
1756 all_flags |= AAS_PKG_SCALAR; /* $pkg */
1760 if (o->op_private & OPpSPLIT_ASSIGN) {
1761 /* the assign in @a = split() has been optimised away
1762 * and the @a attached directly to the split op
1763 * Treat the array as appearing on the RHS, i.e.
1764 * ... = (@a = split)
1769 if (o->op_flags & OPf_STACKED) {
1770 /* @{expr} = split() - the array expression is tacked
1771 * on as an extra child to split - process kid */
1772 next_kid = cLISTOPo->op_last;
1776 /* ... else array is directly attached to split op */
1778 all_flags |= (PL_op->op_private & OPpSPLIT_LEX)
1779 ? ((o->op_private & OPpLVAL_INTRO)
1780 ? AAS_MY_AGG : AAS_LEX_AGG)
1785 /* other args of split can't be returned */
1786 all_flags |= AAS_SAFE_SCALAR;
1790 /* undef on LHS following a var is significant, e.g.
1792 * @a = (($x, undef) = (2 => $x));
1793 * # @a shoul be (2,1) not (2,2)
1795 * undef on RHS counts as a scalar:
1796 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe
1798 if ((!rhs && *scalars_p) || rhs)
1800 flags = AAS_SAFE_SCALAR;
1805 /* these are all no-ops; they don't push a potentially common SV
1806 * onto the stack, so they are neither AAS_DANGEROUS nor
1807 * AAS_SAFE_SCALAR */
1810 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */
1815 /* these do nothing, but may have children */
1819 if (PL_opargs[o->op_type] & OA_DANGEROUS) {
1821 flags = AAS_DANGEROUS;
1825 if ( (PL_opargs[o->op_type] & OA_TARGLEX)
1826 && (o->op_private & OPpTARGET_MY))
1829 all_flags |= S_aassign_padcheck(aTHX_ o, rhs)
1830 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR;
1834 /* if its an unrecognised, non-dangerous op, assume that it
1835 * is the cause of at least one safe scalar */
1837 flags = AAS_SAFE_SCALAR;
1843 /* by default, process all kids next
1844 * XXX this assumes that all other ops are "transparent" - i.e. that
1845 * they can return some of their children. While this true for e.g.
1846 * sort and grep, it's not true for e.g. map. We really need a
1847 * 'transparent' flag added to regen/opcodes
1849 if (o->op_flags & OPf_KIDS) {
1850 next_kid = cUNOPo->op_first;
1851 /* these ops do nothing but may have children; but their
1852 * children should also be treated as top-level */
1853 if ( o == effective_top_op
1854 && (o->op_type == OP_NULL || o->op_type == OP_LIST)
1856 effective_top_op = next_kid;
1860 /* If next_kid is set, someone in the code above wanted us to process
1861 * that kid and all its remaining siblings. Otherwise, work our way
1862 * back up the tree */
1866 return all_flags; /* at top; no parents/siblings to try */
1867 if (OpHAS_SIBLING(o)) {
1868 next_kid = o->op_sibparent;
1869 if (o == effective_top_op)
1870 effective_top_op = next_kid;
1872 else if (o == effective_top_op)
1873 effective_top_op = o->op_sibparent;
1874 o = o->op_sibparent; /* try parent's next sibling */
1880 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start'
1881 * that potentially represent a series of one or more aggregate derefs
1882 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert
1883 * the whole chain to a single OP_MULTIDEREF op (maybe with a few
1884 * additional ops left in too).
1886 * The caller will have already verified that the first few ops in the
1887 * chain following 'start' indicate a multideref candidate, and will have
1888 * set 'orig_o' to the point further on in the chain where the first index
1889 * expression (if any) begins. 'orig_action' specifies what type of
1890 * beginning has already been determined by the ops between start..orig_o
1891 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc).
1893 * 'hints' contains any hints flags that need adding (currently just
1894 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller.
1898 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints)
1901 UNOP_AUX_item *arg_buf = NULL;
1902 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */
1903 int index_skip = -1; /* don't output index arg on this action */
1905 /* similar to regex compiling, do two passes; the first pass
1906 * determines whether the op chain is convertible and calculates the
1907 * buffer size; the second pass populates the buffer and makes any
1908 * changes necessary to ops (such as moving consts to the pad on
1911 * NB: for things like Coverity, note that both passes take the same
1912 * path through the logic tree (except for 'if (pass)' bits), since
1913 * both passes are following the same op_next chain; and in
1914 * particular, if it would return early on the second pass, it would
1915 * already have returned early on the first pass.
1917 for (pass = 0; pass < 2; pass++) {
1919 UV action = orig_action;
1920 OP *first_elem_op = NULL; /* first seen aelem/helem */
1921 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */
1922 int action_count = 0; /* number of actions seen so far */
1923 int action_ix = 0; /* action_count % (actions per IV) */
1924 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */
1925 bool is_last = FALSE; /* no more derefs to follow */
1926 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */
1927 UV action_word = 0; /* all actions so far */
1929 UNOP_AUX_item *action_ptr = arg_buf;
1931 argi++; /* reserve slot for first action word */
1934 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1935 case MDEREF_HV_gvhv_helem:
1936 next_is_hash = TRUE;
1938 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1939 case MDEREF_AV_gvav_aelem:
1942 arg_buf[argi].pad_offset = cPADOPx(start)->op_padix;
1943 /* stop it being swiped when nulled */
1944 cPADOPx(start)->op_padix = 0;
1946 arg_buf[argi].sv = cSVOPx(start)->op_sv;
1947 cSVOPx(start)->op_sv = NULL;
1953 case MDEREF_HV_padhv_helem:
1954 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1955 next_is_hash = TRUE;
1957 case MDEREF_AV_padav_aelem:
1958 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1960 arg_buf[argi].pad_offset = start->op_targ;
1961 /* we skip setting op_targ = 0 for now, since the intact
1962 * OP_PADXV is needed by check_hash_fields_and_hekify */
1963 reset_start_targ = TRUE;
1968 case MDEREF_HV_pop_rv2hv_helem:
1969 next_is_hash = TRUE;
1971 case MDEREF_AV_pop_rv2av_aelem:
1975 NOT_REACHED; /* NOTREACHED */
1980 /* look for another (rv2av/hv; get index;
1981 * aelem/helem/exists/delele) sequence */
1986 UV index_type = MDEREF_INDEX_none;
1989 /* if this is not the first lookup, consume the rv2av/hv */
1991 /* for N levels of aggregate lookup, we normally expect
1992 * that the first N-1 [ah]elem ops will be flagged as
1993 * /DEREF (so they autovivifiy if necessary), and the last
1994 * lookup op not to be.
1995 * For other things (like @{$h{k1}{k2}}) extra scope or
1996 * leave ops can appear, so abandon the effort in that
1998 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2001 /* rv2av or rv2hv sKR/1 */
2003 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2004 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2005 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
2008 /* at this point, we wouldn't expect any of these
2009 * possible private flags:
2010 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO
2011 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only)
2013 ASSUME(!(o->op_private &
2014 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING)));
2016 hints = (o->op_private & OPpHINT_STRICT_REFS);
2018 /* make sure the type of the previous /DEREF matches the
2019 * type of the next lookup */
2020 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV));
2023 action = next_is_hash
2024 ? MDEREF_HV_vivify_rv2hv_helem
2025 : MDEREF_AV_vivify_rv2av_aelem;
2029 /* if this is the second pass, and we're at the depth where
2030 * previously we encountered a non-simple index expression,
2031 * stop processing the index at this point */
2032 if (action_count != index_skip) {
2034 /* look for one or more simple ops that return an array
2035 * index or hash key */
2037 switch (o->op_type) {
2039 /* it may be a lexical var index */
2040 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS
2041 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2042 ASSUME(!(o->op_private &
2043 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
2045 if ( OP_GIMME(o,0) == G_SCALAR
2046 && !(o->op_flags & (OPf_REF|OPf_MOD))
2047 && o->op_private == 0)
2050 arg_buf[argi].pad_offset = o->op_targ;
2052 index_type = MDEREF_INDEX_padsv;
2059 /* it's a constant hash index */
2060 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK)))
2061 /* "use constant foo => FOO; $h{+foo}" for
2062 * some weird FOO, can leave you with constants
2063 * that aren't simple strings. It's not worth
2064 * the extra hassle for those edge cases */
2069 OP * helem_op = o->op_next;
2071 ASSUME( helem_op->op_type == OP_HELEM
2072 || helem_op->op_type == OP_NULL
2074 if (helem_op->op_type == OP_HELEM) {
2075 rop = cUNOPx(cBINOPx(helem_op)->op_first);
2076 if ( helem_op->op_private & OPpLVAL_INTRO
2077 || rop->op_type != OP_RV2HV
2081 /* on first pass just check; on second pass
2083 check_hash_fields_and_hekify(rop, cSVOPo, pass);
2088 /* Relocate sv to the pad for thread safety */
2089 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2090 arg_buf[argi].pad_offset = o->op_targ;
2093 arg_buf[argi].sv = cSVOPx_sv(o);
2098 /* it's a constant array index */
2100 SV *ix_sv = cSVOPo->op_sv;
2105 if ( action_count == 0
2108 && ( action == MDEREF_AV_padav_aelem
2109 || action == MDEREF_AV_gvav_aelem)
2111 maybe_aelemfast = TRUE;
2114 arg_buf[argi].iv = iv;
2115 SvREFCNT_dec_NN(cSVOPo->op_sv);
2119 /* we've taken ownership of the SV */
2120 cSVOPo->op_sv = NULL;
2122 index_type = MDEREF_INDEX_const;
2127 /* it may be a package var index */
2129 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS|OPf_SPECIAL)));
2130 ASSUME(!(o->op_private & ~(OPpEARLY_CV)));
2131 if ( (o->op_flags & ~(OPf_PARENS|OPf_SPECIAL)) != OPf_WANT_SCALAR
2132 || o->op_private != 0
2137 if (kid->op_type != OP_RV2SV)
2140 ASSUME(!(kid->op_flags &
2141 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF
2142 |OPf_SPECIAL|OPf_PARENS)));
2143 ASSUME(!(kid->op_private &
2145 |OPpHINT_STRICT_REFS|OPpOUR_INTRO
2146 |OPpDEREF|OPpLVAL_INTRO)));
2147 if( (kid->op_flags &~ OPf_PARENS)
2148 != (OPf_WANT_SCALAR|OPf_KIDS)
2149 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS))
2155 arg_buf[argi].pad_offset = cPADOPx(o)->op_padix;
2156 /* stop it being swiped when nulled */
2157 cPADOPx(o)->op_padix = 0;
2159 arg_buf[argi].sv = cSVOPx(o)->op_sv;
2160 cSVOPo->op_sv = NULL;
2164 index_type = MDEREF_INDEX_gvsv;
2169 } /* action_count != index_skip */
2171 action |= index_type;
2174 /* at this point we have either:
2175 * * detected what looks like a simple index expression,
2176 * and expect the next op to be an [ah]elem, or
2177 * an nulled [ah]elem followed by a delete or exists;
2178 * * found a more complex expression, so something other
2179 * than the above follows.
2182 /* possibly an optimised away [ah]elem (where op_next is
2183 * exists or delete) */
2184 if (o->op_type == OP_NULL)
2187 /* at this point we're looking for an OP_AELEM, OP_HELEM,
2188 * OP_EXISTS or OP_DELETE */
2190 /* if a custom array/hash access checker is in scope,
2191 * abandon optimisation attempt */
2192 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
2193 && PL_check[o->op_type] != Perl_ck_null)
2195 /* similarly for customised exists and delete */
2196 if ( (o->op_type == OP_EXISTS)
2197 && PL_check[o->op_type] != Perl_ck_exists)
2199 if ( (o->op_type == OP_DELETE)
2200 && PL_check[o->op_type] != Perl_ck_delete)
2203 if ( o->op_type != OP_AELEM
2205 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB))
2207 maybe_aelemfast = FALSE;
2209 /* look for aelem/helem/exists/delete. If it's not the last elem
2210 * lookup, it *must* have OPpDEREF_AV/HV, but not many other
2211 * flags; if it's the last, then it mustn't have
2212 * OPpDEREF_AV/HV, but may have lots of other flags, like
2216 if ( index_type == MDEREF_INDEX_none
2217 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM
2218 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE)
2222 /* we have aelem/helem/exists/delete with valid simple index */
2224 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
2225 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV
2226 || (o->op_private & OPpDEREF) == OPpDEREF_HV);
2228 /* This doesn't make much sense but is legal:
2229 * @{ local $x[0][0] } = 1
2230 * Since scope exit will undo the autovivification,
2231 * don't bother in the first place. The OP_LEAVE
2232 * assertion is in case there are other cases of both
2233 * OPpLVAL_INTRO and OPpDEREF which don't include a scope
2234 * exit that would undo the local - in which case this
2235 * block of code would need rethinking.
2237 if (is_deref && (o->op_private & OPpLVAL_INTRO)) {
2240 while (n && ( n->op_type == OP_NULL
2241 || n->op_type == OP_LIST
2242 || n->op_type == OP_SCALAR))
2244 assert(n && n->op_type == OP_LEAVE);
2246 o->op_private &= ~OPpDEREF;
2251 ASSUME(!(o->op_flags &
2252 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS)));
2253 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF)));
2255 ok = (o->op_flags &~ OPf_PARENS)
2256 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD)
2257 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK));
2259 else if (o->op_type == OP_EXISTS) {
2260 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2261 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2262 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB)));
2263 ok = !(o->op_private & ~OPpARG1_MASK);
2265 else if (o->op_type == OP_DELETE) {
2266 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2267 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
2268 ASSUME(!(o->op_private &
2269 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO)));
2270 /* don't handle slices or 'local delete'; the latter
2271 * is fairly rare, and has a complex runtime */
2272 ok = !(o->op_private & ~OPpARG1_MASK);
2273 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM))
2274 /* skip handling run-tome error */
2275 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL));
2278 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM);
2279 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD
2280 |OPf_PARENS|OPf_REF|OPf_SPECIAL)));
2281 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB
2282 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO)));
2283 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV;
2292 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV);
2297 action |= MDEREF_FLAG_last;
2301 /* at this point we have something that started
2302 * promisingly enough (with rv2av or whatever), but failed
2303 * to find a simple index followed by an
2304 * aelem/helem/exists/delete. If this is the first action,
2305 * give up; but if we've already seen at least one
2306 * aelem/helem, then keep them and add a new action with
2307 * MDEREF_INDEX_none, which causes it to do the vivify
2308 * from the end of the previous lookup, and do the deref,
2309 * but stop at that point. So $a[0][expr] will do one
2310 * av_fetch, vivify and deref, then continue executing at
2315 index_skip = action_count;
2316 action |= MDEREF_FLAG_last;
2317 if (index_type != MDEREF_INDEX_none)
2321 action_word |= (action << (action_ix * MDEREF_SHIFT));
2324 /* if there's no space for the next action, reserve a new slot
2325 * for it *before* we start adding args for that action */
2326 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) {
2328 action_ptr->uv = action_word;
2329 action_ptr = arg_buf + argi;
2335 } /* while !is_last */
2340 /* slot reserved for next action word not now needed */
2343 action_ptr->uv = action_word;
2349 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf);
2350 if (index_skip == -1) {
2351 mderef->op_flags = o->op_flags
2352 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0));
2353 if (o->op_type == OP_EXISTS)
2354 mderef->op_private = OPpMULTIDEREF_EXISTS;
2355 else if (o->op_type == OP_DELETE)
2356 mderef->op_private = OPpMULTIDEREF_DELETE;
2358 mderef->op_private = o->op_private
2359 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO);
2361 /* accumulate strictness from every level (although I don't think
2362 * they can actually vary) */
2363 mderef->op_private |= hints;
2365 /* integrate the new multideref op into the optree and the
2368 * In general an op like aelem or helem has two child
2369 * sub-trees: the aggregate expression (a_expr) and the
2370 * index expression (i_expr):
2376 * The a_expr returns an AV or HV, while the i-expr returns an
2377 * index. In general a multideref replaces most or all of a
2378 * multi-level tree, e.g.
2394 * With multideref, all the i_exprs will be simple vars or
2395 * constants, except that i_expr1 may be arbitrary in the case
2396 * of MDEREF_INDEX_none.
2398 * The bottom-most a_expr will be either:
2399 * 1) a simple var (so padXv or gv+rv2Xv);
2400 * 2) a simple scalar var dereferenced (e.g. $r->[0]):
2401 * so a simple var with an extra rv2Xv;
2402 * 3) or an arbitrary expression.
2404 * 'start', the first op in the execution chain, will point to
2405 * 1),2): the padXv or gv op;
2406 * 3): the rv2Xv which forms the last op in the a_expr
2407 * execution chain, and the top-most op in the a_expr
2410 * For all cases, the 'start' node is no longer required,
2411 * but we can't free it since one or more external nodes
2412 * may point to it. E.g. consider
2413 * $h{foo} = $a ? $b : $c
2414 * Here, both the op_next and op_other branches of the
2415 * cond_expr point to the gv[*h] of the hash expression, so
2416 * we can't free the 'start' op.
2418 * For expr->[...], we need to save the subtree containing the
2419 * expression; for the other cases, we just need to save the
2421 * So in all cases, we null the start op and keep it around by
2422 * making it the child of the multideref op; for the expr->
2423 * case, the expr will be a subtree of the start node.
2425 * So in the simple 1,2 case the optree above changes to
2431 * ex-gv (or ex-padxv)
2433 * with the op_next chain being
2435 * -> ex-gv -> multideref -> op-following-ex-exists ->
2437 * In the 3 case, we have
2450 * -> rest-of-a_expr subtree ->
2451 * ex-rv2xv -> multideref -> op-following-ex-exists ->
2454 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none,
2455 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the
2456 * multideref attached as the child, e.g.
2462 * ex-rv2av - i_expr1
2470 /* if we free this op, don't free the pad entry */
2471 if (reset_start_targ)
2475 /* Cut the bit we need to save out of the tree and attach to
2476 * the multideref op, then free the rest of the tree */
2478 /* find parent of node to be detached (for use by splice) */
2480 if ( orig_action == MDEREF_AV_pop_rv2av_aelem
2481 || orig_action == MDEREF_HV_pop_rv2hv_helem)
2483 /* there is an arbitrary expression preceding us, e.g.
2484 * expr->[..]? so we need to save the 'expr' subtree */
2485 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE)
2486 p = cUNOPx(p)->op_first;
2487 ASSUME( start->op_type == OP_RV2AV
2488 || start->op_type == OP_RV2HV);
2491 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem
2492 * above for exists/delete. */
2493 while ( (p->op_flags & OPf_KIDS)
2494 && cUNOPx(p)->op_first != start
2496 p = cUNOPx(p)->op_first;
2498 ASSUME(cUNOPx(p)->op_first == start);
2500 /* detach from main tree, and re-attach under the multideref */
2501 op_sibling_splice(mderef, NULL, 0,
2502 op_sibling_splice(p, NULL, 1, NULL));
2505 start->op_next = mderef;
2507 mderef->op_next = index_skip == -1 ? o->op_next : o;
2509 /* excise and free the original tree, and replace with
2510 * the multideref op */
2511 p = op_sibling_splice(top_op, NULL, -1, mderef);
2522 if (maybe_aelemfast && action_count == 1)
2525 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc(
2526 sizeof(UNOP_AUX_item) * (size + 1));
2527 /* for dumping etc: store the length in a hidden first slot;
2528 * we set the op_aux pointer to the second slot */
2532 } /* for (pass = ...) */
2535 /* See if the ops following o are such that o will always be executed in
2536 * boolean context: that is, the SV which o pushes onto the stack will
2537 * only ever be consumed by later ops via SvTRUE(sv) or similar.
2538 * If so, set a suitable private flag on o. Normally this will be
2539 * bool_flag; but see below why maybe_flag is needed too.
2541 * Typically the two flags you pass will be the generic OPpTRUEBOOL and
2542 * OPpMAYBE_TRUEBOOL, buts it's possible that for some ops those bits may
2543 * already be taken, so you'll have to give that op two different flags.
2545 * More explanation of 'maybe_flag' and 'safe_and' parameters.
2546 * The binary logical ops &&, ||, // (plus 'if' and 'unless' which use
2547 * those underlying ops) short-circuit, which means that rather than
2548 * necessarily returning a truth value, they may return the LH argument,
2549 * which may not be boolean. For example in $x = (keys %h || -1), keys
2550 * should return a key count rather than a boolean, even though its
2551 * sort-of being used in boolean context.
2553 * So we only consider such logical ops to provide boolean context to
2554 * their LH argument if they themselves are in void or boolean context.
2555 * However, sometimes the context isn't known until run-time. In this
2556 * case the op is marked with the maybe_flag flag it.
2558 * Consider the following.
2560 * sub f { ....; if (%h) { .... } }
2562 * This is actually compiled as
2564 * sub f { ....; %h && do { .... } }
2566 * Here we won't know until runtime whether the final statement (and hence
2567 * the &&) is in void context and so is safe to return a boolean value.
2568 * So mark o with maybe_flag rather than the bool_flag.
2569 * Note that there is cost associated with determining context at runtime
2570 * (e.g. a call to block_gimme()), so it may not be worth setting (at
2571 * compile time) and testing (at runtime) maybe_flag if the scalar verses
2572 * boolean costs savings are marginal.
2574 * However, we can do slightly better with && (compared to || and //):
2575 * this op only returns its LH argument when that argument is false. In
2576 * this case, as long as the op promises to return a false value which is
2577 * valid in both boolean and scalar contexts, we can mark an op consumed
2578 * by && with bool_flag rather than maybe_flag.
2579 * For example as long as pp_padhv and pp_rv2hv return &PL_sv_zero rather
2580 * than &PL_sv_no for a false result in boolean context, then it's safe. An
2581 * op which promises to handle this case is indicated by setting safe_and
2586 S_check_for_bool_cxt(OP*o, bool safe_and, U8 bool_flag, U8 maybe_flag)
2591 assert((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR);
2593 /* OPpTARGET_MY and boolean context probably don't mix well.
2594 * If someone finds a valid use case, maybe add an extra flag to this
2595 * function which indicates its safe to do so for this op? */
2596 assert(!( (PL_opargs[o->op_type] & OA_TARGLEX)
2597 && (o->op_private & OPpTARGET_MY)));
2602 switch (lop->op_type) {
2607 /* these two consume the stack argument in the scalar case,
2608 * and treat it as a boolean in the non linenumber case */
2611 if ( ((lop->op_flags & OPf_WANT) == OPf_WANT_LIST)
2612 || (lop->op_private & OPpFLIP_LINENUM))
2618 /* these never leave the original value on the stack */
2627 /* OR DOR and AND evaluate their arg as a boolean, but then may
2628 * leave the original scalar value on the stack when following the
2629 * op_next route. If not in void context, we need to ensure
2630 * that whatever follows consumes the arg only in boolean context
2642 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) {
2646 else if (!(lop->op_flags & OPf_WANT)) {
2647 /* unknown context - decide at runtime */
2662 o->op_private |= flag;
2665 /* mechanism for deferring recursion in rpeep() */
2667 #define MAX_DEFERRED 4
2671 if (defer_ix == (MAX_DEFERRED-1)) { \
2672 OP **defer = defer_queue[defer_base]; \
2673 CALL_RPEEP(*defer); \
2674 op_prune_chain_head(defer); \
2675 defer_base = (defer_base + 1) % MAX_DEFERRED; \
2678 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
2681 #define IS_AND_OP(o) (o->op_type == OP_AND)
2682 #define IS_OR_OP(o) (o->op_type == OP_OR)
2684 /* A peephole optimizer. We visit the ops in the order they're to execute.
2685 * See the comments at the top of this file for more details about when
2686 * peep() is called */
2689 Perl_rpeep(pTHX_ OP *o)
2692 OP* oldoldop = NULL;
2693 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
2697 if (!o || o->op_opt)
2700 assert(o->op_type != OP_FREED);
2704 SAVEVPTR(PL_curcop);
2705 for (;; o = o->op_next) {
2709 while (defer_ix >= 0) {
2711 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
2713 op_prune_chain_head(defer);
2720 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */
2721 assert(!oldoldop || oldoldop->op_next == oldop);
2722 assert(!oldop || oldop->op_next == o);
2724 /* By default, this op has now been optimised. A couple of cases below
2725 clear this again. */
2729 /* look for a series of 1 or more aggregate derefs, e.g.
2730 * $a[1]{foo}[$i]{$k}
2731 * and replace with a single OP_MULTIDEREF op.
2732 * Each index must be either a const, or a simple variable,
2734 * First, look for likely combinations of starting ops,
2735 * corresponding to (global and lexical variants of)
2737 * $r->[...] $r->{...}
2738 * (preceding expression)->[...]
2739 * (preceding expression)->{...}
2740 * and if so, call maybe_multideref() to do a full inspection
2741 * of the op chain and if appropriate, replace with an
2749 switch (o2->op_type) {
2751 /* $pkg[..] : gv[*pkg]
2752 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */
2754 /* Fail if there are new op flag combinations that we're
2755 * not aware of, rather than:
2756 * * silently failing to optimise, or
2757 * * silently optimising the flag away.
2758 * If this ASSUME starts failing, examine what new flag
2759 * has been added to the op, and decide whether the
2760 * optimisation should still occur with that flag, then
2761 * update the code accordingly. This applies to all the
2762 * other ASSUMEs in the block of code too.
2764 ASSUME(!(o2->op_flags &
2765 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL)));
2766 ASSUME(!(o2->op_private & ~OPpEARLY_CV));
2770 if (o2->op_type == OP_RV2AV) {
2771 action = MDEREF_AV_gvav_aelem;
2775 if (o2->op_type == OP_RV2HV) {
2776 action = MDEREF_HV_gvhv_helem;
2780 if (o2->op_type != OP_RV2SV)
2783 /* at this point we've seen gv,rv2sv, so the only valid
2784 * construct left is $pkg->[] or $pkg->{} */
2786 ASSUME(!(o2->op_flags & OPf_STACKED));
2787 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
2788 != (OPf_WANT_SCALAR|OPf_MOD))
2791 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS
2792 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO)));
2793 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO))
2795 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV
2796 && (o2->op_private & OPpDEREF) != OPpDEREF_HV)
2800 if (o2->op_type == OP_RV2AV) {
2801 action = MDEREF_AV_gvsv_vivify_rv2av_aelem;
2804 if (o2->op_type == OP_RV2HV) {
2805 action = MDEREF_HV_gvsv_vivify_rv2hv_helem;
2811 /* $lex->[...]: padsv[$lex] sM/DREFAV */
2813 ASSUME(!(o2->op_flags &
2814 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL)));
2816 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
2817 != (OPf_WANT_SCALAR|OPf_MOD))
2820 ASSUME(!(o2->op_private &
2821 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
2822 /* skip if state or intro, or not a deref */
2823 if ( o2->op_private != OPpDEREF_AV
2824 && o2->op_private != OPpDEREF_HV)
2828 if (o2->op_type == OP_RV2AV) {
2829 action = MDEREF_AV_padsv_vivify_rv2av_aelem;
2832 if (o2->op_type == OP_RV2HV) {
2833 action = MDEREF_HV_padsv_vivify_rv2hv_helem;
2840 /* $lex[..]: padav[@lex:1,2] sR *
2841 * or $lex{..}: padhv[%lex:1,2] sR */
2842 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS|
2843 OPf_REF|OPf_SPECIAL)));
2845 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
2846 != (OPf_WANT_SCALAR|OPf_REF))
2848 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF))
2850 /* OPf_PARENS isn't currently used in this case;
2851 * if that changes, let us know! */
2852 ASSUME(!(o2->op_flags & OPf_PARENS));
2854 /* at this point, we wouldn't expect any of the remaining
2855 * possible private flags:
2856 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL,
2857 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB
2859 * OPpSLICEWARNING shouldn't affect runtime
2861 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING)));
2863 action = o2->op_type == OP_PADAV
2864 ? MDEREF_AV_padav_aelem
2865 : MDEREF_HV_padhv_helem;
2867 S_maybe_multideref(aTHX_ o, o2, action, 0);
2873 action = o2->op_type == OP_RV2AV
2874 ? MDEREF_AV_pop_rv2av_aelem
2875 : MDEREF_HV_pop_rv2hv_helem;
2878 /* (expr)->[...]: rv2av sKR/1;
2879 * (expr)->{...}: rv2hv sKR/1; */
2881 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV);
2883 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
2884 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL)));
2885 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
2888 /* at this point, we wouldn't expect any of these
2889 * possible private flags:
2890 * OPpMAYBE_LVSUB, OPpLVAL_INTRO
2891 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only)
2893 ASSUME(!(o2->op_private &
2894 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING
2896 hints |= (o2->op_private & OPpHINT_STRICT_REFS);
2900 S_maybe_multideref(aTHX_ o, o2, action, hints);
2909 switch (o->op_type) {
2911 PL_curcop = ((COP*)o); /* for warnings */
2914 PL_curcop = ((COP*)o); /* for warnings */
2916 /* Optimise a "return ..." at the end of a sub to just be "...".
2917 * This saves 2 ops. Before:
2918 * 1 <;> nextstate(main 1 -e:1) v ->2
2919 * 4 <@> return K ->5
2920 * 2 <0> pushmark s ->3
2921 * - <1> ex-rv2sv sK/1 ->4
2922 * 3 <#> gvsv[*cat] s ->4
2925 * - <@> return K ->-
2926 * - <0> pushmark s ->2
2927 * - <1> ex-rv2sv sK/1 ->-
2928 * 2 <$> gvsv(*cat) s ->3
2931 OP *next = o->op_next;
2932 OP *sibling = OpSIBLING(o);
2933 if ( OP_TYPE_IS(next, OP_PUSHMARK)
2934 && OP_TYPE_IS(sibling, OP_RETURN)
2935 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
2936 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
2937 ||OP_TYPE_IS(sibling->op_next->op_next,
2939 && cUNOPx(sibling)->op_first == next
2940 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next
2943 /* Look through the PUSHMARK's siblings for one that
2944 * points to the RETURN */
2945 OP *top = OpSIBLING(next);
2946 while (top && top->op_next) {
2947 if (top->op_next == sibling) {
2948 top->op_next = sibling->op_next;
2949 o->op_next = next->op_next;
2952 top = OpSIBLING(top);
2957 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
2959 * This latter form is then suitable for conversion into padrange
2960 * later on. Convert:
2962 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
2966 * nextstate1 -> listop -> nextstate3
2968 * pushmark -> padop1 -> padop2
2971 o->op_next->op_type == OP_PADSV
2972 || o->op_next->op_type == OP_PADAV
2973 || o->op_next->op_type == OP_PADHV
2975 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
2976 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
2977 && o->op_next->op_next->op_next && (
2978 o->op_next->op_next->op_next->op_type == OP_PADSV
2979 || o->op_next->op_next->op_next->op_type == OP_PADAV
2980 || o->op_next->op_next->op_next->op_type == OP_PADHV
2982 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
2983 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
2984 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
2985 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
2987 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm;
2990 ns2 = pad1->op_next;
2991 pad2 = ns2->op_next;
2992 ns3 = pad2->op_next;
2994 /* we assume here that the op_next chain is the same as
2995 * the op_sibling chain */
2996 assert(OpSIBLING(o) == pad1);
2997 assert(OpSIBLING(pad1) == ns2);
2998 assert(OpSIBLING(ns2) == pad2);
2999 assert(OpSIBLING(pad2) == ns3);
3001 /* excise and delete ns2 */
3002 op_sibling_splice(NULL, pad1, 1, NULL);
3005 /* excise pad1 and pad2 */
3006 op_sibling_splice(NULL, o, 2, NULL);
3008 /* create new listop, with children consisting of:
3009 * a new pushmark, pad1, pad2. */
3010 newop = newLISTOP(OP_LIST, 0, pad1, pad2);
3011 newop->op_flags |= OPf_PARENS;
3012 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
3014 /* insert newop between o and ns3 */
3015 op_sibling_splice(NULL, o, 0, newop);
3017 /*fixup op_next chain */
3018 newpm = cUNOPx(newop)->op_first; /* pushmark */
3019 o ->op_next = newpm;
3020 newpm->op_next = pad1;
3021 pad1 ->op_next = pad2;
3022 pad2 ->op_next = newop; /* listop */
3023 newop->op_next = ns3;
3025 /* Ensure pushmark has this flag if padops do */
3026 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) {
3027 newpm->op_flags |= OPf_MOD;
3033 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
3034 to carry two labels. For now, take the easier option, and skip
3035 this optimisation if the first NEXTSTATE has a label. */
3036 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
3037 OP *nextop = o->op_next;
3039 switch (nextop->op_type) {
3044 nextop = nextop->op_next;
3050 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
3053 oldop->op_next = nextop;
3055 /* Skip (old)oldop assignment since the current oldop's
3056 op_next already points to the next op. */
3063 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
3064 if (o->op_next->op_private & OPpTARGET_MY) {
3065 if (o->op_flags & OPf_STACKED) /* chained concats */
3066 break; /* ignore_optimization */
3068 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
3069 o->op_targ = o->op_next->op_targ;
3070 o->op_next->op_targ = 0;
3071 o->op_private |= OPpTARGET_MY;
3074 op_null(o->op_next);
3078 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
3079 break; /* Scalar stub must produce undef. List stub is noop */
3083 if (o->op_targ == OP_NEXTSTATE
3084 || o->op_targ == OP_DBSTATE)
3086 PL_curcop = ((COP*)o);
3088 /* XXX: We avoid setting op_seq here to prevent later calls
3089 to rpeep() from mistakenly concluding that optimisation
3090 has already occurred. This doesn't fix the real problem,
3091 though (See 20010220.007 (#5874)). AMS 20010719 */
3092 /* op_seq functionality is now replaced by op_opt */
3100 oldop->op_next = o->op_next;
3114 convert repeat into a stub with no kids.
3116 if (o->op_next->op_type == OP_CONST
3117 || ( o->op_next->op_type == OP_PADSV
3118 && !(o->op_next->op_private & OPpLVAL_INTRO))
3119 || ( o->op_next->op_type == OP_GV
3120 && o->op_next->op_next->op_type == OP_RV2SV
3121 && !(o->op_next->op_next->op_private
3122 & (OPpLVAL_INTRO|OPpOUR_INTRO))))
3124 const OP *kid = o->op_next->op_next;
3125 if (o->op_next->op_type == OP_GV)
3127 /* kid is now the ex-list. */
3128 if (kid->op_type == OP_NULL
3129 && (kid = kid->op_next)->op_type == OP_CONST
3130 /* kid is now the repeat count. */
3131 && kid->op_next->op_type == OP_REPEAT
3132 && kid->op_next->op_private & OPpREPEAT_DOLIST
3133 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST
3134 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0
3137 o = kid->op_next; /* repeat */
3139 op_free(cBINOPo->op_first);
3140 op_free(cBINOPo->op_last );
3141 o->op_flags &=~ OPf_KIDS;
3142 /* stub is a baseop; repeat is a binop */
3143 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP));
3144 OpTYPE_set(o, OP_STUB);
3150 /* Convert a series of PAD ops for my vars plus support into a
3151 * single padrange op. Basically
3153 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
3155 * becomes, depending on circumstances, one of
3157 * padrange ----------------------------------> (list) -> rest
3158 * padrange --------------------------------------------> rest
3160 * where all the pad indexes are sequential and of the same type
3162 * We convert the pushmark into a padrange op, then skip
3163 * any other pad ops, and possibly some trailing ops.
3164 * Note that we don't null() the skipped ops, to make it
3165 * easier for Deparse to undo this optimisation (and none of
3166 * the skipped ops are holding any resourses). It also makes
3167 * it easier for find_uninit_var(), as it can just ignore
3168 * padrange, and examine the original pad ops.
3172 OP *followop = NULL; /* the op that will follow the padrange op */
3175 PADOFFSET base = 0; /* init only to stop compiler whining */
3176 bool gvoid = 0; /* init only to stop compiler whining */
3177 bool defav = 0; /* seen (...) = @_ */
3178 bool reuse = 0; /* reuse an existing padrange op */
3180 /* look for a pushmark -> gv[_] -> rv2av */
3185 if ( p->op_type == OP_GV
3186 && cGVOPx_gv(p) == PL_defgv
3187 && (rv2av = p->op_next)
3188 && rv2av->op_type == OP_RV2AV
3189 && !(rv2av->op_flags & OPf_REF)
3190 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
3191 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
3194 if (q->op_type == OP_NULL)
3196 if (q->op_type == OP_PUSHMARK) {
3206 /* scan for PAD ops */
3208 for (p = p->op_next; p; p = p->op_next) {
3209 if (p->op_type == OP_NULL)
3212 if (( p->op_type != OP_PADSV
3213 && p->op_type != OP_PADAV
3214 && p->op_type != OP_PADHV
3216 /* any private flag other than INTRO? e.g. STATE */
3217 || (p->op_private & ~OPpLVAL_INTRO)
3221 /* let $a[N] potentially be optimised into AELEMFAST_LEX
3223 if ( p->op_type == OP_PADAV
3225 && p->op_next->op_type == OP_CONST
3226 && p->op_next->op_next
3227 && p->op_next->op_next->op_type == OP_AELEM
3231 /* for 1st padop, note what type it is and the range
3232 * start; for the others, check that it's the same type
3233 * and that the targs are contiguous */
3235 intro = (p->op_private & OPpLVAL_INTRO);
3237 gvoid = OP_GIMME(p,0) == G_VOID;
3240 if ((p->op_private & OPpLVAL_INTRO) != intro)
3242 /* Note that you'd normally expect targs to be
3243 * contiguous in my($a,$b,$c), but that's not the case
3244 * when external modules start doing things, e.g.
3245 * Function::Parameters */
3246 if (p->op_targ != base + count)
3248 assert(p->op_targ == base + count);
3249 /* Either all the padops or none of the padops should
3250 be in void context. Since we only do the optimisa-
3251 tion for av/hv when the aggregate itself is pushed
3252 on to the stack (one item), there is no need to dis-
3253 tinguish list from scalar context. */
3254 if (gvoid != (OP_GIMME(p,0) == G_VOID))
3258 /* for AV, HV, only when we're not flattening */
3259 if ( p->op_type != OP_PADSV
3261 && !(p->op_flags & OPf_REF)
3265 if (count >= OPpPADRANGE_COUNTMASK)
3268 /* there's a biggest base we can fit into a
3269 * SAVEt_CLEARPADRANGE in pp_padrange.
3270 * (The sizeof() stuff will be constant-folded, and is
3271 * intended to avoid getting "comparison is always false"
3272 * compiler warnings. See the comments above
3273 * MEM_WRAP_CHECK for more explanation on why we do this
3274 * in a weird way to avoid compiler warnings.)
3277 && (8*sizeof(base) >
3278 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT
3280 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
3282 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
3286 /* Success! We've got another valid pad op to optimise away */
3288 followop = p->op_next;
3291 if (count < 1 || (count == 1 && !defav))
3294 /* pp_padrange in specifically compile-time void context
3295 * skips pushing a mark and lexicals; in all other contexts
3296 * (including unknown till runtime) it pushes a mark and the
3297 * lexicals. We must be very careful then, that the ops we
3298 * optimise away would have exactly the same effect as the
3300 * In particular in void context, we can only optimise to
3301 * a padrange if we see the complete sequence
3302 * pushmark, pad*v, ...., list
3303 * which has the net effect of leaving the markstack as it
3304 * was. Not pushing onto the stack (whereas padsv does touch
3305 * the stack) makes no difference in void context.
3309 if (followop->op_type == OP_LIST
3310 && OP_GIMME(followop,0) == G_VOID
3313 followop = followop->op_next; /* skip OP_LIST */
3315 /* consolidate two successive my(...);'s */
3318 && oldoldop->op_type == OP_PADRANGE
3319 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
3320 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
3321 && !(oldoldop->op_flags & OPf_SPECIAL)
3324 assert(oldoldop->op_next == oldop);
3325 assert( oldop->op_type == OP_NEXTSTATE
3326 || oldop->op_type == OP_DBSTATE);
3327 assert(oldop->op_next == o);
3330 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
3332 /* Do not assume pad offsets for $c and $d are con-
3337 if ( oldoldop->op_targ + old_count == base
3338 && old_count < OPpPADRANGE_COUNTMASK - count) {
3339 base = oldoldop->op_targ;
3345 /* if there's any immediately following singleton
3346 * my var's; then swallow them and the associated
3348 * my ($a,$b); my $c; my $d;
3353 while ( ((p = followop->op_next))
3354 && ( p->op_type == OP_PADSV
3355 || p->op_type == OP_PADAV
3356 || p->op_type == OP_PADHV)
3357 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
3358 && (p->op_private & OPpLVAL_INTRO) == intro
3359 && !(p->op_private & ~OPpLVAL_INTRO)
3361 && ( p->op_next->op_type == OP_NEXTSTATE
3362 || p->op_next->op_type == OP_DBSTATE)
3363 && count < OPpPADRANGE_COUNTMASK
3364 && base + count == p->op_targ
3367 followop = p->op_next;
3375 assert(oldoldop->op_type == OP_PADRANGE);
3376 oldoldop->op_next = followop;
3377 oldoldop->op_private = (intro | count);
3383 /* Convert the pushmark into a padrange.
3384 * To make Deparse easier, we guarantee that a padrange was
3385 * *always* formerly a pushmark */
3386 assert(o->op_type == OP_PUSHMARK);
3387 o->op_next = followop;
3388 OpTYPE_set(o, OP_PADRANGE);
3390 /* bit 7: INTRO; bit 6..0: count */
3391 o->op_private = (intro | count);
3392 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
3393 | gvoid * OPf_WANT_VOID
3394 | (defav ? OPf_SPECIAL : 0));
3400 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3401 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3406 /*'keys %h' in void or scalar context: skip the OP_KEYS
3407 * and perform the functionality directly in the RV2HV/PADHV
3410 if (o->op_flags & OPf_REF) {
3412 U8 want = (k->op_flags & OPf_WANT);
3414 && k->op_type == OP_KEYS
3415 && ( want == OPf_WANT_VOID
3416 || want == OPf_WANT_SCALAR)
3417 && !(k->op_private & OPpMAYBE_LVSUB)
3418 && !(k->op_flags & OPf_MOD)
3420 o->op_next = k->op_next;
3421 o->op_flags &= ~(OPf_REF|OPf_WANT);
3422 o->op_flags |= want;
3423 o->op_private |= (o->op_type == OP_PADHV ?
3424 OPpPADHV_ISKEYS : OPpRV2HV_ISKEYS);
3425 /* for keys(%lex), hold onto the OP_KEYS's targ
3426 * since padhv doesn't have its own targ to return
3428 if (!(o->op_type ==OP_PADHV && want == OPf_WANT_SCALAR))
3433 /* see if %h is used in boolean context */
3434 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3435 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
3438 if (o->op_type != OP_PADHV)
3442 if ( o->op_type == OP_PADAV
3443 && (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR
3445 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3448 /* Skip over state($x) in void context. */
3449 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO)
3450 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID)
3452 oldop->op_next = o->op_next;
3453 goto redo_nextstate;
3455 if (o->op_type != OP_PADAV)
3459 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
3460 OP* const pop = (o->op_type == OP_PADAV) ?
3461 o->op_next : o->op_next->op_next;
3463 if (pop && pop->op_type == OP_CONST &&
3464 ((PL_op = pop->op_next)) &&
3465 pop->op_next->op_type == OP_AELEM &&
3466 !(pop->op_next->op_private &
3467 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
3468 (i = SvIV(cSVOPx(pop)->op_sv)) >= -128 && i <= 127)
3471 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
3472 no_bareword_allowed(pop);
3473 if (o->op_type == OP_GV)
3474 op_null(o->op_next);
3475 op_null(pop->op_next);
3477 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
3478 o->op_next = pop->op_next->op_next;
3479 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
3480 o->op_private = (U8)i;
3481 if (o->op_type == OP_GV) {
3484 o->op_type = OP_AELEMFAST;
3487 o->op_type = OP_AELEMFAST_LEX;
3489 if (o->op_type != OP_GV)
3493 /* Remove $foo from the op_next chain in void context. */
3495 && ( o->op_next->op_type == OP_RV2SV
3496 || o->op_next->op_type == OP_RV2AV
3497 || o->op_next->op_type == OP_RV2HV )
3498 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
3499 && !(o->op_next->op_private & OPpLVAL_INTRO))
3501 oldop->op_next = o->op_next->op_next;
3502 /* Reprocess the previous op if it is a nextstate, to
3503 allow double-nextstate optimisation. */
3505 if (oldop->op_type == OP_NEXTSTATE) {
3515 else if (o->op_next->op_type == OP_RV2SV) {
3516 if (!(o->op_next->op_private & OPpDEREF)) {
3517 op_null(o->op_next);
3518 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
3520 o->op_next = o->op_next->op_next;
3521 OpTYPE_set(o, OP_GVSV);
3524 else if (o->op_next->op_type == OP_READLINE
3525 && o->op_next->op_next->op_type == OP_CONCAT
3526 && (o->op_next->op_next->op_flags & OPf_STACKED))
3528 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
3529 OpTYPE_set(o, OP_RCATLINE);
3530 o->op_flags |= OPf_STACKED;
3531 op_null(o->op_next->op_next);
3532 op_null(o->op_next);
3543 case OP_CMPCHAIN_AND:
3545 while (cLOGOP->op_other->op_type == OP_NULL)
3546 cLOGOP->op_other = cLOGOP->op_other->op_next;
3547 while (o->op_next && ( o->op_type == o->op_next->op_type
3548 || o->op_next->op_type == OP_NULL))
3549 o->op_next = o->op_next->op_next;
3551 /* If we're an OR and our next is an AND in void context, we'll
3552 follow its op_other on short circuit, same for reverse.
3553 We can't do this with OP_DOR since if it's true, its return
3554 value is the underlying value which must be evaluated
3558 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
3559 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
3561 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
3563 o->op_next = cLOGOPx(o->op_next)->op_other;
3565 DEFER(cLOGOP->op_other);
3570 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3571 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3581 while (cLOGOP->op_other->op_type == OP_NULL)
3582 cLOGOP->op_other = cLOGOP->op_other->op_next;
3583 DEFER(cLOGOP->op_other);
3588 while (cLOOP->op_redoop->op_type == OP_NULL)
3589 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
3590 while (cLOOP->op_nextop->op_type == OP_NULL)
3591 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
3592 while (cLOOP->op_lastop->op_type == OP_NULL)
3593 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
3594 /* a while(1) loop doesn't have an op_next that escapes the
3595 * loop, so we have to explicitly follow the op_lastop to
3596 * process the rest of the code */
3597 DEFER(cLOOP->op_lastop);
3601 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
3602 DEFER(cLOGOPo->op_other);
3605 case OP_ENTERTRYCATCH:
3606 assert(cLOGOPo->op_other->op_type == OP_CATCH);
3607 /* catch body is the ->op_other of the OP_CATCH */
3608 DEFER(cLOGOPx(cLOGOPo->op_other)->op_other);
3612 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
3613 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
3614 assert(!(cPMOP->op_pmflags & PMf_ONCE));
3615 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
3616 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
3617 cPMOP->op_pmstashstartu.op_pmreplstart
3618 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
3619 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
3625 if (o->op_flags & OPf_SPECIAL) {
3626 /* first arg is a code block */
3627 OP * const nullop = OpSIBLING(cLISTOP->op_first);
3628 OP * kid = cUNOPx(nullop)->op_first;
3630 assert(nullop->op_type == OP_NULL);
3631 assert(kid->op_type == OP_SCOPE
3632 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
3633 /* since OP_SORT doesn't have a handy op_other-style
3634 * field that can point directly to the start of the code
3635 * block, store it in the otherwise-unused op_next field
3636 * of the top-level OP_NULL. This will be quicker at
3637 * run-time, and it will also allow us to remove leading
3638 * OP_NULLs by just messing with op_nexts without
3639 * altering the basic op_first/op_sibling layout. */
3640 kid = kLISTOP->op_first;
3642 (kid->op_type == OP_NULL
3643 && ( kid->op_targ == OP_NEXTSTATE
3644 || kid->op_targ == OP_DBSTATE ))
3645 || kid->op_type == OP_STUB
3646 || kid->op_type == OP_ENTER
3647 || (PL_parser && PL_parser->error_count));
3648 nullop->op_next = kid->op_next;
3649 DEFER(nullop->op_next);
3652 /* check that RHS of sort is a single plain array */
3653 oright = cUNOPo->op_first;
3654 if (!oright || oright->op_type != OP_PUSHMARK)
3657 if (o->op_private & OPpSORT_INPLACE)
3660 /* reverse sort ... can be optimised. */
3661 if (!OpHAS_SIBLING(cUNOPo)) {
3662 /* Nothing follows us on the list. */
3663 OP * const reverse = o->op_next;
3665 if (reverse->op_type == OP_REVERSE &&
3666 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
3667 OP * const pushmark = cUNOPx(reverse)->op_first;
3668 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
3669 && (OpSIBLING(cUNOPx(pushmark)) == o)) {
3670 /* reverse -> pushmark -> sort */
3671 o->op_private |= OPpSORT_REVERSE;
3673 pushmark->op_next = oright->op_next;
3683 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
3685 LISTOP *enter, *exlist;
3687 if (o->op_private & OPpSORT_INPLACE)
3690 enter = cLISTOPx(o->op_next);
3693 if (enter->op_type == OP_NULL) {
3694 enter = cLISTOPx(enter->op_next);
3698 /* for $a (...) will have OP_GV then OP_RV2GV here.
3699 for (...) just has an OP_GV. */
3700 if (enter->op_type == OP_GV) {
3701 gvop = (OP *) enter;
3702 enter = cLISTOPx(enter->op_next);
3705 if (enter->op_type == OP_RV2GV) {
3706 enter = cLISTOPx(enter->op_next);
3712 if (enter->op_type != OP_ENTERITER)
3715 iter = enter->op_next;
3716 if (!iter || iter->op_type != OP_ITER)
3719 expushmark = enter->op_first;
3720 if (!expushmark || expushmark->op_type != OP_NULL
3721 || expushmark->op_targ != OP_PUSHMARK)
3724 exlist = cLISTOPx(OpSIBLING(expushmark));
3725 if (!exlist || exlist->op_type != OP_NULL
3726 || exlist->op_targ != OP_LIST)
3729 if (exlist->op_last != o) {
3730 /* Mmm. Was expecting to point back to this op. */
3733 theirmark = exlist->op_first;
3734 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
3737 if (OpSIBLING(theirmark) != o) {
3738 /* There's something between the mark and the reverse, eg
3739 for (1, reverse (...))
3744 ourmark = cLISTOPo->op_first;
3745 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
3748 ourlast = cLISTOPo->op_last;
3749 if (!ourlast || ourlast->op_next != o)
3752 rv2av = OpSIBLING(ourmark);
3753 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av)
3754 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
3755 /* We're just reversing a single array. */
3756 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
3757 enter->op_flags |= OPf_STACKED;
3760 /* We don't have control over who points to theirmark, so sacrifice
3762 theirmark->op_next = ourmark->op_next;
3763 theirmark->op_flags = ourmark->op_flags;
3764 ourlast->op_next = gvop ? gvop : (OP *) enter;
3767 enter->op_private |= OPpITER_REVERSED;
3768 iter->op_private |= OPpITER_REVERSED;
3774 NOT_REACHED; /* NOTREACHED */
3779 if ((o->op_flags & OPf_KIDS) &&
3780 (cUNOPx(o)->op_first->op_type == OP_PADSV)) {
3789 OP * padsv = cUNOPx(o)->op_first;
3790 o->op_private = OPpTARGET_MY |
3791 (padsv->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
3792 o->op_targ = padsv->op_targ; padsv->op_targ = 0;
3794 /* Optimizer does NOT seem to fix up the padsv op_next ptr */
3796 oldoldop->op_next = o;
3800 } else if (o->op_next->op_type == OP_PADSV) {
3801 OP * padsv = o->op_next;
3802 OP * sassign = (padsv->op_next &&
3803 padsv->op_next->op_type == OP_SASSIGN) ?
3804 padsv->op_next : NULL;
3805 if (sassign && cBINOPx(sassign)->op_first == o) {
3812 * NOTE: undef does not have the "T" flag set in
3813 * regen/opcodes, as this would cause
3814 * S_maybe_targlex to do the optimization.
3815 * Seems easier to keep it all here, rather
3816 * than have an undef-specific branch in
3817 * S_maybe_targlex just to add the
3818 * OPpUNDEF_KEEP_PV flag.
3820 o->op_private = OPpTARGET_MY | OPpUNDEF_KEEP_PV |
3821 (padsv->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
3822 o->op_targ = padsv->op_targ; padsv->op_targ = 0;
3825 /* Optimizer DOES seems to fix up the op_next ptrs */
3832 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
3833 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
3838 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)
3839 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb)))
3842 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
3844 sv = newRV((SV *)PL_compcv);
3848 OpTYPE_set(o, OP_CONST);
3849 o->op_flags |= OPf_SPECIAL;
3855 if (OP_GIMME(o,0) == G_VOID
3856 || ( o->op_next->op_type == OP_LINESEQ
3857 && ( o->op_next->op_next->op_type == OP_LEAVESUB
3858 || ( o->op_next->op_next->op_type == OP_RETURN
3859 && !CvLVALUE(PL_compcv)))))
3861 OP *right = cBINOP->op_first;
3880 OP *left = OpSIBLING(right);
3881 if (left->op_type == OP_SUBSTR
3882 && (left->op_private & 7) < 4) {
3885 op_sibling_splice(o, NULL, 1, NULL);
3886 /* and insert it as second child of OP_SUBSTR */
3887 op_sibling_splice(left, cBINOPx(left)->op_first, 0,
3889 left->op_private |= OPpSUBSTR_REPL_FIRST;
3891 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
3895 OP* rhs = cBINOPx(o)->op_first;
3896 OP* lval = cBINOPx(o)->op_last;
3898 /* Combine a simple SASSIGN OP with a PADSV lvalue child OP
3899 * into a single OP. */
3901 /* This optimization covers arbitrarily complicated RHS OP
3902 * trees. Separate optimizations may exist for specific,
3903 * single RHS OPs, such as:
3904 * "my $foo = undef;" or "my $bar = $other_padsv;" */
3906 if (!(o->op_private & (OPpASSIGN_BACKWARDS|OPpASSIGN_CV_TO_GV))
3907 && lval && (lval->op_type == OP_PADSV) &&
3908 !(lval->op_private & OPpDEREF)
3909 /* skip if padrange has already gazumped the padsv */
3913 /* SASSIGN's bitfield flags, such as op_moresib and
3914 * op_slabbed, will be carried over unchanged. */
3915 OpTYPE_set(o, OP_PADSV_STORE);
3917 /* Explicitly craft the new OP's op_flags, carrying
3918 * some bits over from the SASSIGN */
3920 OPf_KIDS | OPf_STACKED |
3921 (o->op_flags & (OPf_WANT|OPf_PARENS))
3924 /* Reset op_private flags, taking relevant private flags
3926 o->op_private = (lval->op_private &
3927 (OPpLVAL_INTRO|OPpPAD_STATE|OPpDEREF));
3929 /* Steal the targ from the PADSV */
3930 o->op_targ = lval->op_targ; lval->op_targ = 0;
3932 /* Fixup op_next ptrs */
3933 assert(oldop->op_type == OP_PADSV);
3934 /* oldoldop can be arbitrarily deep in the RHS OP tree */
3935 oldoldop->op_next = o;
3937 /* Even when (rhs != oldoldop), rhs might still have a
3938 * relevant op_next ptr to lval. This is definitely true
3939 * when rhs is OP_NULL with a LOGOP kid (e.g. orassign).
3940 * There may be other cases. */
3941 if (rhs->op_next == lval)
3944 /* Now null-out the PADSV */
3947 /* NULL the previous op ptrs, so rpeep can continue */
3948 oldoldop = NULL; oldop = NULL;
3954 int l, r, lr, lscalars, rscalars;
3956 /* handle common vars detection, e.g. ($a,$b) = ($b,$a).
3957 Note that we do this now rather than in newASSIGNOP(),
3958 since only by now are aliased lexicals flagged as such
3960 See the essay "Common vars in list assignment" above for
3961 the full details of the rationale behind all the conditions
3964 PL_generation sorcery:
3965 To detect whether there are common vars, the global var
3966 PL_generation is incremented for each assign op we scan.
3967 Then we run through all the lexical variables on the LHS,
3968 of the assignment, setting a spare slot in each of them to
3969 PL_generation. Then we scan the RHS, and if any lexicals
3970 already have that value, we know we've got commonality.
3971 Also, if the generation number is already set to
3972 PERL_INT_MAX, then the variable is involved in aliasing, so
3973 we also have potential commonality in that case.
3979 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, &lscalars);
3982 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, &rscalars);
3986 /* After looking for things which are *always* safe, this main
3987 * if/else chain selects primarily based on the type of the
3988 * LHS, gradually working its way down from the more dangerous
3989 * to the more restrictive and thus safer cases */
3991 if ( !l /* () = ....; */
3992 || !r /* .... = (); */
3993 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */
3994 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */
3995 || (lscalars < 2) /* (undef, $x) = ... */
3997 NOOP; /* always safe */
3999 else if (l & AAS_DANGEROUS) {
4000 /* always dangerous */
4001 o->op_private |= OPpASSIGN_COMMON_SCALAR;
4002 o->op_private |= OPpASSIGN_COMMON_AGG;
4004 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) {
4005 /* package vars are always dangerous - too many
4006 * aliasing possibilities */
4007 if (l & AAS_PKG_SCALAR)
4008 o->op_private |= OPpASSIGN_COMMON_SCALAR;
4009 if (l & AAS_PKG_AGG)
4010 o->op_private |= OPpASSIGN_COMMON_AGG;
4012 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG
4013 |AAS_LEX_SCALAR|AAS_LEX_AGG))
4015 /* LHS contains only lexicals and safe ops */
4017 if (l & (AAS_MY_AGG|AAS_LEX_AGG))
4018 o->op_private |= OPpASSIGN_COMMON_AGG;
4020 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) {
4021 if (lr & AAS_LEX_SCALAR_COMM)
4022 o->op_private |= OPpASSIGN_COMMON_SCALAR;
4023 else if ( !(l & AAS_LEX_SCALAR)
4028 * as scalar-safe for performance reasons.
4029 * (it will still have been marked _AGG if necessary */
4032 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS))
4033 /* if there are only lexicals on the LHS and no
4034 * common ones on the RHS, then we assume that the
4035 * only way those lexicals could also get
4036 * on the RHS is via some sort of dereffing or
4039 * ($lex, $x) = (1, $$r)
4040 * and in this case we assume the var must have
4041 * a bumped ref count. So if its ref count is 1,
4042 * it must only be on the LHS.
4044 o->op_private |= OPpASSIGN_COMMON_RC1;
4049 * may have to handle aggregate on LHS, but we can't
4050 * have common scalars. */
4053 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1);
4055 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
4056 S_check_for_bool_cxt(o, 1, OPpASSIGN_TRUEBOOL, 0);
4062 /* if the op is used in boolean context, set the TRUEBOOL flag
4063 * which enables an optimisation at runtime which avoids creating
4064 * a stack temporary for known-true package names */
4065 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
4066 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
4070 /* see if the op is used in known boolean context,
4071 * but not if OA_TARGLEX optimisation is enabled */
4072 if ( (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR
4073 && !(o->op_private & OPpTARGET_MY)
4075 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
4079 /* see if the op is used in known boolean context */
4080 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
4081 S_check_for_bool_cxt(o, 1, OPpTRUEBOOL, 0);
4085 Perl_cpeep_t cpeep =
4086 XopENTRYCUSTOM(o, xop_peep);
4088 cpeep(aTHX_ o, oldop);
4093 /* did we just null the current op? If so, re-process it to handle
4094 * eliding "empty" ops from the chain */
4095 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
4108 Perl_peep(pTHX_ OP *o)
4114 * ex: set ts=8 sts=4 sw=4 et: