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