This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make RC-stack-aware: unwrap rv2av etc pp fns
[perl5.git] / pp_hot.c
1 /*    pp_hot.c
2  *
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
5  *
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.
8  *
9  */
10
11 /*
12  * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
13  * shaking the air.
14  *
15  *                  Awake!  Awake!  Fear, Fire, Foes!  Awake!
16  *                               Fire, Foes!  Awake!
17  *
18  *     [p.1007 of _The Lord of the Rings_, VI/viii: "The Scouring of the Shire"]
19  */
20
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.
26  *
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
31  * performance.
32  */
33
34 #include "EXTERN.h"
35 #define PERL_IN_PP_HOT_C
36 #include "perl.h"
37 #include "regcomp.h"
38
39 /* Hot code. */
40
41
42 #ifdef PERL_RC_STACK
43
44 /* common code for pp_wrap() and xs_wrap():
45  * free any original arguments, and bump and shift down any return
46  * args
47  */
48
49 STATIC void
50 S_pp_xs_wrap_return(pTHX_ I32 nargs, I32 old_sp)
51 {
52     I32 nret = (I32)(PL_stack_sp - PL_stack_base) - old_sp;
53     assert(nret >= 0);
54
55     /* bump any returned values */
56     if (nret) {
57         SV **svp = PL_stack_sp - nret + 1;
58         while (svp <= PL_stack_sp) {
59             SvREFCNT_inc(*svp);
60             svp++;
61         }
62     }
63
64     PL_curstackinfo->si_stack_nonrc_base = 0;
65
66     /* free the original args and shift the returned valued down */
67     if (nargs) {
68         SV **svp = PL_stack_sp - nret;
69         I32 i = nargs;
70         while (i--) {
71             SvREFCNT_dec(*svp);
72             *svp = NULL;
73             svp--;
74         }
75
76         if (nret) {
77             Move(PL_stack_sp - nret + 1,
78                  PL_stack_sp - nret - nargs + 1,
79                  nret, SV*);
80         }
81         PL_stack_sp -= nargs;
82     }
83 }
84
85 /* pp_wrap():
86  * wrapper function for pp() functions to turn them into functions
87  * that can operate on a reference-counted stack, by taking a non-
88  * reference-counted copy of the current stack frame, calling the real
89  * pp() function, then incrementing the reference count of any returned
90  * args.
91  *
92  * nargs or nlists indicate the number of stack arguments or the
93  * number of stack lists (delimited by MARKs) which the function expects.
94  */
95 OP*
96 Perl_pp_wrap(pTHX_ Perl_ppaddr_t real_pp_fn, I32 nargs, int nlists)
97 {
98     PERL_ARGS_ASSERT_PP_WRAP;
99
100     if (!rpp_stack_is_rc())
101         /* stack-already non-RC; nothing needing wrapping */
102         return real_pp_fn(aTHX);
103
104     OP *next_op;
105     I32 old_sp = (I32)(PL_stack_sp - PL_stack_base);
106
107     assert(nargs  >= 0);
108     assert(nlists >= 0);
109     assert(AvREAL(PL_curstack));
110
111     PL_curstackinfo->si_stack_nonrc_base = PL_stack_sp - PL_stack_base + 1;
112
113     if (nlists) {
114         assert(nargs == 0);
115         I32 mark  = PL_markstack_ptr[-nlists+1];
116         nargs = (PL_stack_sp - PL_stack_base) - mark;
117         assert(nlists <= 2); /* if ever more, make below a loop */
118         PL_markstack_ptr[0]  += nargs;
119         if (nlists == 2)
120             PL_markstack_ptr[-1] += nargs;
121     }
122
123     if (nargs) {
124         /* duplicate all the arg pointers further up the stack */
125         rpp_extend(nargs);
126         Copy(PL_stack_sp - nargs + 1, PL_stack_sp + 1, nargs, SV*);
127         PL_stack_sp += nargs;
128     }
129
130     next_op = real_pp_fn(aTHX);
131
132     /* we should still be a split stack */
133     assert(AvREAL(PL_curstack));
134     assert(PL_curstackinfo->si_stack_nonrc_base);
135
136     S_pp_xs_wrap_return(aTHX_ nargs, old_sp);
137
138     return next_op;
139 }
140
141
142 /* xs_wrap():
143  * similar in concept to pp_wrap: make a non-referenced-counted copy of
144  * a (not refcount aware) XS sub's args, call the XS subs, then bump any
145  * return values and free the original args */
146
147 void
148 Perl_xs_wrap(pTHX_ XSUBADDR_t xsub, CV *cv)
149 {
150     PERL_ARGS_ASSERT_XS_WRAP;
151
152     I32 old_sp = (I32)(PL_stack_sp - PL_stack_base);
153     I32 mark  = PL_markstack_ptr[0];
154     I32 nargs = (PL_stack_sp - PL_stack_base) - mark;
155
156     /* we should be a fully refcounted stack */
157     assert(AvREAL(PL_curstack));
158     assert(!PL_curstackinfo->si_stack_nonrc_base);
159
160     PL_curstackinfo->si_stack_nonrc_base = PL_stack_sp - PL_stack_base + 1;
161
162
163     if (nargs) {
164         /* duplicate all the arg pointers further up the stack */
165         rpp_extend(nargs);
166         Copy(PL_stack_sp - nargs + 1, PL_stack_sp + 1, nargs, SV*);
167         PL_stack_sp += nargs;
168         PL_markstack_ptr[0]  += nargs;
169     }
170
171     xsub(aTHX_ cv);
172
173     S_pp_xs_wrap_return(aTHX_ nargs, old_sp);
174 }
175
176 #endif
177
178
179 /* ----------------------------------------------------------- */
180
181
182 PP(pp_const)
183 {
184     rpp_xpush_1(cSVOP_sv);
185     return NORMAL;
186 }
187
188 PP(pp_nextstate)
189 {
190     PL_curcop = (COP*)PL_op;
191     TAINT_NOT;          /* Each statement is presumed innocent */
192     rpp_popfree_to(PL_stack_base + CX_CUR()->blk_oldsp);
193     FREETMPS;
194     PERL_ASYNC_CHECK();
195     return NORMAL;
196 }
197
198 PP(pp_gvsv)
199 {
200     assert(SvTYPE(cGVOP_gv) == SVt_PVGV);
201     rpp_xpush_1(
202             UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)
203                 ? save_scalar(cGVOP_gv)
204                 : GvSVn(cGVOP_gv));
205     return NORMAL;
206 }
207
208
209 /* also used for: pp_lineseq() pp_regcmaybe() pp_scalar() pp_scope() */
210
211 PP(pp_null)
212 {
213     return NORMAL;
214 }
215
216 /* This is sometimes called directly by pp_coreargs, pp_grepstart and
217    amagic_call. */
218 PP(pp_pushmark)
219 {
220     PUSHMARK(PL_stack_sp);
221     return NORMAL;
222 }
223
224 PP_wrapped(pp_stringify, 1, 0)
225 {
226     dSP; dTARGET;
227     SV * const sv = TOPs;
228     SETs(TARG);
229     sv_copypv(TARG, sv);
230     SvSETMAGIC(TARG);
231     /* no PUTBACK, SETs doesn't inc/dec SP */
232     return NORMAL;
233 }
234
235 PP(pp_gv)
236 {
237     /* cGVOP_gv might be a real GV or might be an RV to a CV */
238     assert(SvTYPE(cGVOP_gv) == SVt_PVGV ||
239            (SvTYPE(cGVOP_gv) <= SVt_PVMG && SvROK(cGVOP_gv) && SvTYPE(SvRV(cGVOP_gv)) == SVt_PVCV));
240     rpp_xpush_1(MUTABLE_SV(cGVOP_gv));
241     return NORMAL;
242 }
243
244
245 /* also used for: pp_andassign() */
246
247 PP_wrapped(pp_and, 2, 0)
248 {
249     PERL_ASYNC_CHECK();
250     {
251         /* SP is not used to remove a variable that is saved across the
252           sv_2bool_flags call in SvTRUE_NN, if a RISC/CISC or low/high machine
253           register or load/store vs direct mem ops macro is introduced, this
254           should be a define block between direct PL_stack_sp and dSP operations,
255           presently, using PL_stack_sp is bias towards CISC cpus */
256         SV * const sv = *PL_stack_sp;
257         if (!SvTRUE_NN(sv))
258             return NORMAL;
259         else {
260             if (PL_op->op_type == OP_AND)
261                 --PL_stack_sp;
262             return cLOGOP->op_other;
263         }
264     }
265 }
266
267 /*
268  * Mashup of simple padsv + sassign OPs
269  * Doesn't support the following lengthy and unlikely sassign case:
270  *    (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV))
271  *  These cases have a separate optimization, so are not handled here:
272  *    (PL_op->op_private & OPpASSIGN_BACKWARDS) {or,and,dor}assign
273 */
274
275 PP_wrapped(pp_padsv_store,1,0)
276 {
277     dSP;
278     OP * const op = PL_op;
279     SV** const padentry = &PAD_SVl(op->op_targ);
280     SV* targ = *padentry; /* lvalue to assign into */
281     SV* const val = TOPs; /* RHS value to assign */
282
283     /* !OPf_STACKED is not handled by this OP */
284     assert(op->op_flags & OPf_STACKED);
285
286     /* Inlined, simplified pp_padsv here */
287     if ((op->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) == OPpLVAL_INTRO) {
288         save_clearsv(padentry);
289     }
290
291     /* Inlined, simplified pp_sassign from here */
292     assert(TAINTING_get || !TAINT_get);
293     if (UNLIKELY(TAINT_get) && !SvTAINTED(val))
294         TAINT_NOT;
295
296     if (
297       UNLIKELY(SvTEMP(targ)) && !SvSMAGICAL(targ) && SvREFCNT(targ) == 1 &&
298       (!isGV_with_GP(targ) || SvFAKE(targ)) && ckWARN(WARN_MISC)
299     )
300         Perl_warner(aTHX_
301             packWARN(WARN_MISC), "Useless assignment to a temporary"
302         );
303     SvSetMagicSV(targ, val);
304
305     SETs(targ);
306     RETURN;
307 }
308
309 /* A mashup of simplified AELEMFAST_LEX + SASSIGN OPs */
310
311 PP_wrapped(pp_aelemfastlex_store, 1, 0)
312 {
313     dSP;
314     OP * const op = PL_op;
315     SV* const val = TOPs; /* RHS value to assign */
316     AV * const av = MUTABLE_AV(PAD_SV(op->op_targ));
317     const I8 key   = (I8)PL_op->op_private;
318     SV * targ = NULL;
319
320     /* !OPf_STACKED is not handled by this OP */
321     assert(op->op_flags & OPf_STACKED);
322
323     /* Inlined, simplified pp_aelemfast here */
324     assert(SvTYPE(av) == SVt_PVAV);
325
326     /* inlined av_fetch() for simple cases ... */
327     if (!SvRMAGICAL(av) && key >=0 && key <= AvFILLp(av)) {
328         targ = AvARRAY(av)[key];
329     }
330     /* ... else do it the hard way */
331     if (!targ) {
332         SV **svp = av_fetch(av, key, 1);
333
334         if (svp)
335             targ = *svp;
336         else
337             DIE(aTHX_ PL_no_aelem, (int)key);
338     }
339
340     /* Inlined, simplified pp_sassign from here */
341     assert(TAINTING_get || !TAINT_get);
342     if (UNLIKELY(TAINT_get) && !SvTAINTED(val))
343         TAINT_NOT;
344
345     /* This assertion is a deviation from pp_sassign, which uses an if()
346      * condition to check for "Useless assignment to a temporary" and
347      * warns if the condition is true. Here, the condition should NEVER
348      * be true when the LHS is the result of an array fetch. The
349      * assertion is here as a final check that this remains the case.
350      */
351     assert(!(SvTEMP(targ) && SvREFCNT(targ) == 1 && !SvSMAGICAL(targ)));
352
353     SvSetMagicSV(targ, val);
354
355     SETs(targ);
356     RETURN;
357 }
358
359 PP_wrapped(pp_sassign, 2, 0)
360 {
361     dSP;
362     /* sassign keeps its args in the optree traditionally backwards.
363        So we pop them differently.
364     */
365     SV *left = POPs; SV *right = TOPs;
366
367     if (PL_op->op_private & OPpASSIGN_BACKWARDS) { /* {or,and,dor}assign */
368         SV * const temp = left;
369         left = right; right = temp;
370     }
371     assert(TAINTING_get || !TAINT_get);
372     if (UNLIKELY(TAINT_get) && !SvTAINTED(right))
373         TAINT_NOT;
374     if (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV)) {
375         /* *foo =\&bar */
376         SV * const cv = SvRV(right);
377         const U32 cv_type = SvTYPE(cv);
378         const bool is_gv = isGV_with_GP(left);
379         const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM;
380
381         if (!got_coderef) {
382             assert(SvROK(cv));
383         }
384
385         /* Can do the optimisation if left (LVALUE) is not a typeglob,
386            right (RVALUE) is a reference to something, and we're in void
387            context. */
388         if (!got_coderef && !is_gv && GIMME_V == G_VOID) {
389             /* Is the target symbol table currently empty?  */
390             GV * const gv = gv_fetchsv_nomg(left, GV_NOINIT, SVt_PVGV);
391             if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) {
392                 /* Good. Create a new proxy constant subroutine in the target.
393                    The gv becomes a(nother) reference to the constant.  */
394                 SV *const value = SvRV(cv);
395
396                 SvUPGRADE(MUTABLE_SV(gv), SVt_IV);
397                 SvPCS_IMPORTED_on(gv);
398                 SvRV_set(gv, value);
399                 SvREFCNT_inc_simple_void(value);
400                 SETs(left);
401                 RETURN;
402             }
403         }
404
405         /* Need to fix things up.  */
406         if (!is_gv) {
407             /* Need to fix GV.  */
408             left = MUTABLE_SV(gv_fetchsv_nomg(left,GV_ADD, SVt_PVGV));
409         }
410
411         if (!got_coderef) {
412             /* We've been returned a constant rather than a full subroutine,
413                but they expect a subroutine reference to apply.  */
414             if (SvROK(cv)) {
415                 ENTER_with_name("sassign_coderef");
416                 SvREFCNT_inc_void(SvRV(cv));
417                 /* newCONSTSUB takes a reference count on the passed in SV
418                    from us.  We set the name to NULL, otherwise we get into
419                    all sorts of fun as the reference to our new sub is
420                    donated to the GV that we're about to assign to.
421                 */
422                 SvRV_set(right, MUTABLE_SV(newCONSTSUB(GvSTASH(left), NULL,
423                                                       SvRV(cv))));
424                 SvREFCNT_dec_NN(cv);
425                 LEAVE_with_name("sassign_coderef");
426             } else {
427                 /* What can happen for the corner case *{"BONK"} = \&{"BONK"};
428                    is that
429                    First:   ops for \&{"BONK"}; return us the constant in the
430                             symbol table
431                    Second:  ops for *{"BONK"} cause that symbol table entry
432                             (and our reference to it) to be upgraded from RV
433                             to typeblob)
434                    Thirdly: We get here. cv is actually PVGV now, and its
435                             GvCV() is actually the subroutine we're looking for
436
437                    So change the reference so that it points to the subroutine
438                    of that typeglob, as that's what they were after all along.
439                 */
440                 GV *const upgraded = MUTABLE_GV(cv);
441                 CV *const source = GvCV(upgraded);
442
443                 assert(source);
444                 assert(CvFLAGS(source) & CVf_CONST);
445
446                 SvREFCNT_inc_simple_void_NN(source);
447                 SvREFCNT_dec_NN(upgraded);
448                 SvRV_set(right, MUTABLE_SV(source));
449             }
450         }
451
452     }
453     if (
454       rpp_is_lone(left) && !SvSMAGICAL(left) &&
455       (!isGV_with_GP(left) || SvFAKE(left)) && ckWARN(WARN_MISC)
456     )
457         Perl_warner(aTHX_
458             packWARN(WARN_MISC), "Useless assignment to a temporary"
459         );
460     SvSetMagicSV(left, right);
461     SETs(left);
462     RETURN;
463 }
464
465 PP_wrapped(pp_cond_expr, 1, 0)
466 {
467     dSP;
468     SV *sv;
469
470     PERL_ASYNC_CHECK();
471     sv = POPs;
472     RETURNOP(SvTRUE_NN(sv) ? cLOGOP->op_other : cLOGOP->op_next);
473 }
474
475 PP(pp_unstack)
476 {
477     PERL_CONTEXT *cx;
478     PERL_ASYNC_CHECK();
479     TAINT_NOT;          /* Each statement is presumed innocent */
480     cx  = CX_CUR();
481     rpp_popfree_to(PL_stack_base + CX_CUR()->blk_oldsp);
482     FREETMPS;
483     if (!(PL_op->op_flags & OPf_SPECIAL)) {
484         assert(CxTYPE(cx) == CXt_BLOCK || CxTYPE_is_LOOP(cx));
485         CX_LEAVE_SCOPE(cx);
486     }
487     return NORMAL;
488 }
489
490
491 /* The main body of pp_concat, not including the magic/overload and
492  * stack handling.
493  * It does targ = left . right.
494  * Moved into a separate function so that pp_multiconcat() can use it
495  * too.
496  */
497
498 PERL_STATIC_INLINE void
499 S_do_concat(pTHX_ SV *left, SV *right, SV *targ, U8 targmy)
500 {
501     bool lbyte;
502     STRLEN rlen;
503     const char *rpv = NULL;
504     bool rbyte = FALSE;
505     bool rcopied = FALSE;
506
507     if (TARG == right && right != left) { /* $r = $l.$r */
508         rpv = SvPV_nomg_const(right, rlen);
509         rbyte = !DO_UTF8(right);
510         right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
511         rpv = SvPV_const(right, rlen);  /* no point setting UTF-8 here */
512         rcopied = TRUE;
513     }
514
515     if (TARG != left) { /* not $l .= $r */
516         STRLEN llen;
517         const char* const lpv = SvPV_nomg_const(left, llen);
518         lbyte = !DO_UTF8(left);
519         sv_setpvn(TARG, lpv, llen);
520         if (!lbyte)
521             SvUTF8_on(TARG);
522         else
523             SvUTF8_off(TARG);
524     }
525     else { /* $l .= $r   and   left == TARG */
526         if (!SvOK(left)) {
527             if ((left == right                          /* $l .= $l */
528                  || targmy)                             /* $l = $l . $r */
529                 && ckWARN(WARN_UNINITIALIZED)
530                 )
531                 report_uninit(left);
532             SvPVCLEAR(left);
533         }
534         else {
535             SvPV_force_nomg_nolen(left);
536         }
537         lbyte = !DO_UTF8(left);
538         if (IN_BYTES)
539             SvUTF8_off(left);
540     }
541
542     if (!rcopied) {
543         rpv = SvPV_nomg_const(right, rlen);
544         rbyte = !DO_UTF8(right);
545     }
546     if (lbyte != rbyte) {
547         if (lbyte)
548             sv_utf8_upgrade_nomg(TARG);
549         else {
550             if (!rcopied)
551                 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
552             sv_utf8_upgrade_nomg(right);
553             rpv = SvPV_nomg_const(right, rlen);
554         }
555     }
556     sv_catpvn_nomg(TARG, rpv, rlen);
557     SvSETMAGIC(TARG);
558 }
559
560
561 PP_wrapped(pp_concat, 2, 0)
562 {
563   dSP; dATARGET; tryAMAGICbin_MG(concat_amg, AMGf_assign);
564   {
565     dPOPTOPssrl;
566     S_do_concat(aTHX_ left, right, targ, PL_op->op_private & OPpTARGET_MY);
567     SETs(TARG);
568     RETURN;
569   }
570 }
571
572
573 /* pp_multiconcat()
574
575 Concatenate one or more args, possibly interleaved with constant string
576 segments. The result may be assigned to, or appended to, a variable or
577 expression.
578
579 Several op_flags and/or op_private bits indicate what the target is, and
580 whether it's appended to. Valid permutations are:
581
582     -                                  (PADTMP) = (A.B.C....)
583     OPpTARGET_MY                       $lex     = (A.B.C....)
584     OPpTARGET_MY,OPpLVAL_INTRO         my $lex  = (A.B.C....)
585     OPpTARGET_MY,OPpMULTICONCAT_APPEND $lex    .= (A.B.C....)
586     OPf_STACKED                        expr     = (A.B.C....)
587     OPf_STACKED,OPpMULTICONCAT_APPEND  expr    .= (A.B.C....)
588
589 Other combinations like (A.B).(C.D) are not optimised into a multiconcat
590 op, as it's too hard to get the correct ordering of ties, overload etc.
591
592 In addition:
593
594     OPpMULTICONCAT_FAKE:       not a real concat, instead an optimised
595                                sprintf "...%s...". Don't call '.'
596                                overloading: only use '""' overloading.
597
598     OPpMULTICONCAT_STRINGIFY:  the RHS was of the form
599                                "...$a...$b..." rather than
600                                "..." . $a . "..." . $b . "..."
601
602 An OP_MULTICONCAT is of type UNOP_AUX. The fixed slots of the aux array are
603 defined with PERL_MULTICONCAT_IX_FOO constants, where:
604
605
606     FOO       index description
607     --------  ----- ----------------------------------
608     NARGS     0     number of arguments
609     PLAIN_PV  1     non-utf8 constant string
610     PLAIN_LEN 2     non-utf8 constant string length
611     UTF8_PV   3     utf8 constant string
612     UTF8_LEN  4     utf8 constant string length
613     LENGTHS   5     first of nargs+1 const segment lengths
614
615 The idea is that a general string concatenation will have a fixed (known
616 at compile time) number of variable args, interspersed with constant
617 strings, e.g. "a=$a b=$b\n"
618
619 All the constant string segments "a=", " b=" and "\n" are stored as a
620 single string "a= b=\n", pointed to from the PLAIN_PV/UTF8_PV slot, along
621 with a series of segment lengths: e.g. 2,3,1. In the case where the
622 constant string is plain but has a different utf8 representation, both
623 variants are stored, and two sets of (nargs+1) segments lengths are stored
624 in the slots beginning at PERL_MULTICONCAT_IX_LENGTHS.
625
626 A segment length of -1 indicates that there is no constant string at that
627 point; this distinguishes between e.g. ($a . $b) and ($a . "" . $b), which
628 have differing overloading behaviour.
629
630 */
631
632
633 /* how many stack arguments a multiconcat op expects */
634 #ifdef PERL_RC_STACK
635 STATIC I32
636 S_multiconcat_argcount(pTHX)
637 {
638     UNOP_AUX_item *aux = cUNOP_AUXx(PL_op)->op_aux;
639     SSize_t nargs = aux[PERL_MULTICONCAT_IX_NARGS].ssize;
640     if (PL_op->op_flags & OPf_STACKED)
641         nargs++;
642     return nargs;
643 }
644 #endif
645
646 PP_wrapped(pp_multiconcat, S_multiconcat_argcount(aTHX), 0)
647 {
648     dSP;
649     SV *targ;                /* The SV to be assigned or appended to */
650     char *targ_pv;           /* where within SvPVX(targ) we're writing to */
651     STRLEN targ_len;         /* SvCUR(targ) */
652     SV **toparg;             /* the highest arg position on the stack */
653     UNOP_AUX_item *aux;      /* PL_op->op_aux buffer */
654     UNOP_AUX_item *const_lens; /* the segment length array part of aux */
655     const char *const_pv;    /* the current segment of the const string buf */
656     SSize_t nargs;           /* how many args were expected */
657     SSize_t stack_adj;       /* how much to adjust SP on return */
658     STRLEN grow;             /* final size of destination string (targ) */
659     UV targ_count;           /* how many times targ has appeared on the RHS */
660     bool is_append;          /* OPpMULTICONCAT_APPEND flag is set */
661     bool slow_concat;        /* args too complex for quick concat */
662     U32  dst_utf8;           /* the result will be utf8 (indicate this with
663                                 SVf_UTF8 in a U32, rather than using bool,
664                                 for ease of testing and setting) */
665     /* for each arg, holds the result of an SvPV() call */
666     struct multiconcat_svpv {
667         const char   *pv;
668         SSize_t       len;
669     }
670         *targ_chain,         /* chain of slots where targ has appeared on RHS */
671         *svpv_p,             /* ptr for looping through svpv_buf */
672         *svpv_base,          /* first slot (may be greater than svpv_buf), */
673         *svpv_end,           /* and slot after highest result so far, of: */
674         svpv_buf[PERL_MULTICONCAT_MAXARG]; /* buf for storing SvPV() results */
675
676     aux   = cUNOP_AUXx(PL_op)->op_aux;
677     stack_adj = nargs = aux[PERL_MULTICONCAT_IX_NARGS].ssize;
678     is_append = cBOOL(PL_op->op_private & OPpMULTICONCAT_APPEND);
679
680     /* get targ from the stack or pad */
681
682     if (PL_op->op_flags & OPf_STACKED) {
683         if (is_append) {
684             /* for 'expr .= ...', expr is the bottom item on the stack */
685             targ = SP[-nargs];
686             stack_adj++;
687         }
688         else
689             /* for 'expr = ...', expr is the top item on the stack */
690             targ = POPs;
691     }
692     else {
693         SV **svp = &(PAD_SVl(PL_op->op_targ));
694         targ = *svp;
695         if (PL_op->op_private & OPpLVAL_INTRO) {
696             assert(PL_op->op_private & OPpTARGET_MY);
697             save_clearsv(svp);
698         }
699         if (!nargs)
700             /* $lex .= "const" doesn't cause anything to be pushed */
701             EXTEND(SP,1);
702     }
703
704     toparg = SP;
705     SP -= (nargs - 1);
706     grow          = 1;    /* allow for '\0' at minimum */
707     targ_count    = 0;
708     targ_chain    = NULL;
709     targ_len      = 0;
710     svpv_end      = svpv_buf;
711                     /* only utf8 variants of the const strings? */
712     dst_utf8      = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv ? 0 : SVf_UTF8;
713
714
715     /* --------------------------------------------------------------
716      * Phase 1:
717      *
718      * stringify (i.e. SvPV()) every arg and store the resultant pv/len/utf8
719      * triplets in svpv_buf[]. Also increment 'grow' by the args' lengths.
720      *
721      * utf8 is indicated by storing a negative length.
722      *
723      * Where an arg is actually targ, the stringification is deferred:
724      * the length is set to 0, and the slot is added to targ_chain.
725      *
726      * If a magic, overloaded, or otherwise weird arg is found, which
727      * might have side effects when stringified, the loop is abandoned and
728      * we goto a code block where a more basic 'emulate calling
729      * pp_cpncat() on each arg in turn' is done.
730      */
731
732     for (; SP <= toparg; SP++, svpv_end++) {
733         U32 utf8;
734         STRLEN len;
735         SV *sv;
736
737         assert(svpv_end - svpv_buf < PERL_MULTICONCAT_MAXARG);
738
739         sv = *SP;
740
741         /* this if/else chain is arranged so that common/simple cases
742          * take few conditionals */
743
744         if (LIKELY((SvFLAGS(sv) & (SVs_GMG|SVf_ROK|SVf_POK)) == SVf_POK)) {
745             /* common case: sv is a simple non-magical PV */
746             if (targ == sv) {
747                 /* targ appears on RHS.
748                  * Delay storing PV pointer; instead, add slot to targ_chain
749                  * so it can be populated later, after targ has been grown and
750                  * we know its final SvPVX() address.
751                  */
752               targ_on_rhs:
753                 svpv_end->len = 0; /* zerojng here means we can skip
754                                       updating later if targ_len == 0 */
755                 svpv_end->pv  = (char*)targ_chain;
756                 targ_chain    = svpv_end;
757                 targ_count++;
758                 continue;
759             }
760
761             len           = SvCUR(sv);
762             svpv_end->pv  = SvPVX(sv);
763         }
764         else if (UNLIKELY(SvFLAGS(sv) & (SVs_GMG|SVf_ROK)))
765             /* may have side effects: tie, overload etc.
766              * Abandon 'stringify everything first' and handle
767              * args in strict order. Note that already-stringified args
768              * will be reprocessed, which is safe because the each first
769              * stringification would have been idempotent.
770              */
771             goto do_magical;
772         else if (SvNIOK(sv)) {
773             if (targ == sv)
774               goto targ_on_rhs;
775             /* stringify general valid scalar */
776             svpv_end->pv = sv_2pv_flags(sv, &len, 0);
777         }
778         else if (!SvOK(sv)) {
779             if (ckWARN(WARN_UNINITIALIZED))
780                 /* an undef value in the presence of warnings may trigger
781                  * side affects */
782                 goto do_magical;
783             svpv_end->pv = "";
784             len = 0;
785         }
786         else
787             goto do_magical; /* something weird */
788
789         utf8 = (SvFLAGS(sv) & SVf_UTF8);
790         dst_utf8   |= utf8;
791         ASSUME(len < SSize_t_MAX);
792         svpv_end->len = utf8 ? -(SSize_t)len : (SSize_t)len;
793         grow += len;
794     }
795
796     /* --------------------------------------------------------------
797      * Phase 2:
798      *
799      * Stringify targ:
800      *
801      * if targ appears on the RHS or is appended to, force stringify it;
802      * otherwise set it to "". Then set targ_len.
803      */
804
805     if (is_append) {
806         /* abandon quick route if using targ might have side effects */
807         if (UNLIKELY(SvFLAGS(targ) & (SVs_GMG|SVf_ROK)))
808             goto do_magical;
809
810         if (SvOK(targ)) {
811             U32 targ_utf8;
812           stringify_targ:
813             SvPV_force_nomg_nolen(targ);
814             targ_utf8 = SvFLAGS(targ) & SVf_UTF8;
815             if (UNLIKELY(dst_utf8 & ~targ_utf8)) {
816                  if (LIKELY(!IN_BYTES))
817                     sv_utf8_upgrade_nomg(targ);
818             }
819             else
820                 dst_utf8 |= targ_utf8;
821
822             targ_len = SvCUR(targ);
823             grow += targ_len * (targ_count + is_append);
824             goto phase3;
825         }
826         else if (ckWARN(WARN_UNINITIALIZED))
827             /* warning might have side effects */
828             goto do_magical;
829         /* the undef targ will be silently SvPVCLEAR()ed below */
830     }
831     else if (UNLIKELY(SvTYPE(targ) >= SVt_REGEXP)) {
832         /* Assigning to some weird LHS type. Don't force the LHS to be an
833          * empty string; instead, do things 'long hand' by using the
834          * overload code path, which concats to a TEMP sv and does
835          * sv_catsv() calls rather than COPY()s. This ensures that even
836          * bizarre code like this doesn't break or crash:
837          *    *F = *F . *F.
838          * (which makes the 'F' typeglob an alias to the
839          * '*main::F*main::F' typeglob).
840          */
841         goto do_magical;
842     }
843     else if (targ_chain)
844         /* targ was found on RHS.
845          * Force stringify it, using the same code as the append branch
846          * above, except that we don't need the magic/overload/undef
847          * checks as these will already have been done in the phase 1
848          * loop.
849          */
850         goto stringify_targ;
851
852     /* unrolled SvPVCLEAR() - mostly: no need to grow or set SvCUR() to 0;
853      * those will be done later. */
854     SV_CHECK_THINKFIRST_COW_DROP(targ);
855     SvUPGRADE(targ, SVt_PV);
856     SvFLAGS(targ) &= ~(SVf_OK|SVf_IVisUV|SVf_UTF8);
857     SvFLAGS(targ) |= (SVf_POK|SVp_POK|dst_utf8);
858
859   phase3:
860
861     /* --------------------------------------------------------------
862      * Phase 3:
863      *
864      * UTF-8 tweaks and grow targ:
865      *
866      * Now that we know the length and utf8-ness of both the targ and
867      * args, grow targ to the size needed to accumulate all the args, based
868      * on whether targ appears on the RHS, whether we're appending, and
869      * whether any non-utf8 args expand in size if converted to utf8.
870      *
871      * For the latter, if dst_utf8 we scan non-utf8 args looking for
872      * variant chars, and adjust the svpv->len value of those args to the
873      * utf8 size and negate it to flag them. At the same time we un-negate
874      * the lens of any utf8 args since after this phase we no longer care
875      * whether an arg is utf8 or not.
876      *
877      * Finally, initialise const_lens and const_pv based on utf8ness.
878      * Note that there are 3 permutations:
879      *
880      * * If the constant string is invariant whether utf8 or not (e.g. "abc"),
881      *   then aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN] are the same as
882      *        aux[PERL_MULTICONCAT_IX_UTF8_PV/LEN] and there is one set of
883      *   segment lengths.
884      *
885      * * If the string is fully utf8, e.g. "\x{100}", then
886      *   aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN] == (NULL,0) and there is
887      *   one set of segment lengths.
888      *
889      * * If the string has different plain and utf8 representations
890      *   (e.g. "\x80"), then aux[PERL_MULTICONCAT_IX_PLAIN_PV/LEN]]
891      *   holds the plain rep, while aux[PERL_MULTICONCAT_IX_UTF8_PV/LEN]
892      *   holds the utf8 rep, and there are 2 sets of segment lengths,
893      *   with the utf8 set following after the plain set.
894      *
895      * On entry to this section the (pv,len) pairs in svpv_buf have the
896      * following meanings:
897      *    (pv,  len) a plain string
898      *    (pv, -len) a utf8 string
899      *    (NULL,  0) left-most targ \ linked together R-to-L
900      *    (next,  0) other targ     / in targ_chain
901      */
902
903     /* turn off utf8 handling if 'use bytes' is in scope */
904     if (UNLIKELY(dst_utf8 && IN_BYTES)) {
905         dst_utf8 = 0;
906         SvUTF8_off(targ);
907         /* undo all the negative lengths which flag utf8-ness */
908         for (svpv_p = svpv_buf; svpv_p < svpv_end; svpv_p++) {
909             SSize_t len = svpv_p->len;
910             if (len < 0)
911                 svpv_p->len = -len;
912         }
913     }
914
915     /* grow += total of lengths of constant string segments */
916     {
917         SSize_t len;
918         len = aux[dst_utf8 ? PERL_MULTICONCAT_IX_UTF8_LEN
919                            : PERL_MULTICONCAT_IX_PLAIN_LEN].ssize;
920         slow_concat = cBOOL(len);
921         grow += len;
922     }
923
924     const_lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
925
926     if (dst_utf8) {
927         const_pv = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
928         if (   aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv
929             && const_pv != aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv)
930             /* separate sets of lengths for plain and utf8 */
931             const_lens += nargs + 1;
932
933         /* If the result is utf8 but some of the args aren't,
934          * calculate how much extra growth is needed for all the chars
935          * which will expand to two utf8 bytes.
936          * Also, if the growth is non-zero, negate the length to indicate
937          * that this is a variant string. Conversely, un-negate the
938          * length on utf8 args (which was only needed to flag non-utf8
939          * args in this loop */
940         for (svpv_p = svpv_buf; svpv_p < svpv_end; svpv_p++) {
941             SSize_t len, extra;
942
943             len = svpv_p->len;
944             if (len <= 0) {
945                 svpv_p->len = -len;
946                 continue;
947             }
948
949             extra = variant_under_utf8_count((U8 *) svpv_p->pv,
950                                              (U8 *) svpv_p->pv + len);
951             if (UNLIKELY(extra)) {
952                 grow       += extra;
953                               /* -ve len indicates special handling */
954                 svpv_p->len = -(len + extra);
955                 slow_concat = TRUE;
956             }
957         }
958     }
959     else
960         const_pv = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
961
962     /* unrolled SvGROW(), except don't check for SVf_IsCOW, which should
963      * already have been dropped */
964     assert(!SvIsCOW(targ));
965     targ_pv = (SvLEN(targ) < (grow) ? sv_grow(targ,grow) : SvPVX(targ));
966
967
968     /* --------------------------------------------------------------
969      * Phase 4:
970      *
971      * Now that targ has been grown, we know the final address of the targ
972      * PVX, if needed. Preserve / move targ contents if appending or if
973      * targ appears on RHS.
974      *
975      * Also update svpv_buf slots in targ_chain.
976      *
977      * Don't bother with any of this if the target length is zero:
978      * targ_len is set to zero unless we're appending or targ appears on
979      * RHS.  And even if it is, we can optimise by skipping this chunk of
980      * code for zero targ_len. In the latter case, we don't need to update
981      * the slots in targ_chain with the (zero length) target string, since
982      * we set the len in such slots to 0 earlier, and since the Copy() is
983      * skipped on zero length, it doesn't matter what svpv_p->pv contains.
984      *
985      * On entry to this section the (pv,len) pairs in svpv_buf have the
986      * following meanings:
987      *    (pv,  len)         a pure-plain or utf8 string
988      *    (pv, -(len+extra)) a plain string which will expand by 'extra'
989      *                         bytes when converted to utf8
990      *    (NULL,  0)         left-most targ \ linked together R-to-L
991      *    (next,  0)         other targ     / in targ_chain
992      *
993      * On exit, the targ contents will have been moved to the
994      * earliest place they are needed (e.g. $x = "abc$x" will shift them
995      * 3 bytes, while $x .= ... will leave them at the beginning);
996      * and dst_pv will point to the location within SvPVX(targ) where the
997      * next arg should be copied.
998      */
999
1000     svpv_base = svpv_buf;
1001
1002     if (targ_len) {
1003         struct multiconcat_svpv *tc_stop;
1004         char *targ_buf = targ_pv; /* ptr to original targ string */
1005
1006         assert(is_append || targ_count);
1007
1008         if (is_append) {
1009             targ_pv += targ_len;
1010             tc_stop = NULL;
1011         }
1012         else {
1013             /* The targ appears on RHS, e.g. '$t = $a . $t . $t'.
1014              * Move the current contents of targ to the first
1015              * position where it's needed, and use that as the src buffer
1016              * for any further uses (such as the second RHS $t above).
1017              * In calculating the first position, we need to sum the
1018              * lengths of all consts and args before that.
1019              */
1020
1021             UNOP_AUX_item *lens = const_lens;
1022                                 /* length of first const string segment */
1023             STRLEN offset       = lens->ssize > 0 ? lens->ssize : 0;
1024
1025             assert(targ_chain);
1026             svpv_p = svpv_base;
1027
1028             for (;;) {
1029                 SSize_t len;
1030                 if (!svpv_p->pv)
1031                     break; /* the first targ argument */
1032                 /* add lengths of the next arg and const string segment */
1033                 len = svpv_p->len;
1034                 if (len < 0)  /* variant args have this */
1035                     len = -len;
1036                 offset += (STRLEN)len;
1037                 len = (++lens)->ssize;
1038                 offset += (len >= 0) ? (STRLEN)len : 0;
1039                 if (!offset) {
1040                     /* all args and consts so far are empty; update
1041                      * the start position for the concat later */
1042                     svpv_base++;
1043                     const_lens++;
1044                 }
1045                 svpv_p++;
1046                 assert(svpv_p < svpv_end);
1047             }
1048
1049             if (offset) {
1050                 targ_buf += offset;
1051                 Move(targ_pv, targ_buf, targ_len, char);
1052                 /* a negative length implies don't Copy(), but do increment */
1053                 svpv_p->len = -((SSize_t)targ_len);
1054                 slow_concat = TRUE;
1055             }
1056             else {
1057                 /* skip the first targ copy */
1058                 svpv_base++;
1059                 const_lens++;
1060                 targ_pv += targ_len;
1061             }
1062
1063             /* Don't populate the first targ slot in the loop below; it's
1064              * either not used because we advanced svpv_base beyond it, or
1065              * we already stored the special -targ_len value in it
1066              */
1067             tc_stop = svpv_p;
1068         }
1069
1070         /* populate slots in svpv_buf representing targ on RHS */
1071         while (targ_chain != tc_stop) {
1072             struct multiconcat_svpv *p = targ_chain;
1073             targ_chain = (struct multiconcat_svpv *)(p->pv);
1074             p->pv  = targ_buf;
1075             p->len = (SSize_t)targ_len;
1076         }
1077     }
1078
1079
1080     /* --------------------------------------------------------------
1081      * Phase 5:
1082      *
1083      * Append all the args in svpv_buf, plus the const strings, to targ.
1084      *
1085      * On entry to this section the (pv,len) pairs in svpv_buf have the
1086      * following meanings:
1087      *    (pv,  len)         a pure-plain or utf8 string (which may be targ)
1088      *    (pv, -(len+extra)) a plain string which will expand by 'extra'
1089      *                         bytes when converted to utf8
1090      *    (0,  -len)         left-most targ, whose content has already
1091      *                         been copied. Just advance targ_pv by len.
1092      */
1093
1094     /* If there are no constant strings and no special case args
1095      * (svpv_p->len < 0), use a simpler, more efficient concat loop
1096      */
1097     if (!slow_concat) {
1098         for (svpv_p = svpv_base; svpv_p < svpv_end; svpv_p++) {
1099             SSize_t len = svpv_p->len;
1100             if (!len)
1101                 continue;
1102             Copy(svpv_p->pv, targ_pv, len, char);
1103             targ_pv += len;
1104         }
1105         const_lens += (svpv_end - svpv_base + 1);
1106     }
1107     else {
1108         /* Note that we iterate the loop nargs+1 times: to append nargs
1109          * arguments and nargs+1 constant strings. For example, "-$a-$b-"
1110          */
1111         svpv_p = svpv_base;
1112
1113         for (;;) {
1114             SSize_t len = (const_lens++)->ssize;
1115
1116             /* append next const string segment */
1117             if (len > 0) {
1118                 Copy(const_pv, targ_pv, len, char);
1119                 targ_pv   += len;
1120                 const_pv += len;
1121             }
1122
1123             if (svpv_p == svpv_end)
1124                 break;
1125
1126             /* append next arg */
1127             len = svpv_p->len;
1128
1129             if (LIKELY(len > 0)) {
1130                 Copy(svpv_p->pv, targ_pv, len, char);
1131                 targ_pv += len;
1132             }
1133             else if (UNLIKELY(len < 0)) {
1134                 /* negative length indicates two special cases */
1135                 const char *p = svpv_p->pv;
1136                 len = -len;
1137                 if (UNLIKELY(p)) {
1138                     /* copy plain-but-variant pv to a utf8 targ */
1139                     char * end_pv = targ_pv + len;
1140                     assert(dst_utf8);
1141                     while (targ_pv < end_pv) {
1142                         U8 c = (U8) *p++;
1143                         append_utf8_from_native_byte(c, (U8**)&targ_pv);
1144                     }
1145                 }
1146                 else
1147                     /* arg is already-copied targ */
1148                     targ_pv += len;
1149             }
1150
1151             ++svpv_p;
1152         }
1153     }
1154
1155     *targ_pv = '\0';
1156     SvCUR_set(targ, targ_pv - SvPVX(targ));
1157     assert(grow >= SvCUR(targ) + 1);
1158     assert(SvLEN(targ) >= SvCUR(targ) + 1);
1159
1160     /* --------------------------------------------------------------
1161      * Phase 6:
1162      *
1163      * return result
1164      */
1165
1166     SP -= stack_adj;
1167     SvTAINT(targ);
1168     SETTARG;
1169     RETURN;
1170
1171     /* --------------------------------------------------------------
1172      * Phase 7:
1173      *
1174      * We only get here if any of the args (or targ too in the case of
1175      * append) have something which might cause side effects, such
1176      * as magic, overload, or an undef value in the presence of warnings.
1177      * In that case, any earlier attempt to stringify the args will have
1178      * been abandoned, and we come here instead.
1179      *
1180      * Here, we concat each arg in turn the old-fashioned way: essentially
1181      * emulating pp_concat() in a loop. This means that all the weird edge
1182      * cases will be handled correctly, if not necessarily speedily.
1183      *
1184      * Note that some args may already have been stringified - those are
1185      * processed again, which is safe, since only args without side-effects
1186      * were stringified earlier.
1187      */
1188
1189   do_magical:
1190     {
1191         SSize_t i, n;
1192         SV *left = NULL;
1193         SV *right;
1194         SV* nexttarg;
1195         bool nextappend;
1196         U32 utf8 = 0;
1197         SV **svp;
1198         const char    *cpv  = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
1199         SV            *csv  = NULL; /* SV which will hold cpv */
1200         UNOP_AUX_item *lens = aux + PERL_MULTICONCAT_IX_LENGTHS;
1201         Size_t arg_count = 0; /* how many args have been processed */
1202
1203         if (!cpv) {
1204             cpv = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
1205             utf8 = SVf_UTF8;
1206         }
1207
1208         svp = toparg - nargs + 1;
1209
1210         /* iterate for:
1211          *   nargs arguments,
1212          *   plus possible nargs+1 consts,
1213          *   plus, if appending, a final targ in an extra last iteration
1214          */
1215
1216         n = nargs *2 + 1;
1217         for (i = 0; i <= n; i++) {
1218             SSize_t len;
1219
1220             /* if necessary, stringify the final RHS result in
1221              * something like $targ .= "$a$b$c" - simulating
1222              * pp_stringify
1223              */
1224             if (    i == n
1225                 && (PL_op->op_private &OPpMULTICONCAT_STRINGIFY)
1226                 && !(SvPOK(left))
1227                 /* extra conditions for backwards compatibility:
1228                  * probably incorrect, but keep the existing behaviour
1229                  * for now. The rules are:
1230                  *     $x   = "$ov"     single arg: stringify;
1231                  *     $x   = "$ov$y"   multiple args: don't stringify,
1232                  *     $lex = "$ov$y$z" except TARGMY with at least 2 concats
1233                  */
1234                 && (   arg_count == 1
1235                     || (     arg_count >= 3
1236                         && !is_append
1237                         &&  (PL_op->op_private & OPpTARGET_MY)
1238                         && !(PL_op->op_private & OPpLVAL_INTRO)
1239                        )
1240                    )
1241             )
1242             {
1243                 assert(aux[PERL_MULTICONCAT_IX_PADTMP2].pad_offset);
1244                 SV *tmp = PAD_SV(aux[PERL_MULTICONCAT_IX_PADTMP2].pad_offset);
1245                 sv_copypv(tmp, left);
1246                 SvSETMAGIC(tmp);
1247                 left = tmp;
1248             }
1249
1250             /* do one extra iteration to handle $targ in $targ .= ... */
1251             if (i == n && !is_append)
1252                 break;
1253
1254             /* get the next arg SV or regen the next const SV */
1255             len = lens[i >> 1].ssize;
1256             if (i == n) {
1257                 /* handle the final targ .= (....) */
1258                 right = left;
1259                 left = targ;
1260             }
1261             else if (i & 1)
1262                 right = svp[(i >> 1)];
1263             else if (len < 0)
1264                 continue; /* no const in this position */
1265             else {
1266                 /* Use one of our PADTMPs to fake up the SV which would
1267                  * have been returned by an OP_CONST.  Try to reuse it if
1268                  * possible. If the refcount has gone up, something like
1269                  * overload code has taken a reference to it, so abandon
1270                  * it */
1271                 if (!csv || SvREFCNT(csv) > 1 || SvLEN(csv) != 0) {
1272                     if (csv)
1273                         csv = newSV_type_mortal(SVt_PV);
1274                     else {
1275                         assert(aux[PERL_MULTICONCAT_IX_PADTMP1].pad_offset);
1276                         csv = PAD_SV(
1277                                 aux[PERL_MULTICONCAT_IX_PADTMP1].pad_offset);
1278                         SvUPGRADE(csv, SVt_PV);
1279                     }
1280
1281                     if (utf8)
1282                         SvUTF8_on(csv);
1283                     SvREADONLY_on(csv);
1284                     SvPOK_on(csv);
1285                 }
1286                 /* use the const string buffer directly with the
1287                  * SvLEN==0 trick */
1288
1289                 /* cast away constness because we think we know it's safe
1290                  * (SvREADONLY) */
1291                 SvPV_set(csv, (char *)cpv);
1292                 SvLEN_set(csv, 0);
1293                 SvCUR_set(csv, len);
1294
1295                 right = csv;
1296                 cpv += len;
1297             }
1298
1299             arg_count++;
1300
1301             if (arg_count <= 1) {
1302                 left = right;
1303                 continue; /* need at least two SVs to concat together */
1304             }
1305
1306             if (arg_count == 2 && i < n) {
1307                 /* for the first concat, use one of the PADTMPs to emulate
1308                  * the PADTMP from OP_CONST. In later iterations this will
1309                  * be appended to */
1310                 nexttarg = PAD_SV(aux[PERL_MULTICONCAT_IX_PADTMP0].pad_offset);
1311                 nextappend = FALSE;
1312             }
1313             else {
1314                 nexttarg = left;
1315                 nextappend = TRUE;
1316             }
1317
1318             /* Handle possible overloading.
1319              * This is basically an unrolled
1320              *     tryAMAGICbin_MG(concat_amg, AMGf_assign);
1321              * and
1322              *     Perl_try_amagic_bin()
1323              * call, but using left and right rather than SP[-1], SP[0],
1324              * and not relying on OPf_STACKED implying .=
1325              */
1326
1327             if ((SvFLAGS(left)|SvFLAGS(right)) & (SVf_ROK|SVs_GMG)) {
1328                 SvGETMAGIC(left);
1329                 if (left != right)
1330                     SvGETMAGIC(right);
1331
1332                 if ((SvAMAGIC(left) || SvAMAGIC(right))
1333                     /* sprintf doesn't do concat overloading,
1334                      * but allow for $x .= sprintf(...)
1335                      */
1336                     && (   !(PL_op->op_private & OPpMULTICONCAT_FAKE)
1337                         || i == n)
1338                     )
1339                 {
1340                     SV * const tmpsv = amagic_call(left, right, concat_amg,
1341                                                 (nextappend ? AMGf_assign: 0));
1342                     if (tmpsv) {
1343                         /* NB: tryAMAGICbin_MG() includes an OPpTARGET_MY test
1344                          * here, which isn't needed as any implicit
1345                          * assign done under OPpTARGET_MY is done after
1346                          * this loop */
1347                         if (nextappend) {
1348                             sv_setsv(left, tmpsv);
1349                             SvSETMAGIC(left);
1350                         }
1351                         else
1352                             left = tmpsv;
1353                         continue;
1354                     }
1355                 }
1356
1357                 /* if both args are the same magical value, make one a copy */
1358                 if (left == right && SvGMAGICAL(left)) {
1359                     SV * targetsv = right;
1360                     /* Print the uninitialized warning now, so it includes the
1361                      * variable name. */
1362                     if (!SvOK(right)) {
1363                         if (ckWARN(WARN_UNINITIALIZED))
1364                             report_uninit(right);
1365                         targetsv = &PL_sv_no;
1366                     }
1367                     left = sv_mortalcopy_flags(targetsv, 0);
1368                     SvGETMAGIC(right);
1369                 }
1370             }
1371
1372             /* nexttarg = left . right */
1373             S_do_concat(aTHX_ left, right, nexttarg, 0);
1374             left = nexttarg;
1375         }
1376
1377         SP = toparg - stack_adj + 1;
1378
1379         /* Return the result of all RHS concats, unless this op includes
1380          * an assign ($lex = x.y.z or expr = x.y.z), in which case copy
1381          * to target (which will be $lex or expr).
1382          * If we are appending, targ will already have been appended to in
1383          * the loop */
1384         if (  !is_append
1385             && (   (PL_op->op_flags   & OPf_STACKED)
1386                 || (PL_op->op_private & OPpTARGET_MY))
1387         ) {
1388             sv_setsv(targ, left);
1389             SvSETMAGIC(targ);
1390         }
1391         else
1392             targ = left;
1393         SETs(targ);
1394         RETURN;
1395     }
1396 }
1397
1398
1399 /* push the elements of av onto the stack.
1400  * Returns PL_op->op_next to allow tail-call optimisation of its callers */
1401
1402 STATIC OP*
1403 S_pushav(pTHX_ AV* const av)
1404 {
1405     const SSize_t maxarg = AvFILL(av) + 1;
1406     rpp_extend(maxarg);
1407     if (UNLIKELY(SvRMAGICAL(av))) {
1408         PADOFFSET i;
1409         for (i=0; i < (PADOFFSET)maxarg; i++) {
1410             SV ** const svp = av_fetch(av, i, FALSE);
1411             rpp_push_1(LIKELY(svp)
1412                        ? *svp
1413                        : UNLIKELY(PL_op->op_flags & OPf_MOD)
1414                           ? av_nonelem(av,i)
1415                           : &PL_sv_undef
1416             );
1417         }
1418     }
1419     else {
1420         PADOFFSET i;
1421         for (i=0; i < (PADOFFSET)maxarg; i++) {
1422             SV *sv = AvARRAY(av)[i];
1423             rpp_push_1(LIKELY(sv)
1424                        ? sv
1425                        : UNLIKELY(PL_op->op_flags & OPf_MOD)
1426                           ? av_nonelem(av,i)
1427                           : &PL_sv_undef
1428             );
1429         }
1430     }
1431     return NORMAL;
1432 }
1433
1434
1435 /* ($lex1,@lex2,...)   or my ($lex1,@lex2,...)  */
1436
1437 PP(pp_padrange)
1438 {
1439     PADOFFSET base = PL_op->op_targ;
1440     int count = (int)(PL_op->op_private) & OPpPADRANGE_COUNTMASK;
1441     if (PL_op->op_flags & OPf_SPECIAL) {
1442         /* fake the RHS of my ($x,$y,..) = @_ */
1443         PUSHMARK(PL_stack_sp);
1444         (void)S_pushav(aTHX_ GvAVn(PL_defgv));
1445     }
1446
1447     /* note, this is only skipped for compile-time-known void cxt */
1448     if ((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID) {
1449         int i;
1450
1451         rpp_extend(count);
1452         PUSHMARK(PL_stack_sp);
1453         for (i = 0; i <count; i++)
1454             rpp_push_1(PAD_SV(base+i));
1455     }
1456
1457     if (PL_op->op_private & OPpLVAL_INTRO) {
1458         SV **svp = &(PAD_SVl(base));
1459         const UV payload = (UV)(
1460                       (base << (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT))
1461                     | (count << SAVE_TIGHT_SHIFT)
1462                     | SAVEt_CLEARPADRANGE);
1463         int i;
1464
1465         STATIC_ASSERT_STMT(OPpPADRANGE_COUNTMASK + 1 == (1 << OPpPADRANGE_COUNTSHIFT));
1466         assert((payload >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
1467                 == (Size_t)base);
1468         {
1469             dSS_ADD;
1470             SS_ADD_UV(payload);
1471             SS_ADD_END(1);
1472         }
1473
1474         for (i = 0; i <count; i++)
1475             SvPADSTALE_off(*svp++); /* mark lexical as active */
1476     }
1477     return NORMAL;
1478 }
1479
1480
1481 PP(pp_padsv)
1482 {
1483     {
1484         OP * const op = PL_op;
1485         /* access PL_curpad once */
1486         SV ** const padentry = &(PAD_SVl(op->op_targ));
1487         {
1488             dTARG;
1489             TARG = *padentry;
1490             rpp_xpush_1(TARG);
1491         }
1492         if (op->op_flags & OPf_MOD) {
1493             if (op->op_private & OPpLVAL_INTRO)
1494                 if (!(op->op_private & OPpPAD_STATE))
1495                     save_clearsv(padentry);
1496             if (op->op_private & OPpDEREF) {
1497                 /* *sp is equivalent to TARG here.  Using *sp rather
1498                    than TARG reduces the scope of TARG, so it does not
1499                    span the call to save_clearsv, resulting in smaller
1500                    machine code. */
1501                 rpp_replace_1_1(
1502                     vivify_ref(*PL_stack_sp, op->op_private & OPpDEREF));
1503             }
1504         }
1505         return op->op_next;
1506     }
1507 }
1508
1509 PP_wrapped(pp_readline, ((PL_op->op_flags & OPf_STACKED) ? 2 : 1), 0)
1510 {
1511     dSP;
1512     /* pp_coreargs pushes a NULL to indicate no args passed to
1513      * CORE::readline() */
1514     if (TOPs) {
1515         SvGETMAGIC(TOPs);
1516         tryAMAGICunTARGETlist(iter_amg, 0);
1517         PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
1518     }
1519     else PL_last_in_gv = PL_argvgv, PL_stack_sp--;
1520     if (!isGV_with_GP(PL_last_in_gv)) {
1521         if (SvROK(PL_last_in_gv) && isGV_with_GP(SvRV(PL_last_in_gv)))
1522             PL_last_in_gv = MUTABLE_GV(SvRV(PL_last_in_gv));
1523         else {
1524             dSP;
1525             XPUSHs(MUTABLE_SV(PL_last_in_gv));
1526             PUTBACK;
1527             Perl_pp_rv2gv(aTHX);
1528             PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
1529             assert((SV*)PL_last_in_gv == &PL_sv_undef || isGV_with_GP(PL_last_in_gv));
1530         }
1531     }
1532     return do_readline();
1533 }
1534
1535 PP_wrapped(pp_eq, 2, 0)
1536 {
1537     dSP;
1538     SV *left, *right;
1539     U32 flags_and, flags_or;
1540
1541     tryAMAGICbin_MG(eq_amg, AMGf_numeric);
1542     right = POPs;
1543     left  = TOPs;
1544     flags_and = SvFLAGS(left) & SvFLAGS(right);
1545     flags_or  = SvFLAGS(left) | SvFLAGS(right);
1546
1547     SETs(boolSV(
1548         ( (flags_and & SVf_IOK) && ((flags_or & SVf_IVisUV) ==0 ) )
1549         ?    (SvIVX(left) == SvIVX(right))
1550         : (flags_and & SVf_NOK)
1551         ?    (SvNVX(left) == SvNVX(right))
1552         : ( do_ncmp(left, right) == 0)
1553     ));
1554     RETURN;
1555 }
1556
1557
1558 /* also used for: pp_i_preinc() */
1559
1560 PP_wrapped(pp_preinc, 1, 0)
1561 {
1562     SV *sv = *PL_stack_sp;
1563
1564     if (LIKELY(((sv->sv_flags &
1565                         (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1566                          SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1567                 == SVf_IOK))
1568         && SvIVX(sv) != IV_MAX)
1569     {
1570         SvIV_set(sv, SvIVX(sv) + 1);
1571     }
1572     else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_inc */
1573         sv_inc(sv);
1574     SvSETMAGIC(sv);
1575     return NORMAL;
1576 }
1577
1578
1579 /* also used for: pp_i_predec() */
1580
1581 PP_wrapped(pp_predec, 1, 0)
1582 {
1583     SV *sv = *PL_stack_sp;
1584
1585     if (LIKELY(((sv->sv_flags &
1586                         (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
1587                          SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
1588                 == SVf_IOK))
1589         && SvIVX(sv) != IV_MIN)
1590     {
1591         SvIV_set(sv, SvIVX(sv) - 1);
1592     }
1593     else /* Do all the PERL_PRESERVE_IVUV and hard cases  in sv_dec */
1594         sv_dec(sv);
1595     SvSETMAGIC(sv);
1596     return NORMAL;
1597 }
1598
1599
1600 /* also used for: pp_orassign() */
1601
1602 PP_wrapped(pp_or, 1, 0)
1603 {
1604     dSP;
1605     SV *sv;
1606     PERL_ASYNC_CHECK();
1607     sv = TOPs;
1608     if (SvTRUE_NN(sv))
1609         RETURN;
1610     else {
1611         if (PL_op->op_type == OP_OR)
1612             --SP;
1613         RETURNOP(cLOGOP->op_other);
1614     }
1615 }
1616
1617
1618 /* also used for: pp_dor() pp_dorassign() */
1619
1620 PP_wrapped(pp_defined, 1, 0)
1621 {
1622     dSP;
1623     SV* sv = TOPs;
1624     bool defined = FALSE;
1625     const int op_type = PL_op->op_type;
1626     const bool is_dor = (op_type == OP_DOR || op_type == OP_DORASSIGN);
1627
1628     if (is_dor) {
1629         PERL_ASYNC_CHECK();
1630         if (UNLIKELY(!sv || !SvANY(sv))) {
1631             if (op_type == OP_DOR)
1632                 --SP;
1633             RETURNOP(cLOGOP->op_other);
1634         }
1635     }
1636     else {
1637         /* OP_DEFINED */
1638         if (UNLIKELY(!sv || !SvANY(sv)))
1639             RETSETNO;
1640     }
1641
1642     /* Historically what followed was a switch on SvTYPE(sv), handling SVt_PVAV,
1643      * SVt_PVCV, SVt_PVHV and "default". `defined &sub` is still valid syntax,
1644      * hence we still need the special case PVCV code. But AVs and HVs now
1645      * should never arrive here... */
1646 #ifdef DEBUGGING
1647     assert(SvTYPE(sv) != SVt_PVAV);
1648     assert(SvTYPE(sv) != SVt_PVHV);
1649 #endif
1650
1651     if (UNLIKELY(SvTYPE(sv) == SVt_PVCV)) {
1652         if (CvROOT(sv) || CvXSUB(sv))
1653             defined = TRUE;
1654     }
1655     else {
1656         SvGETMAGIC(sv);
1657         if (SvOK(sv))
1658             defined = TRUE;
1659     }
1660
1661     if (is_dor) {
1662         if(defined) 
1663             RETURN; 
1664         if(op_type == OP_DOR)
1665             --SP;
1666         RETURNOP(cLOGOP->op_other);
1667     }
1668     /* assuming OP_DEFINED */
1669     if(defined) 
1670         RETSETYES;
1671     RETSETNO;
1672 }
1673
1674
1675
1676 PP(pp_add)
1677 {
1678     bool useleft; SV *svl, *svr;
1679     SV *targ = (PL_op->op_flags & OPf_STACKED)
1680                     ? PL_stack_sp[-1]
1681                     : PAD_SV(PL_op->op_targ);
1682
1683     if (rpp_try_AMAGIC_2(add_amg, AMGf_assign|AMGf_numeric))
1684         return NORMAL;
1685
1686     svr = PL_stack_sp[0];
1687     svl = PL_stack_sp[-1];
1688
1689 #ifdef PERL_PRESERVE_IVUV
1690
1691     /* special-case some simple common cases */
1692     if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
1693         IV il, ir;
1694         U32 flags = (svl->sv_flags & svr->sv_flags);
1695         if (flags & SVf_IOK) {
1696             /* both args are simple IVs */
1697             UV topl, topr;
1698             il = SvIVX(svl);
1699             ir = SvIVX(svr);
1700           do_iv:
1701             topl = ((UV)il) >> (UVSIZE * 8 - 2);
1702             topr = ((UV)ir) >> (UVSIZE * 8 - 2);
1703
1704             /* if both are in a range that can't under/overflow, do a
1705              * simple integer add: if the top of both numbers
1706              * are 00  or 11, then it's safe */
1707             if (!( ((topl+1) | (topr+1)) & 2)) {
1708                 TARGi(il + ir, 0); /* args not GMG, so can't be tainted */
1709                 rpp_replace_2_1(targ);
1710                 return NORMAL;
1711             }
1712             goto generic;
1713         }
1714         else if (flags & SVf_NOK) {
1715             /* both args are NVs */
1716             NV nl = SvNVX(svl);
1717             NV nr = SvNVX(svr);
1718
1719             if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
1720                 /* nothing was lost by converting to IVs */
1721                 goto do_iv;
1722             }
1723             TARGn(nl + nr, 0); /* args not GMG, so can't be tainted */
1724             rpp_replace_2_1(targ);
1725             return NORMAL;
1726         }
1727     }
1728
1729   generic:
1730
1731     useleft = USE_LEFT(svl);
1732     /* We must see if we can perform the addition with integers if possible,
1733        as the integer code detects overflow while the NV code doesn't.
1734        If either argument hasn't had a numeric conversion yet attempt to get
1735        the IV. It's important to do this now, rather than just assuming that
1736        it's not IOK as a PV of "9223372036854775806" may not take well to NV
1737        addition, and an SV which is NOK, NV=6.0 ought to be coerced to
1738        integer in case the second argument is IV=9223372036854775806
1739        We can (now) rely on sv_2iv to do the right thing, only setting the
1740        public IOK flag if the value in the NV (or PV) slot is truly integer.
1741
1742        A side effect is that this also aggressively prefers integer maths over
1743        fp maths for integer values.
1744
1745        How to detect overflow?
1746
1747        C 99 section 6.2.6.1 says
1748
1749        The range of nonnegative values of a signed integer type is a subrange
1750        of the corresponding unsigned integer type, and the representation of
1751        the same value in each type is the same. A computation involving
1752        unsigned operands can never overflow, because a result that cannot be
1753        represented by the resulting unsigned integer type is reduced modulo
1754        the number that is one greater than the largest value that can be
1755        represented by the resulting type.
1756
1757        (the 9th paragraph)
1758
1759        which I read as "unsigned ints wrap."
1760
1761        signed integer overflow seems to be classed as "exception condition"
1762
1763        If an exceptional condition occurs during the evaluation of an
1764        expression (that is, if the result is not mathematically defined or not
1765        in the range of representable values for its type), the behavior is
1766        undefined.
1767
1768        (6.5, the 5th paragraph)
1769
1770        I had assumed that on 2s complement machines signed arithmetic would
1771        wrap, hence coded pp_add and pp_subtract on the assumption that
1772        everything perl builds on would be happy.  After much wailing and
1773        gnashing of teeth it would seem that irix64 knows its ANSI spec well,
1774        knows that it doesn't need to, and doesn't.  Bah.  Anyway, the all-
1775        unsigned code below is actually shorter than the old code. :-)
1776     */
1777
1778     if (SvIV_please_nomg(svr)) {
1779         /* Unless the left argument is integer in range we are going to have to
1780            use NV maths. Hence only attempt to coerce the right argument if
1781            we know the left is integer.  */
1782         UV auv = 0;
1783         bool auvok = FALSE;
1784         bool a_valid = 0;
1785
1786         if (!useleft) {
1787             auv = 0;
1788             a_valid = auvok = 1;
1789             /* left operand is undef, treat as zero. + 0 is identity,
1790                Could TARGi or TARGu right now, but space optimise by not
1791                adding lots of code to speed up what is probably a rare-ish
1792                case. */
1793         } else {
1794             /* Left operand is defined, so is it IV? */
1795             if (SvIV_please_nomg(svl)) {
1796                 if ((auvok = SvUOK(svl)))
1797                     auv = SvUVX(svl);
1798                 else {
1799                     const IV aiv = SvIVX(svl);
1800                     if (aiv >= 0) {
1801                         auv = aiv;
1802                         auvok = 1;      /* Now acting as a sign flag.  */
1803                     } else {
1804                         /* Using 0- here and later to silence bogus warning
1805                          * from MS VC */
1806                         auv = (UV) (0 - (UV) aiv);
1807                     }
1808                 }
1809                 a_valid = 1;
1810             }
1811         }
1812         if (a_valid) {
1813             bool result_good = 0;
1814             UV result;
1815             UV buv;
1816             bool buvok = SvUOK(svr);
1817         
1818             if (buvok)
1819                 buv = SvUVX(svr);
1820             else {
1821                 const IV biv = SvIVX(svr);
1822                 if (biv >= 0) {
1823                     buv = biv;
1824                     buvok = 1;
1825                 } else
1826                     buv = (UV) (0 - (UV) biv);
1827             }
1828             /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1829                else "IV" now, independent of how it came in.
1830                if a, b represents positive, A, B negative, a maps to -A etc
1831                a + b =>  (a + b)
1832                A + b => -(a - b)
1833                a + B =>  (a - b)
1834                A + B => -(a + b)
1835                all UV maths. negate result if A negative.
1836                add if signs same, subtract if signs differ. */
1837
1838             if (auvok ^ buvok) {
1839                 /* Signs differ.  */
1840                 if (auv >= buv) {
1841                     result = auv - buv;
1842                     /* Must get smaller */
1843                     if (result <= auv)
1844                         result_good = 1;
1845                 } else {
1846                     result = buv - auv;
1847                     if (result <= buv) {
1848                         /* result really should be -(auv-buv). as its negation
1849                            of true value, need to swap our result flag  */
1850                         auvok = !auvok;
1851                         result_good = 1;
1852                     }
1853                 }
1854             } else {
1855                 /* Signs same */
1856                 result = auv + buv;
1857                 if (result >= auv)
1858                     result_good = 1;
1859             }
1860             if (result_good) {
1861                 if (auvok)
1862                     TARGu(result,1);
1863                 else {
1864                     /* Negate result */
1865                     if (result <= (UV)IV_MIN)
1866                         TARGi(result == (UV)IV_MIN
1867                                 ? IV_MIN : -(IV)result, 1);
1868                     else {
1869                         /* result valid, but out of range for IV.  */
1870                         TARGn(-(NV)result, 1);
1871                     }
1872                 }
1873                 rpp_replace_2_1(targ);
1874                 return NORMAL;
1875             } /* Overflow, drop through to NVs.  */
1876         }
1877     }
1878
1879 #else
1880     useleft = USE_LEFT(svl);
1881 #endif
1882
1883     {
1884         NV value = SvNV_nomg(svr);
1885         if (!useleft) {
1886             /* left operand is undef, treat as zero. + 0.0 is identity. */
1887             TARGn(value, 1);
1888         }
1889         else {
1890             TARGn(value + SvNV_nomg(svl), 1);
1891         }
1892         rpp_replace_2_1(targ);
1893         return NORMAL;
1894     }
1895 }
1896
1897
1898 /* also used for: pp_aelemfast_lex() */
1899
1900 PP(pp_aelemfast)
1901 {
1902     AV * const av = PL_op->op_type == OP_AELEMFAST_LEX
1903         ? MUTABLE_AV(PAD_SV(PL_op->op_targ)) : GvAVn(cGVOP_gv);
1904     const U32 lval = PL_op->op_flags & OPf_MOD;
1905     const I8 key   = (I8)PL_op->op_private;
1906     SV** svp;
1907     SV *sv;
1908
1909     assert(SvTYPE(av) == SVt_PVAV);
1910
1911     /* inlined av_fetch() for simple cases ... */
1912     if (!SvRMAGICAL(av) && key >= 0 && key <= AvFILLp(av)) {
1913         sv = AvARRAY(av)[key];
1914         if (sv)
1915             goto ret;
1916         if (!lval) {
1917             sv = &PL_sv_undef;
1918             goto ret;
1919         }
1920     }
1921
1922     /* ... else do it the hard way */
1923     svp = av_fetch(av, key, lval);
1924     sv = (svp ? *svp : &PL_sv_undef);
1925
1926     if (UNLIKELY(!svp && lval))
1927         DIE(aTHX_ PL_no_aelem, (int)key);
1928
1929     if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
1930         mg_get(sv);
1931
1932   ret:
1933     rpp_xpush_1(sv);
1934     return NORMAL;
1935 }
1936
1937 PP_wrapped(pp_join, 0, 1)
1938 {
1939     dSP; dMARK; dTARGET;
1940     MARK++;
1941     do_join(TARG, *MARK, MARK, SP);
1942     SP = MARK;
1943     SETs(TARG);
1944     RETURN;
1945 }
1946
1947 /* Oversized hot code. */
1948
1949 /* also used for: pp_say() */
1950
1951 PP_wrapped(pp_print, 0, 1)
1952 {
1953     dSP; dMARK; dORIGMARK;
1954     PerlIO *fp;
1955     MAGIC *mg;
1956     GV * const gv
1957         = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
1958     IO *io = GvIO(gv);
1959
1960     if (io
1961         && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1962     {
1963       had_magic:
1964         if (MARK == ORIGMARK) {
1965             /* If using default handle then we need to make space to
1966              * pass object as 1st arg, so move other args up ...
1967              */
1968             MEXTEND(SP, 1);
1969             ++MARK;
1970             Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
1971             ++SP;
1972         }
1973         return Perl_tied_method(aTHX_ SV_CONST(PRINT), mark - 1, MUTABLE_SV(io),
1974                                 mg,
1975                                 (G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK
1976                                  | (PL_op->op_type == OP_SAY
1977                                     ? TIED_METHOD_SAY : 0)), sp - mark);
1978     }
1979     if (!io) {
1980         if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv)))
1981             && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1982             goto had_magic;
1983         report_evil_fh(gv);
1984         SETERRNO(EBADF,RMS_IFI);
1985         goto just_say_no;
1986     }
1987     else if (!(fp = IoOFP(io))) {
1988         if (IoIFP(io))
1989             report_wrongway_fh(gv, '<');
1990         else
1991             report_evil_fh(gv);
1992         SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
1993         goto just_say_no;
1994     }
1995     else {
1996         SV * const ofs = GvSV(PL_ofsgv); /* $, */
1997         MARK++;
1998         if (ofs && (SvGMAGICAL(ofs) || SvOK(ofs))) {
1999             while (MARK <= SP) {
2000                 if (!do_print(*MARK, fp))
2001                     break;
2002                 MARK++;
2003                 if (MARK <= SP) {
2004                     /* don't use 'ofs' here - it may be invalidated by magic callbacks */
2005                     if (!do_print(GvSV(PL_ofsgv), fp)) {
2006                         MARK--;
2007                         break;
2008                     }
2009                 }
2010             }
2011         }
2012         else {
2013             while (MARK <= SP) {
2014                 if (!do_print(*MARK, fp))
2015                     break;
2016                 MARK++;
2017             }
2018         }
2019         if (MARK <= SP)
2020             goto just_say_no;
2021         else {
2022             if (PL_op->op_type == OP_SAY) {
2023                 if (PerlIO_write(fp, "\n", 1) == 0 || PerlIO_error(fp))
2024                     goto just_say_no;
2025             }
2026             else if (PL_ors_sv && SvOK(PL_ors_sv))
2027                 if (!do_print(PL_ors_sv, fp)) /* $\ */
2028                     goto just_say_no;
2029
2030             if (IoFLAGS(io) & IOf_FLUSH)
2031                 if (PerlIO_flush(fp) == EOF)
2032                     goto just_say_no;
2033         }
2034     }
2035     SP = ORIGMARK;
2036     XPUSHs(&PL_sv_yes);
2037     RETURN;
2038
2039   just_say_no:
2040     SP = ORIGMARK;
2041     XPUSHs(&PL_sv_undef);
2042     RETURN;
2043 }
2044
2045
2046 /* do the common parts of pp_padhv() and pp_rv2hv()
2047  * It assumes the caller has done rpp_extend(1) or equivalent.
2048  * 'is_keys' indicates the OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS flag is set.
2049  * 'has_targ' indicates that the op has a target - this should
2050  * be a compile-time constant so that the code can constant-folded as
2051  * appropriate. has_targ also implies that the caller has left an
2052  * arg on the stack which needs freeing.
2053  * */
2054
2055 PERL_STATIC_INLINE OP*
2056 S_padhv_rv2hv_common(pTHX_ HV *hv, U8 gimme, bool is_keys, bool has_targ)
2057 {
2058     assert(PL_op->op_type == OP_PADHV || PL_op->op_type == OP_RV2HV);
2059
2060     if (gimme == G_LIST) {
2061         /* push all (key,value) pairs onto stack */
2062         if (has_targ) { /* i.e. if has arg still on stack */
2063 #ifdef PERL_RC_STACK
2064             SSize_t sp_base = PL_stack_sp - PL_stack_base;
2065             hv_pushkv(hv, 3);
2066             /* Now safe to free the original arg on the stack and shuffle
2067              * down one place anything pushed on top of it */
2068             SSize_t nitems = PL_stack_sp - (PL_stack_base + sp_base);
2069             SV *old_sv = PL_stack_sp[-nitems];
2070             if (nitems)
2071                 Move(PL_stack_sp - nitems + 1,
2072                      PL_stack_sp - nitems,    nitems, SV*);
2073             PL_stack_sp--;
2074             SvREFCNT_dec_NN(old_sv);
2075 #else
2076             rpp_popfree_1();
2077             hv_pushkv(hv, 3);
2078 #endif
2079         }
2080         else
2081             hv_pushkv(hv, 3);
2082         return NORMAL;
2083     }
2084
2085     if (is_keys)
2086         /* 'keys %h' masquerading as '%h': reset iterator */
2087         (void)hv_iterinit(hv);
2088
2089     if (gimme == G_VOID) {
2090         if (has_targ)
2091             rpp_popfree_1();
2092         return NORMAL;
2093     }
2094
2095     bool is_bool = (     PL_op->op_private & OPpTRUEBOOL
2096                    || (  PL_op->op_private & OPpMAYBE_TRUEBOOL
2097                       && block_gimme() == G_VOID));
2098
2099     MAGIC *is_tied_mg = SvRMAGICAL(hv)
2100                         ? mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied)
2101                         : NULL;
2102
2103     IV  i = 0;
2104     SV *sv = NULL;
2105     if (UNLIKELY(is_tied_mg)) {
2106         if (is_keys && !is_bool) {
2107             i = 0;
2108             while (hv_iternext(hv))
2109                 i++;
2110             /* hv finished with. Safe to free arg now */
2111             if (has_targ)
2112                 rpp_popfree_1();
2113             goto push_i;
2114         }
2115         else {
2116             sv = magic_scalarpack(hv, is_tied_mg);
2117             /* hv finished with. Safe to free arg now */
2118             if (has_targ)
2119                 rpp_popfree_1();
2120             goto push_sv;
2121         }
2122     }
2123     else {
2124 #if defined(DYNAMIC_ENV_FETCH) && defined(VMS)
2125         /* maybe nothing set up %ENV for iteration yet...
2126            do this always (not just if HvUSEDKEYS(hv) is currently 0) because
2127            we ought to give a *consistent* answer to "how many keys?"
2128            whether we ask this op in scalar context, or get the list of all
2129            keys then check its length, and whether we do either with or without
2130            an %ENV lookup first. prime_env_iter() returns quickly if nothing
2131            needs doing. */
2132         if (SvRMAGICAL((const SV *)hv)
2133             && mg_find((const SV *)hv, PERL_MAGIC_env)) {
2134             prime_env_iter();
2135         }
2136 #endif
2137         i = HvUSEDKEYS(hv);
2138
2139         /* hv finished with. Safe to free arg now */
2140         if (has_targ)
2141             rpp_popfree_1();
2142
2143         if (is_bool) {
2144             sv = i ? &PL_sv_yes : &PL_sv_zero;
2145           push_sv:
2146             rpp_push_1(sv);
2147         }
2148         else {
2149           push_i:
2150             if (has_targ) {
2151                 dTARGET;
2152                 TARGi(i,1);
2153                 rpp_push_1(targ);
2154             }
2155             else
2156             if (is_keys) {
2157                 /* parent op should be an unused OP_KEYS whose targ we can
2158                  * use */
2159                 dTARG;
2160                 OP *k;
2161
2162                 assert(!OpHAS_SIBLING(PL_op));
2163                 k = PL_op->op_sibparent;
2164                 assert(k->op_type == OP_KEYS);
2165                 TARG = PAD_SV(k->op_targ);
2166                 TARGi(i,1);
2167                 rpp_push_1(targ);
2168             }
2169             else
2170                 rpp_push_1_norc(newSViv(i));
2171         }
2172     }
2173
2174     return NORMAL;
2175 }
2176
2177
2178 /* This is also called directly by pp_lvavref.  */
2179 PP(pp_padav)
2180 {
2181     dTARGET;
2182     U8 gimme;
2183
2184     assert(SvTYPE(TARG) == SVt_PVAV);
2185     if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
2186         if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
2187             SAVECLEARSV(PAD_SVl(PL_op->op_targ));
2188
2189
2190     if (PL_op->op_flags & OPf_REF)
2191         goto ret;
2192
2193     if (PL_op->op_private & OPpMAYBE_LVSUB) {
2194         const I32 flags = is_lvalue_sub();
2195         if (flags && !(flags & OPpENTERSUB_INARGS)) {
2196             if (GIMME_V == G_SCALAR)
2197                 /* diag_listed_as: Can't return %s to lvalue scalar context */
2198                 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
2199             goto ret;
2200        }
2201     }
2202
2203     gimme = GIMME_V;
2204     if (gimme == G_LIST)
2205         return S_pushav(aTHX_ (AV*)TARG);
2206
2207     if (gimme == G_VOID)
2208         return NORMAL;
2209
2210     {
2211         const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
2212         if (!maxarg)
2213             targ = &PL_sv_zero;
2214         else if (PL_op->op_private & OPpTRUEBOOL)
2215             targ = &PL_sv_yes;
2216         else {
2217             rpp_extend(1);
2218             rpp_push_1_norc(newSViv(maxarg));
2219             return NORMAL;
2220         }
2221     }
2222
2223   ret:
2224     rpp_xpush_1(targ);
2225     return NORMAL;
2226 }
2227
2228
2229 PP(pp_padhv)
2230 {
2231     dTARGET;
2232     U8 gimme;
2233
2234     assert(SvTYPE(TARG) == SVt_PVHV);
2235     if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
2236         if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
2237             SAVECLEARSV(PAD_SVl(PL_op->op_targ));
2238
2239     rpp_extend(1);
2240
2241     if (PL_op->op_flags & OPf_REF) {
2242         rpp_push_1(TARG);
2243         return NORMAL;
2244     }
2245     else if (PL_op->op_private & OPpMAYBE_LVSUB) {
2246         const I32 flags = is_lvalue_sub();
2247         if (flags && !(flags & OPpENTERSUB_INARGS)) {
2248             if (GIMME_V == G_SCALAR)
2249                 /* diag_listed_as: Can't return %s to lvalue scalar context */
2250                 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
2251             rpp_push_1(TARG);
2252             return NORMAL;
2253         }
2254     }
2255
2256     gimme = GIMME_V;
2257
2258     return S_padhv_rv2hv_common(aTHX_ (HV*)TARG, gimme,
2259                         cBOOL(PL_op->op_private & OPpPADHV_ISKEYS),
2260                         0 /* has_targ*/);
2261 }
2262
2263
2264 /* also used for: pp_rv2hv() */
2265 /* also called directly by pp_lvavref */
2266
2267 PP(pp_rv2av)
2268 {
2269     SV *sv = *PL_stack_sp;
2270     const U8 gimme = GIMME_V;
2271     static const char an_array[] = "an ARRAY";
2272     static const char a_hash[] = "a HASH";
2273     const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV
2274                           || PL_op->op_type == OP_LVAVREF;
2275     const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV;
2276
2277     SvGETMAGIC(sv);
2278     if (SvROK(sv)) {
2279         if (UNLIKELY(SvAMAGIC(sv))) {
2280             sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg);
2281         }
2282         sv = SvRV(sv);
2283         if (UNLIKELY(SvTYPE(sv) != type))
2284             /* diag_listed_as: Not an ARRAY reference */
2285             DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash);
2286         else if (UNLIKELY(PL_op->op_flags & OPf_MOD
2287                 && PL_op->op_private & OPpLVAL_INTRO))
2288             Perl_croak(aTHX_ "%s", PL_no_localize_ref);
2289     }
2290     else if (UNLIKELY(SvTYPE(sv) != type)) {
2291             GV *gv;
2292         
2293             if (!isGV_with_GP(sv)) {
2294                 gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash,
2295                                      type);
2296                 if (!gv)
2297                     return NORMAL;
2298             }
2299             else {
2300                 gv = MUTABLE_GV(sv);
2301             }
2302             sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv));
2303             if (PL_op->op_private & OPpLVAL_INTRO)
2304                 sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv));
2305     }
2306     if (PL_op->op_flags & OPf_REF) {
2307         rpp_replace_1_1(sv);
2308         return NORMAL;
2309     }
2310     else if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
2311               const I32 flags = is_lvalue_sub();
2312               if (flags && !(flags & OPpENTERSUB_INARGS)) {
2313                 if (gimme != G_LIST)
2314                     goto croak_cant_return;
2315                 rpp_replace_1_1(sv);
2316                 return NORMAL;
2317               }
2318     }
2319
2320     if (is_pp_rv2av) {
2321         AV *const av = MUTABLE_AV(sv);
2322
2323         if (gimme == G_LIST) {
2324 #ifdef PERL_RC_STACK
2325             SSize_t sp_base = PL_stack_sp - PL_stack_base;
2326             (void)S_pushav(aTHX_ av);
2327             /* Now safe to free the original arg on the stack and shuffle
2328              * down one place anything pushed on top of it */
2329             SSize_t nitems = PL_stack_sp - (PL_stack_base + sp_base);
2330             SV *old_sv = PL_stack_sp[-nitems];
2331             if (nitems)
2332                 Move(PL_stack_sp - nitems + 1,
2333                      PL_stack_sp - nitems,    nitems, SV*);
2334             PL_stack_sp--;
2335             SvREFCNT_dec_NN(old_sv);
2336             return NORMAL;
2337 #else
2338             rpp_popfree_1();
2339             return S_pushav(aTHX_ av);
2340 #endif
2341         }
2342
2343         if (gimme == G_SCALAR) {
2344             const SSize_t maxarg = AvFILL(av) + 1;
2345             if (PL_op->op_private & OPpTRUEBOOL)
2346                 rpp_replace_1_1(maxarg ? &PL_sv_yes : &PL_sv_zero);
2347             else {
2348                 dTARGET;
2349                 TARGi(maxarg, 1);
2350                 rpp_replace_1_1(targ);
2351             }
2352         }
2353     }
2354     else {
2355         /* this static function is responsible for popping sv off stack */
2356         return S_padhv_rv2hv_common(aTHX_ (HV*)sv, gimme,
2357                         cBOOL(PL_op->op_private & OPpRV2HV_ISKEYS),
2358                         1 /* has_targ*/);
2359     }
2360     return NORMAL;
2361
2362  croak_cant_return:
2363     Perl_croak(aTHX_ "Can't return %s to lvalue scalar context",
2364                is_pp_rv2av ? "array" : "hash");
2365 }
2366
2367
2368 STATIC void
2369 S_do_oddball(pTHX_ SV **oddkey, SV **firstkey)
2370 {
2371     PERL_ARGS_ASSERT_DO_ODDBALL;
2372
2373     if (*oddkey) {
2374         if (ckWARN(WARN_MISC)) {
2375             const char *err;
2376             if (oddkey == firstkey &&
2377                 SvROK(*oddkey) &&
2378                 (SvTYPE(SvRV(*oddkey)) == SVt_PVAV ||
2379                  SvTYPE(SvRV(*oddkey)) == SVt_PVHV))
2380             {
2381                 err = "Reference found where even-sized list expected";
2382             }
2383             else
2384                 err = "Odd number of elements in hash assignment";
2385             Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err);
2386         }
2387
2388     }
2389 }
2390
2391
2392 /* Do a mark and sweep with the SVf_BREAK flag to detect elements which
2393  * are common to both the LHS and RHS of an aassign, and replace them
2394  * with copies. All these copies are made before the actual list assign is
2395  * done.
2396  *
2397  * For example in ($a,$b) = ($b,$a), assigning the value of the first RHS
2398  * element ($b) to the first LH element ($a), modifies $a; when the
2399  * second assignment is done, the second RH element now has the wrong
2400  * value. So we initially replace the RHS with ($b, mortalcopy($a)).
2401  * Note that we don't need to make a mortal copy of $b.
2402  *
2403  * The algorithm below works by, for every RHS element, mark the
2404  * corresponding LHS target element with SVf_BREAK. Then if the RHS
2405  * element is found with SVf_BREAK set, it means it would have been
2406  * modified, so make a copy.
2407  * Note that by scanning both LHS and RHS in lockstep, we avoid
2408  * unnecessary copies (like $b above) compared with a naive
2409  * "mark all LHS; copy all marked RHS; unmark all LHS".
2410  *
2411  * If the LHS element is a 'my' declaration' and has a refcount of 1, then
2412  * it can't be common and can be skipped.
2413  *
2414  * On DEBUGGING builds it takes an extra boolean, fake. If true, it means
2415  * that we thought we didn't need to call S_aassign_copy_common(), but we
2416  * have anyway for sanity checking. If we find we need to copy, then panic.
2417  */
2418
2419 PERL_STATIC_INLINE void
2420 S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem,
2421         SV **firstrelem, SV **lastrelem
2422 #ifdef DEBUGGING
2423         , bool fake
2424 #endif
2425 )
2426 {
2427     SV **relem;
2428     SV **lelem;
2429     SSize_t lcount = lastlelem - firstlelem + 1;
2430     bool marked = FALSE; /* have we marked any LHS with SVf_BREAK ? */
2431     bool const do_rc1 = cBOOL(PL_op->op_private & OPpASSIGN_COMMON_RC1);
2432     bool copy_all = FALSE;
2433
2434     assert(!PL_in_clean_all); /* SVf_BREAK not already in use */
2435     assert(firstlelem < lastlelem); /* at least 2 LH elements */
2436     assert(firstrelem < lastrelem); /* at least 2 RH elements */
2437
2438
2439     lelem = firstlelem;
2440     /* we never have to copy the first RH element; it can't be corrupted
2441      * by assigning something to the corresponding first LH element.
2442      * So this scan does in a loop: mark LHS[N]; test RHS[N+1]
2443      */
2444     relem = firstrelem + 1;
2445
2446     for (; relem <= lastrelem; relem++) {
2447         SV *svr;
2448
2449         /* mark next LH element */
2450
2451         if (--lcount >= 0) {
2452             SV *svl = *lelem++;
2453
2454             if (UNLIKELY(!svl)) {/* skip AV alias marker */
2455                 assert (lelem <= lastlelem);
2456                 svl = *lelem++;
2457                 lcount--;
2458             }
2459
2460             assert(svl);
2461             if (SvSMAGICAL(svl)) {
2462                 copy_all = TRUE;
2463             }
2464             if (SvTYPE(svl) == SVt_PVAV || SvTYPE(svl) == SVt_PVHV) {
2465                 if (!marked)
2466                     return;
2467                 /* this LH element will consume all further args;
2468                  * no need to mark any further LH elements (if any).
2469                  * But we still need to scan any remaining RHS elements;
2470                  * set lcount negative to distinguish from  lcount == 0,
2471                  * so the loop condition continues being true
2472                  */
2473                 lcount = -1;
2474                 lelem--; /* no need to unmark this element */
2475             }
2476             else if (!(do_rc1 && SvREFCNT(svl) == 1) && !SvIMMORTAL(svl)) {
2477                 SvFLAGS(svl) |= SVf_BREAK;
2478                 marked = TRUE;
2479             }
2480             else if (!marked) {
2481                 /* don't check RH element if no SVf_BREAK flags set yet */
2482                 if (!lcount)
2483                     break;
2484                 continue;
2485             }
2486         }
2487
2488         /* see if corresponding RH element needs copying */
2489
2490         assert(marked);
2491         svr = *relem;
2492         assert(svr);
2493
2494         if (UNLIKELY(SvFLAGS(svr) & (SVf_BREAK|SVs_GMG) || copy_all)) {
2495             U32 brk = (SvFLAGS(svr) & SVf_BREAK);
2496
2497 #ifdef DEBUGGING
2498             if (fake) {
2499                 /* op_dump(PL_op); */
2500                 Perl_croak(aTHX_
2501                     "panic: aassign skipped needed copy of common RH elem %"
2502                         UVuf, (UV)(relem - firstrelem));
2503             }
2504 #endif
2505
2506             TAINT_NOT;  /* Each item is independent */
2507
2508             /* Dear TODO test in t/op/sort.t, I love you.
2509                (It's relying on a panic, not a "semi-panic" from newSVsv()
2510                and then an assertion failure below.)  */
2511             if (UNLIKELY(SvIS_FREED(svr))) {
2512                 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p",
2513                            (void*)svr);
2514             }
2515             /* avoid break flag while copying; otherwise COW etc
2516              * disabled... */
2517             SvFLAGS(svr) &= ~SVf_BREAK;
2518             /* Not newSVsv(), as it does not allow copy-on-write,
2519                resulting in wasteful copies.
2520                Also, we use SV_NOSTEAL in case the SV is used more than
2521                once, e.g.  (...) = (f())[0,0]
2522                Where the same SV appears twice on the RHS without a ref
2523                count bump.  (Although I suspect that the SV won't be
2524                stealable here anyway - DAPM).
2525                */
2526             *relem = sv_mortalcopy_flags(svr,
2527                                 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
2528             /* ... but restore afterwards in case it's needed again,
2529              * e.g. ($a,$b,$c) = (1,$a,$a)
2530              */
2531             SvFLAGS(svr) |= brk;
2532         }
2533
2534         if (!lcount)
2535             break;
2536     }
2537
2538     if (!marked)
2539         return;
2540
2541     /*unmark LHS */
2542
2543     while (lelem > firstlelem) {
2544         SV * const svl = *(--lelem);
2545         if (svl)
2546             SvFLAGS(svl) &= ~SVf_BREAK;
2547     }
2548 }
2549
2550
2551
2552 PP_wrapped(pp_aassign, 0, 2)
2553 {
2554     dSP;
2555     SV **lastlelem = PL_stack_sp;
2556     SV **lastrelem = PL_stack_base + POPMARK;
2557     SV **firstrelem = PL_stack_base + POPMARK + 1;
2558     SV **firstlelem = lastrelem + 1;
2559
2560     SV **relem;
2561     SV **lelem;
2562     U8 gimme;
2563     /* PL_delaymagic is restored by JMPENV_POP on dieing, so we
2564      * only need to save locally, not on the save stack */
2565     U16 old_delaymagic = PL_delaymagic;
2566 #ifdef DEBUGGING
2567     bool fake = 0;
2568 #endif
2569
2570     PL_delaymagic = DM_DELAY;           /* catch simultaneous items */
2571
2572     /* If there's a common identifier on both sides we have to take
2573      * special care that assigning the identifier on the left doesn't
2574      * clobber a value on the right that's used later in the list.
2575      */
2576
2577     /* at least 2 LH and RH elements, or commonality isn't an issue */
2578     if (firstlelem < lastlelem && firstrelem < lastrelem) {
2579         for (relem = firstrelem+1; relem <= lastrelem; relem++) {
2580             if (SvGMAGICAL(*relem))
2581                 goto do_scan;
2582         }
2583         for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2584             if (*lelem && SvSMAGICAL(*lelem))
2585                 goto do_scan;
2586         }
2587         if ( PL_op->op_private & (OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1) ) {
2588             if (PL_op->op_private & OPpASSIGN_COMMON_RC1) {
2589                 /* skip the scan if all scalars have a ref count of 1 */
2590                 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2591                     SV *sv = *lelem;
2592                     if (!sv || SvREFCNT(sv) == 1)
2593                         continue;
2594                     if (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVAV)
2595                         goto do_scan;
2596                     break;
2597                 }
2598             }
2599             else {
2600             do_scan:
2601                 S_aassign_copy_common(aTHX_
2602                                       firstlelem, lastlelem, firstrelem, lastrelem
2603 #ifdef DEBUGGING
2604                     , fake
2605 #endif
2606                 );
2607             }
2608         }
2609     }
2610 #ifdef DEBUGGING
2611     else {
2612         /* on debugging builds, do the scan even if we've concluded we
2613          * don't need to, then panic if we find commonality. Note that the
2614          * scanner assumes at least 2 elements */
2615         if (firstlelem < lastlelem && firstrelem < lastrelem) {
2616             fake = 1;
2617             goto do_scan;
2618         }
2619     }
2620 #endif
2621
2622     gimme = GIMME_V;
2623     relem = firstrelem;
2624     lelem = firstlelem;
2625
2626     if (relem > lastrelem)
2627         goto no_relems;
2628
2629     /* first lelem loop while there are still relems */
2630     while (LIKELY(lelem <= lastlelem)) {
2631         bool alias = FALSE;
2632         SV *lsv = *lelem++;
2633
2634         TAINT_NOT; /* Each item stands on its own, taintwise. */
2635
2636         assert(relem <= lastrelem);
2637         if (UNLIKELY(!lsv)) {
2638             alias = TRUE;
2639             lsv = *lelem++;
2640             ASSUME(SvTYPE(lsv) == SVt_PVAV);
2641         }
2642
2643         switch (SvTYPE(lsv)) {
2644         case SVt_PVAV: {
2645             SV **svp;
2646             SSize_t i;
2647             SSize_t tmps_base;
2648             SSize_t nelems = lastrelem - relem + 1;
2649             AV *ary = MUTABLE_AV(lsv);
2650
2651             /* Assigning to an aggregate is tricky. First there is the
2652              * issue of commonality, e.g. @a = ($a[0]). Since the
2653              * stack isn't refcounted, clearing @a prior to storing
2654              * elements will free $a[0]. Similarly with
2655              *    sub FETCH { $status[$_[1]] } @status = @tied[0,1];
2656              *
2657              * The way to avoid these issues is to make the copy of each
2658              * SV (and we normally store a *copy* in the array) *before*
2659              * clearing the array. But this has a problem in that
2660              * if the code croaks during copying, the not-yet-stored copies
2661              * could leak. One way to avoid this is to make all the copies
2662              * mortal, but that's quite expensive.
2663              *
2664              * The current solution to these issues is to use a chunk
2665              * of the tmps stack as a temporary refcounted-stack. SVs
2666              * will be put on there during processing to avoid leaks,
2667              * but will be removed again before the end of this block,
2668              * so free_tmps() is never normally called. Also, the
2669              * sv_refcnt of the SVs doesn't have to be manipulated, since
2670              * the ownership of 1 reference count is transferred directly
2671              * from the tmps stack to the AV when the SV is stored.
2672              *
2673              * We disarm slots in the temps stack by storing PL_sv_undef
2674              * there: it doesn't matter if that SV's refcount is
2675              * repeatedly decremented during a croak. But usually this is
2676              * only an interim measure. By the end of this code block
2677              * we try where possible to not leave any PL_sv_undef's on the
2678              * tmps stack e.g. by shuffling newer entries down.
2679              *
2680              * There is one case where we don't copy: non-magical
2681              * SvTEMP(sv)'s with a ref count of 1. The only owner of these
2682              * is on the tmps stack, so its safe to directly steal the SV
2683              * rather than copying. This is common in things like function
2684              * returns, map etc, which all return a list of such SVs.
2685              *
2686              * Note however something like @a = (f())[0,0], where there is
2687              * a danger of the same SV being shared:  this avoided because
2688              * when the SV is stored as $a[0], its ref count gets bumped,
2689              * so the RC==1 test fails and the second element is copied
2690              * instead.
2691              *
2692              * We also use one slot in the tmps stack to hold an extra
2693              * ref to the array, to ensure it doesn't get prematurely
2694              * freed. Again, this is removed before the end of this block.
2695              *
2696              * Note that OPpASSIGN_COMMON_AGG is used to flag a possible
2697              * @a = ($a[0]) case, but the current implementation uses the
2698              * same algorithm regardless, so ignores that flag. (It *is*
2699              * used in the hash branch below, however).
2700             */
2701
2702             /* Reserve slots for ary, plus the elems we're about to copy,
2703              * then protect ary and temporarily void the remaining slots
2704              * with &PL_sv_undef */
2705             EXTEND_MORTAL(nelems + 1);
2706             PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(ary);
2707             tmps_base = PL_tmps_ix + 1;
2708             for (i = 0; i < nelems; i++)
2709                 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2710             PL_tmps_ix += nelems;
2711
2712             /* Make a copy of each RHS elem and save on the tmps_stack
2713              * (or pass through where we can optimise away the copy) */
2714
2715             if (UNLIKELY(alias)) {
2716                 U32 lval = (gimme == G_LIST)
2717                                 ? (PL_op->op_flags & OPf_MOD || LVRET) : 0;
2718                 for (svp = relem; svp <= lastrelem; svp++) {
2719                     SV *rsv = *svp;
2720
2721                     SvGETMAGIC(rsv);
2722                     if (!SvROK(rsv))
2723                         DIE(aTHX_ "Assigned value is not a reference");
2724                     if (SvTYPE(SvRV(rsv)) > SVt_PVLV)
2725                    /* diag_listed_as: Assigned value is not %s reference */
2726                         DIE(aTHX_
2727                            "Assigned value is not a SCALAR reference");
2728                     if (lval)
2729                         *svp = rsv = sv_mortalcopy(rsv);
2730                     /* XXX else check for weak refs?  */
2731                     rsv = SvREFCNT_inc_NN(SvRV(rsv));
2732                     assert(tmps_base <= PL_tmps_max);
2733                     PL_tmps_stack[tmps_base++] = rsv;
2734                 }
2735             }
2736             else {
2737                 for (svp = relem; svp <= lastrelem; svp++) {
2738                     SV *rsv = *svp;
2739
2740                     if (rpp_is_lone(rsv) && !SvGMAGICAL(rsv)) {
2741                         /* can skip the copy */
2742                         SvREFCNT_inc_simple_void_NN(rsv);
2743                         SvTEMP_off(rsv);
2744                     }
2745                     else {
2746                         SV *nsv;
2747                         /* see comment in S_aassign_copy_common about
2748                          * SV_NOSTEAL */
2749                         nsv = newSVsv_flags(rsv,
2750                                 (SV_DO_COW_SVSETSV|SV_NOSTEAL|SV_GMAGIC));
2751                         rsv = *svp = nsv;
2752                     }
2753
2754                     assert(tmps_base <= PL_tmps_max);
2755                     PL_tmps_stack[tmps_base++] = rsv;
2756                 }
2757             }
2758
2759             if (SvRMAGICAL(ary) || AvFILLp(ary) >= 0) /* may be non-empty */
2760                 av_clear(ary);
2761
2762             /* store in the array, the SVs that are in the tmps stack */
2763
2764             tmps_base -= nelems;
2765
2766             if (SvMAGICAL(ary) || SvREADONLY(ary) || !AvREAL(ary)) {
2767                 /* for arrays we can't cheat with, use the official API */
2768                 av_extend(ary, nelems - 1);
2769                 for (i = 0; i < nelems; i++) {
2770                     SV **svp = &(PL_tmps_stack[tmps_base + i]);
2771                     SV *rsv = *svp;
2772                     /* A tied store won't take ownership of rsv, so keep
2773                      * the 1 refcnt on the tmps stack; otherwise disarm
2774                      * the tmps stack entry */
2775                     if (av_store(ary, i, rsv))
2776                         *svp = &PL_sv_undef;
2777                     /* av_store() may have added set magic to rsv */;
2778                     SvSETMAGIC(rsv);
2779                 }
2780                 /* disarm ary refcount: see comments below about leak */
2781                 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
2782             }
2783             else {
2784                 /* directly access/set the guts of the AV */
2785                 SSize_t fill = nelems - 1;
2786                 if (fill > AvMAX(ary))
2787                     av_extend_guts(ary, fill, &AvMAX(ary), &AvALLOC(ary),
2788                                     &AvARRAY(ary));
2789                 AvFILLp(ary) = fill;
2790                 Copy(&(PL_tmps_stack[tmps_base]), AvARRAY(ary), nelems, SV*);
2791                 /* Quietly remove all the SVs from the tmps stack slots,
2792                  * since ary has now taken ownership of the refcnt.
2793                  * Also remove ary: which will now leak if we die before
2794                  * the SvREFCNT_dec_NN(ary) below */
2795                 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
2796                     Move(&PL_tmps_stack[tmps_base + nelems],
2797                          &PL_tmps_stack[tmps_base - 1],
2798                          PL_tmps_ix - (tmps_base + nelems) + 1,
2799                          SV*);
2800                 PL_tmps_ix -= (nelems + 1);
2801             }
2802
2803             if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
2804                 /* its assumed @ISA set magic can't die and leak ary */
2805                 SvSETMAGIC(MUTABLE_SV(ary));
2806             SvREFCNT_dec_NN(ary);
2807
2808             relem = lastrelem + 1;
2809             goto no_relems;
2810         }
2811
2812         case SVt_PVHV: {                                /* normal hash */
2813
2814             SV **svp;
2815             bool dirty_tmps;
2816             SSize_t i;
2817             SSize_t tmps_base;
2818             SSize_t nelems = lastrelem - relem + 1;
2819             HV *hash = MUTABLE_HV(lsv);
2820
2821             if (UNLIKELY(nelems & 1)) {
2822                 do_oddball(lastrelem, relem);
2823                 /* we have firstlelem to reuse, it's not needed any more */
2824                 *++lastrelem = &PL_sv_undef;
2825                 nelems++;
2826             }
2827
2828             /* See the SVt_PVAV branch above for a long description of
2829              * how the following all works. The main difference for hashes
2830              * is that we treat keys and values separately (and have
2831              * separate loops for them): as for arrays, values are always
2832              * copied (except for the SvTEMP optimisation), since they
2833              * need to be stored in the hash; while keys are only
2834              * processed where they might get prematurely freed or
2835              * whatever. */
2836
2837             /* tmps stack slots:
2838              * * reserve a slot for the hash keepalive;
2839              * * reserve slots for the hash values we're about to copy;
2840              * * preallocate for the keys we'll possibly copy or refcount bump
2841              *   later;
2842              * then protect hash and temporarily void the remaining
2843              * value slots with &PL_sv_undef */
2844             EXTEND_MORTAL(nelems + 1);
2845
2846              /* convert to number of key/value pairs */
2847              nelems >>= 1;
2848
2849             PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hash);
2850             tmps_base = PL_tmps_ix + 1;
2851             for (i = 0; i < nelems; i++)
2852                 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2853             PL_tmps_ix += nelems;
2854
2855             /* Make a copy of each RHS hash value and save on the tmps_stack
2856              * (or pass through where we can optimise away the copy) */
2857
2858             for (svp = relem + 1; svp <= lastrelem; svp += 2) {
2859                 SV *rsv = *svp;
2860
2861                 if (rpp_is_lone(rsv) && !SvGMAGICAL(rsv)) {
2862                     /* can skip the copy */
2863                     SvREFCNT_inc_simple_void_NN(rsv);
2864                     SvTEMP_off(rsv);
2865                 }
2866                 else {
2867                     SV *nsv;
2868                     /* see comment in S_aassign_copy_common about
2869                      * SV_NOSTEAL */
2870                     nsv = newSVsv_flags(rsv,
2871                             (SV_DO_COW_SVSETSV|SV_NOSTEAL|SV_GMAGIC));
2872                     rsv = *svp = nsv;
2873                 }
2874
2875                 assert(tmps_base <= PL_tmps_max);
2876                 PL_tmps_stack[tmps_base++] = rsv;
2877             }
2878             tmps_base -= nelems;
2879
2880
2881             /* possibly protect keys */
2882
2883             if (UNLIKELY(gimme == G_LIST)) {
2884                 /* handle e.g.
2885                 *     @a = ((%h = ($$r, 1)), $r = "x");
2886                 *     $_++ for %h = (1,2,3,4);
2887                 */
2888                 EXTEND_MORTAL(nelems);
2889                 for (svp = relem; svp <= lastrelem; svp += 2)
2890                     *svp = sv_mortalcopy_flags(*svp,
2891                                 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
2892             }
2893             else if (PL_op->op_private & OPpASSIGN_COMMON_AGG) {
2894                 /* for possible commonality, e.g.
2895                  *       %h = ($h{a},1)
2896                  * avoid premature freeing RHS keys by mortalising
2897                  * them.
2898                  * For a magic element, make a copy so that its magic is
2899                  * called *before* the hash is emptied (which may affect
2900                  * a tied value for example).
2901                  * In theory we should check for magic keys in all
2902                  * cases, not just under OPpASSIGN_COMMON_AGG, but in
2903                  * practice, !OPpASSIGN_COMMON_AGG implies only
2904                  * constants or padtmps on the RHS.
2905                  */
2906                 EXTEND_MORTAL(nelems);
2907                 for (svp = relem; svp <= lastrelem; svp += 2) {
2908                     SV *rsv = *svp;
2909                     if (UNLIKELY(SvGMAGICAL(rsv))) {
2910                         SSize_t n;
2911                         *svp = sv_mortalcopy_flags(*svp,
2912                                 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
2913                         /* allow other branch to continue pushing
2914                          * onto tmps stack without checking each time */
2915                         n = (lastrelem - relem) >> 1;
2916                         EXTEND_MORTAL(n);
2917                     }
2918                     else
2919                         PL_tmps_stack[++PL_tmps_ix] =
2920                                     SvREFCNT_inc_simple_NN(rsv);
2921                 }
2922             }
2923
2924             if (SvRMAGICAL(hash) || HvUSEDKEYS(hash))
2925                 hv_clear(hash);
2926
2927             /* "nelems" was converted to the number of pairs earlier. */
2928             if (nelems > PERL_HASH_DEFAULT_HvMAX) {
2929                 hv_ksplit(hash, nelems);
2930             }
2931
2932             /* now assign the keys and values to the hash */
2933
2934             dirty_tmps = FALSE;
2935
2936             if (UNLIKELY(gimme == G_LIST)) {
2937                 /* @a = (%h = (...)) etc */
2938                 SV **svp;
2939                 SV **topelem = relem;
2940
2941                 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
2942                     SV *key = *svp++;
2943                     SV *val = *svp;
2944                     /* remove duplicates from list we return */
2945                     if (!hv_exists_ent(hash, key, 0)) {
2946                         /* copy key back: possibly to an earlier
2947                          * stack location if we encountered dups earlier,
2948                          * The values will be updated later
2949                          */
2950                         *topelem = key;
2951                         topelem += 2;
2952                     }
2953                     /* A tied store won't take ownership of val, so keep
2954                      * the 1 refcnt on the tmps stack; otherwise disarm
2955                      * the tmps stack entry */
2956                     if (hv_store_ent(hash, key, val, 0))
2957                         PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2958                     else
2959                         dirty_tmps = TRUE;
2960                     /* hv_store_ent() may have added set magic to val */;
2961                     SvSETMAGIC(val);
2962                 }
2963                 if (topelem < svp) {
2964                     /* at this point we have removed the duplicate key/value
2965                      * pairs from the stack, but the remaining values may be
2966                      * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed
2967                      * the (a 2), but the stack now probably contains
2968                      * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) }
2969                      * obliterates the earlier key. So refresh all values. */
2970                     lastrelem = topelem - 1;
2971                     while (relem < lastrelem) {
2972                         HE *he;
2973                         he = hv_fetch_ent(hash, *relem++, 0, 0);
2974                         *relem++ = (he ? HeVAL(he) : &PL_sv_undef);
2975                     }
2976                 }
2977             }
2978             else {
2979                 SV **svp;
2980                 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
2981                     SV *key = *svp++;
2982                     SV *val = *svp;
2983                     if (hv_store_ent(hash, key, val, 0))
2984                         PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
2985                     else
2986                         dirty_tmps = TRUE;
2987                     /* hv_store_ent() may have added set magic to val */;
2988                     SvSETMAGIC(val);
2989                 }
2990             }
2991
2992             if (dirty_tmps) {
2993                 /* there are still some 'live' recounts on the tmps stack
2994                  * - usually caused by storing into a tied hash. So let
2995                  * free_tmps() do the proper but slow job later.
2996                  * Just disarm hash refcount: see comments below about leak
2997                  */
2998                 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
2999             }
3000             else {
3001                 /* Quietly remove all the SVs from the tmps stack slots,
3002                  * since hash has now taken ownership of the refcnt.
3003                  * Also remove hash: which will now leak if we die before
3004                  * the SvREFCNT_dec_NN(hash) below */
3005                 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
3006                     Move(&PL_tmps_stack[tmps_base + nelems],
3007                          &PL_tmps_stack[tmps_base - 1],
3008                          PL_tmps_ix - (tmps_base + nelems) + 1,
3009                          SV*);
3010                 PL_tmps_ix -= (nelems + 1);
3011             }
3012
3013             SvREFCNT_dec_NN(hash);
3014
3015             relem = lastrelem + 1;
3016             goto no_relems;
3017         }
3018
3019         default:
3020             if (!SvIMMORTAL(lsv)) {
3021                 SV *ref;
3022
3023                 if (UNLIKELY(
3024                     rpp_is_lone(lsv) && !SvSMAGICAL(lsv) &&
3025                   (!isGV_with_GP(lsv) || SvFAKE(lsv)) && ckWARN(WARN_MISC)
3026                 ))
3027                     Perl_warner(aTHX_
3028                        packWARN(WARN_MISC),
3029                       "Useless assignment to a temporary"
3030                     );
3031
3032                 /* avoid freeing $$lsv if it might be needed for further
3033                  * elements, e.g. ($ref, $foo) = (1, $$ref) */
3034                 if (   SvROK(lsv)
3035                     && ( ((ref = SvRV(lsv)), SvREFCNT(ref)) == 1)
3036                     && lelem <= lastlelem
3037                 ) {
3038                     SSize_t ix;
3039                     SvREFCNT_inc_simple_void_NN(ref);
3040                     /* an unrolled sv_2mortal */
3041                     ix = ++PL_tmps_ix;
3042                     if (UNLIKELY(ix >= PL_tmps_max))
3043                         /* speculatively grow enough to cover other
3044                          * possible refs */
3045                          (void)tmps_grow_p(ix + (lastlelem - lelem));
3046                     PL_tmps_stack[ix] = ref;
3047                 }
3048
3049                 sv_setsv(lsv, *relem);
3050                 *relem = lsv;
3051                 SvSETMAGIC(lsv);
3052             }
3053             if (++relem > lastrelem)
3054                 goto no_relems;
3055             break;
3056         } /* switch */
3057     } /* while */
3058
3059
3060   no_relems:
3061
3062     /* simplified lelem loop for when there are no relems left */
3063     while (LIKELY(lelem <= lastlelem)) {
3064         SV *lsv = *lelem++;
3065
3066         TAINT_NOT; /* Each item stands on its own, taintwise. */
3067
3068         if (UNLIKELY(!lsv)) {
3069             lsv = *lelem++;
3070             ASSUME(SvTYPE(lsv) == SVt_PVAV);
3071         }
3072
3073         switch (SvTYPE(lsv)) {
3074         case SVt_PVAV:
3075             if (SvRMAGICAL(lsv) || AvFILLp((SV*)lsv) >= 0) {
3076                 av_clear((AV*)lsv);
3077                 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
3078                     SvSETMAGIC(lsv);
3079             }
3080             break;
3081
3082         case SVt_PVHV:
3083             if (SvRMAGICAL(lsv) || HvUSEDKEYS((HV*)lsv))
3084                 hv_clear((HV*)lsv);
3085             break;
3086
3087         default:
3088             if (!SvIMMORTAL(lsv)) {
3089                 sv_set_undef(lsv);
3090                 SvSETMAGIC(lsv);
3091             }
3092             *relem++ = lsv;
3093             break;
3094         } /* switch */
3095     } /* while */
3096
3097     TAINT_NOT; /* result of list assign isn't tainted */
3098
3099     if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) {
3100         /* Will be used to set PL_tainting below */
3101         Uid_t tmp_uid  = PerlProc_getuid();
3102         Uid_t tmp_euid = PerlProc_geteuid();
3103         Gid_t tmp_gid  = PerlProc_getgid();
3104         Gid_t tmp_egid = PerlProc_getegid();
3105
3106         /* XXX $> et al currently silently ignore failures */
3107         if (PL_delaymagic & DM_UID) {
3108 #ifdef HAS_SETRESUID
3109             PERL_UNUSED_RESULT(
3110                setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid  : (Uid_t)-1,
3111                          (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
3112                          (Uid_t)-1));
3113 #elif defined(HAS_SETREUID)
3114             PERL_UNUSED_RESULT(
3115                 setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid  : (Uid_t)-1,
3116                          (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1));
3117 #else
3118 #    ifdef HAS_SETRUID
3119             if ((PL_delaymagic & DM_UID) == DM_RUID) {
3120                 PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid));
3121                 PL_delaymagic &= ~DM_RUID;
3122             }
3123 #    endif /* HAS_SETRUID */
3124 #    ifdef HAS_SETEUID
3125             if ((PL_delaymagic & DM_UID) == DM_EUID) {
3126                 PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid));
3127                 PL_delaymagic &= ~DM_EUID;
3128             }
3129 #    endif /* HAS_SETEUID */
3130             if (PL_delaymagic & DM_UID) {
3131                 if (PL_delaymagic_uid != PL_delaymagic_euid)
3132                     DIE(aTHX_ "No setreuid available");
3133                 PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
3134             }
3135 #endif /* HAS_SETRESUID */
3136
3137             tmp_uid  = PerlProc_getuid();
3138             tmp_euid = PerlProc_geteuid();
3139         }
3140         /* XXX $> et al currently silently ignore failures */
3141         if (PL_delaymagic & DM_GID) {
3142 #ifdef HAS_SETRESGID
3143             PERL_UNUSED_RESULT(
3144                 setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid  : (Gid_t)-1,
3145                           (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
3146                           (Gid_t)-1));
3147 #elif defined(HAS_SETREGID)
3148             PERL_UNUSED_RESULT(
3149                 setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid  : (Gid_t)-1,
3150                          (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1));
3151 #else
3152 #    ifdef HAS_SETRGID
3153             if ((PL_delaymagic & DM_GID) == DM_RGID) {
3154                 PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid));
3155                 PL_delaymagic &= ~DM_RGID;
3156             }
3157 #    endif /* HAS_SETRGID */
3158 #    ifdef HAS_SETEGID
3159             if ((PL_delaymagic & DM_GID) == DM_EGID) {
3160                 PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid));
3161                 PL_delaymagic &= ~DM_EGID;
3162             }
3163 #    endif /* HAS_SETEGID */
3164             if (PL_delaymagic & DM_GID) {
3165                 if (PL_delaymagic_gid != PL_delaymagic_egid)
3166                     DIE(aTHX_ "No setregid available");
3167                 PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
3168             }
3169 #endif /* HAS_SETRESGID */
3170
3171             tmp_gid  = PerlProc_getgid();
3172             tmp_egid = PerlProc_getegid();
3173         }
3174         TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
3175 #ifdef NO_TAINT_SUPPORT
3176         PERL_UNUSED_VAR(tmp_uid);
3177         PERL_UNUSED_VAR(tmp_euid);
3178         PERL_UNUSED_VAR(tmp_gid);
3179         PERL_UNUSED_VAR(tmp_egid);
3180 #endif
3181     }
3182     PL_delaymagic = old_delaymagic;
3183
3184     if (gimme == G_VOID)
3185         SP = firstrelem - 1;
3186     else if (gimme == G_SCALAR) {
3187         SP = firstrelem;
3188         EXTEND(SP,1);
3189         if (PL_op->op_private & OPpASSIGN_TRUEBOOL)
3190             SETs((firstlelem - firstrelem) ? &PL_sv_yes : &PL_sv_zero);
3191         else {
3192             dTARGET;
3193             SETi(firstlelem - firstrelem);
3194         }
3195     }
3196     else
3197         SP = relem - 1;
3198
3199     RETURN;
3200 }
3201
3202 PP(pp_qr)
3203 {
3204     PMOP * const pm = cPMOP;
3205     REGEXP * rx = PM_GETRE(pm);
3206     regexp *prog = ReANY(rx);
3207     SV * const pkg = RXp_ENGINE(prog)->qr_package(aTHX_ (rx));
3208     SV * const rv = newSV_type_mortal(SVt_IV);
3209     CV **cvp;
3210     CV *cv;
3211
3212     SvUPGRADE(rv, SVt_IV);
3213     /* For a subroutine describing itself as "This is a hacky workaround" I'm
3214        loathe to use it here, but it seems to be the right fix. Or close.
3215        The key part appears to be that it's essential for pp_qr to return a new
3216        object (SV), which implies that there needs to be an effective way to
3217        generate a new SV from the existing SV that is pre-compiled in the
3218        optree.  */
3219     SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx)));
3220     SvROK_on(rv);
3221
3222     cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv);
3223     if (UNLIKELY((cv = *cvp) && CvCLONE(*cvp))) {
3224         *cvp = cv_clone(cv);
3225         SvREFCNT_dec_NN(cv);
3226     }
3227
3228     if (pkg) {
3229         HV *const stash = gv_stashsv(pkg, GV_ADD);
3230         SvREFCNT_dec_NN(pkg);
3231         (void)sv_bless(rv, stash);
3232     }
3233
3234     if (UNLIKELY(RXp_ISTAINTED(prog))) {
3235         SvTAINTED_on(rv);
3236         SvTAINTED_on(SvRV(rv));
3237     }
3238     rpp_xpush_1(rv);
3239     return NORMAL;
3240 }
3241
3242 STATIC bool
3243 S_are_we_in_Debug_EXECUTE_r(pTHX)
3244 {
3245     /* Given a 'use re' is in effect, does it ask for outputting execution
3246      * debug info?
3247      *
3248      * This is separated from the sole place it's called, an inline function,
3249      * because it is the large-ish slow portion of the function */
3250
3251     DECLARE_AND_GET_RE_DEBUG_FLAGS_NON_REGEX;
3252
3253     return cBOOL(RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MASK));
3254 }
3255
3256 PERL_STATIC_INLINE bool
3257 S_should_we_output_Debug_r(pTHX_ regexp *prog)
3258 {
3259     PERL_ARGS_ASSERT_SHOULD_WE_OUTPUT_DEBUG_R;
3260
3261     /* pp_match can output regex debugging info.  This function returns a
3262      * boolean as to whether or not it should.
3263      *
3264      * Under -Dr, it should.  Any reasonable compiler will optimize this bit of
3265      * code away on non-debugging builds. */
3266     if (UNLIKELY(DEBUG_r_TEST)) {
3267         return TRUE;
3268     }
3269
3270     /* If the regex engine is using the non-debugging execution routine, then
3271      * no debugging should be output.  Same if the field is NULL that pluggable
3272      * engines are not supposed to fill. */
3273     if (     LIKELY(prog->engine->exec == &Perl_regexec_flags)
3274         || UNLIKELY(prog->engine->op_comp == NULL))
3275     {
3276         return FALSE;
3277     }
3278
3279     /* Otherwise have to check */
3280     return S_are_we_in_Debug_EXECUTE_r(aTHX);
3281 }
3282
3283 PP_wrapped(pp_match, ((PL_op->op_flags & OPf_STACKED) ? 1 : 0), 0)
3284 {
3285     dSP; dTARG;
3286     PMOP *pm = cPMOP;
3287     PMOP *dynpm = pm;
3288     const char *s;
3289     const char *strend;
3290     SSize_t curpos = 0; /* initial pos() or current $+[0] */
3291     I32 global;
3292     U8 r_flags = 0;
3293     const char *truebase;                       /* Start of string  */
3294     REGEXP *rx = PM_GETRE(pm);
3295     regexp *prog = ReANY(rx);
3296     bool rxtainted;
3297     const U8 gimme = GIMME_V;
3298     STRLEN len;
3299     const I32 oldsave = PL_savestack_ix;
3300     I32 had_zerolen = 0;
3301     MAGIC *mg = NULL;
3302
3303     if (PL_op->op_flags & OPf_STACKED)
3304         TARG = POPs;
3305     else {
3306         if (ARGTARG)
3307             GETTARGET;
3308         else {
3309             TARG = DEFSV;
3310         }
3311         EXTEND(SP,1);
3312     }
3313
3314     PUTBACK;                            /* EVAL blocks need stack_sp. */
3315     /* Skip get-magic if this is a qr// clone, because regcomp has
3316        already done it. */
3317     truebase = prog->mother_re
3318          ? SvPV_nomg_const(TARG, len)
3319          : SvPV_const(TARG, len);
3320     if (!truebase)
3321         DIE(aTHX_ "panic: pp_match");
3322     strend = truebase + len;
3323     rxtainted = (RXp_ISTAINTED(prog) ||
3324                  (TAINT_get && (pm->op_pmflags & PMf_RETAINT)));
3325     TAINT_NOT;
3326
3327     /* We need to know this in case we fail out early - pos() must be reset */
3328     global = dynpm->op_pmflags & PMf_GLOBAL;
3329
3330     /* PMdf_USED is set after a ?? matches once */
3331     if (
3332 #ifdef USE_ITHREADS
3333         SvREADONLY(PL_regex_pad[pm->op_pmoffset])
3334 #else
3335         pm->op_pmflags & PMf_USED
3336 #endif
3337     ) {
3338         if (UNLIKELY(should_we_output_Debug_r(prog))) {
3339             PerlIO_printf(Perl_debug_log, "?? already matched once");
3340         }
3341         goto nope;
3342     }
3343
3344     /* handle the empty pattern */
3345     if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
3346         if (PL_curpm == PL_reg_curpm) {
3347             if (PL_curpm_under) {
3348                 if (PL_curpm_under == PL_reg_curpm) {
3349                     Perl_croak(aTHX_ "Infinite recursion via empty pattern");
3350                 } else {
3351                     pm = PL_curpm_under;
3352                 }
3353             }
3354         } else {
3355             pm = PL_curpm;
3356         }
3357         rx = PM_GETRE(pm);
3358         prog = ReANY(rx);
3359     }
3360
3361     if (RXp_MINLEN(prog) >= 0 && (STRLEN)RXp_MINLEN(prog) > len) {
3362         if (UNLIKELY(should_we_output_Debug_r(prog))) {
3363             PerlIO_printf(Perl_debug_log,
3364                 "String shorter than min possible regex match (%zd < %zd)\n",
3365                                                         len, RXp_MINLEN(prog));
3366         }
3367         goto nope;
3368     }
3369
3370     /* get pos() if //g */
3371     if (global) {
3372         mg = mg_find_mglob(TARG);
3373         if (mg && mg->mg_len >= 0) {
3374             curpos = MgBYTEPOS(mg, TARG, truebase, len);
3375             /* last time pos() was set, it was zero-length match */
3376             if (mg->mg_flags & MGf_MINMATCH)
3377                 had_zerolen = 1;
3378         }
3379     }
3380
3381 #ifdef PERL_SAWAMPERSAND
3382     if (       RXp_NPARENS(prog)
3383             || PL_sawampersand
3384             || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
3385             || (dynpm->op_pmflags & PMf_KEEPCOPY)
3386     )
3387 #endif
3388     {
3389         r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE);
3390         /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer
3391          * only on the first iteration. Therefore we need to copy $' as well
3392          * as $&, to make the rest of the string available for captures in
3393          * subsequent iterations */
3394         if (! (global && gimme == G_LIST))
3395             r_flags |= REXEC_COPY_SKIP_POST;
3396     };
3397 #ifdef PERL_SAWAMPERSAND
3398     if (dynpm->op_pmflags & PMf_KEEPCOPY)
3399         /* handle KEEPCOPY in pmop but not rx, eg $r=qr/a/; /$r/p */
3400         r_flags &= ~(REXEC_COPY_SKIP_PRE|REXEC_COPY_SKIP_POST);
3401 #endif
3402
3403     s = truebase;
3404
3405   play_it_again:
3406     if (global)
3407         s = truebase + curpos;
3408
3409     if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase,
3410                      had_zerolen, TARG, NULL, r_flags))
3411         goto nope;
3412
3413     PL_curpm = pm;
3414     if (dynpm->op_pmflags & PMf_ONCE)
3415 #ifdef USE_ITHREADS
3416         SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]);
3417 #else
3418         dynpm->op_pmflags |= PMf_USED;
3419 #endif
3420
3421     if (rxtainted)
3422         RXp_MATCH_TAINTED_on(prog);
3423     TAINT_IF(RXp_MATCH_TAINTED(prog));
3424
3425     /* update pos */
3426
3427     if (global && (gimme != G_LIST || (dynpm->op_pmflags & PMf_CONTINUE))) {
3428         if (!mg)
3429             mg = sv_magicext_mglob(TARG);
3430         MgBYTEPOS_set(mg, TARG, truebase, RXp_OFFS_END(prog,0));
3431         if (RXp_ZERO_LEN(prog))
3432             mg->mg_flags |= MGf_MINMATCH;
3433         else
3434             mg->mg_flags &= ~MGf_MINMATCH;
3435     }
3436
3437     if ((!RXp_NPARENS(prog) && !global) || gimme != G_LIST) {
3438         LEAVE_SCOPE(oldsave);
3439         RETPUSHYES;
3440     }
3441
3442     /* push captures on stack */
3443
3444     {
3445         const I32 logical_nparens = RXp_LOGICAL_NPARENS(prog);
3446         /* This following statement is *devious* code. If we are in a global
3447            match and the pattern has no parens in it we should return $&
3448            (offset pair 0). So we set logical_paren to 1 when we should return
3449            $&, otherwise we set it to 0.
3450
3451            This allows us to simply add logical_nparens to logical_paren to
3452            compute the number of elements we are going to return.
3453
3454            In the loop intit we "not" it with: logical_paren = !logical_paren
3455            which results in it being 0 inside the loop when we want to return
3456            $&, and results in it being 1 when we want to return the parens.
3457            Thus we either loop over 1..logical_nparens, or just over 0.
3458
3459            This is an elegant way to do this code wise, but is super devious
3460            and potentially confusing. When I first saw this logic I thought
3461            "WTF?". But it makes sense after you poke it a while.
3462
3463            Frankly I probably would have done it differently, but it works so
3464            I am leaving it. - Yves */
3465         I32 logical_paren = (global && !logical_nparens) ? 1 : 0;
3466         I32 *l2p = RXp_LOGICAL_TO_PARNO(prog);
3467         /* this is used to step through the physical parens associated
3468          * with a given logical paren. */
3469         I32 *p2l_next = RXp_PARNO_TO_LOGICAL_NEXT(prog);
3470
3471         SPAGAIN;                        /* EVAL blocks could move the stack. */
3472         EXTEND(SP, logical_nparens + logical_paren);    /* devious code ... */
3473         EXTEND_MORTAL(logical_nparens + logical_paren); /* ... see above */
3474
3475         /* loop over the logical parens in the pattern. This may not
3476            correspond to the actual paren checked, as branch reset may
3477            mean that there is more than one paren "behind" the logical
3478            parens. Eg, in /(?|(a)|(b))/ there are two parens, but one
3479            logical paren. */
3480         for (logical_paren = !logical_paren;
3481              logical_paren <= logical_nparens;
3482              logical_paren++)
3483         {
3484             /* now convert the logical_paren to the physical parens which
3485                are "behind" it. If branch reset was not used then
3486                physical_paren and logical_paren are the same as each other
3487                and we will only perform one iteration of the loop */
3488             I32 phys_paren = l2p ? l2p[logical_paren] : logical_paren;
3489             SSize_t offs_start, offs_end;
3490             /* We check the loop invariants below and break out of the loop
3491                explicitly if our checks fail, so we use while (1) here to
3492                avoid double testing a conditional. */
3493             while (1) {
3494                 /* Check end offset first, as the start might be >=0 even
3495                    though the end is -1, so testing the end first helps
3496                    use avoid the start check.  Really we should be able to
3497                    get away with ONLY testing the end, but testing both
3498                    doesn't hurt much and preserves sanity. */
3499                 if (((offs_end   = RXp_OFFS_END(prog, phys_paren))   != -1) &&
3500                     ((offs_start = RXp_OFFS_START(prog, phys_paren)) != -1))
3501                 {
3502                     const SSize_t len = offs_end - offs_start;
3503                     const char * const s = offs_start + truebase;
3504                     if ( UNLIKELY( len < 0 || len > strend - s) ) {
3505                         DIE(aTHX_ "panic: pp_match start/end pointers, paren=%" I32df ", "
3506                             "start=%zd, end=%zd, s=%p, strend=%p, len=%zd",
3507                             phys_paren, offs_start, offs_end, s, strend, len);
3508                     }
3509                     PUSHs(newSVpvn_flags(s, len,
3510                         (DO_UTF8(TARG))
3511                         ? SVf_UTF8|SVs_TEMP
3512                         : SVs_TEMP)
3513                     );
3514                     break;
3515                 } else if (!p2l_next || !(phys_paren = p2l_next[phys_paren])) {
3516                     /* Either logical_paren and phys_paren are the same and
3517                        we won't have a p2l_next, or they aren't the same (and
3518                        we do have a p2l_next) but we have exhausted the list
3519                        of physical parens associated with this logical paren.
3520                        Either way we are done, and we can push undef and break
3521                        out of the loop. */
3522                     PUSHs(sv_newmortal());
3523                     break;
3524                 }
3525             }
3526         }
3527         if (global) {
3528             curpos = (UV)RXp_OFFS_END(prog,0);
3529             had_zerolen = RXp_ZERO_LEN(prog);
3530             PUTBACK;                    /* EVAL blocks may use stack */
3531             r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
3532             goto play_it_again;
3533         }
3534         LEAVE_SCOPE(oldsave);
3535         RETURN;
3536     }
3537     NOT_REACHED; /* NOTREACHED */
3538
3539   nope:
3540     if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
3541         if (!mg)
3542             mg = mg_find_mglob(TARG);
3543         if (mg)
3544             mg->mg_len = -1;
3545     }
3546     LEAVE_SCOPE(oldsave);
3547     if (gimme == G_LIST)
3548         RETURN;
3549     RETPUSHNO;
3550 }
3551
3552 OP *
3553 Perl_do_readline(pTHX)
3554 {
3555     dSP; dTARGETSTACKED;
3556     SV *sv;
3557     STRLEN tmplen = 0;
3558     STRLEN offset;
3559     PerlIO *fp;
3560     IO * const io = GvIO(PL_last_in_gv);
3561     const I32 type = PL_op->op_type;
3562     const U8 gimme = GIMME_V;
3563
3564     if (io) {
3565         const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
3566         if (mg) {
3567             Perl_tied_method(aTHX_ SV_CONST(READLINE), SP, MUTABLE_SV(io), mg, gimme, 0);
3568             if (gimme == G_SCALAR) {
3569                 SPAGAIN;
3570                 SvSetSV_nosteal(TARG, TOPs);
3571                 SETTARG;
3572             }
3573             return NORMAL;
3574         }
3575     }
3576     fp = NULL;
3577     if (io) {
3578         fp = IoIFP(io);
3579         if (!fp) {
3580             if (IoFLAGS(io) & IOf_ARGV) {
3581                 if (IoFLAGS(io) & IOf_START) {
3582                     IoLINES(io) = 0;
3583                     if (av_count(GvAVn(PL_last_in_gv)) == 0) {
3584                         IoFLAGS(io) &= ~IOf_START;
3585                         do_open6(PL_last_in_gv, "-", 1, NULL, NULL, 0);
3586                         SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */
3587                         sv_setpvs(GvSVn(PL_last_in_gv), "-");
3588                         SvSETMAGIC(GvSV(PL_last_in_gv));
3589                         fp = IoIFP(io);
3590                         goto have_fp;
3591                     }
3592                 }
3593                 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
3594                 if (!fp) { /* Note: fp != IoIFP(io) */
3595                     (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
3596                 }
3597             }
3598             else if (type == OP_GLOB)
3599                 fp = Perl_start_glob(aTHX_ POPs, io);
3600         }
3601         else if (type == OP_GLOB)
3602             SP--;
3603         else if (IoTYPE(io) == IoTYPE_WRONLY) {
3604             report_wrongway_fh(PL_last_in_gv, '>');
3605         }
3606     }
3607     if (!fp) {
3608         if ((!io || !(IoFLAGS(io) & IOf_START))
3609             && ckWARN(WARN_CLOSED)
3610             && type != OP_GLOB)
3611         {
3612             report_evil_fh(PL_last_in_gv);
3613         }
3614         if (gimme == G_SCALAR) {
3615             /* undef TARG, and push that undefined value */
3616             if (type != OP_RCATLINE) {
3617                 sv_set_undef(TARG);
3618             }
3619             PUSHTARG;
3620         }
3621         RETURN;
3622     }
3623   have_fp:
3624     if (gimme == G_SCALAR) {
3625         sv = TARG;
3626         if (type == OP_RCATLINE && SvGMAGICAL(sv))
3627             mg_get(sv);
3628         if (SvROK(sv)) {
3629             if (type == OP_RCATLINE)
3630                 SvPV_force_nomg_nolen(sv);
3631             else
3632                 sv_unref(sv);
3633         }
3634         else if (isGV_with_GP(sv)) {
3635             SvPV_force_nomg_nolen(sv);
3636         }
3637         SvUPGRADE(sv, SVt_PV);
3638         tmplen = SvLEN(sv);     /* remember if already alloced */
3639         if (!tmplen && !SvREADONLY(sv) && !SvIsCOW(sv)) {
3640             /* try short-buffering it. Please update t/op/readline.t
3641              * if you change the growth length.
3642              */
3643             Sv_Grow(sv, 80);
3644         }
3645         offset = 0;
3646         if (type == OP_RCATLINE && SvOK(sv)) {
3647             if (!SvPOK(sv)) {
3648                 SvPV_force_nomg_nolen(sv);
3649             }
3650             offset = SvCUR(sv);
3651         }
3652     }
3653     else {
3654         sv = sv_2mortal(newSV(80));
3655         offset = 0;
3656     }
3657
3658     /* This should not be marked tainted if the fp is marked clean */
3659 #define MAYBE_TAINT_LINE(io, sv) \
3660     if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
3661         TAINT;                          \
3662         SvTAINTED_on(sv);               \
3663     }
3664
3665 /* delay EOF state for a snarfed empty file */
3666 #define SNARF_EOF(gimme,rs,io,sv) \
3667     (gimme != G_SCALAR || SvCUR(sv)                                     \
3668      || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
3669
3670     for (;;) {
3671         PUTBACK;
3672         if (!sv_gets(sv, fp, offset)
3673             && (type == OP_GLOB
3674                 || SNARF_EOF(gimme, PL_rs, io, sv)
3675                 || PerlIO_error(fp)))
3676         {
3677             if (IoFLAGS(io) & IOf_ARGV) {
3678                 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
3679                 if (fp) {
3680                     continue;
3681                 }
3682                 (void)do_close(PL_last_in_gv, FALSE);
3683             }
3684             else if (type == OP_GLOB) {
3685                 /* clear any errors here so we only fail on the pclose()
3686                    failing, which should only happen on the child
3687                    failing
3688                 */
3689                 PerlIO_clearerr(fp);
3690                 if (!do_close(PL_last_in_gv, FALSE)) {
3691                     Perl_ck_warner(aTHX_ packWARN(WARN_GLOB),
3692                                    "glob failed (child exited with status %d%s)",
3693                                    (int)(STATUS_CURRENT >> 8),
3694                                    (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
3695                 }
3696             }
3697             if (gimme == G_SCALAR) {
3698                 if (type != OP_RCATLINE) {
3699                     SV_CHECK_THINKFIRST_COW_DROP(TARG);
3700                     SvOK_off(TARG);
3701                 }
3702                 SPAGAIN;
3703                 PUSHTARG;
3704             }
3705             MAYBE_TAINT_LINE(io, sv);
3706             RETURN;
3707         }
3708         MAYBE_TAINT_LINE(io, sv);
3709         IoLINES(io)++;
3710         IoFLAGS(io) |= IOf_NOLINE;
3711         SvSETMAGIC(sv);
3712         SPAGAIN;
3713         XPUSHs(sv);
3714         if (type == OP_GLOB) {
3715             const char *t1;
3716             Stat_t statbuf;
3717
3718             if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
3719                 char * const tmps = SvEND(sv) - 1;
3720                 if (*tmps == *SvPVX_const(PL_rs)) {
3721                     *tmps = '\0';
3722                     SvCUR_set(sv, SvCUR(sv) - 1);
3723                 }
3724             }
3725             for (t1 = SvPVX_const(sv); *t1; t1++)
3726 #ifdef __VMS
3727                 if (memCHRs("*%?", *t1))
3728 #else
3729                 if (memCHRs("$&*(){}[]'\";\\|?<>~`", *t1))
3730 #endif
3731                         break;
3732             if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &statbuf) < 0) {
3733                 (void)POPs;             /* Unmatched wildcard?  Chuck it... */
3734                 continue;
3735             }
3736         } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
3737              if (ckWARN(WARN_UTF8)) {
3738                 const U8 * const s = (const U8*)SvPVX_const(sv) + offset;
3739                 const STRLEN len = SvCUR(sv) - offset;
3740                 const U8 *f;
3741
3742                 if (!is_utf8_string_loc(s, len, &f))
3743                     /* Emulate :encoding(utf8) warning in the same case. */
3744                     Perl_warner(aTHX_ packWARN(WARN_UTF8),
3745                                 "utf8 \"\\x%02X\" does not map to Unicode",
3746                                 f < (U8*)SvEND(sv) ? *f : 0);
3747              }
3748         }
3749         if (gimme == G_LIST) {
3750             if (SvLEN(sv) - SvCUR(sv) > 20) {
3751                 SvPV_shrink_to_cur(sv);
3752             }
3753             sv = sv_2mortal(newSV(80));
3754             continue;
3755         }
3756         else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
3757             /* try to reclaim a bit of scalar space (only on 1st alloc) */
3758             const STRLEN new_len
3759                 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
3760             SvPV_renew(sv, new_len);
3761         }
3762         RETURN;
3763     }
3764 }
3765
3766 PP_wrapped(pp_helem, 2, 0)
3767 {
3768     dSP;
3769     HE* he;
3770     SV **svp;
3771     SV * const keysv = POPs;
3772     HV * const hv = MUTABLE_HV(POPs);
3773     const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3774     const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
3775     SV *sv;
3776     const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3777     bool preeminent = TRUE;
3778
3779     if (SvTYPE(hv) != SVt_PVHV)
3780         RETPUSHUNDEF;
3781
3782     if (localizing) {
3783         MAGIC *mg;
3784         HV *stash;
3785
3786         /* Try to preserve the existence of a tied hash
3787          * element by using EXISTS and DELETE if possible.
3788          * Fall back to FETCH and STORE otherwise. */
3789         if (SvCANEXISTDELETE(hv))
3790             preeminent = hv_exists_ent(hv, keysv, 0);
3791     }
3792
3793     he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
3794     svp = he ? &HeVAL(he) : NULL;
3795     if (lval) {
3796         if (!svp || !*svp || *svp == &PL_sv_undef) {
3797             SV* lv;
3798             SV* key2;
3799             if (!defer) {
3800                 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
3801             }
3802             lv = newSV_type_mortal(SVt_PVLV);
3803             LvTYPE(lv) = 'y';
3804             sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0);
3805             SvREFCNT_dec_NN(key2);      /* sv_magic() increments refcount */
3806             LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
3807             LvTARGLEN(lv) = 1;
3808             PUSHs(lv);
3809             RETURN;
3810         }
3811         if (localizing) {
3812             if (HvNAME_get(hv) && isGV_or_RVCV(*svp))
3813                 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
3814             else if (preeminent)
3815                 save_helem_flags(hv, keysv, svp,
3816                      (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
3817             else
3818                 SAVEHDELETE(hv, keysv);
3819         }
3820         else if (PL_op->op_private & OPpDEREF) {
3821             PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
3822             RETURN;
3823         }
3824     }
3825     sv = (svp && *svp ? *svp : &PL_sv_undef);
3826     /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this
3827      * was to make C<local $tied{foo} = $tied{foo}> possible.
3828      * However, it seems no longer to be needed for that purpose, and
3829      * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g>
3830      * would loop endlessly since the pos magic is getting set on the
3831      * mortal copy and lost. However, the copy has the effect of
3832      * triggering the get magic, and losing it altogether made things like
3833      * c<$tied{foo};> in void context no longer do get magic, which some
3834      * code relied on. Also, delayed triggering of magic on @+ and friends
3835      * meant the original regex may be out of scope by now. So as a
3836      * compromise, do the get magic here. (The MGf_GSKIP flag will stop it
3837      * being called too many times). */
3838     if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv))
3839         mg_get(sv);
3840     PUSHs(sv);
3841     RETURN;
3842 }
3843
3844
3845 /* a stripped-down version of Perl_softref2xv() for use by
3846  * pp_multideref(), which doesn't use PL_op->op_flags */
3847
3848 STATIC GV *
3849 S_softref2xv_lite(pTHX_ SV *const sv, const char *const what,
3850                 const svtype type)
3851 {
3852     if (PL_op->op_private & HINT_STRICT_REFS) {
3853         if (SvOK(sv))
3854             Perl_die(aTHX_ PL_no_symref_sv, sv,
3855                      (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
3856         else
3857             Perl_die(aTHX_ PL_no_usym, what);
3858     }
3859     if (!SvOK(sv))
3860         Perl_die(aTHX_ PL_no_usym, what);
3861     return gv_fetchsv_nomg(sv, GV_ADD, type);
3862 }
3863
3864
3865 /* how many stack arguments a multideref op expects */
3866 #ifdef PERL_RC_STACK
3867 STATIC I32
3868 S_multideref_argcount(pTHX)
3869 {
3870     UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux;
3871     UV actions = items->uv;
3872     I32 nargs;
3873
3874     switch (actions & MDEREF_ACTION_MASK) {
3875     case MDEREF_AV_pop_rv2av_aelem:             /* expr->[...] */
3876     case MDEREF_HV_pop_rv2hv_helem:             /* expr->{...} */
3877         nargs = 1;
3878         break;
3879     default:
3880         nargs = 0;
3881     }
3882     return nargs;
3883 }
3884 #endif
3885
3886
3887 /* Handle one or more aggregate derefs and array/hash indexings, e.g.
3888  * $h->{foo}  or  $a[0]{$key}[$i]  or  f()->[1]
3889  *
3890  * op_aux points to an array of unions of UV / IV / SV* / PADOFFSET.
3891  * Each of these either contains a set of actions, or an argument, such as
3892  * an IV to use as an array index, or a lexical var to retrieve.
3893  * Several actions are stored per UV; we keep shifting new actions off the
3894  * one UV, and only reload when it becomes zero.
3895  */
3896
3897 PP_wrapped(pp_multideref, S_multideref_argcount(aTHX), 0)
3898 {
3899     SV *sv = NULL; /* init to avoid spurious 'may be used uninitialized' */
3900     UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux;
3901     UV actions = items->uv;
3902
3903     assert(actions);
3904     /* this tells find_uninit_var() where we're up to */
3905     PL_multideref_pc = items;
3906
3907     while (1) {
3908         /* there are three main classes of action; the first retrieves
3909          * the initial AV or HV from a variable or the stack; the second
3910          * does the equivalent of an unrolled (/DREFAV, rv2av, aelem),
3911          * the third an unrolled (/DREFHV, rv2hv, helem).
3912          */
3913         switch (actions & MDEREF_ACTION_MASK) {
3914
3915         case MDEREF_reload:
3916             actions = (++items)->uv;
3917             continue;
3918
3919         case MDEREF_AV_padav_aelem:                 /* $lex[...] */
3920             sv = PAD_SVl((++items)->pad_offset);
3921             goto do_AV_aelem;
3922
3923         case MDEREF_AV_gvav_aelem:                  /* $pkg[...] */
3924             sv = UNOP_AUX_item_sv(++items);
3925             assert(isGV_with_GP(sv));
3926             sv = (SV*)GvAVn((GV*)sv);
3927             goto do_AV_aelem;
3928
3929         case MDEREF_AV_pop_rv2av_aelem:             /* expr->[...] */
3930             {
3931                 dSP;
3932                 sv = POPs;
3933                 PUTBACK;
3934                 goto do_AV_rv2av_aelem;
3935             }
3936
3937         case MDEREF_AV_gvsv_vivify_rv2av_aelem:     /* $pkg->[...] */
3938             sv = UNOP_AUX_item_sv(++items);
3939             assert(isGV_with_GP(sv));
3940             sv = GvSVn((GV*)sv);
3941             goto do_AV_vivify_rv2av_aelem;
3942
3943         case MDEREF_AV_padsv_vivify_rv2av_aelem:     /* $lex->[...] */
3944             sv = PAD_SVl((++items)->pad_offset);
3945             /* FALLTHROUGH */
3946
3947         do_AV_vivify_rv2av_aelem:
3948         case MDEREF_AV_vivify_rv2av_aelem:           /* vivify, ->[...] */
3949             /* this is the OPpDEREF action normally found at the end of
3950              * ops like aelem, helem, rv2sv */
3951             sv = vivify_ref(sv, OPpDEREF_AV);
3952             /* FALLTHROUGH */
3953
3954         do_AV_rv2av_aelem:
3955             /* this is basically a copy of pp_rv2av when it just has the
3956              * sKR/1 flags */
3957             SvGETMAGIC(sv);
3958             if (LIKELY(SvROK(sv))) {
3959                 if (UNLIKELY(SvAMAGIC(sv))) {
3960                     sv = amagic_deref_call(sv, to_av_amg);
3961                 }
3962                 sv = SvRV(sv);
3963                 if (UNLIKELY(SvTYPE(sv) != SVt_PVAV))
3964                     DIE(aTHX_ "Not an ARRAY reference");
3965             }
3966             else if (SvTYPE(sv) != SVt_PVAV) {
3967                 if (!isGV_with_GP(sv))
3968                     sv = (SV*)S_softref2xv_lite(aTHX_ sv, "an ARRAY", SVt_PVAV);
3969                 sv = MUTABLE_SV(GvAVn((GV*)sv));
3970             }
3971             /* FALLTHROUGH */
3972
3973         do_AV_aelem:
3974             {
3975                 /* retrieve the key; this may be either a lexical or package
3976                  * var (whose index/ptr is stored as an item) or a signed
3977                  * integer constant stored as an item.
3978                  */
3979                 SV *elemsv;
3980                 IV elem = 0; /* to shut up stupid compiler warnings */
3981
3982
3983                 assert(SvTYPE(sv) == SVt_PVAV);
3984
3985                 switch (actions & MDEREF_INDEX_MASK) {
3986                 case MDEREF_INDEX_none:
3987                     goto finish;
3988                 case MDEREF_INDEX_const:
3989                     elem  = (++items)->iv;
3990                     break;
3991                 case MDEREF_INDEX_padsv:
3992                     elemsv = PAD_SVl((++items)->pad_offset);
3993                     goto check_elem;
3994                 case MDEREF_INDEX_gvsv:
3995                     elemsv = UNOP_AUX_item_sv(++items);
3996                     assert(isGV_with_GP(elemsv));
3997                     elemsv = GvSVn((GV*)elemsv);
3998                 check_elem:
3999                     if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv)
4000                                             && ckWARN(WARN_MISC)))
4001                         Perl_warner(aTHX_ packWARN(WARN_MISC),
4002                                 "Use of reference \"%" SVf "\" as array index",
4003                                 SVfARG(elemsv));
4004                     /* the only time that S_find_uninit_var() needs this
4005                      * is to determine which index value triggered the
4006                      * undef warning. So just update it here. Note that
4007                      * since we don't save and restore this var (e.g. for
4008                      * tie or overload execution), its value will be
4009                      * meaningless apart from just here */
4010                     PL_multideref_pc = items;
4011                     elem = SvIV(elemsv);
4012                     break;
4013                 }
4014
4015
4016                 /* this is basically a copy of pp_aelem with OPpDEREF skipped */
4017
4018                 if (!(actions & MDEREF_FLAG_last)) {
4019                     SV** svp = av_fetch((AV*)sv, elem, 1);
4020                     if (!svp || ! (sv=*svp))
4021                         DIE(aTHX_ PL_no_aelem, elem);
4022                     break;
4023                 }
4024
4025                 if (PL_op->op_private &
4026                     (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
4027                 {
4028                     if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
4029                         sv = av_exists((AV*)sv, elem) ? &PL_sv_yes : &PL_sv_no;
4030                     }
4031                     else {
4032                         I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
4033                         sv = av_delete((AV*)sv, elem, discard);
4034                         if (discard)
4035                             return NORMAL;
4036                         if (!sv)
4037                             sv = &PL_sv_undef;
4038                     }
4039                 }
4040                 else {
4041                     const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
4042                     const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
4043                     const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4044                     bool preeminent = TRUE;
4045                     AV *const av = (AV*)sv;
4046                     SV** svp;
4047
4048                     if (UNLIKELY(localizing)) {
4049                         MAGIC *mg;
4050                         HV *stash;
4051
4052                         /* Try to preserve the existence of a tied array
4053                          * element by using EXISTS and DELETE if possible.
4054                          * Fall back to FETCH and STORE otherwise. */
4055                         if (SvCANEXISTDELETE(av))
4056                             preeminent = av_exists(av, elem);
4057                     }
4058
4059                     svp = av_fetch(av, elem, lval && !defer);
4060
4061                     if (lval) {
4062                         if (!svp || !(sv = *svp)) {
4063                             IV len;
4064                             if (!defer)
4065                                 DIE(aTHX_ PL_no_aelem, elem);
4066                             len = av_top_index(av);
4067                             /* Resolve a negative index that falls within
4068                              * the array.  Leave it negative it if falls
4069                              * outside the array.  */
4070                              if (elem < 0 && len + elem >= 0)
4071                                  elem = len + elem;
4072                              if (elem >= 0 && elem <= len)
4073                                  /* Falls within the array.  */
4074                                  sv = av_nonelem(av,elem);
4075                              else
4076                                  /* Falls outside the array.  If it is neg-
4077                                     ative, magic_setdefelem will use the
4078                                     index for error reporting.  */
4079                                 sv = sv_2mortal(newSVavdefelem(av,elem,1));
4080                         }
4081                         else {
4082                             if (UNLIKELY(localizing)) {
4083                                 if (preeminent) {
4084                                     save_aelem(av, elem, svp);
4085                                     sv = *svp; /* may have changed */
4086                                 }
4087                                 else
4088                                     SAVEADELETE(av, elem);
4089                             }
4090                         }
4091                     }
4092                     else {
4093                         sv = (svp ? *svp : &PL_sv_undef);
4094                         /* see note in pp_helem() */
4095                         if (SvRMAGICAL(av) && SvGMAGICAL(sv))
4096                             mg_get(sv);
4097                     }
4098                 }
4099
4100             }
4101           finish:
4102             {
4103                 dSP;
4104                 XPUSHs(sv);
4105                 RETURN;
4106             }
4107             /* NOTREACHED */
4108
4109
4110
4111
4112         case MDEREF_HV_padhv_helem:                 /* $lex{...} */
4113             sv = PAD_SVl((++items)->pad_offset);
4114             goto do_HV_helem;
4115
4116         case MDEREF_HV_gvhv_helem:                  /* $pkg{...} */
4117             sv = UNOP_AUX_item_sv(++items);
4118             assert(isGV_with_GP(sv));
4119             sv = (SV*)GvHVn((GV*)sv);
4120             goto do_HV_helem;
4121
4122         case MDEREF_HV_pop_rv2hv_helem:             /* expr->{...} */
4123             {
4124                 dSP;
4125                 sv = POPs;
4126                 PUTBACK;
4127                 goto do_HV_rv2hv_helem;
4128             }
4129
4130         case MDEREF_HV_gvsv_vivify_rv2hv_helem:     /* $pkg->{...} */
4131             sv = UNOP_AUX_item_sv(++items);
4132             assert(isGV_with_GP(sv));
4133             sv = GvSVn((GV*)sv);
4134             goto do_HV_vivify_rv2hv_helem;
4135
4136         case MDEREF_HV_padsv_vivify_rv2hv_helem:    /* $lex->{...} */
4137             sv = PAD_SVl((++items)->pad_offset);
4138             /* FALLTHROUGH */
4139
4140         do_HV_vivify_rv2hv_helem:
4141         case MDEREF_HV_vivify_rv2hv_helem:           /* vivify, ->{...} */
4142             /* this is the OPpDEREF action normally found at the end of
4143              * ops like aelem, helem, rv2sv */
4144             sv = vivify_ref(sv, OPpDEREF_HV);
4145             /* FALLTHROUGH */
4146
4147         do_HV_rv2hv_helem:
4148             /* this is basically a copy of pp_rv2hv when it just has the
4149              * sKR/1 flags (and pp_rv2hv is aliased to pp_rv2av) */
4150
4151             SvGETMAGIC(sv);
4152             if (LIKELY(SvROK(sv))) {
4153                 if (UNLIKELY(SvAMAGIC(sv))) {
4154                     sv = amagic_deref_call(sv, to_hv_amg);
4155                 }
4156                 sv = SvRV(sv);
4157                 if (UNLIKELY(SvTYPE(sv) != SVt_PVHV))
4158                     DIE(aTHX_ "Not a HASH reference");
4159             }
4160             else if (SvTYPE(sv) != SVt_PVHV) {
4161                 if (!isGV_with_GP(sv))
4162                     sv = (SV*)S_softref2xv_lite(aTHX_ sv, "a HASH", SVt_PVHV);
4163                 sv = MUTABLE_SV(GvHVn((GV*)sv));
4164             }
4165             /* FALLTHROUGH */
4166
4167         do_HV_helem:
4168             {
4169                 /* retrieve the key; this may be either a lexical / package
4170                  * var or a string constant, whose index/ptr is stored as an
4171                  * item
4172                  */
4173                 SV *keysv = NULL; /* to shut up stupid compiler warnings */
4174
4175                 assert(SvTYPE(sv) == SVt_PVHV);
4176
4177                 switch (actions & MDEREF_INDEX_MASK) {
4178                 case MDEREF_INDEX_none:
4179                     goto finish;
4180
4181                 case MDEREF_INDEX_const:
4182                     keysv = UNOP_AUX_item_sv(++items);
4183                     break;
4184
4185                 case MDEREF_INDEX_padsv:
4186                     keysv = PAD_SVl((++items)->pad_offset);
4187                     break;
4188
4189                 case MDEREF_INDEX_gvsv:
4190                     keysv = UNOP_AUX_item_sv(++items);
4191                     keysv = GvSVn((GV*)keysv);
4192                     break;
4193                 }
4194
4195                 /* see comment above about setting this var */
4196                 PL_multideref_pc = items;
4197
4198
4199                 /* ensure that candidate CONSTs have been HEKified */
4200                 assert(   ((actions & MDEREF_INDEX_MASK) != MDEREF_INDEX_const)
4201                        || SvTYPE(keysv) >= SVt_PVMG
4202                        || !SvOK(keysv)
4203                        || SvROK(keysv)
4204                        || SvIsCOW_shared_hash(keysv));
4205
4206                 /* this is basically a copy of pp_helem with OPpDEREF skipped */
4207
4208                 if (!(actions & MDEREF_FLAG_last)) {
4209                     HE *he = hv_fetch_ent((HV*)sv, keysv, 1, 0);
4210                     if (!he || !(sv=HeVAL(he)) || sv == &PL_sv_undef)
4211                         DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4212                     break;
4213                 }
4214
4215                 if (PL_op->op_private &
4216                     (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
4217                 {
4218                     if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
4219                         sv = hv_exists_ent((HV*)sv, keysv, 0)
4220                                                 ? &PL_sv_yes : &PL_sv_no;
4221                     }
4222                     else {
4223                         I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
4224                         sv = hv_delete_ent((HV*)sv, keysv, discard, 0);
4225                         if (discard)
4226                             return NORMAL;
4227                         if (!sv)
4228                             sv = &PL_sv_undef;
4229                     }
4230                 }
4231                 else {
4232                     const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
4233                     const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
4234                     const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4235                     bool preeminent = TRUE;
4236                     SV **svp;
4237                     HV * const hv = (HV*)sv;
4238                     HE* he;
4239
4240                     if (UNLIKELY(localizing)) {
4241                         MAGIC *mg;
4242                         HV *stash;
4243
4244                         /* Try to preserve the existence of a tied hash
4245                          * element by using EXISTS and DELETE if possible.
4246                          * Fall back to FETCH and STORE otherwise. */
4247                         if (SvCANEXISTDELETE(hv))
4248                             preeminent = hv_exists_ent(hv, keysv, 0);
4249                     }
4250
4251                     he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
4252                     svp = he ? &HeVAL(he) : NULL;
4253
4254
4255                     if (lval) {
4256                         if (!svp || !(sv = *svp) || sv == &PL_sv_undef) {
4257                             SV* lv;
4258                             SV* key2;
4259                             if (!defer)
4260                                 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4261                             lv = newSV_type_mortal(SVt_PVLV);
4262                             LvTYPE(lv) = 'y';
4263                             sv_magic(lv, key2 = newSVsv(keysv),
4264                                                 PERL_MAGIC_defelem, NULL, 0);
4265                             /* sv_magic() increments refcount */
4266                             SvREFCNT_dec_NN(key2);
4267                             LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
4268                             LvTARGLEN(lv) = 1;
4269                             sv = lv;
4270                         }
4271                         else {
4272                             if (localizing) {
4273                                 if (HvNAME_get(hv) && isGV_or_RVCV(sv))
4274                                     save_gp(MUTABLE_GV(sv),
4275                                         !(PL_op->op_flags & OPf_SPECIAL));
4276                                 else if (preeminent) {
4277                                     save_helem_flags(hv, keysv, svp,
4278                                          (PL_op->op_flags & OPf_SPECIAL)
4279                                             ? 0 : SAVEf_SETMAGIC);
4280                                     sv = *svp; /* may have changed */
4281                                 }
4282                                 else
4283                                     SAVEHDELETE(hv, keysv);
4284                             }
4285                         }
4286                     }
4287                     else {
4288                         sv = (svp && *svp ? *svp : &PL_sv_undef);
4289                         /* see note in pp_helem() */
4290                         if (SvRMAGICAL(hv) && SvGMAGICAL(sv))
4291                             mg_get(sv);
4292                     }
4293                 }
4294                 goto finish;
4295             }
4296
4297         } /* switch */
4298
4299         actions >>= MDEREF_SHIFT;
4300     } /* while */
4301     /* NOTREACHED */
4302 }
4303
4304
4305 PP(pp_iter)
4306 {
4307     PERL_CONTEXT *cx = CX_CUR();
4308     SV **itersvp = CxITERVAR(cx);
4309     const U8 type = CxTYPE(cx);
4310
4311     /* Classic "for" syntax iterates one-at-a-time.
4312        Many-at-a-time for loops are only for lexicals declared as part of the
4313        for loop, and rely on all the lexicals being in adjacent pad slots.
4314
4315        Curiously, even if the iterator variable is a lexical, the pad offset is
4316        stored in the targ slot of the ENTERITER op, meaning that targ of this OP
4317        has always been zero. Hence we can use this op's targ to hold "how many"
4318        for many-at-a-time. We actually store C<how_many - 1>, so that for the
4319        case of one-at-a-time we have zero (as before), as this makes all the
4320        logic of the for loop below much simpler, with all the other
4321        one-at-a-time cases just falling out of this "naturally". */
4322     PADOFFSET how_many = PL_op->op_targ;
4323     PADOFFSET i = 0;
4324
4325     assert(itersvp);
4326
4327     for (; i <= how_many; ++i ) {
4328         SV *oldsv;
4329         SV *sv;
4330         AV *av;
4331         IV ix;
4332         IV inc;
4333
4334         switch (type) {
4335
4336         case CXt_LOOP_LAZYSV: /* string increment */
4337             {
4338                 SV* cur = cx->blk_loop.state_u.lazysv.cur;
4339                 SV *end = cx->blk_loop.state_u.lazysv.end;
4340                 /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no.
4341                    It has SvPVX of "" and SvCUR of 0, which is what we want.  */
4342                 STRLEN maxlen = 0;
4343                 const char *max = SvPV_const(end, maxlen);
4344                 bool pad_it = FALSE;
4345                 if (DO_UTF8(end) && IN_UNI_8_BIT)
4346                     maxlen = sv_len_utf8_nomg(end);
4347                 if (UNLIKELY(SvNIOK(cur) || SvCUR(cur) > maxlen)) {
4348                     if (LIKELY(!i)) {
4349                         goto retno;
4350                     }
4351                     /* We are looping n-at-a-time and the range isn't a multiple
4352                        of n, so we fill the rest of the lexicals with undef.
4353                        This only happens on the last iteration of the loop, and
4354                        we will have already set up the "terminate next time"
4355                        condition earlier in this for loop for this call of the
4356                        ITER op when we set up the lexical corresponding to the
4357                        last value in the range. Hence we don't goto retno (yet),
4358                        and just below we don't repeat the setup for "terminate
4359                        next time". */
4360                     pad_it = TRUE;
4361                 }
4362
4363                 oldsv = *itersvp;
4364                 /* NB: on the first iteration, oldsv will have a ref count of at
4365                  * least 2 (one extra from blk_loop.itersave), so the GV or pad
4366                  * slot will get localised; on subsequent iterations the RC==1
4367                  * optimisation may kick in and the SV will be reused. */
4368                 if (UNLIKELY(pad_it)) {
4369                     *itersvp = &PL_sv_undef;
4370                     SvREFCNT_dec(oldsv);
4371                 }
4372                 else if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
4373                     /* safe to reuse old SV */
4374                     sv_setsv(oldsv, cur);
4375                 }
4376                 else {
4377                     /* we need a fresh SV every time so that loop body sees a
4378                      * completely new SV for closures/references to work as
4379                      * they used to */
4380                     *itersvp = newSVsv(cur);
4381                     SvREFCNT_dec(oldsv);
4382                 }
4383
4384                 if (UNLIKELY(pad_it)) {
4385                     /* We're "beyond the end" of the iterator here, filling the
4386                        extra lexicals with undef, so we mustn't do anything
4387                        (further) to the iterator itself at this point.
4388                        (Observe how the other two blocks modify the iterator's
4389                        value) */
4390                 }
4391                 else if (strEQ(SvPVX_const(cur), max))
4392                     sv_setiv(cur, 0); /* terminate next time */
4393                 else
4394                     sv_inc(cur);
4395                 break;
4396             }
4397
4398         case CXt_LOOP_LAZYIV: /* integer increment */
4399             {
4400                 IV cur = cx->blk_loop.state_u.lazyiv.cur;
4401                 bool pad_it = FALSE;
4402                 if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end)) {
4403                     if (LIKELY(!i)) {
4404                         goto retno;
4405                     }
4406                     pad_it = TRUE;
4407                 }
4408
4409                 oldsv = *itersvp;
4410                 /* see NB comment above */
4411                 if (UNLIKELY(pad_it)) {
4412                     *itersvp = &PL_sv_undef;
4413                     SvREFCNT_dec(oldsv);
4414                 }
4415                 else if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
4416                     /* safe to reuse old SV */
4417
4418                     if (    (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV))
4419                          == SVt_IV) {
4420                         /* Cheap SvIOK_only().
4421                          * Assert that flags which SvIOK_only() would test or
4422                          * clear can't be set, because we're SVt_IV */
4423                         assert(!(SvFLAGS(oldsv) &
4424                                  (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK)))));
4425                         SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK);
4426                         /* SvIV_set() where sv_any points to head */
4427                         oldsv->sv_u.svu_iv = cur;
4428
4429                     }
4430                     else
4431                         sv_setiv(oldsv, cur);
4432                 }
4433                 else {
4434                     /* we need a fresh SV every time so that loop body sees a
4435                      * completely new SV for closures/references to work as they
4436                      * used to */
4437                     *itersvp = newSViv(cur);
4438                     SvREFCNT_dec(oldsv);
4439                 }
4440
4441                 if (UNLIKELY(pad_it)) {
4442                     /* We're good (see "We are looping n-at-a-time" comment
4443                        above). */
4444                 }
4445                 else if (UNLIKELY(cur == IV_MAX)) {
4446                     /* Handle end of range at IV_MAX */
4447                     cx->blk_loop.state_u.lazyiv.end = IV_MIN;
4448                 } else
4449                     ++cx->blk_loop.state_u.lazyiv.cur;
4450                 break;
4451             }
4452
4453         case CXt_LOOP_LIST: /* for (1,2,3) */
4454
4455             assert(OPpITER_REVERSED == 2); /* so inc becomes -1 or 1 */
4456             inc = (IV)1 - (IV)(PL_op->op_private & OPpITER_REVERSED);
4457             ix = (cx->blk_loop.state_u.stack.ix += inc);
4458             if (UNLIKELY(inc > 0
4459                          ? ix > cx->blk_oldsp
4460                          : ix <= cx->blk_loop.state_u.stack.basesp)
4461                 ) {
4462                 if (LIKELY(!i)) {
4463                     goto retno;
4464                 }
4465
4466                 sv = &PL_sv_undef;
4467             }
4468             else {
4469                 sv = PL_stack_base[ix];
4470             }
4471
4472             av = NULL;
4473             goto loop_ary_common;
4474
4475         case CXt_LOOP_ARY: /* for (@ary) */
4476
4477             av = cx->blk_loop.state_u.ary.ary;
4478             inc = (IV)1 - (IV)(PL_op->op_private & OPpITER_REVERSED);
4479             ix = (cx->blk_loop.state_u.ary.ix += inc);
4480             if (UNLIKELY(inc > 0
4481                          ? ix > AvFILL(av)
4482                          : ix < 0)
4483                 ) {
4484                 if (LIKELY(!i)) {
4485                     goto retno;
4486                 }
4487
4488                 sv = &PL_sv_undef;
4489             } else if (UNLIKELY(SvRMAGICAL(av))) {
4490                 SV * const * const svp = av_fetch(av, ix, FALSE);
4491                 sv = svp ? *svp : NULL;
4492             }
4493             else {
4494                 sv = AvARRAY(av)[ix];
4495             }
4496
4497         loop_ary_common:
4498
4499             if (UNLIKELY(cx->cx_type & CXp_FOR_LVREF)) {
4500                 SvSetMagicSV(*itersvp, sv);
4501                 break;
4502             }
4503
4504             if (LIKELY(sv)) {
4505                 if (UNLIKELY(SvIS_FREED(sv))) {
4506                     *itersvp = NULL;
4507                     Perl_croak(aTHX_ "Use of freed value in iteration");
4508                 }
4509                 if (SvPADTMP(sv)) {
4510                     sv = newSVsv(sv);
4511                 }
4512                 else {
4513                     SvTEMP_off(sv);
4514                     SvREFCNT_inc_simple_void_NN(sv);
4515                 }
4516             }
4517             else if (av) {
4518                 sv = newSVavdefelem(av, ix, 0);
4519             }
4520             else
4521                 sv = &PL_sv_undef;
4522
4523             oldsv = *itersvp;
4524             *itersvp = sv;
4525             SvREFCNT_dec(oldsv);
4526             break;
4527
4528         default:
4529             DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx));
4530         }
4531
4532         /* Only relevant for a many-at-a-time loop: */
4533         ++itersvp;
4534     }
4535
4536     /* Try to bypass pushing &PL_sv_yes and calling pp_and(); instead
4537      * jump straight to the AND op's op_other */
4538     assert(PL_op->op_next->op_type == OP_AND);
4539     if (PL_op->op_next->op_ppaddr == Perl_pp_and) {
4540         return cLOGOPx(PL_op->op_next)->op_other;
4541     }
4542     else {
4543         /* An XS module has replaced the op_ppaddr, so fall back to the slow,
4544          * obvious way. */
4545         /* pp_enteriter should have pre-extended the stack */
4546         EXTEND_SKIP(PL_stack_sp, 1);
4547         *++PL_stack_sp = &PL_sv_yes;
4548         return PL_op->op_next;
4549     }
4550
4551   retno:
4552     /* Try to bypass pushing &PL_sv_no and calling pp_and(); instead
4553      * jump straight to the AND op's op_next */
4554     assert(PL_op->op_next->op_type == OP_AND);
4555     /* pp_enteriter should have pre-extended the stack */
4556     EXTEND_SKIP(PL_stack_sp, 1);
4557     /* we only need this for the rare case where the OP_AND isn't
4558      * in void context, e.g. $x = do { for (..) {...} };
4559      * (or for when an XS module has replaced the op_ppaddr)
4560      * but it's cheaper to just push it rather than testing first
4561      */
4562     *++PL_stack_sp = &PL_sv_no;
4563     if (PL_op->op_next->op_ppaddr == Perl_pp_and) {
4564         return PL_op->op_next->op_next;
4565     }
4566     else {
4567         /* An XS module has replaced the op_ppaddr, so fall back to the slow,
4568          * obvious way. */
4569         return PL_op->op_next;
4570     }
4571 }
4572
4573
4574 /*
4575 A description of how taint works in pattern matching and substitution.
4576
4577 This is all conditional on NO_TAINT_SUPPORT remaining undefined (the default).
4578 Under NO_TAINT_SUPPORT, taint-related operations should become no-ops.
4579
4580 While the pattern is being assembled/concatenated and then compiled,
4581 PL_tainted will get set (via TAINT_set) if any component of the pattern
4582 is tainted, e.g. /.*$tainted/.  At the end of pattern compilation,
4583 the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via
4584 TAINT_get).  It will also be set if any component of the pattern matches
4585 based on locale-dependent behavior.
4586
4587 When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to
4588 the pattern is marked as tainted. This means that subsequent usage, such
4589 as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED,
4590 on the new pattern too.
4591
4592 RXf_TAINTED_SEEN is used post-execution by the get magic code
4593 of $1 et al to indicate whether the returned value should be tainted.
4594 It is the responsibility of the caller of the pattern (i.e. pp_match,
4595 pp_subst etc) to set this flag for any other circumstances where $1 needs
4596 to be tainted.
4597
4598 The taint behaviour of pp_subst (and pp_substcont) is quite complex.
4599
4600 There are three possible sources of taint
4601     * the source string
4602     * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN)
4603     * the replacement string (or expression under /e)
4604     
4605 There are four destinations of taint and they are affected by the sources
4606 according to the rules below:
4607
4608     * the return value (not including /r):
4609         tainted by the source string and pattern, but only for the
4610         number-of-iterations case; boolean returns aren't tainted;
4611     * the modified string (or modified copy under /r):
4612         tainted by the source string, pattern, and replacement strings;
4613     * $1 et al:
4614         tainted by the pattern, and under 'use re "taint"', by the source
4615         string too;
4616     * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted:
4617         should always be unset before executing subsequent code.
4618
4619 The overall action of pp_subst is:
4620
4621     * at the start, set bits in rxtainted indicating the taint status of
4622         the various sources.
4623
4624     * After each pattern execution, update the SUBST_TAINT_PAT bit in
4625         rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the
4626         pattern has subsequently become tainted via locale ops.
4627
4628     * If control is being passed to pp_substcont to execute a /e block,
4629         save rxtainted in the CXt_SUBST block, for future use by
4630         pp_substcont.
4631
4632     * Whenever control is being returned to perl code (either by falling
4633         off the "end" of pp_subst/pp_substcont, or by entering a /e block),
4634         use the flag bits in rxtainted to make all the appropriate types of
4635         destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1
4636         et al will appear tainted.
4637
4638 pp_match is just a simpler version of the above.
4639
4640 */
4641
4642 PP_wrapped(pp_subst, ((PL_op->op_flags & OPf_STACKED) ? 2 : 1), 0)
4643 {
4644     dSP; dTARG;
4645     PMOP *pm = cPMOP;
4646     PMOP *rpm = pm;
4647     char *s;
4648     char *strend;
4649     const char *c;
4650     STRLEN clen;
4651     SSize_t iters = 0;
4652     SSize_t maxiters;
4653     bool once;
4654     U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits.
4655                         See "how taint works" above */
4656     char *orig;
4657     U8 r_flags;
4658     REGEXP *rx = PM_GETRE(pm);
4659     regexp *prog = ReANY(rx);
4660     STRLEN len;
4661     int force_on_match = 0;
4662     const I32 oldsave = PL_savestack_ix;
4663     bool doutf8 = FALSE; /* whether replacement is in utf8 */
4664 #ifdef PERL_ANY_COW
4665     bool was_cow;
4666 #endif
4667     SV *nsv = NULL;
4668     /* known replacement string? */
4669     SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL;
4670
4671     PERL_ASYNC_CHECK();
4672
4673     if (PL_op->op_flags & OPf_STACKED)
4674         TARG = POPs;
4675     else {
4676         if (ARGTARG)
4677             GETTARGET;
4678         else {
4679             TARG = DEFSV;
4680         }
4681         EXTEND(SP,1);
4682     }
4683
4684     SvGETMAGIC(TARG); /* must come before cow check */
4685 #ifdef PERL_ANY_COW
4686     /* note that a string might get converted to COW during matching */
4687     was_cow = cBOOL(SvIsCOW(TARG));
4688 #endif
4689     if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
4690 #ifndef PERL_ANY_COW
4691         if (SvIsCOW(TARG))
4692             sv_force_normal_flags(TARG,0);
4693 #endif
4694         if ((SvREADONLY(TARG)
4695                 || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG))
4696                       || SvTYPE(TARG) > SVt_PVLV)
4697                      && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
4698             Perl_croak_no_modify();
4699     }
4700     PUTBACK;
4701
4702     orig = SvPV_nomg(TARG, len);
4703     /* note we don't (yet) force the var into being a string; if we fail
4704      * to match, we leave as-is; on successful match however, we *will*
4705      * coerce into a string, then repeat the match */
4706     if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG))
4707         force_on_match = 1;
4708
4709     /* only replace once? */
4710     once = !(rpm->op_pmflags & PMf_GLOBAL);
4711
4712     /* See "how taint works" above */
4713     if (TAINTING_get) {
4714         rxtainted  = (
4715             (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0)
4716           | (RXp_ISTAINTED(prog) ? SUBST_TAINT_PAT : 0)
4717           | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0)
4718           | ((  (once && !(rpm->op_pmflags & PMf_NONDESTRUCT))
4719              || (PL_op->op_private & OPpTRUEBOOL)) ? SUBST_TAINT_BOOLRET : 0));
4720         TAINT_NOT;
4721     }
4722
4723   force_it:
4724     if (!pm || !orig)
4725         DIE(aTHX_ "panic: pp_subst, pm=%p, orig=%p", pm, orig);
4726
4727     strend = orig + len;
4728     /* We can match twice at each position, once with zero-length,
4729      * second time with non-zero.
4730      * Don't handle utf8 specially; we can use length-in-bytes as an
4731      * upper bound on length-in-characters, and avoid the cpu-cost of
4732      * computing a tighter bound. */
4733     maxiters = 2 * len + 10;
4734
4735     /* handle the empty pattern */
4736     if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
4737         if (PL_curpm == PL_reg_curpm) {
4738             if (PL_curpm_under) {
4739                 if (PL_curpm_under == PL_reg_curpm) {
4740                     Perl_croak(aTHX_ "Infinite recursion via empty pattern");
4741                 } else {
4742                     pm = PL_curpm_under;
4743                 }
4744             }
4745         } else {
4746             pm = PL_curpm;
4747         }
4748         rx = PM_GETRE(pm);
4749         prog = ReANY(rx);
4750     }
4751
4752 #ifdef PERL_SAWAMPERSAND
4753     r_flags = (    RXp_NPARENS(prog)
4754                 || PL_sawampersand
4755                 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
4756                 || (rpm->op_pmflags & PMf_KEEPCOPY)
4757               )
4758           ? REXEC_COPY_STR
4759           : 0;
4760 #else
4761     r_flags = REXEC_COPY_STR;
4762 #endif
4763
4764     if (!CALLREGEXEC(rx, orig, strend, orig, 0, TARG, NULL, r_flags))
4765     {
4766         SPAGAIN;
4767         PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no);
4768         LEAVE_SCOPE(oldsave);
4769         RETURN;
4770     }
4771     PL_curpm = pm;
4772
4773     /* known replacement string? */
4774     if (dstr) {
4775         /* replacement needing upgrading? */
4776         if (DO_UTF8(TARG) && !doutf8) {
4777              nsv = sv_newmortal();
4778              SvSetSV(nsv, dstr);
4779              sv_utf8_upgrade(nsv);
4780              c = SvPV_const(nsv, clen);
4781              doutf8 = TRUE;
4782         }
4783         else {
4784             c = SvPV_const(dstr, clen);
4785             doutf8 = DO_UTF8(dstr);
4786         }
4787
4788         if (UNLIKELY(TAINT_get))
4789             rxtainted |= SUBST_TAINT_REPL;
4790     }
4791     else {
4792         c = NULL;
4793         doutf8 = FALSE;
4794     }
4795     
4796     /* can do inplace substitution? */
4797     if (c
4798 #ifdef PERL_ANY_COW
4799         && !was_cow
4800 #endif
4801         && (SSize_t)clen <= RXp_MINLENRET(prog)
4802         && (  once
4803            || !(r_flags & REXEC_COPY_STR)
4804            || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN))
4805            )
4806         && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST)
4807         && (!doutf8 || SvUTF8(TARG))
4808         && !(rpm->op_pmflags & PMf_NONDESTRUCT))
4809     {
4810
4811 #ifdef PERL_ANY_COW
4812         /* string might have got converted to COW since we set was_cow */
4813         if (SvIsCOW(TARG)) {
4814           if (!force_on_match)
4815             goto have_a_cow;
4816           assert(SvVOK(TARG));
4817         }
4818 #endif
4819         if (force_on_match) {
4820             /* redo the first match, this time with the orig var
4821              * forced into being a string */
4822             force_on_match = 0;
4823             orig = SvPV_force_nomg(TARG, len);
4824             goto force_it;
4825         }
4826
4827         if (once) {
4828             char *d, *m;
4829             if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
4830                 rxtainted |= SUBST_TAINT_PAT;
4831             m = orig + RXp_OFFS_START(prog,0);
4832             d = orig + RXp_OFFS_END(prog,0);
4833             s = orig;
4834             if (m - s > strend - d) {  /* faster to shorten from end */
4835                 SSize_t i;
4836                 if (clen) {
4837                     Copy(c, m, clen, char);
4838                     m += clen;
4839                 }
4840                 i = strend - d;
4841                 if (i > 0) {
4842                     Move(d, m, i, char);
4843                     m += i;
4844                 }
4845                 *m = '\0';
4846                 SvCUR_set(TARG, m - s);
4847             }
4848             else {      /* faster from front */
4849                 SSize_t i = m - s;
4850                 d -= clen;
4851                 if (i > 0)
4852                     Move(s, d - i, i, char);
4853                 sv_chop(TARG, d-i);
4854                 if (clen)
4855                     Copy(c, d, clen, char);
4856             }
4857             SPAGAIN;
4858             PUSHs(&PL_sv_yes);
4859         }
4860         else {
4861             char *d, *m;
4862             d = s = RXp_OFFS_START(prog,0) + orig;
4863             do {
4864                 SSize_t i;
4865                 if (UNLIKELY(iters++ > maxiters))
4866                     DIE(aTHX_ "Substitution loop");
4867                 /* run time pattern taint, eg locale */
4868                 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
4869                     rxtainted |= SUBST_TAINT_PAT;
4870                 m = RXp_OFFS_START(prog,0) + orig;
4871                 if ((i = m - s)) {
4872                     if (s != d)
4873                         Move(s, d, i, char);
4874                     d += i;
4875                 }
4876                 if (clen) {
4877                     Copy(c, d, clen, char);
4878                     d += clen;
4879                 }
4880                 s = RXp_OFFS_END(prog,0) + orig;
4881             } while (CALLREGEXEC(rx, s, strend, orig,
4882                                  s == m, /* don't match same null twice */
4883                                  TARG, NULL,
4884                      REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
4885             if (s != d) {
4886                 SSize_t i = strend - s;
4887                 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
4888                 Move(s, d, i+1, char);          /* include the NUL */
4889             }
4890             SPAGAIN;
4891             assert(iters);
4892             if (PL_op->op_private & OPpTRUEBOOL)
4893                 PUSHs(&PL_sv_yes);
4894             else
4895                 mPUSHi(iters);
4896         }
4897     }
4898     else {
4899         bool first;
4900         char *m;
4901         SV *repl;
4902         if (force_on_match) {
4903             /* redo the first match, this time with the orig var
4904              * forced into being a string */
4905             force_on_match = 0;
4906             if (rpm->op_pmflags & PMf_NONDESTRUCT) {
4907                 /* I feel that it should be possible to avoid this mortal copy
4908                    given that the code below copies into a new destination.
4909                    However, I suspect it isn't worth the complexity of
4910                    unravelling the C<goto force_it> for the small number of
4911                    cases where it would be viable to drop into the copy code. */
4912                 TARG = sv_2mortal(newSVsv(TARG));
4913             }
4914             orig = SvPV_force_nomg(TARG, len);
4915             goto force_it;
4916         }
4917 #ifdef PERL_ANY_COW
4918       have_a_cow:
4919 #endif
4920         if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
4921             rxtainted |= SUBST_TAINT_PAT;
4922         repl = dstr;
4923         s = RXp_OFFS_START(prog,0) + orig;
4924         dstr = newSVpvn_flags(orig, s-orig,
4925                     SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0));
4926         if (!c) {
4927             PERL_CONTEXT *cx;
4928             SPAGAIN;
4929             m = orig;
4930             /* note that a whole bunch of local vars are saved here for
4931              * use by pp_substcont: here's a list of them in case you're
4932              * searching for places in this sub that uses a particular var:
4933              * iters maxiters r_flags oldsave rxtainted orig dstr targ
4934              * s m strend rx once */
4935             CX_PUSHSUBST(cx);
4936             RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot);
4937         }
4938         first = TRUE;
4939         do {
4940             if (UNLIKELY(iters++ > maxiters))
4941                 DIE(aTHX_ "Substitution loop");
4942             if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
4943                 rxtainted |= SUBST_TAINT_PAT;
4944             if (RXp_MATCH_COPIED(prog) && RXp_SUBBEG(prog) != orig) {
4945                 char *old_s    = s;
4946                 char *old_orig = orig;
4947                 assert(RXp_SUBOFFSET(prog) == 0);
4948
4949                 orig = RXp_SUBBEG(prog);
4950                 s = orig + (old_s - old_orig);
4951                 strend = s + (strend - old_s);
4952             }
4953             m = RXp_OFFS_START(prog,0) + orig;
4954             sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG));
4955             s = RXp_OFFS_END(prog,0) + orig;
4956             if (first) {
4957                 /* replacement already stringified */
4958               if (clen)
4959                 sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8);
4960               first = FALSE;
4961             }
4962             else {
4963                 sv_catsv(dstr, repl);
4964             }
4965             if (once)
4966                 break;
4967         } while (CALLREGEXEC(rx, s, strend, orig,
4968                              s == m,    /* Yields minend of 0 or 1 */
4969                              TARG, NULL,
4970                     REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
4971         assert(strend >= s);
4972         sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG));
4973
4974         if (rpm->op_pmflags & PMf_NONDESTRUCT) {
4975             /* From here on down we're using the copy, and leaving the original
4976                untouched.  */
4977             TARG = dstr;
4978             SPAGAIN;
4979             PUSHs(dstr);
4980         } else {
4981 #ifdef PERL_ANY_COW
4982             /* The match may make the string COW. If so, brilliant, because
4983                that's just saved us one malloc, copy and free - the regexp has
4984                donated the old buffer, and we malloc an entirely new one, rather
4985                than the regexp malloc()ing a buffer and copying our original,
4986                only for us to throw it away here during the substitution.  */
4987             if (SvIsCOW(TARG)) {
4988                 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
4989             } else
4990 #endif
4991             {
4992                 SvPV_free(TARG);
4993             }
4994             SvPV_set(TARG, SvPVX(dstr));
4995             SvCUR_set(TARG, SvCUR(dstr));
4996             SvLEN_set(TARG, SvLEN(dstr));
4997             SvFLAGS(TARG) |= SvUTF8(dstr);
4998             SvPV_set(dstr, NULL);
4999
5000             SPAGAIN;
5001             if (PL_op->op_private & OPpTRUEBOOL)
5002                 PUSHs(&PL_sv_yes);
5003             else
5004                 mPUSHi(iters);
5005         }
5006     }
5007
5008     if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
5009         (void)SvPOK_only_UTF8(TARG);
5010     }
5011
5012     /* See "how taint works" above */
5013     if (TAINTING_get) {
5014         if ((rxtainted & SUBST_TAINT_PAT) ||
5015             ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) ==
5016                                 (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
5017         )
5018             (RXp_MATCH_TAINTED_on(prog)); /* taint $1 et al */
5019
5020         if (!(rxtainted & SUBST_TAINT_BOOLRET)
5021             && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
5022         )
5023             SvTAINTED_on(TOPs);  /* taint return value */
5024         else
5025             SvTAINTED_off(TOPs);  /* may have got tainted earlier */
5026
5027         /* needed for mg_set below */
5028         TAINT_set(
5029           cBOOL(rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
5030         );
5031         SvTAINT(TARG);
5032     }
5033     SvSETMAGIC(TARG); /* PL_tainted must be correctly set for this mg_set */
5034     TAINT_NOT;
5035     LEAVE_SCOPE(oldsave);
5036     RETURN;
5037 }
5038
5039
5040 PP(pp_grepwhile)
5041 {
5042     /* Understanding the stack during a grep.
5043      *
5044      * 'grep expr, args' is implemented in the form of
5045      *     grepstart;
5046      *     do {
5047      *          expr;
5048      *          grepwhile;
5049      *     } while (args);
5050      *
5051      * The stack examples below are in the form of 'perl -Ds' output,
5052      * where any stack element indexed by PL_markstack_ptr[i] has a star
5053      * just to the right of it.  In addition, the corresponding i value
5054      * is displayed under the indexed stack element.
5055      *
5056      * On entry to grepwhile, the stack looks like this:
5057      *
5058      *      =>   *  M1..Mn  X1  *  X2..Xn  C  *  R1..Rn  BOOL
5059      *       [-2]          [-1]           [0]
5060      *
5061      * where:
5062      *   M1..Mn   Accumulated args which have been matched so far.
5063      *   X1..Xn   Random discardable elements from previous iterations.
5064      *   C        The current (just processed) arg, still aliased to $_.
5065      *   R1..Rn   The args remaining to be processed.
5066      *   BOOL     the result of the just-executed grep expression.
5067      *
5068      * Note that it is easiest to think of the top two stack marks as both
5069      * being one too high, and so it would make more sense to have had the
5070      * marks like this:
5071      *
5072      *      =>   *  M1..Mn  *  X1..Xn  *  C  R1..Rn  BOOL
5073      *      [-2]       [-1]        [0]
5074      *
5075      * where the stack is divided neatly into 3 groups:
5076      *   - matched,
5077      *   - discarded,
5078      *   - being, or yet to be, processed.
5079      * But off-by-one is the way it is currently, and it works as long as
5080      * we keep it consistent and bear it in mind.
5081      *
5082      * pp_grepwhile() does the following:
5083      *
5084      * - for a match, replace the X1 pointer with a pointer to C and bump
5085      *     PL_markstack_ptr[-1]
5086      * - if more args to process, bump PL_markstack_ptr[0] and update the
5087      *     $_ alias, else
5088      * - remove top 3 MARKs and return M1..Mn, or a scalar,
5089      *     or void as appropriate.
5090      *
5091      */
5092
5093     bool match = SvTRUE_NN(*PL_stack_sp);
5094     rpp_popfree_1();
5095
5096     if (match) {
5097         SV **from_p = PL_stack_base + PL_markstack_ptr[0];
5098         SV **to_p   = PL_stack_base + PL_markstack_ptr[-1]++;
5099         SV *from    = *from_p;
5100         SV *to      = *to_p;
5101
5102         if (from != to) {
5103             *to_p = from;
5104 #ifdef PERL_RC_STACK
5105             SvREFCNT_inc_simple_void_NN(from);
5106             SvREFCNT_dec(to);
5107 #endif
5108         }
5109     }
5110
5111     ++*PL_markstack_ptr;
5112     FREETMPS;
5113     LEAVE_with_name("grep_item");                                       /* exit inner scope */
5114
5115     /* All done yet? */
5116     if (UNLIKELY(PL_stack_base + *PL_markstack_ptr > PL_stack_sp)) {
5117         I32 items;
5118         const U8 gimme = GIMME_V;
5119
5120         LEAVE_with_name("grep");                                        /* exit outer scope */
5121         (void)POPMARK;                          /* pop src */
5122         items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
5123         (void)POPMARK;                          /* pop dst */
5124         SV **base = PL_stack_base + POPMARK;    /* pop original mark */
5125
5126         if (gimme == G_LIST)
5127             rpp_popfree_to(base + items);
5128         else {
5129             rpp_popfree_to(base);
5130             if (gimme == G_SCALAR) {
5131                 if (PL_op->op_private & OPpTRUEBOOL)
5132                     rpp_push_1(items ? &PL_sv_yes : &PL_sv_zero);
5133                 else {
5134                     dTARGET;
5135                     TARGi(items,1);
5136                     rpp_push_1(TARG);
5137                 }
5138             }
5139         }
5140
5141         return NORMAL;
5142     }
5143     else {
5144         SV *src;
5145
5146         ENTER_with_name("grep_item");                                   /* enter inner scope */
5147         SAVEVPTR(PL_curpm);
5148
5149         src = PL_stack_base[TOPMARK];
5150         if (SvPADTMP(src)) {
5151             SV *newsrc = sv_mortalcopy(src);
5152              PL_stack_base[TOPMARK] = newsrc;
5153 #ifdef PERL_RC_STACK
5154             SvREFCNT_inc_simple_void_NN(newsrc);
5155             SvREFCNT_dec(src);
5156 #endif
5157             src = newsrc;
5158             PL_tmps_floor++;
5159         }
5160         SvTEMP_off(src);
5161         DEFSV_set(src);
5162
5163         return cLOGOP->op_other;
5164     }
5165 }
5166
5167
5168 /* leave_adjust_stacks():
5169  *
5170  * Process a scope's return args (in the range from_sp+1 .. PL_stack_sp),
5171  * positioning them at to_sp+1 onwards, and do the equivalent of a
5172  * FREEMPS and TAINT_NOT.
5173  *
5174  * Not intended to be called in void context.
5175  *
5176  * When leaving a sub, eval, do{} or other scope, the things that need
5177  * doing to process the return args are:
5178  *    * in scalar context, only return the last arg (or PL_sv_undef if none);
5179  *    * for the types of return that return copies of their args (such
5180  *      as rvalue sub return), make a mortal copy of every return arg,
5181  *      except where we can optimise the copy away without it being
5182  *      semantically visible;
5183  *    * make sure that the arg isn't prematurely freed; in the case of an
5184  *      arg not copied, this may involve mortalising it. For example, in
5185  *      C<sub f { my $x = ...; $x }>, $x would be freed when we do
5186  *      CX_LEAVE_SCOPE(cx) unless it's protected or copied.
5187  *
5188  * What condition to use when deciding whether to pass the arg through
5189  * or make a copy, is determined by the 'pass' arg; its valid values are:
5190  *   0: rvalue sub/eval exit
5191  *   1: other rvalue scope exit
5192  *   2: :lvalue sub exit in rvalue context
5193  *   3: :lvalue sub exit in lvalue context and other lvalue scope exits
5194  *
5195  * There is a big issue with doing a FREETMPS. We would like to free any
5196  * temps created by the last statement which the sub executed, rather than
5197  * leaving them for the caller. In a situation where a sub call isn't
5198  * soon followed by a nextstate (e.g. nested recursive calls, a la
5199  * fibonacci()), temps can accumulate, causing memory and performance
5200  * issues.
5201  *
5202  * On the other hand, we don't want to free any TEMPs which are keeping
5203  * alive any return args that we skipped copying; nor do we wish to undo
5204  * any mortalising done here.
5205  *
5206  * The solution is to split the temps stack frame into two, with a cut
5207  * point delineating the two halves. We arrange that by the end of this
5208  * function, all the temps stack frame entries we wish to keep are in the
5209  * range  PL_tmps_floor+1.. tmps_base-1, while the ones to free now are in
5210  * the range  tmps_base .. PL_tmps_ix.  During the course of this
5211  * function, tmps_base starts off as PL_tmps_floor+1, then increases
5212  * whenever we find or create a temp that we know should be kept. In
5213  * general the stuff above tmps_base is undecided until we reach the end,
5214  * and we may need a sort stage for that.
5215  *
5216  * To determine whether a TEMP is keeping a return arg alive, every
5217  * arg that is kept rather than copied and which has the SvTEMP flag
5218  * set, has the flag temporarily unset, to mark it. At the end we scan
5219  * the temps stack frame above the cut for entries without SvTEMP and
5220  * keep them, while turning SvTEMP on again. Note that if we die before
5221  * the SvTEMPs flags are set again, its safe: at worst, subsequent use of
5222  * those SVs may be slightly less efficient.
5223  *
5224  * In practice various optimisations for some common cases mean we can
5225  * avoid most of the scanning and swapping about with the temps stack.
5226  */
5227
5228 void
5229 Perl_leave_adjust_stacks(pTHX_ SV **from_sp, SV **to_sp, U8 gimme, int pass)
5230 {
5231     SSize_t tmps_base; /* lowest index into tmps stack that needs freeing now */
5232     SSize_t nargs;
5233
5234     PERL_ARGS_ASSERT_LEAVE_ADJUST_STACKS;
5235
5236     TAINT_NOT;
5237
5238     if (gimme == G_LIST) {
5239         nargs = PL_stack_sp - from_sp;
5240         from_sp++;
5241     }
5242     else {
5243         assert(gimme == G_SCALAR);
5244         if (UNLIKELY(from_sp >= PL_stack_sp)) {
5245             /* no return args */
5246             assert(from_sp == PL_stack_sp);
5247             rpp_extend(1);
5248             *++PL_stack_sp = &PL_sv_undef;
5249         }
5250         from_sp = PL_stack_sp;
5251         nargs   = 1;
5252     }
5253
5254     /* common code for G_SCALAR and G_LIST */
5255
5256 #ifdef PERL_RC_STACK
5257     {
5258         /* free any items from the stack which are about to get
5259          * over-written */
5260         SV **p = from_sp - 1;
5261         assert(p >= to_sp);
5262         while (p > to_sp) {
5263             SV *sv = *p;
5264             *p-- = NULL;
5265             SvREFCNT_dec(sv);
5266         }
5267     }
5268 #endif
5269
5270
5271     tmps_base = PL_tmps_floor + 1;
5272
5273     assert(nargs >= 0);
5274     if (nargs) {
5275         /* pointer version of tmps_base. Not safe across temp stack
5276          * reallocs. */
5277         SV **tmps_basep;
5278
5279         EXTEND_MORTAL(nargs); /* one big extend for worst-case scenario */
5280         tmps_basep = PL_tmps_stack + tmps_base;
5281
5282         /* process each return arg */
5283
5284         do {
5285             SV *sv = *from_sp++;
5286
5287             assert(PL_tmps_ix + nargs < PL_tmps_max);
5288 #ifdef DEBUGGING
5289             /* PADTMPs with container set magic shouldn't appear in the
5290              * wild. This assert is more important for pp_leavesublv(),
5291              * but by testing for it here, we're more likely to catch
5292              * bad cases (what with :lvalue subs not being widely
5293              * deployed). The two issues are that for something like
5294              *     sub :lvalue { $tied{foo} }
5295              * or
5296              *     sub :lvalue { substr($foo,1,2) }
5297              * pp_leavesublv() will croak if the sub returns a PADTMP,
5298              * and currently functions like pp_substr() return a mortal
5299              * rather than using their PADTMP when returning a PVLV.
5300              * This is because the PVLV will hold a ref to $foo,
5301              * so $foo would get delayed in being freed while
5302              * the PADTMP SV remained in the PAD.
5303              * So if this assert fails it means either:
5304              *  1) there is pp code similar to pp_substr that is
5305              *     returning a PADTMP instead of a mortal, and probably
5306              *     needs fixing, or
5307              *  2) pp_leavesublv is making unwarranted assumptions
5308              *     about always croaking on a PADTMP
5309              */
5310             if (SvPADTMP(sv) && SvSMAGICAL(sv)) {
5311                 MAGIC *mg;
5312                 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
5313                     assert(PERL_MAGIC_TYPE_IS_VALUE_MAGIC(mg->mg_type));
5314                 }
5315             }
5316 #endif
5317
5318             if (
5319                pass == 0 ? (rpp_is_lone(sv) && !SvMAGICAL(sv))
5320              : pass == 1 ? ((SvTEMP(sv) || SvPADTMP(sv)) && !SvMAGICAL(sv) && SvREFCNT(sv) == 1)
5321              : pass == 2 ? (!SvPADTMP(sv))
5322              : 1)
5323             {
5324                 /* pass through: skip copy for logic or optimisation
5325                  * reasons; instead mortalise it, except that ... */
5326
5327 #ifdef PERL_RC_STACK
5328                 from_sp[-1] = NULL;
5329 #endif
5330                 *++to_sp = sv;
5331
5332                 if (SvTEMP(sv)) {
5333                     /* ... since this SV is an SvTEMP , we don't need to
5334                      * re-mortalise it; instead we just need to ensure
5335                      * that its existing entry in the temps stack frame
5336                      * ends up below the cut and so avoids being freed
5337                      * this time round. We mark it as needing to be kept
5338                      * by temporarily unsetting SvTEMP; then at the end,
5339                      * we shuffle any !SvTEMP entries on the tmps stack
5340                      * back below the cut.
5341                      * However, there's a significant chance that there's
5342                      * a 1:1 correspondence between the first few (or all)
5343                      * elements in the return args stack frame and those
5344                      * in the temps stack frame; e,g.:
5345                      *      sub f { ....; map {...} .... },
5346                      * or if we're exiting multiple scopes and one of the
5347                      * inner scopes has already made mortal copies of each
5348                      * return arg.
5349                      *
5350                      * If so, this arg sv will correspond to the next item
5351                      * on the tmps stack above the cut, and so can be kept
5352                      * merely by moving the cut boundary up one, rather
5353                      * than messing with SvTEMP.  If all args are 1:1 then
5354                      * we can avoid the sorting stage below completely.
5355                      *
5356                      * If there are no items above the cut on the tmps
5357                      * stack, then the SvTEMP must comne from an item
5358                      * below the cut, so there's nothing to do.
5359                      */
5360                     if (tmps_basep <= &PL_tmps_stack[PL_tmps_ix]) {
5361                         if (sv == *tmps_basep)
5362                             tmps_basep++;
5363                         else
5364                             SvTEMP_off(sv);
5365                     }
5366                 }
5367                 else if (!SvPADTMP(sv)) {
5368                     /* mortalise arg to avoid it being freed during save
5369                      * stack unwinding. Pad tmps don't need mortalising as
5370                      * they're never freed. This is the equivalent of
5371                      * sv_2mortal(SvREFCNT_inc(sv)), except that:
5372                      *  * it assumes that the temps stack has already been
5373                      *    extended;
5374                      *  * it puts the new item at the cut rather than at
5375                      *    ++PL_tmps_ix, moving the previous occupant there
5376                      *    instead.
5377                      */
5378                     if (!SvIMMORTAL(sv)) {
5379                         SvREFCNT_inc_simple_void_NN(sv);
5380                         SvTEMP_on(sv);
5381                         /* Note that if there's nothing above the cut,
5382                          * this copies the garbage one slot above
5383                          * PL_tmps_ix onto itself. This is harmless (the
5384                          * stack's already been extended), but might in
5385                          * theory trigger warnings from tools like ASan
5386                          */
5387                         PL_tmps_stack[++PL_tmps_ix] = *tmps_basep;
5388                         *tmps_basep++ = sv;
5389                     }
5390                 }
5391             }
5392             else {
5393                 /* Make a mortal copy of the SV.
5394                  * The following code is the equivalent of sv_mortalcopy()
5395                  * except that:
5396                  *  * it assumes the temps stack has already been extended;
5397                  *  * it optimises the copying for some simple SV types;
5398                  *  * it puts the new item at the cut rather than at
5399                  *    ++PL_tmps_ix, moving the previous occupant there
5400                  *    instead.
5401                  */
5402                 SV *newsv = newSV_type(SVt_NULL);
5403
5404                 PL_tmps_stack[++PL_tmps_ix] = *tmps_basep;
5405                 /* put it on the tmps stack early so it gets freed if we die */
5406                 *tmps_basep++ = newsv;
5407
5408                 if (SvTYPE(sv) <= SVt_IV) {
5409                     /* arg must be one of undef, IV/UV, or RV: skip
5410                      * sv_setsv_flags() and do the copy directly */
5411                     U32 dstflags;
5412                     U32 srcflags = SvFLAGS(sv);
5413
5414                     assert(!SvGMAGICAL(sv));
5415                     if (srcflags & (SVf_IOK|SVf_ROK)) {
5416                         SET_SVANY_FOR_BODYLESS_IV(newsv);
5417
5418                         if (srcflags & SVf_ROK) {
5419                             newsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(sv));
5420                             /* SV type plus flags */
5421                             dstflags = (SVt_IV|SVf_ROK|SVs_TEMP);
5422                         }
5423                         else {
5424                             /* both src and dst are <= SVt_IV, so sv_any
5425                              * points to the head; so access the heads
5426                              * directly rather than going via sv_any.
5427                              */
5428                             assert(    &(sv->sv_u.svu_iv)
5429                                     == &(((XPVIV*) SvANY(sv))->xiv_iv));
5430                             assert(    &(newsv->sv_u.svu_iv)
5431                                     == &(((XPVIV*) SvANY(newsv))->xiv_iv));
5432                             newsv->sv_u.svu_iv = sv->sv_u.svu_iv;
5433                             /* SV type plus flags */
5434                             dstflags = (SVt_IV|SVf_IOK|SVp_IOK|SVs_TEMP
5435                                             |(srcflags & SVf_IVisUV));
5436                         }
5437                     }
5438                     else {
5439                         assert(!(srcflags & SVf_OK));
5440                         dstflags = (SVt_NULL|SVs_TEMP); /* SV type plus flags */
5441                     }
5442                     SvFLAGS(newsv) = dstflags;
5443
5444                 }
5445                 else {
5446                     /* do the full sv_setsv() */
5447                     SSize_t old_base;
5448
5449                     SvTEMP_on(newsv);
5450                     old_base = tmps_basep - PL_tmps_stack;
5451                     SvGETMAGIC(sv);
5452                     sv_setsv_flags(newsv, sv, SV_DO_COW_SVSETSV);
5453                     /* the mg_get or sv_setsv might have created new temps
5454                      * or realloced the tmps stack; regrow and reload */
5455                     EXTEND_MORTAL(nargs);
5456                     tmps_basep = PL_tmps_stack + old_base;
5457                     TAINT_NOT;  /* Each item is independent */
5458                 }
5459
5460
5461 #ifdef PERL_RC_STACK
5462                 from_sp[-1] = NULL;
5463                 SvREFCNT_dec_NN(sv);
5464                 assert(!to_sp[1]);
5465                 *++to_sp = newsv;
5466                 SvREFCNT_inc_simple_void_NN(newsv);
5467 #else
5468                 *++to_sp = newsv;
5469 #endif
5470
5471             }
5472         } while (--nargs);
5473
5474         /* If there are any temps left above the cut, we need to sort
5475          * them into those to keep and those to free. The only ones to
5476          * keep are those for which we've temporarily unset SvTEMP.
5477          * Work inwards from the two ends at tmps_basep .. PL_tmps_ix,
5478          * swapping pairs as necessary. Stop when we meet in the middle.
5479          */
5480         {
5481             SV **top = PL_tmps_stack + PL_tmps_ix;
5482             while (tmps_basep <= top) {
5483                 SV *sv = *top;
5484                 if (SvTEMP(sv))
5485                     top--;
5486                 else {
5487                     SvTEMP_on(sv);
5488                     *top = *tmps_basep;
5489                     *tmps_basep = sv;
5490                     tmps_basep++;
5491                 }
5492             }
5493         }
5494
5495         tmps_base = tmps_basep - PL_tmps_stack;
5496     }
5497
5498     PL_stack_sp = to_sp;
5499
5500     /* unrolled FREETMPS() but using tmps_base-1 rather than PL_tmps_floor */
5501     while (PL_tmps_ix >= tmps_base) {
5502         SV* const sv = PL_tmps_stack[PL_tmps_ix--];
5503 #ifdef PERL_POISON
5504         PoisonWith(PL_tmps_stack + PL_tmps_ix + 1, 1, SV *, 0xAB);
5505 #endif
5506         if (LIKELY(sv)) {
5507             SvTEMP_off(sv);
5508             SvREFCNT_dec_NN(sv); /* note, can modify tmps_ix!!! */
5509         }
5510     }
5511 }
5512
5513
5514 /* also tail-called by pp_return */
5515
5516 PP(pp_leavesub)
5517 {
5518     U8 gimme;
5519     PERL_CONTEXT *cx;
5520     SV **oldsp;
5521     OP *retop;
5522
5523     cx = CX_CUR();
5524     assert(CxTYPE(cx) == CXt_SUB);
5525
5526     if (CxMULTICALL(cx)) {
5527         /* entry zero of a stack is always PL_sv_undef, which
5528          * simplifies converting a '()' return into undef in scalar context */
5529         assert(PL_stack_sp > PL_stack_base || *PL_stack_base == &PL_sv_undef);
5530         return 0;
5531     }
5532
5533     gimme = cx->blk_gimme;
5534     oldsp = PL_stack_base + cx->blk_oldsp; /* last arg of previous frame */
5535
5536     if (gimme == G_VOID)
5537         rpp_popfree_to(oldsp);
5538     else
5539         leave_adjust_stacks(oldsp, oldsp, gimme, 0);
5540
5541     CX_LEAVE_SCOPE(cx);
5542     cx_popsub(cx);      /* Stack values are safe: release CV and @_ ... */
5543     cx_popblock(cx);
5544     retop = cx->blk_sub.retop;
5545     CX_POP(cx);
5546
5547     return retop;
5548 }
5549
5550
5551 /* clear (if possible) or abandon the current @_. If 'abandon' is true,
5552  * forces an abandon */
5553
5554 void
5555 Perl_clear_defarray(pTHX_ AV* av, bool abandon)
5556 {
5557     PERL_ARGS_ASSERT_CLEAR_DEFARRAY;
5558
5559     if (LIKELY(!abandon && SvREFCNT(av) == 1 && !SvMAGICAL(av))
5560 #ifndef PERL_RC_STACK
5561         && !AvREAL(av)
5562 #endif
5563     ) {
5564         clear_defarray_simple(av);
5565 #ifndef PERL_RC_STACK
5566         AvREIFY_only(av);
5567 #endif
5568     }
5569     else {
5570         /* abandon */
5571         const SSize_t size = AvFILLp(av) + 1;
5572         /* The ternary gives consistency with av_extend() */
5573         AV *newav = newAV_alloc_xz(size < PERL_ARRAY_NEW_MIN_KEY ?
5574                                          PERL_ARRAY_NEW_MIN_KEY : size);
5575 #ifndef PERL_RC_STACK
5576         AvREIFY_only(newav);
5577 #endif
5578         PAD_SVl(0) = MUTABLE_SV(newav);
5579         SvREFCNT_dec_NN(av);
5580     }
5581 }
5582
5583
5584 PP(pp_entersub)
5585 {
5586     GV *gv;
5587     CV *cv;
5588     PERL_CONTEXT *cx;
5589     I32 old_savestack_ix;
5590     SV *sv = *PL_stack_sp;
5591
5592     if (UNLIKELY(!sv))
5593         goto do_die;
5594
5595     /* Locate the CV to call:
5596      * - most common case: RV->CV: f(), $ref->():
5597      *   note that if a sub is compiled before its caller is compiled,
5598      *   the stash entry will be a ref to a CV, rather than being a GV.
5599      * - second most common case: CV: $ref->method()
5600      */
5601
5602     /* a non-magic-RV -> CV ? */
5603     if (LIKELY( (SvFLAGS(sv) & (SVf_ROK|SVs_GMG)) == SVf_ROK)) {
5604         cv = MUTABLE_CV(SvRV(sv));
5605         if (UNLIKELY(SvOBJECT(cv))) /* might be overloaded */
5606             goto do_ref;
5607     }
5608     else
5609         cv = MUTABLE_CV(sv);
5610
5611     /* a CV ? */
5612     if (UNLIKELY(SvTYPE(cv) != SVt_PVCV)) {
5613         /* handle all the weird cases */
5614         switch (SvTYPE(sv)) {
5615         case SVt_PVLV:
5616             if (!isGV_with_GP(sv))
5617                 goto do_default;
5618             /* FALLTHROUGH */
5619         case SVt_PVGV:
5620             cv = GvCVu((const GV *)sv);
5621             if (UNLIKELY(!cv)) {
5622                 HV *stash;
5623                 cv = sv_2cv(sv, &stash, &gv, 0);
5624                 if (!cv) {
5625                     old_savestack_ix = PL_savestack_ix;
5626                     goto try_autoload;
5627                 }
5628             }
5629             break;
5630
5631         default:
5632           do_default:
5633             SvGETMAGIC(sv);
5634             if (SvROK(sv)) {
5635               do_ref:
5636                 if (UNLIKELY(SvAMAGIC(sv))) {
5637                     sv = amagic_deref_call(sv, to_cv_amg);
5638                 }
5639             }
5640             else {
5641                 const char *sym;
5642                 STRLEN len;
5643                 if (UNLIKELY(!SvOK(sv)))
5644                     DIE(aTHX_ PL_no_usym, "a subroutine");
5645
5646                 sym = SvPV_nomg_const(sv, len);
5647                 if (PL_op->op_private & HINT_STRICT_REFS)
5648                     DIE(aTHX_ "Can't use string (\"%" SVf32 "\"%s) as a subroutine ref while \"strict refs\" in use", sv, len>32 ? "..." : "");
5649                 cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv));
5650                 break;
5651             }
5652             cv = MUTABLE_CV(SvRV(sv));
5653             if (LIKELY(SvTYPE(cv) == SVt_PVCV))
5654                 break;
5655             /* FALLTHROUGH */
5656         case SVt_PVHV:
5657         case SVt_PVAV:
5658           do_die:
5659             DIE(aTHX_ "Not a CODE reference");
5660         }
5661     }
5662
5663     /* At this point we want to save PL_savestack_ix, either by doing a
5664      * cx_pushsub(), or for XS, doing an ENTER. But we don't yet know the final
5665      * CV we will be using (so we don't know whether its XS, so we can't
5666      * cx_pushsub() or ENTER yet), and determining cv may itself push stuff on
5667      * the save stack. So remember where we are currently on the save
5668      * stack, and later update the CX or scopestack entry accordingly. */
5669     old_savestack_ix = PL_savestack_ix;
5670
5671     /* these two fields are in a union. If they ever become separate,
5672      * we have to test for both of them being null below */
5673     assert(cv);
5674     assert((void*)&CvROOT(cv) == (void*)&CvXSUB(cv));
5675     while (UNLIKELY(!CvROOT(cv))) {
5676         GV* autogv;
5677         SV* sub_name;
5678
5679         /* anonymous or undef'd function leaves us no recourse */
5680         if (CvLEXICAL(cv) && CvHASGV(cv))
5681             DIE(aTHX_ "Undefined subroutine &%" SVf " called",
5682                        SVfARG(cv_name(cv, NULL, 0)));
5683         if (CvANON(cv) || !CvHASGV(cv)) {
5684             DIE(aTHX_ "Undefined subroutine called");
5685         }
5686
5687         /* autoloaded stub? */
5688         if (cv != GvCV(gv = CvGV(cv))) {
5689             cv = GvCV(gv);
5690         }
5691         /* should call AUTOLOAD now? */
5692         else {
5693           try_autoload:
5694             autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
5695                                      (GvNAMEUTF8(gv) ? SVf_UTF8 : 0)
5696                                     |(PL_op->op_flags & OPf_REF
5697                                        ? GV_AUTOLOAD_ISMETHOD
5698                                        : 0));
5699             cv = autogv ? GvCV(autogv) : NULL;
5700         }
5701         if (!cv) {
5702             sub_name = sv_newmortal();
5703             gv_efullname3(sub_name, gv, NULL);
5704             DIE(aTHX_ "Undefined subroutine &%" SVf " called", SVfARG(sub_name));
5705         }
5706     }
5707
5708     /* unrolled "CvCLONE(cv) && ! CvCLONED(cv)" */
5709     if (UNLIKELY((CvFLAGS(cv) & (CVf_CLONE|CVf_CLONED)) == CVf_CLONE))
5710         DIE(aTHX_ "Closure prototype called");
5711
5712     if (UNLIKELY((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub)
5713             && !CvNODEBUG(cv)))
5714     {
5715          Perl_get_db_sub(aTHX_ &sv, cv);
5716          if (CvISXSUB(cv))
5717              PL_curcopdb = PL_curcop;
5718          if (CvLVALUE(cv)) {
5719              /* check for lsub that handles lvalue subroutines */
5720              cv = GvCV(gv_fetchpvs("DB::lsub", GV_ADDMULTI, SVt_PVCV));
5721              /* if lsub not found then fall back to DB::sub */
5722              if (!cv) cv = GvCV(PL_DBsub);
5723          } else {
5724              cv = GvCV(PL_DBsub);
5725          }
5726
5727         if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
5728             DIE(aTHX_ "No DB::sub routine defined");
5729     }
5730
5731     rpp_popfree_1(); /* finished with sv now */
5732
5733     if (!(CvISXSUB(cv))) {
5734         /* This path taken at least 75% of the time   */
5735         dMARK;
5736         PADLIST *padlist;
5737         I32 depth;
5738         bool hasargs;
5739         U8 gimme;
5740
5741         /* keep PADTMP args alive throughout the call (we need to do this
5742          * because @_ isn't refcounted). Note that we create the mortals
5743          * in the caller's tmps frame, so they won't be freed until after
5744          * we return from the sub.
5745          */
5746         {
5747             SV **svp = MARK;
5748             while (svp < PL_stack_sp) {
5749                 SV *sv = *++svp;
5750                 if (!sv)
5751                     continue;
5752                 if (SvPADTMP(sv)) {
5753                     SV *newsv = sv_mortalcopy(sv);
5754                     *svp = newsv;
5755 #ifdef PERL_RC_STACK
5756                     /* should just skip the mortalisation instead */
5757                     SvREFCNT_inc_simple_void_NN(newsv);
5758                     SvREFCNT_dec_NN(sv);
5759 #endif
5760                     sv = newsv;
5761                 }
5762                 SvTEMP_off(sv);
5763             }
5764         }
5765
5766         gimme = GIMME_V;
5767         cx = cx_pushblock(CXt_SUB, gimme, MARK, old_savestack_ix);
5768         hasargs = cBOOL(PL_op->op_flags & OPf_STACKED);
5769         cx_pushsub(cx, cv, PL_op->op_next, hasargs);
5770
5771         padlist = CvPADLIST(cv);
5772         if (UNLIKELY((depth = ++CvDEPTH(cv)) >= 2))
5773             pad_push(padlist, depth);
5774         PAD_SET_CUR_NOSAVE(padlist, depth);
5775         if (LIKELY(hasargs)) {
5776             AV *const av = MUTABLE_AV(PAD_SVl(0));
5777             SSize_t items;
5778             AV **defavp;
5779
5780             defavp = &GvAV(PL_defgv);
5781             cx->blk_sub.savearray = *defavp;
5782             *defavp = MUTABLE_AV(SvREFCNT_inc_simple_NN(av));
5783
5784             /* it's the responsibility of whoever leaves a sub to ensure
5785              * that a clean, empty AV is left in pad[0]. This is normally
5786              * done by cx_popsub() */
5787
5788 #ifdef PERL_RC_STACK
5789             assert(AvREAL(av));
5790 #else
5791             assert(!AvREAL(av));
5792 #endif
5793             assert(AvFILLp(av) == -1);
5794
5795             items = PL_stack_sp - MARK;
5796             if (UNLIKELY(items - 1 > AvMAX(av))) {
5797                 SV **ary = AvALLOC(av);
5798                 Renew(ary, items, SV*);
5799                 AvMAX(av) = items - 1;
5800                 AvALLOC(av) = ary;
5801                 AvARRAY(av) = ary;
5802             }
5803
5804             if (items)
5805                 Copy(MARK+1,AvARRAY(av),items,SV*);
5806             AvFILLp(av) = items - 1;
5807 #ifdef PERL_RC_STACK
5808             /* transfer ownership of the arguments' refcounts to av */
5809             PL_stack_sp = MARK;
5810 #endif
5811         }
5812         if (UNLIKELY((cx->blk_u16 & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO &&
5813             !CvLVALUE(cv)))
5814             DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf,
5815                 SVfARG(cv_name(cv, NULL, 0)));
5816         /* warning must come *after* we fully set up the context
5817          * stuff so that __WARN__ handlers can safely dounwind()
5818          * if they want to
5819          */
5820         if (UNLIKELY(depth == PERL_SUB_DEPTH_WARN
5821                 && ckWARN(WARN_RECURSION)
5822                 && !(PERLDB_SUB && cv == GvCV(PL_DBsub))))
5823             sub_crush_depth(cv);
5824         return CvSTART(cv);
5825     }
5826     else {
5827         SSize_t markix = TOPMARK;
5828         bool is_scalar;
5829
5830         ENTER;
5831         /* pretend we did the ENTER earlier */
5832         PL_scopestack[PL_scopestack_ix - 1] = old_savestack_ix;
5833
5834         SAVETMPS;
5835
5836         if (UNLIKELY(((PL_op->op_private
5837                & CX_PUSHSUB_GET_LVALUE_MASK(Perl_is_lvalue_sub)
5838              ) & OPpENTERSUB_LVAL_MASK) == OPpLVAL_INTRO &&
5839             !CvLVALUE(cv)))
5840             DIE(aTHX_ "Can't modify non-lvalue subroutine call of &%" SVf,
5841                 SVfARG(cv_name(cv, NULL, 0)));
5842
5843         if (UNLIKELY(!(PL_op->op_flags & OPf_STACKED) && GvAV(PL_defgv))) {
5844             /* Need to copy @_ to stack. Alternative may be to
5845              * switch stack to @_, and copy return values
5846              * back. This would allow popping @_ in XSUB, e.g.. XXXX */
5847             AV * const av = GvAV(PL_defgv);
5848             const SSize_t items = AvFILL(av) + 1;
5849
5850             if (items) {
5851                 SSize_t i = 0;
5852                 const bool m = cBOOL(SvRMAGICAL(av));
5853                 /* Mark is at the end of the stack. */
5854                 rpp_extend(items);
5855                 for (; i < items; ++i)
5856                 {
5857                     SV *sv;
5858                     if (m) {
5859                         SV ** const svp = av_fetch(av, i, 0);
5860                         sv = svp ? *svp : NULL;
5861                     }
5862                     else
5863                         sv = AvARRAY(av)[i];
5864
5865                     rpp_push_1(sv ? sv : av_nonelem(av, i));
5866                 }
5867             }
5868         }
5869         else {
5870             SV **mark = PL_stack_base + markix;
5871             SSize_t items = PL_stack_sp - mark;
5872             while (items--) {
5873                 mark++;
5874                 if (*mark && SvPADTMP(*mark)) {
5875                     SV *oldsv = *mark;
5876                     SV *newsv = sv_mortalcopy(oldsv);
5877                     *mark = newsv;
5878 #ifdef PERL_RC_STACK
5879                     /* should just skip the mortalisation instead */
5880                     SvREFCNT_inc_simple_void_NN(newsv);
5881                     SvREFCNT_dec_NN(oldsv);
5882 #endif
5883                 }
5884             }
5885         }
5886
5887         /* We assume first XSUB in &DB::sub is the called one. */
5888         if (UNLIKELY(PL_curcopdb)) {
5889             SAVEVPTR(PL_curcop);
5890             PL_curcop = PL_curcopdb;
5891             PL_curcopdb = NULL;
5892         }
5893         /* Do we need to open block here? XXXX */
5894
5895         /* calculate gimme here as PL_op might get changed and then not
5896          * restored until the LEAVE further down */
5897         is_scalar = (GIMME_V == G_SCALAR);
5898
5899         /* CvXSUB(cv) must not be NULL because newXS() refuses NULL xsub address */
5900         assert(CvXSUB(cv));
5901
5902         rpp_invoke_xs(cv);
5903
5904 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
5905         /* This duplicates the check done in runops_debug(), but provides more
5906          * information in the common case of the fault being with an XSUB.
5907          *
5908          * It should also catch an XSUB pushing more than it extends
5909          * in scalar context.
5910         */
5911         if (PL_curstackinfo->si_stack_hwm < PL_stack_sp - PL_stack_base)
5912             Perl_croak_nocontext(
5913                 "panic: XSUB %s::%s (%s) failed to extend arg stack: "
5914                 "base=%p, sp=%p, hwm=%p\n",
5915                     HvNAME(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)), CvFILE(cv),
5916                     PL_stack_base, PL_stack_sp,
5917                     PL_stack_base + PL_curstackinfo->si_stack_hwm);
5918 #endif
5919         /* Enforce some sanity in scalar context. */
5920         if (is_scalar) {
5921             SV **svp = PL_stack_base + markix + 1;
5922             if (svp != PL_stack_sp) {
5923 #ifdef PERL_RC_STACK
5924                 if (svp < PL_stack_sp) {
5925                     /* move return value to bottom of stack frame
5926                      * and free everything else */
5927                     SV* retsv = *PL_stack_sp;
5928                     *PL_stack_sp = *svp;
5929                     *svp = retsv;
5930                     rpp_popfree_to(svp);
5931                 }
5932                 else
5933                     *++PL_stack_sp = &PL_sv_undef;
5934 #else
5935                 *svp = svp > PL_stack_sp ? &PL_sv_undef : *PL_stack_sp;
5936                 PL_stack_sp = svp;
5937 #endif
5938             }
5939         }
5940         LEAVE;
5941         return NORMAL;
5942     }
5943 }
5944
5945 void
5946 Perl_sub_crush_depth(pTHX_ CV *cv)
5947 {
5948     PERL_ARGS_ASSERT_SUB_CRUSH_DEPTH;
5949
5950     if (CvANON(cv))
5951         Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
5952     else {
5953         Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%" SVf "\"",
5954                     SVfARG(cv_name(cv,NULL,0)));
5955     }
5956 }
5957
5958
5959
5960 /* like croak, but report in context of caller */
5961
5962 void
5963 Perl_croak_caller(const char *pat, ...)
5964 {
5965     dTHX;
5966     va_list args;
5967     const PERL_CONTEXT *cx = caller_cx(0, NULL);
5968
5969     /* make error appear at call site */
5970     assert(cx);
5971     PL_curcop = cx->blk_oldcop;
5972
5973     va_start(args, pat);
5974     vcroak(pat, &args);
5975     NOT_REACHED; /* NOTREACHED */
5976     va_end(args);
5977 }
5978
5979
5980 PP_wrapped(pp_aelem, 2, 0)
5981 {
5982     dSP;
5983     SV** svp;
5984     SV* const elemsv = POPs;
5985     IV elem = SvIV(elemsv);
5986     AV *const av = MUTABLE_AV(POPs);
5987     const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
5988     const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
5989     const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5990     bool preeminent = TRUE;
5991     SV *sv;
5992
5993     if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC)))
5994         Perl_warner(aTHX_ packWARN(WARN_MISC),
5995                     "Use of reference \"%" SVf "\" as array index",
5996                     SVfARG(elemsv));
5997     if (UNLIKELY(SvTYPE(av) != SVt_PVAV))
5998         RETPUSHUNDEF;
5999
6000     if (UNLIKELY(localizing)) {
6001         MAGIC *mg;
6002         HV *stash;
6003
6004         /* Try to preserve the existence of a tied array
6005          * element by using EXISTS and DELETE if possible.
6006          * Fall back to FETCH and STORE otherwise. */
6007         if (SvCANEXISTDELETE(av))
6008             preeminent = av_exists(av, elem);
6009     }
6010
6011     svp = av_fetch(av, elem, lval && !defer);
6012     if (lval) {
6013 #ifdef PERL_MALLOC_WRAP
6014          if (SvUOK(elemsv)) {
6015               const UV uv = SvUV(elemsv);
6016               elem = uv > IV_MAX ? IV_MAX : uv;
6017          }
6018          else if (SvNOK(elemsv))
6019               elem = (IV)SvNV(elemsv);
6020          if (elem > 0) {
6021               MEM_WRAP_CHECK_s(elem,SV*,"Out of memory during array extend");
6022          }
6023 #endif
6024         if (!svp || !*svp) {
6025             IV len;
6026             if (!defer)
6027                 DIE(aTHX_ PL_no_aelem, elem);
6028             len = av_top_index(av);
6029             /* Resolve a negative index that falls within the array.  Leave
6030                it negative it if falls outside the array.  */
6031             if (elem < 0 && len + elem >= 0)
6032                 elem = len + elem;
6033             if (elem >= 0 && elem <= len)
6034                 /* Falls within the array.  */
6035                 PUSHs(av_nonelem(av,elem));
6036             else
6037                 /* Falls outside the array.  If it is negative,
6038                    magic_setdefelem will use the index for error reporting.
6039                  */
6040                 mPUSHs(newSVavdefelem(av, elem, 1));
6041             RETURN;
6042         }
6043         if (UNLIKELY(localizing)) {
6044             if (preeminent)
6045                 save_aelem(av, elem, svp);
6046             else
6047                 SAVEADELETE(av, elem);
6048         }
6049         else if (PL_op->op_private & OPpDEREF) {
6050             PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
6051             RETURN;
6052         }
6053     }
6054     sv = (svp ? *svp : &PL_sv_undef);
6055     if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
6056         mg_get(sv);
6057     PUSHs(sv);
6058     RETURN;
6059 }
6060
6061 SV*
6062 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
6063 {
6064     PERL_ARGS_ASSERT_VIVIFY_REF;
6065
6066     SvGETMAGIC(sv);
6067     if (!SvOK(sv)) {
6068         if (SvREADONLY(sv))
6069             Perl_croak_no_modify();
6070         prepare_SV_for_RV(sv);
6071         switch (to_what) {
6072         case OPpDEREF_SV:
6073             SvRV_set(sv, newSV_type(SVt_NULL));
6074             break;
6075         case OPpDEREF_AV:
6076             SvRV_set(sv, MUTABLE_SV(newAV()));
6077             break;
6078         case OPpDEREF_HV:
6079             SvRV_set(sv, MUTABLE_SV(newHV()));
6080             break;
6081         }
6082         SvROK_on(sv);
6083         SvSETMAGIC(sv);
6084         SvGETMAGIC(sv);
6085     }
6086     if (SvGMAGICAL(sv)) {
6087         /* copy the sv without magic to prevent magic from being
6088            executed twice */
6089         SV* msv = sv_newmortal();
6090         sv_setsv_nomg(msv, sv);
6091         return msv;
6092     }
6093     return sv;
6094 }
6095
6096 PERL_STATIC_INLINE HV *
6097 S_opmethod_stash(pTHX_ SV* meth)
6098 {
6099     SV* ob;
6100     HV* stash;
6101
6102     SV* const sv = PL_stack_base + TOPMARK == PL_stack_sp
6103         ? (Perl_croak(aTHX_ "Can't call method \"%" SVf "\" without a "
6104                             "package or object reference", SVfARG(meth)),
6105            (SV *)NULL)
6106         : *(PL_stack_base + TOPMARK + 1);
6107
6108     PERL_ARGS_ASSERT_OPMETHOD_STASH;
6109
6110     if (UNLIKELY(!sv))
6111        undefined:
6112         Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on an undefined value",
6113                    SVfARG(meth));
6114
6115     if (UNLIKELY(SvGMAGICAL(sv))) mg_get(sv);
6116     else if (SvIsCOW_shared_hash(sv)) { /* MyClass->meth() */
6117         stash = gv_stashsv(sv, GV_CACHE_ONLY);
6118         if (stash) return stash;
6119     }
6120
6121     if (SvROK(sv))
6122         ob = MUTABLE_SV(SvRV(sv));
6123     else if (!SvOK(sv)) goto undefined;
6124     else if (isGV_with_GP(sv)) {
6125         if (!GvIO(sv))
6126             Perl_croak(aTHX_ "Can't call method \"%" SVf "\" "
6127                              "without a package or object reference",
6128                               SVfARG(meth));
6129         ob = sv;
6130         if (SvTYPE(ob) == SVt_PVLV && LvTYPE(ob) == 'y') {
6131             assert(!LvTARGLEN(ob));
6132             ob = LvTARG(ob);
6133             assert(ob);
6134         }
6135         /* Replace the object at the base of the stack frame.
6136          * This is "below" whatever pp_wrap has wrapped, so needs freeing.
6137          */
6138         SV *newsv = sv_2mortal(newRV(ob));
6139         SV **svp = (PL_stack_base + TOPMARK + 1);
6140 #ifdef PERL_RC_STACK
6141         SV *oldsv = *svp;
6142 #endif
6143         *svp = newsv;
6144 #ifdef PERL_RC_STACK
6145         SvREFCNT_inc_simple_void_NN(newsv);
6146         SvREFCNT_dec_NN(oldsv);
6147 #endif
6148     }
6149     else {
6150         /* this isn't a reference */
6151         GV* iogv;
6152         STRLEN packlen;
6153         const char * const packname = SvPV_nomg_const(sv, packlen);
6154         const U32 packname_utf8 = SvUTF8(sv);
6155         stash = gv_stashpvn(packname, packlen, packname_utf8 | GV_CACHE_ONLY);
6156         if (stash) return stash;
6157
6158         if (!(iogv = gv_fetchpvn_flags(
6159                 packname, packlen, packname_utf8, SVt_PVIO
6160              )) ||
6161             !(ob=MUTABLE_SV(GvIO(iogv))))
6162         {
6163             /* this isn't the name of a filehandle either */
6164             if (!packlen)
6165             {
6166                 Perl_croak(aTHX_ "Can't call method \"%" SVf "\" "
6167                                  "without a package or object reference",
6168                                   SVfARG(meth));
6169             }
6170             /* assume it's a package name */
6171             stash = gv_stashpvn(packname, packlen, packname_utf8);
6172             if (stash) return stash;
6173             else return MUTABLE_HV(sv);
6174         }
6175         /* it _is_ a filehandle name -- replace with a reference.
6176          * Replace the object at the base of the stack frame.
6177          * This is "below" whatever pp_wrap has wrapped, so needs freeing.
6178          */
6179         SV *newsv = sv_2mortal(newRV(MUTABLE_SV(iogv)));
6180         SV **svp = (PL_stack_base + TOPMARK + 1);
6181 #ifdef PERL_RC_STACK
6182         SV *oldsv = *svp;
6183 #endif
6184         *svp = newsv;
6185 #ifdef PERL_RC_STACK
6186         SvREFCNT_inc_simple_void_NN(newsv);
6187         SvREFCNT_dec_NN(oldsv);
6188 #endif
6189     }
6190
6191     /* if we got here, ob should be an object or a glob */
6192     if (!ob || !(SvOBJECT(ob)
6193                  || (isGV_with_GP(ob)
6194                      && (ob = MUTABLE_SV(GvIO((const GV *)ob)))
6195                      && SvOBJECT(ob))))
6196     {
6197         Perl_croak(aTHX_ "Can't call method \"%" SVf "\" on unblessed reference",
6198                    SVfARG((SvPOK(meth) && SvPVX(meth) == PL_isa_DOES)
6199                                         ? newSVpvs_flags("DOES", SVs_TEMP)
6200                                         : meth));
6201     }
6202
6203     return SvSTASH(ob);
6204 }
6205
6206 PP_wrapped(pp_method, 1, 0)
6207 {
6208     dSP;
6209     GV* gv;
6210     HV* stash;
6211     SV* const meth = TOPs;
6212
6213     if (SvROK(meth)) {
6214         SV* const rmeth = SvRV(meth);
6215         if (SvTYPE(rmeth) == SVt_PVCV) {
6216             SETs(rmeth);
6217             RETURN;
6218         }
6219     }
6220
6221     stash = opmethod_stash(meth);
6222
6223     gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK);
6224     assert(gv);
6225
6226     SETs(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
6227     RETURN;
6228 }
6229
6230 #define METHOD_CHECK_CACHE(stash,cache,meth)                            \
6231     const HE* const he = hv_fetch_ent(cache, meth, 0, 0);               \
6232     if (he) {                                                           \
6233         gv = MUTABLE_GV(HeVAL(he));                                     \
6234         if (isGV(gv) && GvCV(gv) && (!GvCVGEN(gv) || GvCVGEN(gv)        \
6235              == (PL_sub_generation + HvMROMETA(stash)->cache_gen)))     \
6236         {                                                               \
6237             rpp_xpush_1(MUTABLE_SV(GvCV(gv)));                          \
6238             return NORMAL;                                              \
6239         }                                                               \
6240     }                                                                   \
6241
6242 PP(pp_method_named)
6243 {
6244     GV* gv;
6245     SV* const meth = cMETHOP_meth;
6246     HV* const stash = opmethod_stash(meth);
6247
6248     if (LIKELY(SvTYPE(stash) == SVt_PVHV)) {
6249         METHOD_CHECK_CACHE(stash, stash, meth);
6250     }
6251
6252     gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK);
6253     assert(gv);
6254
6255     rpp_xpush_1(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
6256     return NORMAL;
6257 }
6258
6259 PP(pp_method_super)
6260 {
6261     GV* gv;
6262     HV* cache;
6263     SV* const meth = cMETHOP_meth;
6264     HV* const stash = CopSTASH(PL_curcop);
6265     /* Actually, SUPER doesn't need real object's (or class') stash at all,
6266      * as it uses CopSTASH. However, we must ensure that object(class) is
6267      * correct (this check is done by S_opmethod_stash) */
6268     opmethod_stash(meth);
6269
6270     if ((cache = HvMROMETA(stash)->super)) {
6271         METHOD_CHECK_CACHE(stash, cache, meth);
6272     }
6273
6274     gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER);
6275     assert(gv);
6276
6277     rpp_xpush_1(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
6278     return NORMAL;
6279 }
6280
6281 PP(pp_method_redir)
6282 {
6283     GV* gv;
6284     SV* const meth = cMETHOP_meth;
6285     HV* stash = gv_stashsv(cMETHOP_rclass, 0);
6286     opmethod_stash(meth); /* not used but needed for error checks */
6287
6288     if (stash) { METHOD_CHECK_CACHE(stash, stash, meth); }
6289     else stash = MUTABLE_HV(cMETHOP_rclass);
6290
6291     gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK);
6292     assert(gv);
6293
6294     rpp_xpush_1(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
6295     return NORMAL;
6296 }
6297
6298 PP(pp_method_redir_super)
6299 {
6300     GV* gv;
6301     HV* cache;
6302     SV* const meth = cMETHOP_meth;
6303     HV* stash = gv_stashsv(cMETHOP_rclass, 0);
6304     opmethod_stash(meth); /* not used but needed for error checks */
6305
6306     if (UNLIKELY(!stash)) stash = MUTABLE_HV(cMETHOP_rclass);
6307     else if ((cache = HvMROMETA(stash)->super)) {
6308          METHOD_CHECK_CACHE(stash, cache, meth);
6309     }
6310
6311     gv = gv_fetchmethod_sv_flags(stash, meth, GV_AUTOLOAD|GV_CROAK|GV_SUPER);
6312     assert(gv);
6313
6314     rpp_xpush_1(isGV(gv) ? MUTABLE_SV(GvCV(gv)) : MUTABLE_SV(gv));
6315     return NORMAL;
6316 }
6317
6318 /*
6319  * ex: set ts=8 sts=4 sw=4 et:
6320  */