3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
15 * Awake! Awake! Fear, Fire, Foes! Awake!
18 * [p.1007 of _The Lord of the Rings_, VI/viii: "The Scouring of the Shire"]
21 /* This file contains 'hot' pp ("push/pop") functions that
22 * execute the opcodes that make up a perl program. A typical pp function
23 * expects to find its arguments on the stack, and usually pushes its
24 * results onto the stack, hence the 'pp' terminology. Each OP structure
25 * contains a pointer to the relevant pp_foo() function.
27 * By 'hot', we mean common ops whose execution speed is critical.
28 * By gathering them together into a single file, we encourage
29 * CPU cache hits on hot code. Also it could be taken as a warning not to
30 * change any code in this file unless you're sure it won't affect
35 #define PERL_IN_PP_HOT_C
50 PL_curcop = (COP*)PL_op;
51 TAINT_NOT; /* Each statement is presumed innocent */
52 PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp;
62 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO))
63 PUSHs(save_scalar(cGVOP_gv));
65 PUSHs(GvSVn(cGVOP_gv));
70 /* also used for: pp_lineseq() pp_regcmaybe() pp_scalar() pp_scope() */
77 /* This is sometimes called directly by pp_coreargs, pp_grepstart and
81 PUSHMARK(PL_stack_sp);
92 /* no PUTBACK, SETs doesn't inc/dec SP */
99 XPUSHs(MUTABLE_SV(cGVOP_gv));
104 /* also used for: pp_andassign() */
110 /* SP is not used to remove a variable that is saved across the
111 sv_2bool_flags call in SvTRUE_NN, if a RISC/CISC or low/high machine
112 register or load/store vs direct mem ops macro is introduced, this
113 should be a define block between direct PL_stack_sp and dSP operations,
114 presently, using PL_stack_sp is bias towards CISC cpus */
115 SV * const sv = *PL_stack_sp;
119 if (PL_op->op_type == OP_AND)
121 return cLOGOP->op_other;
129 /* sassign keeps its args in the optree traditionally backwards.
130 So we pop them differently.
132 SV *left = POPs; SV *right = TOPs;
134 if (PL_op->op_private & OPpASSIGN_BACKWARDS) { /* {or,and,dor}assign */
135 SV * const temp = left;
136 left = right; right = temp;
138 assert(TAINTING_get || !TAINT_get);
139 if (UNLIKELY(TAINT_get) && !SvTAINTED(right))
141 if (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV)) {
143 SV * const cv = SvRV(right);
144 const U32 cv_type = SvTYPE(cv);
145 const bool is_gv = isGV_with_GP(left);
146 const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM;
152 /* Can do the optimisation if left (LVALUE) is not a typeglob,
153 right (RVALUE) is a reference to something, and we're in void
155 if (!got_coderef && !is_gv && GIMME_V == G_VOID) {
156 /* Is the target symbol table currently empty? */
157 GV * const gv = gv_fetchsv_nomg(left, GV_NOINIT, SVt_PVGV);
158 if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) {
159 /* Good. Create a new proxy constant subroutine in the target.
160 The gv becomes a(nother) reference to the constant. */
161 SV *const value = SvRV(cv);
163 SvUPGRADE(MUTABLE_SV(gv), SVt_IV);
164 SvPCS_IMPORTED_on(gv);
166 SvREFCNT_inc_simple_void(value);
172 /* Need to fix things up. */
174 /* Need to fix GV. */
175 left = MUTABLE_SV(gv_fetchsv_nomg(left,GV_ADD, SVt_PVGV));
179 /* We've been returned a constant rather than a full subroutine,
180 but they expect a subroutine reference to apply. */
182 ENTER_with_name("sassign_coderef");
183 SvREFCNT_inc_void(SvRV(cv));
184 /* newCONSTSUB takes a reference count on the passed in SV
185 from us. We set the name to NULL, otherwise we get into
186 all sorts of fun as the reference to our new sub is
187 donated to the GV that we're about to assign to.
189 SvRV_set(right, MUTABLE_SV(newCONSTSUB(GvSTASH(left), NULL,
192 LEAVE_with_name("sassign_coderef");
194 /* What can happen for the corner case *{"BONK"} = \&{"BONK"};
196 First: ops for \&{"BONK"}; return us the constant in the
198 Second: ops for *{"BONK"} cause that symbol table entry
199 (and our reference to it) to be upgraded from RV
201 Thirdly: We get here. cv is actually PVGV now, and its
202 GvCV() is actually the subroutine we're looking for
204 So change the reference so that it points to the subroutine
205 of that typeglob, as that's what they were after all along.
207 GV *const upgraded = MUTABLE_GV(cv);
208 CV *const source = GvCV(upgraded);
211 assert(CvFLAGS(source) & CVf_CONST);
213 SvREFCNT_inc_simple_void_NN(source);
214 SvREFCNT_dec_NN(upgraded);
215 SvRV_set(right, MUTABLE_SV(source));
221 UNLIKELY(SvTEMP(left)) && !SvSMAGICAL(left) && SvREFCNT(left) == 1 &&
222 (!isGV_with_GP(left) || SvFAKE(left)) && ckWARN(WARN_MISC)
225 packWARN(WARN_MISC), "Useless assignment to a temporary"
227 SvSetMagicSV(left, right);
239 RETURNOP(SvTRUE_NN(sv) ? cLOGOP->op_other : cLOGOP->op_next);
246 TAINT_NOT; /* Each statement is presumed innocent */
248 PL_stack_sp = PL_stack_base + cx->blk_oldsp;
250 if (!(PL_op->op_flags & OPf_SPECIAL)) {
251 assert(CxTYPE(cx) == CXt_BLOCK || CxTYPE_is_LOOP(cx));
258 /* The main body of pp_concat, not including the magic/overload and
260 * It does targ = left . right.
261 * Moved into a separate function so that pp_multiconcat() can use it
265 PERL_STATIC_INLINE void
266 S_do_concat(pTHX_ SV *left, SV *right, SV *targ, U8 targmy)
270 const char *rpv = NULL;
272 bool rcopied = FALSE;
274 if (TARG == right && right != left) { /* $r = $l.$r */
275 rpv = SvPV_nomg_const(right, rlen);
276 rbyte = !DO_UTF8(right);
277 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
278 rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */
282 if (TARG != left) { /* not $l .= $r */
284 const char* const lpv = SvPV_nomg_const(left, llen);
285 lbyte = !DO_UTF8(left);
286 sv_setpvn(TARG, lpv, llen);
292 else { /* $l .= $r and left == TARG */
294 if ((left == right /* $l .= $l */
295 || targmy) /* $l = $l . $r */
296 && ckWARN(WARN_UNINITIALIZED)
302 SvPV_force_nomg_nolen(left);
304 lbyte = !DO_UTF8(left);
310 rpv = SvPV_nomg_const(right, rlen);
311 rbyte = !DO_UTF8(right);
313 if (lbyte != rbyte) {
315 sv_utf8_upgrade_nomg(TARG);
318 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
319 sv_utf8_upgrade_nomg(right);
320 rpv = SvPV_nomg_const(right, rlen);
323 sv_catpvn_nomg(TARG, rpv, rlen);
330 dSP; dATARGET; tryAMAGICbin_MG(concat_amg, AMGf_assign);
333 S_do_concat(aTHX_ left, right, targ, PL_op->op_private & OPpTARGET_MY);
342 Concatenate one or more args, possibly interleaved with constant string
343 segments. The result may be assigned to, or appended to, a variable or
346 Several op_flags and/or op_private bits indicate what the target is, and
347 whether it's appended to. Valid permutations are:
349 - (PADTMP) = (A.B.C....)
350 OPpTARGET_MY $lex = (A.B.C....)
351 OPpTARGET_MY,OPpLVAL_INTRO my $lex = (A.B.C....)
352 OPpTARGET_MY,OPpMULTICONCAT_APPEND $lex .= (A.B.C....)
353 OPf_STACKED expr = (A.B.C....)
354 OPf_STACKED,OPpMULTICONCAT_APPEND expr .= (A.B.C....)
356 Other combinations like (A.B).(C.D) are not optimised into a multiconcat
357 op, as it's too hard to get the correct ordering of ties, overload etc.
361 OPpMULTICONCAT_FAKE: not a real concat, instead an optimised
362 sprintf "...%s...". Don't call '.'
363 overloading: only use '""' overloading.
365 OPpMULTICONCAT_STRINGIFY: the RHS was of the form
366 "...$a...$b..." rather than
367 "..." . $a . "..." . $b . "..."
369 An OP_MULTICONCAT is of type UNOP_AUX. The fixed slots of the aux array are
370 defined with PERL_MULTICONCAT_IX_FOO constants, where:
373 FOO index description
374 -------- ----- ----------------------------------
375 NARGS 0 number of arguments
376 PLAIN_PV 1 non-utf8 constant string
377 PLAIN_LEN 2 non-utf8 constant string length
378 UTF8_PV 3 utf8 constant string
379 UTF8_LEN 4 utf8 constant string length
380 LENGTHS 5 first of nargs+1 const segment lengths
382 The idea is that a general string concatenation will have a fixed (known
383 at compile time) number of variable args, interspersed with constant
384 strings, e.g. "a=$a b=$b\n"
386 All the constant string segments "a=", " b=" and "\n" are stored as a
387 single string "a= b=\n", pointed to from the PLAIN_PV/UTF8_PV slot, along
388 with a series of segment lengths: e.g. 2,3,1. In the case where the
389 constant string is plain but has a different utf8 representation, both
390 variants are stored, and two sets of (nargs+1) segments lengths are stored
391 in the slots beginning at PERL_MULTICONCAT_IX_LENGTHS.
393 A segment length of -1 indicates that there is no constant string at that
394 point; this distinguishes between e.g. ($a . $b) and ($a . "" . $b), which
395 have differing overloading behaviour.
402 SV *targ; /* The SV to be assigned or appended to */
403 char *targ_pv; /* where within SvPVX(targ) we're writing to */
404 STRLEN targ_len; /* SvCUR(targ) */
405 SV **toparg; /* the highest arg position on the stack */
406 UNOP_AUX_item *aux; /* PL_op->op_aux buffer */
407 UNOP_AUX_item *const_lens; /* the segment length array part of aux */
408 const char *const_pv; /* the current segment of the const string buf */
409 SSize_t nargs; /* how many args were expected */
410 SSize_t stack_adj; /* how much to adjust SP on return */
411 STRLEN grow; /* final size of destination string (targ) */
412 UV targ_count; /* how many times targ has appeared on the RHS */
413 bool is_append; /* OPpMULTICONCAT_APPEND flag is set */
414 bool slow_concat; /* args too complex for quick concat */
415 U32 dst_utf8; /* the result will be utf8 (indicate this with
416 SVf_UTF8 in a U32, rather than using bool,
417 for ease of testing and setting) */
418 /* for each arg, holds the result of an SvPV() call */
419 struct multiconcat_svpv {
423 *targ_chain, /* chain of slots where targ has appeared on RHS */
424 *svpv_p, /* ptr for looping through svpv_buf */
425 *svpv_base, /* first slot (may be greater than svpv_buf), */
426 *svpv_end, /* and slot after highest result so far, of: */
427 svpv_buf[PERL_MULTICONCAT_MAXARG]; /* buf for storing SvPV() results */
429 aux = cUNOP_AUXx(PL_op)->op_aux;
430 stack_adj = nargs = aux[PERL_MULTICONCAT_IX_NARGS].ssize;
431 is_append = cBOOL(PL_op->op_private & OPpMULTICONCAT_APPEND);
433 /* get targ from the stack or pad */
435 if (PL_op->op_flags & OPf_STACKED) {
437 /* for 'expr .= ...', expr is the bottom item on the stack */
442 /* for 'expr = ...', expr is the top item on the stack */
446 SV **svp = &(PAD_SVl(PL_op->op_targ));
448 if (PL_op->op_private & OPpLVAL_INTRO) {
449 assert(PL_op->op_private & OPpTARGET_MY);
453 /* $lex .= "const" doesn't cause anything to be pushed */
459 grow = 1; /* allow for '\0' at minimum */
464 /* only utf8 variants of the const strings? */
465 dst_utf8 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv ? 0 : SVf_UTF8;
468 /* --------------------------------------------------------------
471 * stringify (i.e. SvPV()) every arg and store the resultant pv/len/utf8
472 * triplets in svpv_buf[]. Also increment 'grow' by the args' lengths.
474 * utf8 is indicated by storing a negative length.
476 * Where an arg is actually targ, the stringification is deferred:
477 * the length is set to 0, and the slot is added to targ_chain.
479 * If a magic, overloaded, or otherwise weird arg is found, which
480 * might have side effects when stringified, the loop is abandoned and
481 * we goto a code block where a more basic 'emulate calling
482 * pp_cpncat() on each arg in turn' is done.
485 for (; SP <= toparg; SP++, svpv_end++) {
490 assert(svpv_end - svpv_buf < PERL_MULTICONCAT_MAXARG);
494 /* this if/else chain is arranged so that common/simple cases
495 * take few conditionals */
497 if (LIKELY((SvFLAGS(sv) & (SVs_GMG|SVf_ROK|SVf_POK)) == SVf_POK)) {
498 /* common case: sv is a simple non-magical PV */
500 /* targ appears on RHS.
501 * Delay storing PV pointer; instead, add slot to targ_chain
502 * so it can be populated later, after targ has been grown and
503 * we know its final SvPVX() address.
506 svpv_end->len = 0; /* zerojng here means we can skip
507 updating later if targ_len == 0 */
508 svpv_end->pv = (char*)targ_chain;
509 targ_chain = svpv_end;
515 svpv_end->pv = SvPVX(sv);
517 else if (UNLIKELY(SvFLAGS(sv) & (SVs_GMG|SVf_ROK)))
518 /* may have side effects: tie, overload etc.
519 * Abandon 'stringify everything first' and handle
520 * args in strict order. Note that already-stringified args
521 * will be reprocessed, which is safe because the each first
522 * stringification would have been idempotent.
525 else if (SvNIOK(sv)) {
528 /* stringify general valid scalar */
529 svpv_end->pv = sv_2pv_flags(sv, &len, 0);
531 else if (!SvOK(sv)) {
532 if (ckWARN(WARN_UNINITIALIZED))
533 /* an undef value in the presence of warnings may trigger
536 svpv_end->pv = (char*)"";
540 goto do_magical; /* something weird */
542 utf8 = (SvFLAGS(sv) & SVf_UTF8);
544 ASSUME(len < SSize_t_MAX);
545 svpv_end->len = utf8 ? -(SSize_t)len : (SSize_t)len;
549 /* --------------------------------------------------------------
554 * if targ appears on the RHS or is appended to, force stringify it;
555 * otherwise set it to "". Then set targ_len.
559 /* abandon quick route if using targ might have side effects */
560 if (UNLIKELY(SvFLAGS(targ) & (SVs_GMG|SVf_ROK)))
566 SvPV_force_nomg_nolen(targ);
567 targ_utf8 = SvFLAGS(targ) & SVf_UTF8;
568 if (UNLIKELY(dst_utf8 & ~targ_utf8)) {
569 if (LIKELY(!IN_BYTES))
570 sv_utf8_upgrade_nomg(targ);
573 dst_utf8 |= targ_utf8;
575 targ_len = SvCUR(targ);
576 grow += targ_len * (targ_count + is_append);
579 else if (ckWARN(WARN_UNINITIALIZED))
580 /* warning might have side effects */
582 /* the undef targ will be silently SvPVCLEAR()ed below */
584 else if (UNLIKELY(SvTYPE(targ) >= SVt_REGEXP)) {
585 /* Assigning to some weird LHS type. Don't force the LHS to be an
586 * empty string; instead, do things 'long hand' by using the
587 * overload code path, which concats to a TEMP sv and does
588 * sv_catsv() calls rather than COPY()s. This ensures that even
589 * bizarre code like this doesn't break or crash:
591 * (which makes the 'F' typeglob an alias to the
592 * '*main::F*main::F' typeglob).
597 /* targ was found on RHS.
598 * Force stringify it, using the same code as the append branch
599 * above, except that we don't need the magic/overload/undef
600 * checks as these will already have been done in the phase 1
605 /* unrolled SvPVCLEAR() - mostly: no need to grow or set SvCUR() to 0;
606 * those will be done later. */
607 SV_CHECK_THINKFIRST_COW_DROP(targ);
608 SvUPGRADE(targ, SVt_PV);
609 SvFLAGS(targ) &= ~(SVf_OK|SVf_IVisUV|SVf_UTF8);
610 SvFLAGS(targ) |= (SVf_POK|SVp_POK|dst_utf8);
614 /* --------------------------------------------------------------
617 * UTF-8 tweaks and grow targ:
619 * Now that we know the length and utf8-ness of both the targ and
620 * args, grow targ to the size needed to accumulate all the args, based
621 * on whether targ appears on the RHS, whether we're appending, and
622 * whether any non-utf8 args expand in size if converted to utf8.
624 * For the latter, if dst_utf8 we scan non-utf8 args looking for
625 * variant chars, and adjust the svpv->len value of those args to the
626 * utf8 size and negate it to flag them. At the same time we un-negate
627 * the lens of any utf8 args since after this phase we no longer care
628 * whether an arg is utf8 or not.
630 * Finally, initialise const_lens and const_pv based on utf8ness.
631 * Note that there are 3 permutations:
633 * * If the constant string is invariant whether utf8 or not (e.g. "abc"),
634 * then aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN] are the same as
635 * aux[PERL_MULTICONCAT_IX_UTF8_PV/LEN] and there is one set of
638 * * If the string is fully utf8, e.g. "\x{100}", then
639 * aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN] == (NULL,0) and there is
640 * one set of segment lengths.
642 * * If the string has different plain and utf8 representations
643 * (e.g. "\x80"), then aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN]]
644 * holds the plain rep, while aux[PERL_MULTICONCAT_IX_UTF8_PV/LEN]
645 * holds the utf8 rep, and there are 2 sets of segment lengths,
646 * with the utf8 set following after the plain set.
648 * On entry to this section the (pv,len) pairs in svpv_buf have the
649 * following meanings:
650 * (pv, len) a plain string
651 * (pv, -len) a utf8 string
652 * (NULL, 0) left-most targ \ linked together R-to-L
653 * (next, 0) other targ / in targ_chain
656 /* turn off utf8 handling if 'use bytes' is in scope */
657 if (UNLIKELY(dst_utf8 && IN_BYTES)) {
660 /* undo all the negative lengths which flag utf8-ness */
661 for (svpv_p = svpv_buf; svpv_p < svpv_end; svpv_p++) {
662 SSize_t len = svpv_p->len;
668 /* grow += total of lengths of constant string segments */
671 len = aux[dst_utf8 ? PERL_MULTICONCAT_IX_UTF8_LEN
672 : PERL_MULTICONCAT_IX_PLAIN_LEN].ssize;
673 slow_concat = cBOOL(len);
677 const_lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
680 const_pv = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
681 if ( aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv
682 && const_pv != aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv)
683 /* separate sets of lengths for plain and utf8 */
684 const_lens += nargs + 1;
686 /* If the result is utf8 but some of the args aren't,
687 * calculate how much extra growth is needed for all the chars
688 * which will expand to two utf8 bytes.
689 * Also, if the growth is non-zero, negate the length to indicate
690 * that this is a variant string. Conversely, un-negate the
691 * length on utf8 args (which was only needed to flag non-utf8
692 * args in this loop */
693 for (svpv_p = svpv_buf; svpv_p < svpv_end; svpv_p++) {
702 extra = variant_under_utf8_count((U8 *) svpv_p->pv,
703 (U8 *) svpv_p->pv + len);
704 if (UNLIKELY(extra)) {
706 /* -ve len indicates special handling */
707 svpv_p->len = -(len + extra);
713 const_pv = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
715 /* unrolled SvGROW(), except don't check for SVf_IsCOW, which should
716 * already have been dropped */
717 assert(!SvIsCOW(targ));
718 targ_pv = (SvLEN(targ) < (grow) ? sv_grow(targ,grow) : SvPVX(targ));
721 /* --------------------------------------------------------------
724 * Now that targ has been grown, we know the final address of the targ
725 * PVX, if needed. Preserve / move targ contents if appending or if
726 * targ appears on RHS.
728 * Also update svpv_buf slots in targ_chain.
730 * Don't bother with any of this if the target length is zero:
731 * targ_len is set to zero unless we're appending or targ appears on
732 * RHS. And even if it is, we can optimise by skipping this chunk of
733 * code for zero targ_len. In the latter case, we don't need to update
734 * the slots in targ_chain with the (zero length) target string, since
735 * we set the len in such slots to 0 earlier, and since the Copy() is
736 * skipped on zero length, it doesn't matter what svpv_p->pv contains.
738 * On entry to this section the (pv,len) pairs in svpv_buf have the
739 * following meanings:
740 * (pv, len) a pure-plain or utf8 string
741 * (pv, -(len+extra)) a plain string which will expand by 'extra'
742 * bytes when converted to utf8
743 * (NULL, 0) left-most targ \ linked together R-to-L
744 * (next, 0) other targ / in targ_chain
746 * On exit, the targ contents will have been moved to the
747 * earliest place they are needed (e.g. $x = "abc$x" will shift them
748 * 3 bytes, while $x .= ... will leave them at the beginning);
749 * and dst_pv will point to the location within SvPVX(targ) where the
750 * next arg should be copied.
753 svpv_base = svpv_buf;
756 struct multiconcat_svpv *tc_stop;
757 char *targ_buf = targ_pv; /* ptr to original targ string */
759 assert(is_append || targ_count);
766 /* The targ appears on RHS, e.g. '$t = $a . $t . $t'.
767 * Move the current contents of targ to the first
768 * position where it's needed, and use that as the src buffer
769 * for any further uses (such as the second RHS $t above).
770 * In calculating the first position, we need to sum the
771 * lengths of all consts and args before that.
774 UNOP_AUX_item *lens = const_lens;
775 /* length of first const string segment */
776 STRLEN offset = lens->ssize > 0 ? lens->ssize : 0;
784 break; /* the first targ argument */
785 /* add lengths of the next arg and const string segment */
787 if (len < 0) /* variant args have this */
789 offset += (STRLEN)len;
790 len = (++lens)->ssize;
791 offset += (len >= 0) ? (STRLEN)len : 0;
793 /* all args and consts so far are empty; update
794 * the start position for the concat later */
799 assert(svpv_p < svpv_end);
804 Move(targ_pv, targ_buf, targ_len, char);
805 /* a negative length implies don't Copy(), but do increment */
806 svpv_p->len = -((SSize_t)targ_len);
810 /* skip the first targ copy */
816 /* Don't populate the first targ slot in the loop below; it's
817 * either not used because we advanced svpv_base beyond it, or
818 * we already stored the special -targ_len value in it
823 /* populate slots in svpv_buf representing targ on RHS */
824 while (targ_chain != tc_stop) {
825 struct multiconcat_svpv *p = targ_chain;
826 targ_chain = (struct multiconcat_svpv *)(p->pv);
828 p->len = (SSize_t)targ_len;
833 /* --------------------------------------------------------------
836 * Append all the args in svpv_buf, plus the const strings, to targ.
838 * On entry to this section the (pv,len) pairs in svpv_buf have the
839 * following meanings:
840 * (pv, len) a pure-plain or utf8 string (which may be targ)
841 * (pv, -(len+extra)) a plain string which will expand by 'extra'
842 * bytes when converted to utf8
843 * (0, -len) left-most targ, whose content has already
844 * been copied. Just advance targ_pv by len.
847 /* If there are no constant strings and no special case args
848 * (svpv_p->len < 0), use a simpler, more efficient concat loop
851 for (svpv_p = svpv_base; svpv_p < svpv_end; svpv_p++) {
852 SSize_t len = svpv_p->len;
855 Copy(svpv_p->pv, targ_pv, len, char);
858 const_lens += (svpv_end - svpv_base + 1);
861 /* Note that we iterate the loop nargs+1 times: to append nargs
862 * arguments and nargs+1 constant strings. For example, "-$a-$b-"
864 svpv_p = svpv_base - 1;
867 SSize_t len = (const_lens++)->ssize;
869 /* append next const string segment */
871 Copy(const_pv, targ_pv, len, char);
876 if (++svpv_p == svpv_end)
879 /* append next arg */
882 if (LIKELY(len > 0)) {
883 Copy(svpv_p->pv, targ_pv, len, char);
886 else if (UNLIKELY(len < 0)) {
887 /* negative length indicates two special cases */
888 const char *p = svpv_p->pv;
891 /* copy plain-but-variant pv to a utf8 targ */
892 char * end_pv = targ_pv + len;
894 while (targ_pv < end_pv) {
896 append_utf8_from_native_byte(c, (U8**)&targ_pv);
900 /* arg is already-copied targ */
908 SvCUR_set(targ, targ_pv - SvPVX(targ));
909 assert(grow >= SvCUR(targ) + 1);
910 assert(SvLEN(targ) >= SvCUR(targ) + 1);
912 /* --------------------------------------------------------------
923 /* --------------------------------------------------------------
926 * We only get here if any of the args (or targ too in the case of
927 * append) have something which might cause side effects, such
928 * as magic, overload, or an undef value in the presence of warnings.
929 * In that case, any earlier attempt to stringify the args will have
930 * been abandoned, and we come here instead.
932 * Here, we concat each arg in turn the old-fashioned way: essentially
933 * emulating pp_concat() in a loop. This means that all the weird edge
934 * cases will be handled correctly, if not necessarily speedily.
936 * Note that some args may already have been stringified - those are
937 * processed again, which is safe, since only args without side-effects
938 * were stringified earlier.
950 const char *cpv = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
951 UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
952 Size_t arg_count = 0; /* how many args have been processed */
955 cpv = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
959 svp = toparg - nargs + 1;
963 * plus possible nargs+1 consts,
964 * plus, if appending, a final targ in an extra last iteration
968 for (i = 0; i <= n; i++) {
971 /* if necessary, stringify the final RHS result in
972 * something like $targ .= "$a$b$c" - simulating
976 && (PL_op->op_private &OPpMULTICONCAT_STRINGIFY)
978 /* extra conditions for backwards compatibility:
979 * probably incorrect, but keep the existing behaviour
980 * for now. The rules are:
981 * $x = "$ov" single arg: stringify;
982 * $x = "$ov$y" multiple args: don't stringify,
983 * $lex = "$ov$y$z" except TARGMY with at least 2 concats
988 && (PL_op->op_private & OPpTARGET_MY)
989 && !(PL_op->op_private & OPpLVAL_INTRO)
994 SV *tmp = sv_newmortal();
995 sv_copypv(tmp, left);
1000 /* do one extra iteration to handle $targ in $targ .= ... */
1001 if (i == n && !is_append)
1004 /* get the next arg SV or regen the next const SV */
1005 len = lens[i >> 1].ssize;
1007 /* handle the final targ .= (....) */
1012 right = svp[(i >> 1)];
1014 continue; /* no const in this position */
1016 right = newSVpvn_flags(cpv, len, (utf8 | SVs_TEMP));
1022 if (arg_count <= 1) {
1024 continue; /* need at least two SVs to concat together */
1027 if (arg_count == 2 && i < n) {
1028 /* for the first concat, create a mortal acting like the
1029 * padtmp from OP_CONST. In later iterations this will
1031 nexttarg = sv_newmortal();
1039 /* Handle possible overloading.
1040 * This is basically an unrolled
1041 * tryAMAGICbin_MG(concat_amg, AMGf_assign);
1043 * Perl_try_amagic_bin()
1044 * call, but using left and right rather than SP[-1], SP[0],
1045 * and not relying on OPf_STACKED implying .=
1048 if ((SvFLAGS(left)|SvFLAGS(right)) & (SVf_ROK|SVs_GMG)) {
1053 if ((SvAMAGIC(left) || SvAMAGIC(right))
1054 /* sprintf doesn't do concat overloading,
1055 * but allow for $x .= sprintf(...)
1057 && ( !(PL_op->op_private & OPpMULTICONCAT_FAKE)
1061 SV * const tmpsv = amagic_call(left, right, concat_amg,
1062 (nextappend ? AMGf_assign: 0));
1064 /* NB: tryAMAGICbin_MG() includes an OPpTARGET_MY test
1065 * here, which isn't needed as any implicit
1066 * assign done under OPpTARGET_MY is done after
1069 sv_setsv(left, tmpsv);
1078 /* if both args are the same magical value, make one a copy */
1079 if (left == right && SvGMAGICAL(left)) {
1080 left = sv_newmortal();
1081 /* Print the uninitialized warning now, so it includes the
1084 if (ckWARN(WARN_UNINITIALIZED))
1085 report_uninit(right);
1086 sv_setsv_flags(left, &PL_sv_no, 0);
1089 sv_setsv_flags(left, right, 0);
1094 /* nexttarg = left . right */
1095 S_do_concat(aTHX_ left, right, nexttarg, 0);
1099 SP = toparg - stack_adj + 1;
1101 /* Return the result of all RHS concats, unless this op includes
1102 * an assign ($lex = x.y.z or expr = x.y.z), in which case copy
1103 * to target (which will be $lex or expr).
1104 * If we are appending, targ will already have been appended to in
1107 && ( (PL_op->op_flags & OPf_STACKED)
1108 || (PL_op->op_private & OPpTARGET_MY))
1110 sv_setsv(targ, left);
1121 /* push the elements of av onto the stack.
1122 * Returns PL_op->op_next to allow tail-call optimisation of its callers */
1125 S_pushav(pTHX_ AV* const av)
1128 const SSize_t maxarg = AvFILL(av) + 1;
1130 if (UNLIKELY(SvRMAGICAL(av))) {
1132 for (i=0; i < (PADOFFSET)maxarg; i++) {
1133 SV ** const svp = av_fetch(av, i, FALSE);
1134 SP[i+1] = LIKELY(svp)
1136 : UNLIKELY(PL_op->op_flags & OPf_MOD)
1143 for (i=0; i < (PADOFFSET)maxarg; i++) {
1144 SV *sv = AvARRAY(av)[i];
1145 SP[i+1] = LIKELY(sv)
1147 : UNLIKELY(PL_op->op_flags & OPf_MOD)
1158 /* ($lex1,@lex2,...) or my ($lex1,@lex2,...) */
1163 PADOFFSET base = PL_op->op_targ;
1164 int count = (int)(PL_op->op_private) & OPpPADRANGE_COUNTMASK;
1165 if (PL_op->op_flags & OPf_SPECIAL) {
1166 /* fake the RHS of my ($x,$y,..) = @_ */
1168 (void)S_pushav(aTHX_ GvAVn(PL_defgv));
1172 /* note, this is only skipped for compile-time-known void cxt */
1173 if ((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID) {
1178 for (i = 0; i <count; i++)
1179 *++SP = PAD_SV(base+i);
1181 if (PL_op->op_private & OPpLVAL_INTRO) {
1182 SV **svp = &(PAD_SVl(base));
1183 const UV payload = (UV)(
1184 (base << (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT))
1185 | (count << SAVE_TIGHT_SHIFT)
1186 | SAVEt_CLEARPADRANGE);
1189 STATIC_ASSERT_STMT(OPpPADRANGE_COUNTMASK + 1 == (1 << OPpPADRANGE_COUNTSHIFT));
1190 assert((payload >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
1198 for (i = 0; i <count; i++)
1199 SvPADSTALE_off(*svp++); /* mark lexical as active */
1210 OP * const op = PL_op;
1211 /* access PL_curpad once */
1212 SV ** const padentry = &(PAD_SVl(op->op_targ));
1217 PUTBACK; /* no pop/push after this, TOPs ok */
1219 if (op->op_flags & OPf_MOD) {
1220 if (op->op_private & OPpLVAL_INTRO)
1221 if (!(op->op_private & OPpPAD_STATE))
1222 save_clearsv(padentry);
1223 if (op->op_private & OPpDEREF) {
1224 /* TOPs is equivalent to TARG here. Using TOPs (SP) rather
1225 than TARG reduces the scope of TARG, so it does not
1226 span the call to save_clearsv, resulting in smaller
1228 TOPs = vivify_ref(TOPs, op->op_private & OPpDEREF);
1238 /* pp_coreargs pushes a NULL to indicate no args passed to
1239 * CORE::readline() */
1242 tryAMAGICunTARGETlist(iter_amg, 0);
1243 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
1245 else PL_last_in_gv = PL_argvgv, PL_stack_sp--;
1246 if (!isGV_with_GP(PL_last_in_gv)) {
1247 if (SvROK(PL_last_in_gv) && isGV_with_GP(SvRV(PL_last_in_gv)))
1248 PL_last_in_gv = MUTABLE_GV(SvRV(PL_last_in_gv));
1251 XPUSHs(MUTABLE_SV(PL_last_in_gv));
1253 Perl_pp_rv2gv(aTHX);
1254 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
1255 assert((SV*)PL_last_in_gv == &PL_sv_undef || isGV_with_GP(PL_last_in_gv));
1258 return do_readline();
1266 tryAMAGICbin_MG(eq_amg, AMGf_numeric);
1270 (SvIOK_notUV(left) && SvIOK_notUV(right))
1271 ? (SvIVX(left) == SvIVX(right))
1272 : ( do_ncmp(left, right) == 0)
1278 /* also used for: pp_i_preinc() */
1282 SV *sv = *PL_stack_sp;
1284 if (LIKELY(((sv->sv_flags &
1285 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1286 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1288 && SvIVX(sv) != IV_MAX)
1290 SvIV_set(sv, SvIVX(sv) + 1);
1292 else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_inc */
1299 /* also used for: pp_i_predec() */
1303 SV *sv = *PL_stack_sp;
1305 if (LIKELY(((sv->sv_flags &
1306 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1307 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1309 && SvIVX(sv) != IV_MIN)
1311 SvIV_set(sv, SvIVX(sv) - 1);
1313 else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_dec */
1320 /* also used for: pp_orassign() */
1331 if (PL_op->op_type == OP_OR)
1333 RETURNOP(cLOGOP->op_other);
1338 /* also used for: pp_dor() pp_dorassign() */
1345 const int op_type = PL_op->op_type;
1346 const bool is_dor = (op_type == OP_DOR || op_type == OP_DORASSIGN);
1351 if (UNLIKELY(!sv || !SvANY(sv))) {
1352 if (op_type == OP_DOR)
1354 RETURNOP(cLOGOP->op_other);
1360 if (UNLIKELY(!sv || !SvANY(sv)))
1365 switch (SvTYPE(sv)) {
1367 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
1371 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
1375 if (CvROOT(sv) || CvXSUB(sv))
1388 if(op_type == OP_DOR)
1390 RETURNOP(cLOGOP->op_other);
1392 /* assuming OP_DEFINED */
1402 dSP; dATARGET; bool useleft; SV *svl, *svr;
1404 tryAMAGICbin_MG(add_amg, AMGf_assign|AMGf_numeric);
1408 #ifdef PERL_PRESERVE_IVUV
1410 /* special-case some simple common cases */
1411 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1413 U32 flags = (svl->sv_flags & svr->sv_flags);
1414 if (flags & SVf_IOK) {
1415 /* both args are simple IVs */
1420 topl = ((UV)il) >> (UVSIZE * 8 - 2);
1421 topr = ((UV)ir) >> (UVSIZE * 8 - 2);
1423 /* if both are in a range that can't under/overflow, do a
1424 * simple integer add: if the top of both numbers
1425 * are 00 or 11, then it's safe */
1426 if (!( ((topl+1) | (topr+1)) & 2)) {
1428 TARGi(il + ir, 0); /* args not GMG, so can't be tainted */
1434 else if (flags & SVf_NOK) {
1435 /* both args are NVs */
1439 if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
1440 /* nothing was lost by converting to IVs */
1444 TARGn(nl + nr, 0); /* args not GMG, so can't be tainted */
1452 useleft = USE_LEFT(svl);
1453 /* We must see if we can perform the addition with integers if possible,
1454 as the integer code detects overflow while the NV code doesn't.
1455 If either argument hasn't had a numeric conversion yet attempt to get
1456 the IV. It's important to do this now, rather than just assuming that
1457 it's not IOK as a PV of "9223372036854775806" may not take well to NV
1458 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
1459 integer in case the second argument is IV=9223372036854775806
1460 We can (now) rely on sv_2iv to do the right thing, only setting the
1461 public IOK flag if the value in the NV (or PV) slot is truly integer.
1463 A side effect is that this also aggressively prefers integer maths over
1464 fp maths for integer values.
1466 How to detect overflow?
1468 C 99 section 6.2.6.1 says
1470 The range of nonnegative values of a signed integer type is a subrange
1471 of the corresponding unsigned integer type, and the representation of
1472 the same value in each type is the same. A computation involving
1473 unsigned operands can never overflow, because a result that cannot be
1474 represented by the resulting unsigned integer type is reduced modulo
1475 the number that is one greater than the largest value that can be
1476 represented by the resulting type.
1480 which I read as "unsigned ints wrap."
1482 signed integer overflow seems to be classed as "exception condition"
1484 If an exceptional condition occurs during the evaluation of an
1485 expression (that is, if the result is not mathematically defined or not
1486 in the range of representable values for its type), the behavior is
1489 (6.5, the 5th paragraph)
1491 I had assumed that on 2s complement machines signed arithmetic would
1492 wrap, hence coded pp_add and pp_subtract on the assumption that
1493 everything perl builds on would be happy. After much wailing and
1494 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
1495 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
1496 unsigned code below is actually shorter than the old code. :-)
1499 if (SvIV_please_nomg(svr)) {
1500 /* Unless the left argument is integer in range we are going to have to
1501 use NV maths. Hence only attempt to coerce the right argument if
1502 we know the left is integer. */
1509 a_valid = auvok = 1;
1510 /* left operand is undef, treat as zero. + 0 is identity,
1511 Could SETi or SETu right now, but space optimise by not adding
1512 lots of code to speed up what is probably a rarish case. */
1514 /* Left operand is defined, so is it IV? */
1515 if (SvIV_please_nomg(svl)) {
1516 if ((auvok = SvUOK(svl)))
1519 const IV aiv = SvIVX(svl);
1522 auvok = 1; /* Now acting as a sign flag. */
1524 /* Using 0- here and later to silence bogus warning
1526 auv = (UV) (0 - (UV) aiv);
1533 bool result_good = 0;
1536 bool buvok = SvUOK(svr);
1541 const IV biv = SvIVX(svr);
1546 buv = (UV) (0 - (UV) biv);
1548 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1549 else "IV" now, independent of how it came in.
1550 if a, b represents positive, A, B negative, a maps to -A etc
1555 all UV maths. negate result if A negative.
1556 add if signs same, subtract if signs differ. */
1558 if (auvok ^ buvok) {
1562 /* Must get smaller */
1567 if (result <= buv) {
1568 /* result really should be -(auv-buv). as its negation
1569 of true value, need to swap our result flag */
1586 if (result <= (UV)IV_MIN)
1587 SETi(result == (UV)IV_MIN
1588 ? IV_MIN : -(IV)result);
1590 /* result valid, but out of range for IV. */
1591 SETn( -(NV)result );
1595 } /* Overflow, drop through to NVs. */
1600 useleft = USE_LEFT(svl);
1604 NV value = SvNV_nomg(svr);
1607 /* left operand is undef, treat as zero. + 0.0 is identity. */
1611 SETn( value + SvNV_nomg(svl) );
1617 /* also used for: pp_aelemfast_lex() */
1622 AV * const av = PL_op->op_type == OP_AELEMFAST_LEX
1623 ? MUTABLE_AV(PAD_SV(PL_op->op_targ)) : GvAVn(cGVOP_gv);
1624 const U32 lval = PL_op->op_flags & OPf_MOD;
1625 const I8 key = (I8)PL_op->op_private;
1629 assert(SvTYPE(av) == SVt_PVAV);
1633 /* inlined av_fetch() for simple cases ... */
1634 if (!SvRMAGICAL(av) && key >= 0 && key <= AvFILLp(av)) {
1635 sv = AvARRAY(av)[key];
1642 /* ... else do it the hard way */
1643 svp = av_fetch(av, key, lval);
1644 sv = (svp ? *svp : &PL_sv_undef);
1646 if (UNLIKELY(!svp && lval))
1647 DIE(aTHX_ PL_no_aelem, (int)key);
1649 if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
1657 dSP; dMARK; dTARGET;
1659 do_join(TARG, *MARK, MARK, SP);
1665 /* Oversized hot code. */
1667 /* also used for: pp_say() */
1671 dSP; dMARK; dORIGMARK;
1675 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1679 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1682 if (MARK == ORIGMARK) {
1683 /* If using default handle then we need to make space to
1684 * pass object as 1st arg, so move other args up ...
1688 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1691 return Perl_tied_method(aTHX_ SV_CONST(PRINT), mark - 1, MUTABLE_SV(io),
1693 (G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK
1694 | (PL_op->op_type == OP_SAY
1695 ? TIED_METHOD_SAY : 0)), sp - mark);
1698 if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv)))
1699 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1702 SETERRNO(EBADF,RMS_IFI);
1705 else if (!(fp = IoOFP(io))) {
1707 report_wrongway_fh(gv, '<');
1710 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1714 SV * const ofs = GvSV(PL_ofsgv); /* $, */
1716 if (ofs && (SvGMAGICAL(ofs) || SvOK(ofs))) {
1717 while (MARK <= SP) {
1718 if (!do_print(*MARK, fp))
1722 /* don't use 'ofs' here - it may be invalidated by magic callbacks */
1723 if (!do_print(GvSV(PL_ofsgv), fp)) {
1731 while (MARK <= SP) {
1732 if (!do_print(*MARK, fp))
1740 if (PL_op->op_type == OP_SAY) {
1741 if (PerlIO_write(fp, "\n", 1) == 0 || PerlIO_error(fp))
1744 else if (PL_ors_sv && SvOK(PL_ors_sv))
1745 if (!do_print(PL_ors_sv, fp)) /* $\ */
1748 if (IoFLAGS(io) & IOf_FLUSH)
1749 if (PerlIO_flush(fp) == EOF)
1759 XPUSHs(&PL_sv_undef);
1764 /* do the common parts of pp_padhv() and pp_rv2hv()
1765 * It assumes the caller has done EXTEND(SP, 1) or equivalent.
1766 * 'is_keys' indicates the OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS flag is set.
1767 * 'has_targ' indicates that the op has a target - this should
1768 * be a compile-time constant so that the code can constant-folded as
1772 PERL_STATIC_INLINE OP*
1773 S_padhv_rv2hv_common(pTHX_ HV *hv, U8 gimme, bool is_keys, bool has_targ)
1782 assert(PL_op->op_type == OP_PADHV || PL_op->op_type == OP_RV2HV);
1784 if (gimme == G_ARRAY) {
1790 /* 'keys %h' masquerading as '%h': reset iterator */
1791 (void)hv_iterinit(hv);
1793 if (gimme == G_VOID)
1796 is_bool = ( PL_op->op_private & OPpTRUEBOOL
1797 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
1798 && block_gimme() == G_VOID));
1799 is_tied = SvRMAGICAL(hv) && (mg = mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied));
1801 if (UNLIKELY(is_tied)) {
1802 if (is_keys && !is_bool) {
1804 while (hv_iternext(hv))
1809 sv = magic_scalarpack(hv, mg);
1816 sv = i ? &PL_sv_yes : &PL_sv_zero;
1828 /* parent op should be an unused OP_KEYS whose targ we can
1833 assert(!OpHAS_SIBLING(PL_op));
1834 k = PL_op->op_sibparent;
1835 assert(k->op_type == OP_KEYS);
1836 TARG = PAD_SV(k->op_targ);
1849 /* This is also called directly by pp_lvavref. */
1854 assert(SvTYPE(TARG) == SVt_PVAV);
1855 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
1856 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
1857 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
1860 if (PL_op->op_flags & OPf_REF) {
1864 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
1865 const I32 flags = is_lvalue_sub();
1866 if (flags && !(flags & OPpENTERSUB_INARGS)) {
1867 if (GIMME_V == G_SCALAR)
1868 /* diag_listed_as: Can't return %s to lvalue scalar context */
1869 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
1876 if (gimme == G_ARRAY)
1877 return S_pushav(aTHX_ (AV*)TARG);
1879 if (gimme == G_SCALAR) {
1880 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
1883 else if (PL_op->op_private & OPpTRUEBOOL)
1897 assert(SvTYPE(TARG) == SVt_PVHV);
1898 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
1899 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
1900 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
1904 if (PL_op->op_flags & OPf_REF) {
1908 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
1909 const I32 flags = is_lvalue_sub();
1910 if (flags && !(flags & OPpENTERSUB_INARGS)) {
1911 if (GIMME_V == G_SCALAR)
1912 /* diag_listed_as: Can't return %s to lvalue scalar context */
1913 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
1921 return S_padhv_rv2hv_common(aTHX_ (HV*)TARG, gimme,
1922 cBOOL(PL_op->op_private & OPpPADHV_ISKEYS),
1927 /* also used for: pp_rv2hv() */
1928 /* also called directly by pp_lvavref */
1933 const U8 gimme = GIMME_V;
1934 static const char an_array[] = "an ARRAY";
1935 static const char a_hash[] = "a HASH";
1936 const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV
1937 || PL_op->op_type == OP_LVAVREF;
1938 const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV;
1942 if (UNLIKELY(SvAMAGIC(sv))) {
1943 sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg);
1946 if (UNLIKELY(SvTYPE(sv) != type))
1947 /* diag_listed_as: Not an ARRAY reference */
1948 DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash);
1949 else if (UNLIKELY(PL_op->op_flags & OPf_MOD
1950 && PL_op->op_private & OPpLVAL_INTRO))
1951 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
1953 else if (UNLIKELY(SvTYPE(sv) != type)) {
1956 if (!isGV_with_GP(sv)) {
1957 gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash,
1963 gv = MUTABLE_GV(sv);
1965 sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv));
1966 if (PL_op->op_private & OPpLVAL_INTRO)
1967 sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv));
1969 if (PL_op->op_flags & OPf_REF) {
1973 else if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1974 const I32 flags = is_lvalue_sub();
1975 if (flags && !(flags & OPpENTERSUB_INARGS)) {
1976 if (gimme != G_ARRAY)
1977 goto croak_cant_return;
1984 AV *const av = MUTABLE_AV(sv);
1986 if (gimme == G_ARRAY) {
1989 return S_pushav(aTHX_ av);
1992 if (gimme == G_SCALAR) {
1993 const SSize_t maxarg = AvFILL(av) + 1;
1994 if (PL_op->op_private & OPpTRUEBOOL)
1995 SETs(maxarg ? &PL_sv_yes : &PL_sv_zero);
2004 return S_padhv_rv2hv_common(aTHX_ (HV*)sv, gimme,
2005 cBOOL(PL_op->op_private & OPpRV2HV_ISKEYS),
2011 Perl_croak(aTHX_ "Can't return %s to lvalue scalar context",
2012 is_pp_rv2av ? "array" : "hash");
2017 S_do_oddball(pTHX_ SV **oddkey, SV **firstkey)
2019 PERL_ARGS_ASSERT_DO_ODDBALL;
2022 if (ckWARN(WARN_MISC)) {
2024 if (oddkey == firstkey &&
2026 (SvTYPE(SvRV(*oddkey)) == SVt_PVAV ||
2027 SvTYPE(SvRV(*oddkey)) == SVt_PVHV))
2029 err = "Reference found where even-sized list expected";
2032 err = "Odd number of elements in hash assignment";
2033 Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err);
2040 /* Do a mark and sweep with the SVf_BREAK flag to detect elements which
2041 * are common to both the LHS and RHS of an aassign, and replace them
2042 * with copies. All these copies are made before the actual list assign is
2045 * For example in ($a,$b) = ($b,$a), assigning the value of the first RHS
2046 * element ($b) to the first LH element ($a), modifies $a; when the
2047 * second assignment is done, the second RH element now has the wrong
2048 * value. So we initially replace the RHS with ($b, mortalcopy($a)).
2049 * Note that we don't need to make a mortal copy of $b.
2051 * The algorithm below works by, for every RHS element, mark the
2052 * corresponding LHS target element with SVf_BREAK. Then if the RHS
2053 * element is found with SVf_BREAK set, it means it would have been
2054 * modified, so make a copy.
2055 * Note that by scanning both LHS and RHS in lockstep, we avoid
2056 * unnecessary copies (like $b above) compared with a naive
2057 * "mark all LHS; copy all marked RHS; unmark all LHS".
2059 * If the LHS element is a 'my' declaration' and has a refcount of 1, then
2060 * it can't be common and can be skipped.
2062 * On DEBUGGING builds it takes an extra boolean, fake. If true, it means
2063 * that we thought we didn't need to call S_aassign_copy_common(), but we
2064 * have anyway for sanity checking. If we find we need to copy, then panic.
2067 PERL_STATIC_INLINE void
2068 S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem,
2069 SV **firstrelem, SV **lastrelem
2078 SSize_t lcount = lastlelem - firstlelem + 1;
2079 bool marked = FALSE; /* have we marked any LHS with SVf_BREAK ? */
2080 bool const do_rc1 = cBOOL(PL_op->op_private & OPpASSIGN_COMMON_RC1);
2081 bool copy_all = FALSE;
2083 assert(!PL_in_clean_all); /* SVf_BREAK not already in use */
2084 assert(firstlelem < lastlelem); /* at least 2 LH elements */
2085 assert(firstrelem < lastrelem); /* at least 2 RH elements */
2089 /* we never have to copy the first RH element; it can't be corrupted
2090 * by assigning something to the corresponding first LH element.
2091 * So this scan does in a loop: mark LHS[N]; test RHS[N+1]
2093 relem = firstrelem + 1;
2095 for (; relem <= lastrelem; relem++) {
2098 /* mark next LH element */
2100 if (--lcount >= 0) {
2103 if (UNLIKELY(!svl)) {/* skip AV alias marker */
2104 assert (lelem <= lastlelem);
2110 if (SvSMAGICAL(svl)) {
2113 if (SvTYPE(svl) == SVt_PVAV || SvTYPE(svl) == SVt_PVHV) {
2116 /* this LH element will consume all further args;
2117 * no need to mark any further LH elements (if any).
2118 * But we still need to scan any remaining RHS elements;
2119 * set lcount negative to distinguish from lcount == 0,
2120 * so the loop condition continues being true
2123 lelem--; /* no need to unmark this element */
2125 else if (!(do_rc1 && SvREFCNT(svl) == 1) && !SvIMMORTAL(svl)) {
2126 SvFLAGS(svl) |= SVf_BREAK;
2130 /* don't check RH element if no SVf_BREAK flags set yet */
2137 /* see if corresponding RH element needs copying */
2143 if (UNLIKELY(SvFLAGS(svr) & (SVf_BREAK|SVs_GMG) || copy_all)) {
2144 U32 brk = (SvFLAGS(svr) & SVf_BREAK);
2148 /* op_dump(PL_op); */
2150 "panic: aassign skipped needed copy of common RH elem %"
2151 UVuf, (UV)(relem - firstrelem));
2155 TAINT_NOT; /* Each item is independent */
2157 /* Dear TODO test in t/op/sort.t, I love you.
2158 (It's relying on a panic, not a "semi-panic" from newSVsv()
2159 and then an assertion failure below.) */
2160 if (UNLIKELY(SvIS_FREED(svr))) {
2161 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p",
2164 /* avoid break flag while copying; otherwise COW etc
2166 SvFLAGS(svr) &= ~SVf_BREAK;
2167 /* Not newSVsv(), as it does not allow copy-on-write,
2168 resulting in wasteful copies.
2169 Also, we use SV_NOSTEAL in case the SV is used more than
2170 once, e.g. (...) = (f())[0,0]
2171 Where the same SV appears twice on the RHS without a ref
2172 count bump. (Although I suspect that the SV won't be
2173 stealable here anyway - DAPM).
2175 *relem = sv_mortalcopy_flags(svr,
2176 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
2177 /* ... but restore afterwards in case it's needed again,
2178 * e.g. ($a,$b,$c) = (1,$a,$a)
2180 SvFLAGS(svr) |= brk;
2192 while (lelem > firstlelem) {
2193 SV * const svl = *(--lelem);
2195 SvFLAGS(svl) &= ~SVf_BREAK;
2204 SV **lastlelem = PL_stack_sp;
2205 SV **lastrelem = PL_stack_base + POPMARK;
2206 SV **firstrelem = PL_stack_base + POPMARK + 1;
2207 SV **firstlelem = lastrelem + 1;
2212 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
2213 * only need to save locally, not on the save stack */
2214 U16 old_delaymagic = PL_delaymagic;
2219 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
2221 /* If there's a common identifier on both sides we have to take
2222 * special care that assigning the identifier on the left doesn't
2223 * clobber a value on the right that's used later in the list.
2226 /* at least 2 LH and RH elements, or commonality isn't an issue */
2227 if (firstlelem < lastlelem && firstrelem < lastrelem) {
2228 for (relem = firstrelem+1; relem <= lastrelem; relem++) {
2229 if (SvGMAGICAL(*relem))
2232 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2233 if (*lelem && SvSMAGICAL(*lelem))
2236 if ( PL_op->op_private & (OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1) ) {
2237 if (PL_op->op_private & OPpASSIGN_COMMON_RC1) {
2238 /* skip the scan if all scalars have a ref count of 1 */
2239 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2241 if (!sv || SvREFCNT(sv) == 1)
2243 if (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVAV)
2250 S_aassign_copy_common(aTHX_
2251 firstlelem, lastlelem, firstrelem, lastrelem
2261 /* on debugging builds, do the scan even if we've concluded we
2262 * don't need to, then panic if we find commonality. Note that the
2263 * scanner assumes at least 2 elements */
2264 if (firstlelem < lastlelem && firstrelem < lastrelem) {
2275 if (relem > lastrelem)
2278 /* first lelem loop while there are still relems */
2279 while (LIKELY(lelem <= lastlelem)) {
2283 TAINT_NOT; /* Each item stands on its own, taintwise. */
2285 assert(relem <= lastrelem);
2286 if (UNLIKELY(!lsv)) {
2289 ASSUME(SvTYPE(lsv) == SVt_PVAV);
2292 switch (SvTYPE(lsv)) {
2297 SSize_t nelems = lastrelem - relem + 1;
2298 AV *ary = MUTABLE_AV(lsv);
2300 /* Assigning to an aggregate is tricky. First there is the
2301 * issue of commonality, e.g. @a = ($a[0]). Since the
2302 * stack isn't refcounted, clearing @a prior to storing
2303 * elements will free $a[0]. Similarly with
2304 * sub FETCH { $status[$_[1]] } @status = @tied[0,1];
2306 * The way to avoid these issues is to make the copy of each
2307 * SV (and we normally store a *copy* in the array) *before*
2308 * clearing the array. But this has a problem in that
2309 * if the code croaks during copying, the not-yet-stored copies
2310 * could leak. One way to avoid this is to make all the copies
2311 * mortal, but that's quite expensive.
2313 * The current solution to these issues is to use a chunk
2314 * of the tmps stack as a temporary refcounted-stack. SVs
2315 * will be put on there during processing to avoid leaks,
2316 * but will be removed again before the end of this block,
2317 * so free_tmps() is never normally called. Also, the
2318 * sv_refcnt of the SVs doesn't have to be manipulated, since
2319 * the ownership of 1 reference count is transferred directly
2320 * from the tmps stack to the AV when the SV is stored.
2322 * We disarm slots in the temps stack by storing PL_sv_undef
2323 * there: it doesn't matter if that SV's refcount is
2324 * repeatedly decremented during a croak. But usually this is
2325 * only an interim measure. By the end of this code block
2326 * we try where possible to not leave any PL_sv_undef's on the
2327 * tmps stack e.g. by shuffling newer entries down.
2329 * There is one case where we don't copy: non-magical
2330 * SvTEMP(sv)'s with a ref count of 1. The only owner of these
2331 * is on the tmps stack, so its safe to directly steal the SV
2332 * rather than copying. This is common in things like function
2333 * returns, map etc, which all return a list of such SVs.
2335 * Note however something like @a = (f())[0,0], where there is
2336 * a danger of the same SV being shared: this avoided because
2337 * when the SV is stored as $a[0], its ref count gets bumped,
2338 * so the RC==1 test fails and the second element is copied
2341 * We also use one slot in the tmps stack to hold an extra
2342 * ref to the array, to ensure it doesn't get prematurely
2343 * freed. Again, this is removed before the end of this block.
2345 * Note that OPpASSIGN_COMMON_AGG is used to flag a possible
2346 * @a = ($a[0]) case, but the current implementation uses the
2347 * same algorithm regardless, so ignores that flag. (It *is*
2348 * used in the hash branch below, however).
2351 /* Reserve slots for ary, plus the elems we're about to copy,
2352 * then protect ary and temporarily void the remaining slots
2353 * with &PL_sv_undef */
2354 EXTEND_MORTAL(nelems + 1);
2355 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(ary);
2356 tmps_base = PL_tmps_ix + 1;
2357 for (i = 0; i < nelems; i++)
2358 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2359 PL_tmps_ix += nelems;
2361 /* Make a copy of each RHS elem and save on the tmps_stack
2362 * (or pass through where we can optimise away the copy) */
2364 if (UNLIKELY(alias)) {
2365 U32 lval = (gimme == G_ARRAY)
2366 ? (PL_op->op_flags & OPf_MOD || LVRET) : 0;
2367 for (svp = relem; svp <= lastrelem; svp++) {
2372 DIE(aTHX_ "Assigned value is not a reference");
2373 if (SvTYPE(SvRV(rsv)) > SVt_PVLV)
2374 /* diag_listed_as: Assigned value is not %s reference */
2376 "Assigned value is not a SCALAR reference");
2378 *svp = rsv = sv_mortalcopy(rsv);
2379 /* XXX else check for weak refs? */
2380 rsv = SvREFCNT_inc_NN(SvRV(rsv));
2381 assert(tmps_base <= PL_tmps_max);
2382 PL_tmps_stack[tmps_base++] = rsv;
2386 for (svp = relem; svp <= lastrelem; svp++) {
2389 if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) {
2390 /* can skip the copy */
2391 SvREFCNT_inc_simple_void_NN(rsv);
2396 /* do get before newSV, in case it dies and leaks */
2399 /* see comment in S_aassign_copy_common about
2401 sv_setsv_flags(nsv, rsv,
2402 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
2406 assert(tmps_base <= PL_tmps_max);
2407 PL_tmps_stack[tmps_base++] = rsv;
2411 if (SvRMAGICAL(ary) || AvFILLp(ary) >= 0) /* may be non-empty */
2414 /* store in the array, the SVs that are in the tmps stack */
2416 tmps_base -= nelems;
2418 if (SvMAGICAL(ary) || SvREADONLY(ary) || !AvREAL(ary)) {
2419 /* for arrays we can't cheat with, use the official API */
2420 av_extend(ary, nelems - 1);
2421 for (i = 0; i < nelems; i++) {
2422 SV **svp = &(PL_tmps_stack[tmps_base + i]);
2424 /* A tied store won't take ownership of rsv, so keep
2425 * the 1 refcnt on the tmps stack; otherwise disarm
2426 * the tmps stack entry */
2427 if (av_store(ary, i, rsv))
2428 *svp = &PL_sv_undef;
2429 /* av_store() may have added set magic to rsv */;
2432 /* disarm ary refcount: see comments below about leak */
2433 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
2436 /* directly access/set the guts of the AV */
2437 SSize_t fill = nelems - 1;
2438 if (fill > AvMAX(ary))
2439 av_extend_guts(ary, fill, &AvMAX(ary), &AvALLOC(ary),
2441 AvFILLp(ary) = fill;
2442 Copy(&(PL_tmps_stack[tmps_base]), AvARRAY(ary), nelems, SV*);
2443 /* Quietly remove all the SVs from the tmps stack slots,
2444 * since ary has now taken ownership of the refcnt.
2445 * Also remove ary: which will now leak if we die before
2446 * the SvREFCNT_dec_NN(ary) below */
2447 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
2448 Move(&PL_tmps_stack[tmps_base + nelems],
2449 &PL_tmps_stack[tmps_base - 1],
2450 PL_tmps_ix - (tmps_base + nelems) + 1,
2452 PL_tmps_ix -= (nelems + 1);
2455 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
2456 /* its assumed @ISA set magic can't die and leak ary */
2457 SvSETMAGIC(MUTABLE_SV(ary));
2458 SvREFCNT_dec_NN(ary);
2460 relem = lastrelem + 1;
2464 case SVt_PVHV: { /* normal hash */
2470 SSize_t nelems = lastrelem - relem + 1;
2471 HV *hash = MUTABLE_HV(lsv);
2473 if (UNLIKELY(nelems & 1)) {
2474 do_oddball(lastrelem, relem);
2475 /* we have firstlelem to reuse, it's not needed any more */
2476 *++lastrelem = &PL_sv_undef;
2480 /* See the SVt_PVAV branch above for a long description of
2481 * how the following all works. The main difference for hashes
2482 * is that we treat keys and values separately (and have
2483 * separate loops for them): as for arrays, values are always
2484 * copied (except for the SvTEMP optimisation), since they
2485 * need to be stored in the hash; while keys are only
2486 * processed where they might get prematurely freed or
2489 /* tmps stack slots:
2490 * * reserve a slot for the hash keepalive;
2491 * * reserve slots for the hash values we're about to copy;
2492 * * preallocate for the keys we'll possibly copy or refcount bump
2494 * then protect hash and temporarily void the remaining
2495 * value slots with &PL_sv_undef */
2496 EXTEND_MORTAL(nelems + 1);
2498 /* convert to number of key/value pairs */
2501 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hash);
2502 tmps_base = PL_tmps_ix + 1;
2503 for (i = 0; i < nelems; i++)
2504 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2505 PL_tmps_ix += nelems;
2507 /* Make a copy of each RHS hash value and save on the tmps_stack
2508 * (or pass through where we can optimise away the copy) */
2510 for (svp = relem + 1; svp <= lastrelem; svp += 2) {
2513 if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) {
2514 /* can skip the copy */
2515 SvREFCNT_inc_simple_void_NN(rsv);
2520 /* do get before newSV, in case it dies and leaks */
2523 /* see comment in S_aassign_copy_common about
2525 sv_setsv_flags(nsv, rsv,
2526 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
2530 assert(tmps_base <= PL_tmps_max);
2531 PL_tmps_stack[tmps_base++] = rsv;
2533 tmps_base -= nelems;
2536 /* possibly protect keys */
2538 if (UNLIKELY(gimme == G_ARRAY)) {
2540 * @a = ((%h = ($$r, 1)), $r = "x");
2541 * $_++ for %h = (1,2,3,4);
2543 EXTEND_MORTAL(nelems);
2544 for (svp = relem; svp <= lastrelem; svp += 2)
2545 *svp = sv_mortalcopy_flags(*svp,
2546 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
2548 else if (PL_op->op_private & OPpASSIGN_COMMON_AGG) {
2549 /* for possible commonality, e.g.
2551 * avoid premature freeing RHS keys by mortalising
2553 * For a magic element, make a copy so that its magic is
2554 * called *before* the hash is emptied (which may affect
2555 * a tied value for example).
2556 * In theory we should check for magic keys in all
2557 * cases, not just under OPpASSIGN_COMMON_AGG, but in
2558 * practice, !OPpASSIGN_COMMON_AGG implies only
2559 * constants or padtmps on the RHS.
2561 EXTEND_MORTAL(nelems);
2562 for (svp = relem; svp <= lastrelem; svp += 2) {
2564 if (UNLIKELY(SvGMAGICAL(rsv))) {
2566 *svp = sv_mortalcopy_flags(*svp,
2567 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
2568 /* allow other branch to continue pushing
2569 * onto tmps stack without checking each time */
2570 n = (lastrelem - relem) >> 1;
2574 PL_tmps_stack[++PL_tmps_ix] =
2575 SvREFCNT_inc_simple_NN(rsv);
2579 if (SvRMAGICAL(hash) || HvUSEDKEYS(hash))
2582 /* now assign the keys and values to the hash */
2586 if (UNLIKELY(gimme == G_ARRAY)) {
2587 /* @a = (%h = (...)) etc */
2589 SV **topelem = relem;
2591 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
2594 /* remove duplicates from list we return */
2595 if (!hv_exists_ent(hash, key, 0)) {
2596 /* copy key back: possibly to an earlier
2597 * stack location if we encountered dups earlier,
2598 * The values will be updated later
2603 /* A tied store won't take ownership of val, so keep
2604 * the 1 refcnt on the tmps stack; otherwise disarm
2605 * the tmps stack entry */
2606 if (hv_store_ent(hash, key, val, 0))
2607 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2610 /* hv_store_ent() may have added set magic to val */;
2613 if (topelem < svp) {
2614 /* at this point we have removed the duplicate key/value
2615 * pairs from the stack, but the remaining values may be
2616 * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed
2617 * the (a 2), but the stack now probably contains
2618 * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) }
2619 * obliterates the earlier key. So refresh all values. */
2620 lastrelem = topelem - 1;
2621 while (relem < lastrelem) {
2623 he = hv_fetch_ent(hash, *relem++, 0, 0);
2624 *relem++ = (he ? HeVAL(he) : &PL_sv_undef);
2630 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
2633 if (hv_store_ent(hash, key, val, 0))
2634 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2637 /* hv_store_ent() may have added set magic to val */;
2643 /* there are still some 'live' recounts on the tmps stack
2644 * - usually caused by storing into a tied hash. So let
2645 * free_tmps() do the proper but slow job later.
2646 * Just disarm hash refcount: see comments below about leak
2648 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
2651 /* Quietly remove all the SVs from the tmps stack slots,
2652 * since hash has now taken ownership of the refcnt.
2653 * Also remove hash: which will now leak if we die before
2654 * the SvREFCNT_dec_NN(hash) below */
2655 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
2656 Move(&PL_tmps_stack[tmps_base + nelems],
2657 &PL_tmps_stack[tmps_base - 1],
2658 PL_tmps_ix - (tmps_base + nelems) + 1,
2660 PL_tmps_ix -= (nelems + 1);
2663 SvREFCNT_dec_NN(hash);
2665 relem = lastrelem + 1;
2670 if (!SvIMMORTAL(lsv)) {
2674 SvTEMP(lsv) && !SvSMAGICAL(lsv) && SvREFCNT(lsv) == 1 &&
2675 (!isGV_with_GP(lsv) || SvFAKE(lsv)) && ckWARN(WARN_MISC)
2678 packWARN(WARN_MISC),
2679 "Useless assignment to a temporary"
2682 /* avoid freeing $$lsv if it might be needed for further
2683 * elements, e.g. ($ref, $foo) = (1, $$ref) */
2685 && ( ((ref = SvRV(lsv)), SvREFCNT(ref)) == 1)
2686 && lelem <= lastlelem
2689 SvREFCNT_inc_simple_void_NN(ref);
2690 /* an unrolled sv_2mortal */
2692 if (UNLIKELY(ix >= PL_tmps_max))
2693 /* speculatively grow enough to cover other
2695 (void)tmps_grow_p(ix + (lastlelem - lelem));
2696 PL_tmps_stack[ix] = ref;
2699 sv_setsv(lsv, *relem);
2703 if (++relem > lastrelem)
2712 /* simplified lelem loop for when there are no relems left */
2713 while (LIKELY(lelem <= lastlelem)) {
2716 TAINT_NOT; /* Each item stands on its own, taintwise. */
2718 if (UNLIKELY(!lsv)) {
2720 ASSUME(SvTYPE(lsv) == SVt_PVAV);
2723 switch (SvTYPE(lsv)) {
2725 if (SvRMAGICAL(lsv) || AvFILLp((SV*)lsv) >= 0) {
2727 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
2733 if (SvRMAGICAL(lsv) || HvUSEDKEYS((HV*)lsv))
2738 if (!SvIMMORTAL(lsv)) {
2747 TAINT_NOT; /* result of list assign isn't tainted */
2749 if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) {
2750 /* Will be used to set PL_tainting below */
2751 Uid_t tmp_uid = PerlProc_getuid();
2752 Uid_t tmp_euid = PerlProc_geteuid();
2753 Gid_t tmp_gid = PerlProc_getgid();
2754 Gid_t tmp_egid = PerlProc_getegid();
2756 /* XXX $> et al currently silently ignore failures */
2757 if (PL_delaymagic & DM_UID) {
2758 #ifdef HAS_SETRESUID
2760 setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
2761 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
2763 #elif defined(HAS_SETREUID)
2765 setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
2766 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1));
2769 if ((PL_delaymagic & DM_UID) == DM_RUID) {
2770 PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid));
2771 PL_delaymagic &= ~DM_RUID;
2773 # endif /* HAS_SETRUID */
2775 if ((PL_delaymagic & DM_UID) == DM_EUID) {
2776 PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid));
2777 PL_delaymagic &= ~DM_EUID;
2779 # endif /* HAS_SETEUID */
2780 if (PL_delaymagic & DM_UID) {
2781 if (PL_delaymagic_uid != PL_delaymagic_euid)
2782 DIE(aTHX_ "No setreuid available");
2783 PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
2785 #endif /* HAS_SETRESUID */
2787 tmp_uid = PerlProc_getuid();
2788 tmp_euid = PerlProc_geteuid();
2790 /* XXX $> et al currently silently ignore failures */
2791 if (PL_delaymagic & DM_GID) {
2792 #ifdef HAS_SETRESGID
2794 setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
2795 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
2797 #elif defined(HAS_SETREGID)
2799 setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
2800 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1));
2803 if ((PL_delaymagic & DM_GID) == DM_RGID) {
2804 PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid));
2805 PL_delaymagic &= ~DM_RGID;
2807 # endif /* HAS_SETRGID */
2809 if ((PL_delaymagic & DM_GID) == DM_EGID) {
2810 PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid));
2811 PL_delaymagic &= ~DM_EGID;
2813 # endif /* HAS_SETEGID */
2814 if (PL_delaymagic & DM_GID) {
2815 if (PL_delaymagic_gid != PL_delaymagic_egid)
2816 DIE(aTHX_ "No setregid available");
2817 PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
2819 #endif /* HAS_SETRESGID */
2821 tmp_gid = PerlProc_getgid();
2822 tmp_egid = PerlProc_getegid();
2824 TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
2825 #ifdef NO_TAINT_SUPPORT
2826 PERL_UNUSED_VAR(tmp_uid);
2827 PERL_UNUSED_VAR(tmp_euid);
2828 PERL_UNUSED_VAR(tmp_gid);
2829 PERL_UNUSED_VAR(tmp_egid);
2832 PL_delaymagic = old_delaymagic;
2834 if (gimme == G_VOID)
2835 SP = firstrelem - 1;
2836 else if (gimme == G_SCALAR) {
2839 if (PL_op->op_private & OPpASSIGN_TRUEBOOL)
2840 SETs((firstlelem - firstrelem) ? &PL_sv_yes : &PL_sv_zero);
2843 SETi(firstlelem - firstrelem);
2855 PMOP * const pm = cPMOP;
2856 REGEXP * rx = PM_GETRE(pm);
2857 regexp *prog = ReANY(rx);
2858 SV * const pkg = RXp_ENGINE(prog)->qr_package(aTHX_ (rx));
2859 SV * const rv = sv_newmortal();
2863 SvUPGRADE(rv, SVt_IV);
2864 /* For a subroutine describing itself as "This is a hacky workaround" I'm
2865 loathe to use it here, but it seems to be the right fix. Or close.
2866 The key part appears to be that it's essential for pp_qr to return a new
2867 object (SV), which implies that there needs to be an effective way to
2868 generate a new SV from the existing SV that is pre-compiled in the
2870 SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx)));
2873 cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv);
2874 if (UNLIKELY((cv = *cvp) && CvCLONE(*cvp))) {
2875 *cvp = cv_clone(cv);
2876 SvREFCNT_dec_NN(cv);
2880 HV *const stash = gv_stashsv(pkg, GV_ADD);
2881 SvREFCNT_dec_NN(pkg);
2882 (void)sv_bless(rv, stash);
2885 if (UNLIKELY(RXp_ISTAINTED(prog))) {
2887 SvTAINTED_on(SvRV(rv));
2894 S_are_we_in_Debug_EXECUTE_r(pTHX)
2896 /* Given a 'use re' is in effect, does it ask for outputting execution
2899 * This is separated from the sole place it's called, an inline function,
2900 * because it is the large-ish slow portion of the function */
2902 DECLARE_AND_GET_RE_DEBUG_FLAGS_NON_REGEX;
2904 return cBOOL(RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MASK));
2907 PERL_STATIC_INLINE bool
2908 S_should_we_output_Debug_r(pTHX_ regexp *prog)
2910 PERL_ARGS_ASSERT_SHOULD_WE_OUTPUT_DEBUG_R;
2912 /* pp_match can output regex debugging info. This function returns a
2913 * boolean as to whether or not it should.
2915 * Under -Dr, it should. Any reasonable compiler will optimize this bit of
2916 * code away on non-debugging builds. */
2917 if (UNLIKELY(DEBUG_r_TEST)) {
2921 /* If the regex engine is using the non-debugging execution routine, then
2922 * no debugging should be output. Same if the field is NULL that pluggable
2923 * engines are not supposed to fill. */
2924 if ( LIKELY(prog->engine->exec == &Perl_regexec_flags)
2925 || UNLIKELY(prog->engine->op_comp == NULL))
2930 /* Otherwise have to check */
2931 return S_are_we_in_Debug_EXECUTE_r(aTHX);
2941 SSize_t curpos = 0; /* initial pos() or current $+[0] */
2944 const char *truebase; /* Start of string */
2945 REGEXP *rx = PM_GETRE(pm);
2946 regexp *prog = ReANY(rx);
2948 const U8 gimme = GIMME_V;
2950 const I32 oldsave = PL_savestack_ix;
2951 I32 had_zerolen = 0;
2954 if (PL_op->op_flags & OPf_STACKED)
2965 PUTBACK; /* EVAL blocks need stack_sp. */
2966 /* Skip get-magic if this is a qr// clone, because regcomp has
2968 truebase = prog->mother_re
2969 ? SvPV_nomg_const(TARG, len)
2970 : SvPV_const(TARG, len);
2972 DIE(aTHX_ "panic: pp_match");
2973 strend = truebase + len;
2974 rxtainted = (RXp_ISTAINTED(prog) ||
2975 (TAINT_get && (pm->op_pmflags & PMf_RETAINT)));
2978 /* We need to know this in case we fail out early - pos() must be reset */
2979 global = dynpm->op_pmflags & PMf_GLOBAL;
2981 /* PMdf_USED is set after a ?? matches once */
2984 SvREADONLY(PL_regex_pad[pm->op_pmoffset])
2986 pm->op_pmflags & PMf_USED
2989 if (UNLIKELY(should_we_output_Debug_r(prog))) {
2990 PerlIO_printf(Perl_debug_log, "?? already matched once");
2995 /* handle the empty pattern */
2996 if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
2997 if (PL_curpm == PL_reg_curpm) {
2998 if (PL_curpm_under) {
2999 if (PL_curpm_under == PL_reg_curpm) {
3000 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
3002 pm = PL_curpm_under;
3012 if (RXp_MINLEN(prog) >= 0 && (STRLEN)RXp_MINLEN(prog) > len) {
3013 if (UNLIKELY(should_we_output_Debug_r(prog))) {
3014 PerlIO_printf(Perl_debug_log,
3015 "String shorter than min possible regex match (%zd < %zd)\n",
3016 len, RXp_MINLEN(prog));
3021 /* get pos() if //g */
3023 mg = mg_find_mglob(TARG);
3024 if (mg && mg->mg_len >= 0) {
3025 curpos = MgBYTEPOS(mg, TARG, truebase, len);
3026 /* last time pos() was set, it was zero-length match */
3027 if (mg->mg_flags & MGf_MINMATCH)
3032 #ifdef PERL_SAWAMPERSAND
3033 if ( RXp_NPARENS(prog)
3035 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
3036 || (dynpm->op_pmflags & PMf_KEEPCOPY)
3040 r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE);
3041 /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer
3042 * only on the first iteration. Therefore we need to copy $' as well
3043 * as $&, to make the rest of the string available for captures in
3044 * subsequent iterations */
3045 if (! (global && gimme == G_ARRAY))
3046 r_flags |= REXEC_COPY_SKIP_POST;
3048 #ifdef PERL_SAWAMPERSAND
3049 if (dynpm->op_pmflags & PMf_KEEPCOPY)
3050 /* handle KEEPCOPY in pmop but not rx, eg $r=qr/a/; /$r/p */
3051 r_flags &= ~(REXEC_COPY_SKIP_PRE|REXEC_COPY_SKIP_POST);
3058 s = truebase + curpos;
3060 if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase,
3061 had_zerolen, TARG, NULL, r_flags))
3065 if (dynpm->op_pmflags & PMf_ONCE)
3067 SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]);
3069 dynpm->op_pmflags |= PMf_USED;
3073 RXp_MATCH_TAINTED_on(prog);
3074 TAINT_IF(RXp_MATCH_TAINTED(prog));
3078 if (global && (gimme != G_ARRAY || (dynpm->op_pmflags & PMf_CONTINUE))) {
3080 mg = sv_magicext_mglob(TARG);
3081 MgBYTEPOS_set(mg, TARG, truebase, RXp_OFFS(prog)[0].end);
3082 if (RXp_ZERO_LEN(prog))
3083 mg->mg_flags |= MGf_MINMATCH;
3085 mg->mg_flags &= ~MGf_MINMATCH;
3088 if ((!RXp_NPARENS(prog) && !global) || gimme != G_ARRAY) {
3089 LEAVE_SCOPE(oldsave);
3093 /* push captures on stack */
3096 const I32 nparens = RXp_NPARENS(prog);
3097 I32 i = (global && !nparens) ? 1 : 0;
3099 SPAGAIN; /* EVAL blocks could move the stack. */
3100 EXTEND(SP, nparens + i);
3101 EXTEND_MORTAL(nparens + i);
3102 for (i = !i; i <= nparens; i++) {
3103 PUSHs(sv_newmortal());
3104 if (LIKELY((RXp_OFFS(prog)[i].start != -1)
3105 && RXp_OFFS(prog)[i].end != -1 ))
3107 const I32 len = RXp_OFFS(prog)[i].end - RXp_OFFS(prog)[i].start;
3108 const char * const s = RXp_OFFS(prog)[i].start + truebase;
3109 if (UNLIKELY( RXp_OFFS(prog)[i].end < 0
3110 || RXp_OFFS(prog)[i].start < 0
3112 || len > strend - s)
3114 DIE(aTHX_ "panic: pp_match start/end pointers, i=%ld, "
3115 "start=%ld, end=%ld, s=%p, strend=%p, len=%" UVuf,
3116 (long) i, (long) RXp_OFFS(prog)[i].start,
3117 (long)RXp_OFFS(prog)[i].end, s, strend, (UV) len);
3118 sv_setpvn(*SP, s, len);
3119 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
3124 curpos = (UV)RXp_OFFS(prog)[0].end;
3125 had_zerolen = RXp_ZERO_LEN(prog);
3126 PUTBACK; /* EVAL blocks may use stack */
3127 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
3130 LEAVE_SCOPE(oldsave);
3133 NOT_REACHED; /* NOTREACHED */
3136 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
3138 mg = mg_find_mglob(TARG);
3142 LEAVE_SCOPE(oldsave);
3143 if (gimme == G_ARRAY)
3149 Perl_do_readline(pTHX)
3151 dSP; dTARGETSTACKED;
3156 IO * const io = GvIO(PL_last_in_gv);
3157 const I32 type = PL_op->op_type;
3158 const U8 gimme = GIMME_V;
3161 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
3163 Perl_tied_method(aTHX_ SV_CONST(READLINE), SP, MUTABLE_SV(io), mg, gimme, 0);
3164 if (gimme == G_SCALAR) {
3166 SvSetSV_nosteal(TARG, TOPs);
3176 if (IoFLAGS(io) & IOf_ARGV) {
3177 if (IoFLAGS(io) & IOf_START) {
3179 if (av_tindex(GvAVn(PL_last_in_gv)) < 0) {
3180 IoFLAGS(io) &= ~IOf_START;
3181 do_open6(PL_last_in_gv, "-", 1, NULL, NULL, 0);
3182 SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */
3183 sv_setpvs(GvSVn(PL_last_in_gv), "-");
3184 SvSETMAGIC(GvSV(PL_last_in_gv));
3189 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
3190 if (!fp) { /* Note: fp != IoIFP(io) */
3191 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
3194 else if (type == OP_GLOB)
3195 fp = Perl_start_glob(aTHX_ POPs, io);
3197 else if (type == OP_GLOB)
3199 else if (IoTYPE(io) == IoTYPE_WRONLY) {
3200 report_wrongway_fh(PL_last_in_gv, '>');
3204 if ((!io || !(IoFLAGS(io) & IOf_START))
3205 && ckWARN(WARN_CLOSED)
3208 report_evil_fh(PL_last_in_gv);
3210 if (gimme == G_SCALAR) {
3211 /* undef TARG, and push that undefined value */
3212 if (type != OP_RCATLINE) {
3220 if (gimme == G_SCALAR) {
3222 if (type == OP_RCATLINE && SvGMAGICAL(sv))
3225 if (type == OP_RCATLINE)
3226 SvPV_force_nomg_nolen(sv);
3230 else if (isGV_with_GP(sv)) {
3231 SvPV_force_nomg_nolen(sv);
3233 SvUPGRADE(sv, SVt_PV);
3234 tmplen = SvLEN(sv); /* remember if already alloced */
3235 if (!tmplen && !SvREADONLY(sv) && !SvIsCOW(sv)) {
3236 /* try short-buffering it. Please update t/op/readline.t
3237 * if you change the growth length.
3242 if (type == OP_RCATLINE && SvOK(sv)) {
3244 SvPV_force_nomg_nolen(sv);
3250 sv = sv_2mortal(newSV(80));
3254 /* This should not be marked tainted if the fp is marked clean */
3255 #define MAYBE_TAINT_LINE(io, sv) \
3256 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
3261 /* delay EOF state for a snarfed empty file */
3262 #define SNARF_EOF(gimme,rs,io,sv) \
3263 (gimme != G_SCALAR || SvCUR(sv) \
3264 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
3268 if (!sv_gets(sv, fp, offset)
3270 || SNARF_EOF(gimme, PL_rs, io, sv)
3271 || PerlIO_error(fp)))
3273 PerlIO_clearerr(fp);
3274 if (IoFLAGS(io) & IOf_ARGV) {
3275 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
3278 (void)do_close(PL_last_in_gv, FALSE);
3280 else if (type == OP_GLOB) {
3281 if (!do_close(PL_last_in_gv, FALSE)) {
3282 Perl_ck_warner(aTHX_ packWARN(WARN_GLOB),
3283 "glob failed (child exited with status %d%s)",
3284 (int)(STATUS_CURRENT >> 8),
3285 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
3288 if (gimme == G_SCALAR) {
3289 if (type != OP_RCATLINE) {
3290 SV_CHECK_THINKFIRST_COW_DROP(TARG);
3296 MAYBE_TAINT_LINE(io, sv);
3299 MAYBE_TAINT_LINE(io, sv);
3301 IoFLAGS(io) |= IOf_NOLINE;
3305 if (type == OP_GLOB) {
3309 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
3310 char * const tmps = SvEND(sv) - 1;
3311 if (*tmps == *SvPVX_const(PL_rs)) {
3313 SvCUR_set(sv, SvCUR(sv) - 1);
3316 for (t1 = SvPVX_const(sv); *t1; t1++)
3318 if (memCHRs("*%?", *t1))
3320 if (memCHRs("$&*(){}[]'\";\\|?<>~`", *t1))
3323 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &statbuf) < 0) {
3324 (void)POPs; /* Unmatched wildcard? Chuck it... */
3327 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
3328 if (ckWARN(WARN_UTF8)) {
3329 const U8 * const s = (const U8*)SvPVX_const(sv) + offset;
3330 const STRLEN len = SvCUR(sv) - offset;
3333 if (!is_utf8_string_loc(s, len, &f))
3334 /* Emulate :encoding(utf8) warning in the same case. */
3335 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3336 "utf8 \"\\x%02X\" does not map to Unicode",
3337 f < (U8*)SvEND(sv) ? *f : 0);
3340 if (gimme == G_ARRAY) {
3341 if (SvLEN(sv) - SvCUR(sv) > 20) {
3342 SvPV_shrink_to_cur(sv);
3344 sv = sv_2mortal(newSV(80));
3347 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
3348 /* try to reclaim a bit of scalar space (only on 1st alloc) */
3349 const STRLEN new_len
3350 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
3351 SvPV_renew(sv, new_len);
3362 SV * const keysv = POPs;
3363 HV * const hv = MUTABLE_HV(POPs);
3364 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3365 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
3367 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3368 bool preeminent = TRUE;
3370 if (SvTYPE(hv) != SVt_PVHV)
3377 /* If we can determine whether the element exist,
3378 * Try to preserve the existenceness of a tied hash
3379 * element by using EXISTS and DELETE if possible.
3380 * Fallback to FETCH and STORE otherwise. */
3381 if (SvCANEXISTDELETE(hv))
3382 preeminent = hv_exists_ent(hv, keysv, 0);
3385 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
3386 svp = he ? &HeVAL(he) : NULL;
3388 if (!svp || !*svp || *svp == &PL_sv_undef) {
3392 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
3394 lv = sv_newmortal();
3395 sv_upgrade(lv, SVt_PVLV);
3397 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0);
3398 SvREFCNT_dec_NN(key2); /* sv_magic() increments refcount */
3399 LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
3405 if (HvNAME_get(hv) && isGV_or_RVCV(*svp))
3406 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
3407 else if (preeminent)
3408 save_helem_flags(hv, keysv, svp,
3409 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
3411 SAVEHDELETE(hv, keysv);
3413 else if (PL_op->op_private & OPpDEREF) {
3414 PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
3418 sv = (svp && *svp ? *svp : &PL_sv_undef);
3419 /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this
3420 * was to make C<local $tied{foo} = $tied{foo}> possible.
3421 * However, it seems no longer to be needed for that purpose, and
3422 * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g>
3423 * would loop endlessly since the pos magic is getting set on the
3424 * mortal copy and lost. However, the copy has the effect of
3425 * triggering the get magic, and losing it altogether made things like
3426 * c<$tied{foo};> in void context no longer do get magic, which some
3427 * code relied on. Also, delayed triggering of magic on @+ and friends
3428 * meant the original regex may be out of scope by now. So as a
3429 * compromise, do the get magic here. (The MGf_GSKIP flag will stop it
3430 * being called too many times). */
3431 if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv))
3438 /* a stripped-down version of Perl_softref2xv() for use by
3439 * pp_multideref(), which doesn't use PL_op->op_flags */
3442 S_softref2xv_lite(pTHX_ SV *const sv, const char *const what,
3445 if (PL_op->op_private & HINT_STRICT_REFS) {
3447 Perl_die(aTHX_ PL_no_symref_sv, sv,
3448 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
3450 Perl_die(aTHX_ PL_no_usym, what);
3453 Perl_die(aTHX_ PL_no_usym, what);
3454 return gv_fetchsv_nomg(sv, GV_ADD, type);
3458 /* Handle one or more aggregate derefs and array/hash indexings, e.g.
3459 * $h->{foo} or $a[0]{$key}[$i] or f()->[1]
3461 * op_aux points to an array of unions of UV / IV / SV* / PADOFFSET.
3462 * Each of these either contains a set of actions, or an argument, such as
3463 * an IV to use as an array index, or a lexical var to retrieve.
3464 * Several actions re stored per UV; we keep shifting new actions off the
3465 * one UV, and only reload when it becomes zero.
3470 SV *sv = NULL; /* init to avoid spurious 'may be used uninitialized' */
3471 UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux;
3472 UV actions = items->uv;
3475 /* this tells find_uninit_var() where we're up to */
3476 PL_multideref_pc = items;
3479 /* there are three main classes of action; the first retrieve
3480 * the initial AV or HV from a variable or the stack; the second
3481 * does the equivalent of an unrolled (/DREFAV, rv2av, aelem),
3482 * the third an unrolled (/DREFHV, rv2hv, helem).
3484 switch (actions & MDEREF_ACTION_MASK) {
3487 actions = (++items)->uv;
3490 case MDEREF_AV_padav_aelem: /* $lex[...] */
3491 sv = PAD_SVl((++items)->pad_offset);
3494 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
3495 sv = UNOP_AUX_item_sv(++items);
3496 assert(isGV_with_GP(sv));
3497 sv = (SV*)GvAVn((GV*)sv);
3500 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
3505 goto do_AV_rv2av_aelem;
3508 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
3509 sv = UNOP_AUX_item_sv(++items);
3510 assert(isGV_with_GP(sv));
3511 sv = GvSVn((GV*)sv);
3512 goto do_AV_vivify_rv2av_aelem;
3514 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
3515 sv = PAD_SVl((++items)->pad_offset);
3518 do_AV_vivify_rv2av_aelem:
3519 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
3520 /* this is the OPpDEREF action normally found at the end of
3521 * ops like aelem, helem, rv2sv */
3522 sv = vivify_ref(sv, OPpDEREF_AV);
3526 /* this is basically a copy of pp_rv2av when it just has the
3529 if (LIKELY(SvROK(sv))) {
3530 if (UNLIKELY(SvAMAGIC(sv))) {
3531 sv = amagic_deref_call(sv, to_av_amg);
3534 if (UNLIKELY(SvTYPE(sv) != SVt_PVAV))
3535 DIE(aTHX_ "Not an ARRAY reference");
3537 else if (SvTYPE(sv) != SVt_PVAV) {
3538 if (!isGV_with_GP(sv))
3539 sv = (SV*)S_softref2xv_lite(aTHX_ sv, "an ARRAY", SVt_PVAV);
3540 sv = MUTABLE_SV(GvAVn((GV*)sv));
3546 /* retrieve the key; this may be either a lexical or package
3547 * var (whose index/ptr is stored as an item) or a signed
3548 * integer constant stored as an item.
3551 IV elem = 0; /* to shut up stupid compiler warnings */
3554 assert(SvTYPE(sv) == SVt_PVAV);
3556 switch (actions & MDEREF_INDEX_MASK) {
3557 case MDEREF_INDEX_none:
3559 case MDEREF_INDEX_const:
3560 elem = (++items)->iv;
3562 case MDEREF_INDEX_padsv:
3563 elemsv = PAD_SVl((++items)->pad_offset);
3565 case MDEREF_INDEX_gvsv:
3566 elemsv = UNOP_AUX_item_sv(++items);
3567 assert(isGV_with_GP(elemsv));
3568 elemsv = GvSVn((GV*)elemsv);
3570 if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv)
3571 && ckWARN(WARN_MISC)))
3572 Perl_warner(aTHX_ packWARN(WARN_MISC),
3573 "Use of reference \"%" SVf "\" as array index",
3575 /* the only time that S_find_uninit_var() needs this
3576 * is to determine which index value triggered the
3577 * undef warning. So just update it here. Note that
3578 * since we don't save and restore this var (e.g. for
3579 * tie or overload execution), its value will be
3580 * meaningless apart from just here */
3581 PL_multideref_pc = items;
3582 elem = SvIV(elemsv);
3587 /* this is basically a copy of pp_aelem with OPpDEREF skipped */
3589 if (!(actions & MDEREF_FLAG_last)) {
3590 SV** svp = av_fetch((AV*)sv, elem, 1);
3591 if (!svp || ! (sv=*svp))
3592 DIE(aTHX_ PL_no_aelem, elem);
3596 if (PL_op->op_private &
3597 (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
3599 if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
3600 sv = av_exists((AV*)sv, elem) ? &PL_sv_yes : &PL_sv_no;
3603 I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
3604 sv = av_delete((AV*)sv, elem, discard);
3612 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3613 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
3614 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3615 bool preeminent = TRUE;
3616 AV *const av = (AV*)sv;
3619 if (UNLIKELY(localizing)) {
3623 /* If we can determine whether the element exist,
3624 * Try to preserve the existenceness of a tied array
3625 * element by using EXISTS and DELETE if possible.
3626 * Fallback to FETCH and STORE otherwise. */
3627 if (SvCANEXISTDELETE(av))
3628 preeminent = av_exists(av, elem);
3631 svp = av_fetch(av, elem, lval && !defer);
3634 if (!svp || !(sv = *svp)) {
3637 DIE(aTHX_ PL_no_aelem, elem);
3638 len = av_tindex(av);
3639 /* Resolve a negative index that falls within
3640 * the array. Leave it negative it if falls
3641 * outside the array. */
3642 if (elem < 0 && len + elem >= 0)
3644 if (elem >= 0 && elem <= len)
3645 /* Falls within the array. */
3646 sv = av_nonelem(av,elem);
3648 /* Falls outside the array. If it is neg-
3649 ative, magic_setdefelem will use the
3650 index for error reporting. */
3651 sv = sv_2mortal(newSVavdefelem(av,elem,1));
3654 if (UNLIKELY(localizing)) {
3656 save_aelem(av, elem, svp);
3657 sv = *svp; /* may have changed */
3660 SAVEADELETE(av, elem);
3665 sv = (svp ? *svp : &PL_sv_undef);
3666 /* see note in pp_helem() */
3667 if (SvRMAGICAL(av) && SvGMAGICAL(sv))
3684 case MDEREF_HV_padhv_helem: /* $lex{...} */
3685 sv = PAD_SVl((++items)->pad_offset);
3688 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
3689 sv = UNOP_AUX_item_sv(++items);
3690 assert(isGV_with_GP(sv));
3691 sv = (SV*)GvHVn((GV*)sv);
3694 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
3699 goto do_HV_rv2hv_helem;
3702 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
3703 sv = UNOP_AUX_item_sv(++items);
3704 assert(isGV_with_GP(sv));
3705 sv = GvSVn((GV*)sv);
3706 goto do_HV_vivify_rv2hv_helem;
3708 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
3709 sv = PAD_SVl((++items)->pad_offset);
3712 do_HV_vivify_rv2hv_helem:
3713 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
3714 /* this is the OPpDEREF action normally found at the end of
3715 * ops like aelem, helem, rv2sv */
3716 sv = vivify_ref(sv, OPpDEREF_HV);
3720 /* this is basically a copy of pp_rv2hv when it just has the
3721 * sKR/1 flags (and pp_rv2hv is aliased to pp_rv2av) */
3724 if (LIKELY(SvROK(sv))) {
3725 if (UNLIKELY(SvAMAGIC(sv))) {
3726 sv = amagic_deref_call(sv, to_hv_amg);
3729 if (UNLIKELY(SvTYPE(sv) != SVt_PVHV))
3730 DIE(aTHX_ "Not a HASH reference");
3732 else if (SvTYPE(sv) != SVt_PVHV) {
3733 if (!isGV_with_GP(sv))
3734 sv = (SV*)S_softref2xv_lite(aTHX_ sv, "a HASH", SVt_PVHV);
3735 sv = MUTABLE_SV(GvHVn((GV*)sv));
3741 /* retrieve the key; this may be either a lexical / package
3742 * var or a string constant, whose index/ptr is stored as an
3745 SV *keysv = NULL; /* to shut up stupid compiler warnings */
3747 assert(SvTYPE(sv) == SVt_PVHV);
3749 switch (actions & MDEREF_INDEX_MASK) {
3750 case MDEREF_INDEX_none:
3753 case MDEREF_INDEX_const:
3754 keysv = UNOP_AUX_item_sv(++items);
3757 case MDEREF_INDEX_padsv:
3758 keysv = PAD_SVl((++items)->pad_offset);
3761 case MDEREF_INDEX_gvsv:
3762 keysv = UNOP_AUX_item_sv(++items);
3763 keysv = GvSVn((GV*)keysv);
3767 /* see comment above about setting this var */
3768 PL_multideref_pc = items;
3771 /* ensure that candidate CONSTs have been HEKified */
3772 assert( ((actions & MDEREF_INDEX_MASK) != MDEREF_INDEX_const)
3773 || SvTYPE(keysv) >= SVt_PVMG
3776 || SvIsCOW_shared_hash(keysv));
3778 /* this is basically a copy of pp_helem with OPpDEREF skipped */
3780 if (!(actions & MDEREF_FLAG_last)) {
3781 HE *he = hv_fetch_ent((HV*)sv, keysv, 1, 0);
3782 if (!he || !(sv=HeVAL(he)) || sv == &PL_sv_undef)
3783 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
3787 if (PL_op->op_private &
3788 (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
3790 if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
3791 sv = hv_exists_ent((HV*)sv, keysv, 0)
3792 ? &PL_sv_yes : &PL_sv_no;
3795 I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
3796 sv = hv_delete_ent((HV*)sv, keysv, discard, 0);
3804 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3805 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
3806 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3807 bool preeminent = TRUE;
3809 HV * const hv = (HV*)sv;
3812 if (UNLIKELY(localizing)) {
3816 /* If we can determine whether the element exist,
3817 * Try to preserve the existenceness of a tied hash
3818 * element by using EXISTS and DELETE if possible.
3819 * Fallback to FETCH and STORE otherwise. */
3820 if (SvCANEXISTDELETE(hv))
3821 preeminent = hv_exists_ent(hv, keysv, 0);
3824 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
3825 svp = he ? &HeVAL(he) : NULL;
3829 if (!svp || !(sv = *svp) || sv == &PL_sv_undef) {
3833 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
3834 lv = sv_newmortal();
3835 sv_upgrade(lv, SVt_PVLV);
3837 sv_magic(lv, key2 = newSVsv(keysv),
3838 PERL_MAGIC_defelem, NULL, 0);
3839 /* sv_magic() increments refcount */
3840 SvREFCNT_dec_NN(key2);
3841 LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
3847 if (HvNAME_get(hv) && isGV_or_RVCV(sv))
3848 save_gp(MUTABLE_GV(sv),
3849 !(PL_op->op_flags & OPf_SPECIAL));
3850 else if (preeminent) {
3851 save_helem_flags(hv, keysv, svp,
3852 (PL_op->op_flags & OPf_SPECIAL)
3853 ? 0 : SAVEf_SETMAGIC);
3854 sv = *svp; /* may have changed */
3857 SAVEHDELETE(hv, keysv);
3862 sv = (svp && *svp ? *svp : &PL_sv_undef);
3863 /* see note in pp_helem() */
3864 if (SvRMAGICAL(hv) && SvGMAGICAL(sv))
3873 actions >>= MDEREF_SHIFT;
3891 itersvp = CxITERVAR(cx);
3894 switch (CxTYPE(cx)) {
3896 case CXt_LOOP_LAZYSV: /* string increment */
3898 SV* cur = cx->blk_loop.state_u.lazysv.cur;
3899 SV *end = cx->blk_loop.state_u.lazysv.end;
3900 /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no.
3901 It has SvPVX of "" and SvCUR of 0, which is what we want. */
3903 const char *max = SvPV_const(end, maxlen);
3904 if (DO_UTF8(end) && IN_UNI_8_BIT)
3905 maxlen = sv_len_utf8_nomg(end);
3906 if (UNLIKELY(SvNIOK(cur) || SvCUR(cur) > maxlen))
3910 /* NB: on the first iteration, oldsv will have a ref count of at
3911 * least 2 (one extra from blk_loop.itersave), so the GV or pad
3912 * slot will get localised; on subsequent iterations the RC==1
3913 * optimisation may kick in and the SV will be reused. */
3914 if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
3915 /* safe to reuse old SV */
3916 sv_setsv(oldsv, cur);
3920 /* we need a fresh SV every time so that loop body sees a
3921 * completely new SV for closures/references to work as
3923 *itersvp = newSVsv(cur);
3924 SvREFCNT_dec(oldsv);
3926 if (strEQ(SvPVX_const(cur), max))
3927 sv_setiv(cur, 0); /* terminate next time */
3933 case CXt_LOOP_LAZYIV: /* integer increment */
3935 IV cur = cx->blk_loop.state_u.lazyiv.cur;
3936 if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end))
3940 /* see NB comment above */
3941 if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
3942 /* safe to reuse old SV */
3944 if ( (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV))
3947 /* Cheap SvIOK_only().
3948 * Assert that flags which SvIOK_only() would test or
3949 * clear can't be set, because we're SVt_IV */
3950 assert(!(SvFLAGS(oldsv) &
3951 (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK)))));
3952 SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK);
3953 /* SvIV_set() where sv_any points to head */
3954 oldsv->sv_u.svu_iv = cur;
3958 sv_setiv(oldsv, cur);
3962 /* we need a fresh SV every time so that loop body sees a
3963 * completely new SV for closures/references to work as they
3965 *itersvp = newSViv(cur);
3966 SvREFCNT_dec(oldsv);
3969 if (UNLIKELY(cur == IV_MAX)) {
3970 /* Handle end of range at IV_MAX */
3971 cx->blk_loop.state_u.lazyiv.end = IV_MIN;
3973 ++cx->blk_loop.state_u.lazyiv.cur;
3977 case CXt_LOOP_LIST: /* for (1,2,3) */
3979 assert(OPpITER_REVERSED == 2); /* so inc becomes -1 or 1 */
3980 inc = (IV)1 - (IV)(PL_op->op_private & OPpITER_REVERSED);
3981 ix = (cx->blk_loop.state_u.stack.ix += inc);
3982 if (UNLIKELY(inc > 0
3983 ? ix > cx->blk_oldsp
3984 : ix <= cx->blk_loop.state_u.stack.basesp)
3988 sv = PL_stack_base[ix];
3990 goto loop_ary_common;
3992 case CXt_LOOP_ARY: /* for (@ary) */
3994 av = cx->blk_loop.state_u.ary.ary;
3995 inc = (IV)1 - (IV)(PL_op->op_private & OPpITER_REVERSED);
3996 ix = (cx->blk_loop.state_u.ary.ix += inc);
3997 if (UNLIKELY(inc > 0
4003 if (UNLIKELY(SvRMAGICAL(av))) {
4004 SV * const * const svp = av_fetch(av, ix, FALSE);
4005 sv = svp ? *svp : NULL;
4008 sv = AvARRAY(av)[ix];
4013 if (UNLIKELY(cx->cx_type & CXp_FOR_LVREF)) {
4014 SvSetMagicSV(*itersvp, sv);
4019 if (UNLIKELY(SvIS_FREED(sv))) {
4021 Perl_croak(aTHX_ "Use of freed value in iteration");
4028 SvREFCNT_inc_simple_void_NN(sv);
4032 sv = newSVavdefelem(av, ix, 0);
4039 SvREFCNT_dec(oldsv);
4043 DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx));
4046 /* Try to bypass pushing &PL_sv_yes and calling pp_and(); instead
4047 * jump straight to the AND op's op_other */
4048 assert(PL_op->op_next->op_type == OP_AND);
4049 if (PL_op->op_next->op_ppaddr == Perl_pp_and) {
4050 return cLOGOPx(PL_op->op_next)->op_other;
4053 /* An XS module has replaced the op_ppaddr, so fall back to the slow,
4055 /* pp_enteriter should have pre-extended the stack */
4056 EXTEND_SKIP(PL_stack_sp, 1);
4057 *++PL_stack_sp = &PL_sv_yes;
4058 return PL_op->op_next;
4062 /* Try to bypass pushing &PL_sv_no and calling pp_and(); instead
4063 * jump straight to the AND op's op_next */
4064 assert(PL_op->op_next->op_type == OP_AND);
4065 /* pp_enteriter should have pre-extended the stack */
4066 EXTEND_SKIP(PL_stack_sp, 1);
4067 /* we only need this for the rare case where the OP_AND isn't
4068 * in void context, e.g. $x = do { for (..) {...} };
4069 * (or for when an XS module has replaced the op_ppaddr)
4070 * but it's cheaper to just push it rather than testing first
4072 *++PL_stack_sp = &PL_sv_no;
4073 if (PL_op->op_next->op_ppaddr == Perl_pp_and) {
4074 return PL_op->op_next->op_next;
4077 /* An XS module has replaced the op_ppaddr, so fall back to the slow,
4079 return PL_op->op_next;
4085 A description of how taint works in pattern matching and substitution.
4087 This is all conditional on NO_TAINT_SUPPORT not being defined. Under
4088 NO_TAINT_SUPPORT, taint-related operations should become no-ops.
4090 While the pattern is being assembled/concatenated and then compiled,
4091 PL_tainted will get set (via TAINT_set) if any component of the pattern
4092 is tainted, e.g. /.*$tainted/. At the end of pattern compilation,
4093 the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via
4094 TAINT_get). It will also be set if any component of the pattern matches
4095 based on locale-dependent behavior.
4097 When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to
4098 the pattern is marked as tainted. This means that subsequent usage, such
4099 as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED,
4100 on the new pattern too.
4102 RXf_TAINTED_SEEN is used post-execution by the get magic code
4103 of $1 et al to indicate whether the returned value should be tainted.
4104 It is the responsibility of the caller of the pattern (i.e. pp_match,
4105 pp_subst etc) to set this flag for any other circumstances where $1 needs
4108 The taint behaviour of pp_subst (and pp_substcont) is quite complex.
4110 There are three possible sources of taint
4112 * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN)
4113 * the replacement string (or expression under /e)
4115 There are four destinations of taint and they are affected by the sources
4116 according to the rules below:
4118 * the return value (not including /r):
4119 tainted by the source string and pattern, but only for the
4120 number-of-iterations case; boolean returns aren't tainted;
4121 * the modified string (or modified copy under /r):
4122 tainted by the source string, pattern, and replacement strings;
4124 tainted by the pattern, and under 'use re "taint"', by the source
4126 * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted:
4127 should always be unset before executing subsequent code.
4129 The overall action of pp_subst is:
4131 * at the start, set bits in rxtainted indicating the taint status of
4132 the various sources.
4134 * After each pattern execution, update the SUBST_TAINT_PAT bit in
4135 rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the
4136 pattern has subsequently become tainted via locale ops.
4138 * If control is being passed to pp_substcont to execute a /e block,
4139 save rxtainted in the CXt_SUBST block, for future use by
4142 * Whenever control is being returned to perl code (either by falling
4143 off the "end" of pp_subst/pp_substcont, or by entering a /e block),
4144 use the flag bits in rxtainted to make all the appropriate types of
4145 destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1
4146 et al will appear tainted.
4148 pp_match is just a simpler version of the above.
4164 U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits.
4165 See "how taint works" above */
4168 REGEXP *rx = PM_GETRE(pm);
4169 regexp *prog = ReANY(rx);
4171 int force_on_match = 0;
4172 const I32 oldsave = PL_savestack_ix;
4174 bool doutf8 = FALSE; /* whether replacement is in utf8 */
4179 /* known replacement string? */
4180 SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL;
4184 if (PL_op->op_flags & OPf_STACKED)
4195 SvGETMAGIC(TARG); /* must come before cow check */
4197 /* note that a string might get converted to COW during matching */
4198 was_cow = cBOOL(SvIsCOW(TARG));
4200 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
4201 #ifndef PERL_ANY_COW
4203 sv_force_normal_flags(TARG,0);
4205 if ((SvREADONLY(TARG)
4206 || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG))
4207 || SvTYPE(TARG) > SVt_PVLV)
4208 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
4209 Perl_croak_no_modify();
4213 orig = SvPV_nomg(TARG, len);
4214 /* note we don't (yet) force the var into being a string; if we fail
4215 * to match, we leave as-is; on successful match however, we *will*
4216 * coerce into a string, then repeat the match */
4217 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG))
4220 /* only replace once? */
4221 once = !(rpm->op_pmflags & PMf_GLOBAL);
4223 /* See "how taint works" above */
4226 (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0)
4227 | (RXp_ISTAINTED(prog) ? SUBST_TAINT_PAT : 0)
4228 | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0)
4229 | (( (once && !(rpm->op_pmflags & PMf_NONDESTRUCT))
4230 || (PL_op->op_private & OPpTRUEBOOL)) ? SUBST_TAINT_BOOLRET : 0));
4236 DIE(aTHX_ "panic: pp_subst, pm=%p, orig=%p", pm, orig);
4238 strend = orig + len;
4239 slen = DO_UTF8(TARG) ? utf8_length((U8*)orig, (U8*)strend) : len;
4240 maxiters = 2 * slen + 10; /* We can match twice at each
4241 position, once with zero-length,
4242 second time with non-zero. */
4244 /* handle the empty pattern */
4245 if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
4246 if (PL_curpm == PL_reg_curpm) {
4247 if (PL_curpm_under) {
4248 if (PL_curpm_under == PL_reg_curpm) {
4249 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
4251 pm = PL_curpm_under;
4261 #ifdef PERL_SAWAMPERSAND
4262 r_flags = ( RXp_NPARENS(prog)
4264 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
4265 || (rpm->op_pmflags & PMf_KEEPCOPY)
4270 r_flags = REXEC_COPY_STR;
4273 if (!CALLREGEXEC(rx, orig, strend, orig, 0, TARG, NULL, r_flags))
4276 PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no);
4277 LEAVE_SCOPE(oldsave);
4282 /* known replacement string? */
4284 /* replacement needing upgrading? */
4285 if (DO_UTF8(TARG) && !doutf8) {
4286 nsv = sv_newmortal();
4288 sv_utf8_upgrade(nsv);
4289 c = SvPV_const(nsv, clen);
4293 c = SvPV_const(dstr, clen);
4294 doutf8 = DO_UTF8(dstr);
4297 if (UNLIKELY(TAINT_get))
4298 rxtainted |= SUBST_TAINT_REPL;
4305 /* can do inplace substitution? */
4310 && (I32)clen <= RXp_MINLENRET(prog)
4312 || !(r_flags & REXEC_COPY_STR)
4313 || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN))
4315 && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST)
4316 && (!doutf8 || SvUTF8(TARG))
4317 && !(rpm->op_pmflags & PMf_NONDESTRUCT))
4321 /* string might have got converted to COW since we set was_cow */
4322 if (SvIsCOW(TARG)) {
4323 if (!force_on_match)
4325 assert(SvVOK(TARG));
4328 if (force_on_match) {
4329 /* redo the first match, this time with the orig var
4330 * forced into being a string */
4332 orig = SvPV_force_nomg(TARG, len);
4338 if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
4339 rxtainted |= SUBST_TAINT_PAT;
4340 m = orig + RXp_OFFS(prog)[0].start;
4341 d = orig + RXp_OFFS(prog)[0].end;
4343 if (m - s > strend - d) { /* faster to shorten from end */
4346 Copy(c, m, clen, char);
4351 Move(d, m, i, char);
4355 SvCUR_set(TARG, m - s);
4357 else { /* faster from front */
4361 Move(s, d - i, i, char);
4364 Copy(c, d, clen, char);
4371 d = s = RXp_OFFS(prog)[0].start + orig;
4374 if (UNLIKELY(iters++ > maxiters))
4375 DIE(aTHX_ "Substitution loop");
4376 /* run time pattern taint, eg locale */
4377 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
4378 rxtainted |= SUBST_TAINT_PAT;
4379 m = RXp_OFFS(prog)[0].start + orig;
4382 Move(s, d, i, char);
4386 Copy(c, d, clen, char);
4389 s = RXp_OFFS(prog)[0].end + orig;
4390 } while (CALLREGEXEC(rx, s, strend, orig,
4391 s == m, /* don't match same null twice */
4393 REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
4396 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
4397 Move(s, d, i+1, char); /* include the NUL */
4401 if (PL_op->op_private & OPpTRUEBOOL)
4411 if (force_on_match) {
4412 /* redo the first match, this time with the orig var
4413 * forced into being a string */
4415 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
4416 /* I feel that it should be possible to avoid this mortal copy
4417 given that the code below copies into a new destination.
4418 However, I suspect it isn't worth the complexity of
4419 unravelling the C<goto force_it> for the small number of
4420 cases where it would be viable to drop into the copy code. */
4421 TARG = sv_2mortal(newSVsv(TARG));
4423 orig = SvPV_force_nomg(TARG, len);
4429 if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
4430 rxtainted |= SUBST_TAINT_PAT;
4432 s = RXp_OFFS(prog)[0].start + orig;
4433 dstr = newSVpvn_flags(orig, s-orig,
4434 SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0));
4439 /* note that a whole bunch of local vars are saved here for
4440 * use by pp_substcont: here's a list of them in case you're
4441 * searching for places in this sub that uses a particular var:
4442 * iters maxiters r_flags oldsave rxtainted orig dstr targ
4443 * s m strend rx once */
4445 RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot);
4449 if (UNLIKELY(iters++ > maxiters))
4450 DIE(aTHX_ "Substitution loop");
4451 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
4452 rxtainted |= SUBST_TAINT_PAT;
4453 if (RXp_MATCH_COPIED(prog) && RXp_SUBBEG(prog) != orig) {
4455 char *old_orig = orig;
4456 assert(RXp_SUBOFFSET(prog) == 0);
4458 orig = RXp_SUBBEG(prog);
4459 s = orig + (old_s - old_orig);
4460 strend = s + (strend - old_s);
4462 m = RXp_OFFS(prog)[0].start + orig;
4463 sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG));
4464 s = RXp_OFFS(prog)[0].end + orig;
4466 /* replacement already stringified */
4468 sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8);
4472 sv_catsv(dstr, repl);
4476 } while (CALLREGEXEC(rx, s, strend, orig,
4477 s == m, /* Yields minend of 0 or 1 */
4479 REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
4480 assert(strend >= s);
4481 sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG));
4483 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
4484 /* From here on down we're using the copy, and leaving the original
4491 /* The match may make the string COW. If so, brilliant, because
4492 that's just saved us one malloc, copy and free - the regexp has
4493 donated the old buffer, and we malloc an entirely new one, rather
4494 than the regexp malloc()ing a buffer and copying our original,
4495 only for us to throw it away here during the substitution. */
4496 if (SvIsCOW(TARG)) {
4497 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
4503 SvPV_set(TARG, SvPVX(dstr));
4504 SvCUR_set(TARG, SvCUR(dstr));
4505 SvLEN_set(TARG, SvLEN(dstr));
4506 SvFLAGS(TARG) |= SvUTF8(dstr);
4507 SvPV_set(dstr, NULL);
4510 if (PL_op->op_private & OPpTRUEBOOL)
4517 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
4518 (void)SvPOK_only_UTF8(TARG);
4521 /* See "how taint works" above */
4523 if ((rxtainted & SUBST_TAINT_PAT) ||
4524 ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) ==
4525 (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
4527 (RXp_MATCH_TAINTED_on(prog)); /* taint $1 et al */
4529 if (!(rxtainted & SUBST_TAINT_BOOLRET)
4530 && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
4532 SvTAINTED_on(TOPs); /* taint return value */
4534 SvTAINTED_off(TOPs); /* may have got tainted earlier */
4536 /* needed for mg_set below */
4538 cBOOL(rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
4542 SvSETMAGIC(TARG); /* PL_tainted must be correctly set for this mg_set */
4544 LEAVE_SCOPE(oldsave);
4554 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
4555 ++*PL_markstack_ptr;
4557 LEAVE_with_name("grep_item"); /* exit inner scope */
4560 if (UNLIKELY(PL_stack_base + *PL_markstack_ptr > SP)) {
4562 const U8 gimme = GIMME_V;
4564 LEAVE_with_name("grep"); /* exit outer scope */
4565 (void)POPMARK; /* pop src */
4566 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
4567 (void)POPMARK; /* pop dst */
4568 SP = PL_stack_base + POPMARK; /* pop original mark */
4569 if (gimme == G_SCALAR) {
4570 if (PL_op->op_private & OPpTRUEBOOL)
4571 PUSHs(items ? &PL_sv_yes : &PL_sv_zero);
4577 else if (gimme == G_ARRAY)
4584 ENTER_with_name("grep_item"); /* enter inner scope */
4587 src = PL_stack_base[TOPMARK];
4588 if (SvPADTMP(src)) {
4589 src = PL_stack_base[TOPMARK] = sv_mortalcopy(src);
4595 RETURNOP(cLOGOP->op_other);
4599 /* leave_adjust_stacks():
4601 * Process a scope's return args (in the range from_sp+1 .. PL_stack_sp),
4602 * positioning them at to_sp+1 onwards, and do the equivalent of a
4603 * FREEMPS and TAINT_NOT.
4605 * Not intended to be called in void context.
4607 * When leaving a sub, eval, do{} or other scope, the things that need
4608 * doing to process the return args are:
4609 * * in scalar context, only return the last arg (or PL_sv_undef if none);
4610 * * for the types of return that return copies of their args (such
4611 * as rvalue sub return), make a mortal copy of every return arg,
4612 * except where we can optimise the copy away without it being
4613 * semantically visible;
4614 * * make sure that the arg isn't prematurely freed; in the case of an
4615 * arg not copied, this may involve mortalising it. For example, in
4616 * C<sub f { my $x = ...; $x }>, $x would be freed when we do
4617 * CX_LEAVE_SCOPE(cx) unless it's protected or copied.
4619 * What condition to use when deciding whether to pass the arg through
4620 * or make a copy, is determined by the 'pass' arg; its valid values are:
4621 * 0: rvalue sub/eval exit
4622 * 1: other rvalue scope exit
4623 * 2: :lvalue sub exit in rvalue context
4624 * 3: :lvalue sub exit in lvalue context and other lvalue scope exits
4626 * There is a big issue with doing a FREETMPS. We would like to free any
4627 * temps created by the last statement which the sub executed, rather than
4628 * leaving them for the caller. In a situation where a sub call isn't
4629 * soon followed by a nextstate (e.g. nested recursive calls, a la
4630 * fibonacci()), temps can accumulate, causing memory and performance
4633 * On the other hand, we don't want to free any TEMPs which are keeping
4634 * alive any return args that we skipped copying; nor do we wish to undo
4635 * any mortalising done here.
4637 * The solution is to split the temps stack frame into two, with a cut
4638 * point delineating the two halves. We arrange that by the end of this
4639 * function, all the temps stack frame entries we wish to keep are in the
4640 * range PL_tmps_floor+1.. tmps_base-1, while the ones to free now are in
4641 * the range tmps_base .. PL_tmps_ix. During the course of this
4642 * function, tmps_base starts off as PL_tmps_floor+1, then increases
4643 * whenever we find or create a temp that we know should be kept. In
4644 * general the stuff above tmps_base is undecided until we reach the end,
4645 * and we may need a sort stage for that.
4647 * To determine whether a TEMP is keeping a return arg alive, every
4648 * arg that is kept rather than copied and which has the SvTEMP flag
4649 * set, has the flag temporarily unset, to mark it. At the end we scan
4650 * the temps stack frame above the cut for entries without SvTEMP and
4651 * keep them, while turning SvTEMP on again. Note that if we die before
4652 * the SvTEMPs flags are set again, its safe: at worst, subsequent use of
4653 * those SVs may be slightly less efficient.
4655 * In practice various optimisations for some common cases mean we can
4656 * avoid most of the scanning and swapping about with the temps stack.
4660 Perl_leave_adjust_stacks(pTHX_ SV **from_sp, SV **to_sp, U8 gimme, int pass)
4664 SSize_t tmps_base; /* lowest index into tmps stack that needs freeing now */
4667 PERL_ARGS_ASSERT_LEAVE_ADJUST_STACKS;
4671 if (gimme == G_ARRAY) {
4672 nargs = SP - from_sp;
4676 assert(gimme == G_SCALAR);
4677 if (UNLIKELY(from_sp >= SP)) {
4678 /* no return args */
4679 assert(from_sp == SP);
4681 *++SP = &PL_sv_undef;
4691 /* common code for G_SCALAR and G_ARRAY */
4693 tmps_base = PL_tmps_floor + 1;
4697 /* pointer version of tmps_base. Not safe across temp stack
4701 EXTEND_MORTAL(nargs); /* one big extend for worst-case scenario */
4702 tmps_basep = PL_tmps_stack + tmps_base;
4704 /* process each return arg */
4707 SV *sv = *from_sp++;
4709 assert(PL_tmps_ix + nargs < PL_tmps_max);
4711 /* PADTMPs with container set magic shouldn't appear in the
4712 * wild. This assert is more important for pp_leavesublv(),
4713 * but by testing for it here, we're more likely to catch
4714 * bad cases (what with :lvalue subs not being widely
4715 * deployed). The two issues are that for something like
4716 * sub :lvalue { $tied{foo} }
4718 * sub :lvalue { substr($foo,1,2) }
4719 * pp_leavesublv() will croak if the sub returns a PADTMP,
4720 * and currently functions like pp_substr() return a mortal
4721 * rather than using their PADTMP when returning a PVLV.
4722 * This is because the PVLV will hold a ref to $foo,
4723 * so $foo would get delayed in being freed while
4724 * the PADTMP SV remained in the PAD.
4725 * So if this assert fails it means either:
4726 * 1) there is pp code similar to pp_substr that is
4727 * returning a PADTMP instead of a mortal, and probably
4729 * 2) pp_leavesublv is making unwarranted assumptions
4730 * about always croaking on a PADTMP
4732 if (SvPADTMP(sv) && SvSMAGICAL(sv)) {
4734 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
4735 assert(PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type));
4741 pass == 0 ? (SvTEMP(sv) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1)
4742 : pass == 1 ? ((SvTEMP(sv) || SvPADTMP(sv)) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1)
4743 : pass == 2 ? (!SvPADTMP(sv))
4746 /* pass through: skip copy for logic or optimisation
4747 * reasons; instead mortalise it, except that ... */
4751 /* ... since this SV is an SvTEMP , we don't need to
4752 * re-mortalise it; instead we just need to ensure
4753 * that its existing entry in the temps stack frame
4754 * ends up below the cut and so avoids being freed
4755 * this time round. We mark it as needing to be kept
4756 * by temporarily unsetting SvTEMP; then at the end,
4757 * we shuffle any !SvTEMP entries on the tmps stack
4758 * back below the cut.
4759 * However, there's a significant chance that there's
4760 * a 1:1 correspondence between the first few (or all)
4761 * elements in the return args stack frame and those
4762 * in the temps stack frame; e,g.:
4763 * sub f { ....; map {...} .... },
4764 * or if we're exiting multiple scopes and one of the
4765 * inner scopes has already made mortal copies of each
4768 * If so, this arg sv will correspond to the next item
4769 * on the tmps stack above the cut, and so can be kept
4770 * merely by moving the cut boundary up one, rather
4771 * than messing with SvTEMP. If all args are 1:1 then
4772 * we can avoid the sorting stage below completely.
4774 * If there are no items above the cut on the tmps
4775 * stack, then the SvTEMP must comne from an item
4776 * below the cut, so there's nothing to do.
4778 if (tmps_basep <= &PL_tmps_stack[PL_tmps_ix]) {
4779 if (sv == *tmps_basep)
4785 else if (!SvPADTMP(sv)) {
4786 /* mortalise arg to avoid it being freed during save
4787 * stack unwinding. Pad tmps don't need mortalising as
4788 * they're never freed. This is the equivalent of
4789 * sv_2mortal(SvREFCNT_inc(sv)), except that:
4790 * * it assumes that the temps stack has already been
4792 * * it puts the new item at the cut rather than at
4793 * ++PL_tmps_ix, moving the previous occupant there
4796 if (!SvIMMORTAL(sv)) {
4797 SvREFCNT_inc_simple_void_NN(sv);
4799 /* Note that if there's nothing above the cut,
4800 * this copies the garbage one slot above
4801 * PL_tmps_ix onto itself. This is harmless (the
4802 * stack's already been extended), but might in
4803 * theory trigger warnings from tools like ASan
4805 PL_tmps_stack[++PL_tmps_ix] = *tmps_basep;
4811 /* Make a mortal copy of the SV.
4812 * The following code is the equivalent of sv_mortalcopy()
4814 * * it assumes the temps stack has already been extended;
4815 * * it optimises the copying for some simple SV types;
4816 * * it puts the new item at the cut rather than at
4817 * ++PL_tmps_ix, moving the previous occupant there
4820 SV *newsv = newSV(0);
4822 PL_tmps_stack[++PL_tmps_ix] = *tmps_basep;
4823 /* put it on the tmps stack early so it gets freed if we die */
4824 *tmps_basep++ = newsv;
4827 if (SvTYPE(sv) <= SVt_IV) {
4828 /* arg must be one of undef, IV/UV, or RV: skip
4829 * sv_setsv_flags() and do the copy directly */
4831 U32 srcflags = SvFLAGS(sv);
4833 assert(!SvGMAGICAL(sv));
4834 if (srcflags & (SVf_IOK|SVf_ROK)) {
4835 SET_SVANY_FOR_BODYLESS_IV(newsv);
4837 if (srcflags & SVf_ROK) {
4838 newsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(sv));
4839 /* SV type plus flags */
4840 dstflags = (SVt_IV|SVf_ROK|SVs_TEMP);
4843 /* both src and dst are <= SVt_IV, so sv_any
4844 * points to the head; so access the heads
4845 * directly rather than going via sv_any.
4847 assert( &(sv->sv_u.svu_iv)
4848 == &(((XPVIV*) SvANY(sv))->xiv_iv));
4849 assert( &(newsv->sv_u.svu_iv)
4850 == &(((XPVIV*) SvANY(newsv))->xiv_iv));
4851 newsv->sv_u.svu_iv = sv->sv_u.svu_iv;
4852 /* SV type plus flags */
4853 dstflags = (SVt_IV|SVf_IOK|SVp_IOK|SVs_TEMP
4854 |(srcflags & SVf_IVisUV));
4858 assert(!(srcflags & SVf_OK));
4859 dstflags = (SVt_NULL|SVs_TEMP); /* SV type plus flags */
4861 SvFLAGS(newsv) = dstflags;
4865 /* do the full sv_setsv() */
4869 old_base = tmps_basep - PL_tmps_stack;
4871 sv_setsv_flags(newsv, sv, SV_DO_COW_SVSETSV);
4872 /* the mg_get or sv_setsv might have created new temps
4873 * or realloced the tmps stack; regrow and reload */
4874 EXTEND_MORTAL(nargs);
4875 tmps_basep = PL_tmps_stack + old_base;
4876 TAINT_NOT; /* Each item is independent */
4882 /* If there are any temps left above the cut, we need to sort
4883 * them into those to keep and those to free. The only ones to
4884 * keep are those for which we've temporarily unset SvTEMP.
4885 * Work inwards from the two ends at tmps_basep .. PL_tmps_ix,
4886 * swapping pairs as necessary. Stop when we meet in the middle.
4889 SV **top = PL_tmps_stack + PL_tmps_ix;
4890 while (tmps_basep <= top) {
4903 tmps_base = tmps_basep - PL_tmps_stack;
4906 PL_stack_sp = to_sp;
4908 /* unrolled FREETMPS() but using tmps_base-1 rather than PL_tmps_floor */
4909 while (PL_tmps_ix >= tmps_base) {
4910 SV* const sv = PL_tmps_stack[PL_tmps_ix--];
4912 PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
4916 SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
4922 /* also tail-called by pp_return */
4932 assert(CxTYPE(cx) == CXt_SUB);
4934 if (CxMULTICALL(cx)) {
4935 /* entry zero of a stack is always PL_sv_undef, which
4936 * simplifies converting a '()' return into undef in scalar context */
4937 assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
4941 gimme = cx->blk_gimme;
4942 oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */
4944 if (gimme == G_VOID)
4945 PL_stack_sp = oldsp;
4947 leave_adjust_stacks(oldsp, oldsp, gimme, 0);
4950 cx_popsub(cx); /* Stack values are safe: release CV and @_ ... */
4952 retop = cx->blk_sub.retop;
4959 /* clear (if possible) or abandon the current @_. If 'abandon' is true,
4960 * forces an abandon */
4963 Perl_clear_defarray(pTHX_ AV* av, bool abandon)
4965 const SSize_t fill = AvFILLp(av);
4967 PERL_ARGS_ASSERT_CLEAR_DEFARRAY;
4969 if (LIKELY(!abandon && SvREFCNT(av) == 1 && !SvMAGICAL(av))) {
4974 AV *newav = newAV();
4975 av_extend(newav, fill);
4976 AvREIFY_only(newav);
4977 PAD_SVl(0) = MUTABLE_SV(newav);
4978 SvREFCNT_dec_NN(av);
4989 I32 old_savestack_ix;
4994 /* Locate the CV to call:
4995 * - most common case: RV->CV: f(), $ref->():
4996 * note that if a sub is compiled before its caller is compiled,
4997 * the stash entry will be a ref to a CV, rather than being a GV.
4998 * - second most common case: CV: $ref->method()
5001 /* a non-magic-RV -> CV ? */
5002 if (LIKELY( (SvFLAGS(sv) & (SVf_ROK|SVs_GMG)) == SVf_ROK)) {
5003 cv = MUTABLE_CV(SvRV(sv));
5004 if (UNLIKELY(SvOBJECT(cv))) /* might be overloaded */
5008 cv = MUTABLE_CV(sv);
5011 if (UNLIKELY(SvTYPE(cv) != SVt_PVCV)) {
5012 /* handle all the weird cases */
5013 switch (SvTYPE(sv)) {
5015 if (!isGV_with_GP(sv))
5019 cv = GvCVu((const GV *)sv);
5020 if (UNLIKELY(!cv)) {
5022 cv = sv_2cv(sv, &stash, &gv, 0);
5024 old_savestack_ix = PL_savestack_ix;
5035 if (UNLIKELY(SvAMAGIC(sv))) {
5036 sv = amagic_deref_call(sv, to_cv_amg);
5037 /* Don't SPAGAIN here. */
5043 if (UNLIKELY(!SvOK(sv)))
5044 DIE(aTHX_ PL_no_usym, "a subroutine");
5046 sym = SvPV_nomg_const(sv, len);
5047 if (PL_op->op_private & HINT_STRICT_REFS)
5048 DIE(aTHX_ "Can't use string (\"%" SVf32 "\"%s) as a subroutine ref while \"strict refs\" in use", sv, len>32 ? "..." : "");
5049 cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv));
5052 cv = MUTABLE_CV(SvRV(sv));
5053 if (LIKELY(SvTYPE(cv) == SVt_PVCV))
5059 DIE(aTHX_ "Not a CODE reference");
5063 /* At this point we want to save PL_savestack_ix, either by doing a
5064 * cx_pushsub(), or for XS, doing an ENTER. But we don't yet know the final
5065 * CV we will be using (so we don't know whether its XS, so we can't
5066 * cx_pushsub() or ENTER yet), and determining cv may itself push stuff on
5067 * the save stack. So remember where we are currently on the save
5068 * stack, and later update the CX or scopestack entry accordingly. */
5069 old_savestack_ix = PL_savestack_ix;
5071 /* these two fields are in a union. If they ever become separate,
5072 * we have to test for both of them being null below */
5074 assert((void*)&CvROOT(cv) == (void*)&CvXSUB(cv));
5075 while (UNLIKELY(!CvROOT(cv))) {
5079 /* anonymous or undef'd function leaves us no recourse */
5080 if (CvLEXICAL(cv) && CvHASGV(cv))
5081 DIE(aTHX_ "Undefined subroutine &%" SVf " called",
5082 SVfARG(cv_name(cv, NULL, 0)));
5083 if (CvANON(cv) || !CvHASGV(cv)) {
5084 DIE(aTHX_ "Undefined subroutine called");
5087 /* autoloaded stub? */
5088 if (cv != GvCV(gv = CvGV(cv))) {
5091 /* should call AUTOLOAD now? */
5094 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
5095 (GvNAMEUTF8(gv) ? SVf_UTF8 : 0)
5096 |(PL_op->op_flags & OPf_REF
5097 ? GV_AUTOLOAD_ISMETHOD
5099 cv = autogv ? GvCV(autogv) : NULL;
5102 sub_name = sv_newmortal();
5103 gv_efullname3(sub_name, gv, NULL);
5104 DIE(aTHX_ "Undefined subroutine &%" SVf " called", SVfARG(sub_name));
5108 /* unrolled "CvCLONE(cv) && ! CvCLONED(cv)" */
5109 if (UNLIKELY((CvFLAGS(cv) & (CVf_CLONE|CVf_CLONED)) == CVf_CLONE))
5110 DIE(aTHX_ "Closure prototype called");
5112 if (UNLIKELY((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub)
5115 Perl_get_db_sub(aTHX_ &sv, cv);
5117 PL_curcopdb = PL_curcop;
5119 /* check for lsub that handles lvalue subroutines */
5120 cv = GvCV(gv_fetchpvs("DB::lsub", GV_ADDMULTI, SVt_PVCV));
5121 /* if lsub not found then fall back to DB::sub */
5122 if (!cv) cv = GvCV(PL_DBsub);
5124 cv = GvCV(PL_DBsub);
5127 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
5128 DIE(aTHX_ "No DB::sub routine defined");
5131 if (!(CvISXSUB(cv))) {
5132 /* This path taken at least 75% of the time */
5139 /* keep PADTMP args alive throughout the call (we need to do this
5140 * because @_ isn't refcounted). Note that we create the mortals
5141 * in the caller's tmps frame, so they won't be freed until after
5142 * we return from the sub.
5151 *svp = sv = sv_mortalcopy(sv);
5157 cx = cx_pushblock(CXt_SUB, gimme, MARK, old_savestack_ix);
5158 hasargs = cBOOL(PL_op->op_flags & OPf_STACKED);
5159 cx_pushsub(cx, cv, PL_op->op_next, hasargs);
5161 padlist = CvPADLIST(cv);
5162 if (UNLIKELY((depth = ++CvDEPTH(cv)) >= 2))
5163 pad_push(padlist, depth);
5164 PAD_SET_CUR_NOSAVE(padlist, depth);
5165 if (LIKELY(hasargs)) {
5166 AV *const av = MUTABLE_AV(PAD_SVl(0));
5170 defavp = &GvAV(PL_defgv);
5171 cx->blk_sub.savearray = *defavp;
5172 *defavp = MUTABLE_AV(SvREFCNT_inc_simple_NN(av));
5174 /* it's the responsibility of whoever leaves a sub to ensure
5175 * that a clean, empty AV is left in pad[0]. This is normally
5176 * done by cx_popsub() */
5177 assert(!AvREAL(av) && AvFILLp(av) == -1);
5180 if (UNLIKELY(items - 1 > AvMAX(av))) {
5181 SV **ary = AvALLOC(av);
5182 Renew(ary, items, SV*);
5183 AvMAX(av) = items - 1;
5189 Copy(MARK+1,AvARRAY(av),items,SV*);
5190 AvFILLp(av) = items - 1;
5192 if (UNLIKELY((cx->blk_u16 & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO &&
5194 DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf,
5195 SVfARG(cv_name(cv, NULL, 0)));
5196 /* warning must come *after* we fully set up the context
5197 * stuff so that __WARN__ handlers can safely dounwind()
5200 if (UNLIKELY(depth == PERL_SUB_DEPTH_WARN
5201 && ckWARN(WARN_RECURSION)
5202 && !(PERLDB_SUB && cv == GvCV(PL_DBsub))))
5203 sub_crush_depth(cv);
5204 RETURNOP(CvSTART(cv));
5207 SSize_t markix = TOPMARK;
5211 /* pretend we did the ENTER earlier */
5212 PL_scopestack[PL_scopestack_ix - 1] = old_savestack_ix;
5217 if (UNLIKELY(((PL_op->op_private
5218 & CX_PUSHSUB_GET_LVALUE_MASK(Perl_is_lvalue_sub)
5219 ) & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO &&
5221 DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf,
5222 SVfARG(cv_name(cv, NULL, 0)));
5224 if (UNLIKELY(!(PL_op->op_flags & OPf_STACKED) && GvAV(PL_defgv))) {
5225 /* Need to copy @_ to stack. Alternative may be to
5226 * switch stack to @_, and copy return values
5227 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
5228 AV * const av = GvAV(PL_defgv);
5229 const SSize_t items = AvFILL(av) + 1;
5233 const bool m = cBOOL(SvRMAGICAL(av));
5234 /* Mark is at the end of the stack. */
5236 for (; i < items; ++i)
5240 SV ** const svp = av_fetch(av, i, 0);
5241 sv = svp ? *svp : NULL;
5243 else sv = AvARRAY(av)[i];
5244 if (sv) SP[i+1] = sv;
5246 SP[i+1] = av_nonelem(av, i);
5254 SV **mark = PL_stack_base + markix;
5255 SSize_t items = SP - mark;
5258 if (*mark && SvPADTMP(*mark)) {
5259 *mark = sv_mortalcopy(*mark);
5263 /* We assume first XSUB in &DB::sub is the called one. */
5264 if (UNLIKELY(PL_curcopdb)) {
5265 SAVEVPTR(PL_curcop);
5266 PL_curcop = PL_curcopdb;
5269 /* Do we need to open block here? XXXX */
5271 /* calculate gimme here as PL_op might get changed and then not
5272 * restored until the LEAVE further down */
5273 is_scalar = (GIMME_V == G_SCALAR);
5275 /* CvXSUB(cv) must not be NULL because newXS() refuses NULL xsub address */
5277 CvXSUB(cv)(aTHX_ cv);
5279 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
5280 /* This duplicates the check done in runops_debug(), but provides more
5281 * information in the common case of the fault being with an XSUB.
5283 * It should also catch an XSUB pushing more than it extends
5284 * in scalar context.
5286 if (PL_curstackinfo->si_stack_hwm < PL_stack_sp - PL_stack_base)
5287 Perl_croak_nocontext(
5288 "panic: XSUB %s::%s (%s) failed to extend arg stack: "
5289 "base=%p, sp=%p, hwm=%p\n",
5290 HvNAME(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)), CvFILE(cv),
5291 PL_stack_base, PL_stack_sp,
5292 PL_stack_base + PL_curstackinfo->si_stack_hwm);
5294 /* Enforce some sanity in scalar context. */
5296 SV **svp = PL_stack_base + markix + 1;
5297 if (svp != PL_stack_sp) {
5298 *svp = svp > PL_stack_sp ? &PL_sv_undef : *PL_stack_sp;
5308 Perl_sub_crush_depth(pTHX_ CV *cv)
5310 PERL_ARGS_ASSERT_SUB_CRUSH_DEPTH;
5313 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
5315 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%" SVf "\"",
5316 SVfARG(cv_name(cv,NULL,0)));
5322 /* like croak, but report in context of caller */
5325 Perl_croak_caller(const char *pat, ...)
5329 const PERL_CONTEXT *cx = caller_cx(0, NULL);
5331 /* make error appear at call site */
5333 PL_curcop = cx->blk_oldcop;
5335 va_start(args, pat);
5337 NOT_REACHED; /* NOTREACHED */
5346 SV* const elemsv = POPs;
5347 IV elem = SvIV(elemsv);
5348 AV *const av = MUTABLE_AV(POPs);
5349 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
5350 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
5351 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5352 bool preeminent = TRUE;
5355 if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC)))
5356 Perl_warner(aTHX_ packWARN(WARN_MISC),
5357 "Use of reference \"%" SVf "\" as array index",
5359 if (UNLIKELY(SvTYPE(av) != SVt_PVAV))
5362 if (UNLIKELY(localizing)) {
5366 /* If we can determine whether the element exist,
5367 * Try to preserve the existenceness of a tied array
5368 * element by using EXISTS and DELETE if possible.
5369 * Fallback to FETCH and STORE otherwise. */
5370 if (SvCANEXISTDELETE(av))
5371 preeminent = av_exists(av, elem);
5374 svp = av_fetch(av, elem, lval && !defer);
5376 #ifdef PERL_MALLOC_WRAP
5377 if (SvUOK(elemsv)) {
5378 const UV uv = SvUV(elemsv);
5379 elem = uv > IV_MAX ? IV_MAX : uv;
5381 else if (SvNOK(elemsv))
5382 elem = (IV)SvNV(elemsv);
5384 MEM_WRAP_CHECK_s(elem,SV*,"Out of memory during array extend");
5387 if (!svp || !*svp) {
5390 DIE(aTHX_ PL_no_aelem, elem);
5391 len = av_tindex(av);
5392 /* Resolve a negative index that falls within the array. Leave
5393 it negative it if falls outside the array. */
5394 if (elem < 0 && len + elem >= 0)
5396 if (elem >= 0 && elem <= len)
5397 /* Falls within the array. */
5398 PUSHs(av_nonelem(av,elem));
5400 /* Falls outside the array. If it is negative,
5401 magic_setdefelem will use the index for error reporting.
5403 mPUSHs(newSVavdefelem(av, elem, 1));
5406 if (UNLIKELY(localizing)) {
5408 save_aelem(av, elem, svp);
5410 SAVEADELETE(av, elem);
5412 else if (PL_op->op_private & OPpDEREF) {
5413 PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
5417 sv = (svp ? *svp : &PL_sv_undef);
5418 if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
5425 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
5427 PERL_ARGS_ASSERT_VIVIFY_REF;
5432 Perl_croak_no_modify();
5433 prepare_SV_for_RV(sv);
5436 SvRV_set(sv, newSV(0));
5439 SvRV_set(sv, MUTABLE_SV(newAV()));
5442 SvRV_set(sv, MUTABLE_SV(newHV()));
5449 if (SvGMAGICAL(sv)) {
5450 /* copy the sv without magic to prevent magic from being
5452 SV* msv = sv_newmortal();
5453 sv_setsv_nomg(msv, sv);
5459 PERL_STATIC_INLINE HV *
5460 S_opmethod_stash(pTHX_ SV* meth)
5465 SV* const sv = PL_stack_base + TOPMARK == PL_stack_sp
5466 ? (Perl_croak(aTHX_ "Can't call method \"%" SVf "\" without a "
5467 "package or object reference", SVfARG(meth)),
5469 : *(PL_stack_base + TOPMARK + 1);
5471 PERL_ARGS_ASSERT_OPMETHOD_STASH;
5475 Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on an undefined value",
5478 if (UNLIKELY(SvGMAGICAL(sv))) mg_get(sv);
5479 else if (SvIsCOW_shared_hash(sv)) { /* MyClass->meth() */
5480 stash = gv_stashsv(sv, GV_CACHE_ONLY);
5481 if (stash) return stash;
5485 ob = MUTABLE_SV(SvRV(sv));
5486 else if (!SvOK(sv)) goto undefined;
5487 else if (isGV_with_GP(sv)) {
5489 Perl_croak(aTHX_ "Can't call method \"%" SVf "\" "
5490 "without a package or object reference",
5493 if (SvTYPE(ob) == SVt_PVLV && LvTYPE(ob) == 'y') {
5494 assert(!LvTARGLEN(ob));
5498 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(ob));
5501 /* this isn't a reference */
5504 const char * const packname = SvPV_nomg_const(sv, packlen);
5505 const U32 packname_utf8 = SvUTF8(sv);
5506 stash = gv_stashpvn(packname, packlen, packname_utf8 | GV_CACHE_ONLY);
5507 if (stash) return stash;
5509 if (!(iogv = gv_fetchpvn_flags(
5510 packname, packlen, packname_utf8, SVt_PVIO
5512 !(ob=MUTABLE_SV(GvIO(iogv))))
5514 /* this isn't the name of a filehandle either */
5517 Perl_croak(aTHX_ "Can't call method \"%" SVf "\" "
5518 "without a package or object reference",
5521 /* assume it's a package name */
5522 stash = gv_stashpvn(packname, packlen, packname_utf8);
5523 if (stash) return stash;
5524 else return MUTABLE_HV(sv);
5526 /* it _is_ a filehandle name -- replace with a reference */
5527 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV(MUTABLE_SV(iogv)));
5530 /* if we got here, ob should be an object or a glob */
5531 if (!ob || !(SvOBJECT(ob)
5532 || (isGV_with_GP(ob)
5533 && (ob = MUTABLE_SV(GvIO((const GV *)ob)))
5536 Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on unblessed reference",
5537 SVfARG((SvPOK(meth) && SvPVX(meth) == PL_isa_DOES)
5538 ? newSVpvs_flags("DOES", SVs_TEMP)
5550 SV* const meth = TOPs;
5553 SV* const rmeth = SvRV(meth);
5554 if (SvTYPE(rmeth) == SVt_PVCV) {
5560 stash = opmethod_stash(meth);
5562 gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK);
5565 SETs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
5569 #define METHOD_CHECK_CACHE(stash,cache,meth) \
5570 const HE* const he = hv_fetch_ent(cache, meth, 0, 0); \
5572 gv = MUTABLE_GV(HeVAL(he)); \
5573 if (isGV(gv) && GvCV(gv) && (!GvCVGEN(gv) || GvCVGEN(gv) \
5574 == (PL_sub_generation + HvMROMETA(stash)->cache_gen))) \
5576 XPUSHs(MUTABLE_SV(GvCV(gv))); \
5585 SV* const meth = cMETHOPx_meth(PL_op);
5586 HV* const stash = opmethod_stash(meth);
5588 if (LIKELY(SvTYPE(stash) == SVt_PVHV)) {
5589 METHOD_CHECK_CACHE(stash, stash, meth);
5592 gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK);
5595 XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
5604 SV* const meth = cMETHOPx_meth(PL_op);
5605 HV* const stash = CopSTASH(PL_curcop);
5606 /* Actually, SUPER doesn't need real object's (or class') stash at all,
5607 * as it uses CopSTASH. However, we must ensure that object(class) is
5608 * correct (this check is done by S_opmethod_stash) */
5609 opmethod_stash(meth);
5611 if ((cache = HvMROMETA(stash)->super)) {
5612 METHOD_CHECK_CACHE(stash, cache, meth);
5615 gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER);
5618 XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
5626 SV* const meth = cMETHOPx_meth(PL_op);
5627 HV* stash = gv_stashsv(cMETHOPx_rclass(PL_op), 0);
5628 opmethod_stash(meth); /* not used but needed for error checks */
5630 if (stash) { METHOD_CHECK_CACHE(stash, stash, meth); }
5631 else stash = MUTABLE_HV(cMETHOPx_rclass(PL_op));
5633 gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK);
5636 XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
5640 PP(pp_method_redir_super)
5645 SV* const meth = cMETHOPx_meth(PL_op);
5646 HV* stash = gv_stashsv(cMETHOPx_rclass(PL_op), 0);
5647 opmethod_stash(meth); /* not used but needed for error checks */
5649 if (UNLIKELY(!stash)) stash = MUTABLE_HV(cMETHOPx_rclass(PL_op));
5650 else if ((cache = HvMROMETA(stash)->super)) {
5651 METHOD_CHECK_CACHE(stash, cache, meth);
5654 gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER);
5657 XPUSHs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
5662 * ex: set ts=8 sts=4 sw=4 et: