This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_padav(): use S_pushav()
[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
LW
36#include "perl.h"
37
38/* Hot code. */
39
40PP(pp_const)
41{
39644a26 42 dSP;
996c9baa 43 XPUSHs(cSVOP_sv);
a0d0e21e
LW
44 RETURN;
45}
46
47PP(pp_nextstate)
48{
533c011a 49 PL_curcop = (COP*)PL_op;
a0d0e21e 50 TAINT_NOT; /* Each statement is presumed innocent */
4ebe6e95 51 PL_stack_sp = PL_stack_base + CX_CUR()->blk_oldsp;
a0d0e21e 52 FREETMPS;
f410a211 53 PERL_ASYNC_CHECK();
a0d0e21e
LW
54 return NORMAL;
55}
56
57PP(pp_gvsv)
58{
39644a26 59 dSP;
924508f0 60 EXTEND(SP,1);
5d9574c1 61 if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO))
1d7c1841 62 PUSHs(save_scalar(cGVOP_gv));
a0d0e21e 63 else
c69033f2 64 PUSHs(GvSVn(cGVOP_gv));
a0d0e21e
LW
65 RETURN;
66}
67
b1c05ba5
DM
68
69/* also used for: pp_lineseq() pp_regcmaybe() pp_scalar() pp_scope() */
70
a0d0e21e
LW
71PP(pp_null)
72{
73 return NORMAL;
74}
75
3dd9d4e4
FC
76/* This is sometimes called directly by pp_coreargs, pp_grepstart and
77 amagic_call. */
a0d0e21e
LW
78PP(pp_pushmark)
79{
3280af22 80 PUSHMARK(PL_stack_sp);
a0d0e21e
LW
81 return NORMAL;
82}
83
84PP(pp_stringify)
85{
20b7effb 86 dSP; dTARGET;
4cc783ef
DD
87 SV * const sv = TOPs;
88 SETs(TARG);
89 sv_copypv(TARG, sv);
90 SvSETMAGIC(TARG);
91 /* no PUTBACK, SETs doesn't inc/dec SP */
92 return NORMAL;
a0d0e21e
LW
93}
94
95PP(pp_gv)
96{
20b7effb 97 dSP;
ad64d0ec 98 XPUSHs(MUTABLE_SV(cGVOP_gv));
a0d0e21e
LW
99 RETURN;
100}
101
b1c05ba5
DM
102
103/* also used for: pp_andassign() */
104
a0d0e21e
LW
105PP(pp_and)
106{
f410a211 107 PERL_ASYNC_CHECK();
4cc783ef
DD
108 {
109 /* SP is not used to remove a variable that is saved across the
110 sv_2bool_flags call in SvTRUE_NN, if a RISC/CISC or low/high machine
111 register or load/store vs direct mem ops macro is introduced, this
112 should be a define block between direct PL_stack_sp and dSP operations,
113 presently, using PL_stack_sp is bias towards CISC cpus */
114 SV * const sv = *PL_stack_sp;
115 if (!SvTRUE_NN(sv))
116 return NORMAL;
117 else {
118 if (PL_op->op_type == OP_AND)
119 --PL_stack_sp;
120 return cLOGOP->op_other;
121 }
a0d0e21e
LW
122 }
123}
124
125PP(pp_sassign)
126{
20b7effb 127 dSP;
3e75a3c4
RU
128 /* sassign keeps its args in the optree traditionally backwards.
129 So we pop them differently.
130 */
131 SV *left = POPs; SV *right = TOPs;
748a9306 132
354eabfa 133 if (PL_op->op_private & OPpASSIGN_BACKWARDS) { /* {or,and,dor}assign */
0bd48802
AL
134 SV * const temp = left;
135 left = right; right = temp;
a0d0e21e 136 }
d48c660d
DM
137 assert(TAINTING_get || !TAINT_get);
138 if (UNLIKELY(TAINT_get) && !SvTAINTED(right))
a0d0e21e 139 TAINT_NOT;
5d9574c1
DM
140 if (UNLIKELY(PL_op->op_private & OPpASSIGN_CV_TO_GV)) {
141 /* *foo =\&bar */
3e75a3c4 142 SV * const cv = SvRV(right);
e26df76a 143 const U32 cv_type = SvTYPE(cv);
3e75a3c4 144 const bool is_gv = isGV_with_GP(left);
6136c704 145 const bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM;
e26df76a
NC
146
147 if (!got_coderef) {
148 assert(SvROK(cv));
149 }
150
3e75a3c4
RU
151 /* Can do the optimisation if left (LVALUE) is not a typeglob,
152 right (RVALUE) is a reference to something, and we're in void
e26df76a 153 context. */
13be902c 154 if (!got_coderef && !is_gv && GIMME_V == G_VOID) {
e26df76a 155 /* Is the target symbol table currently empty? */
3e75a3c4 156 GV * const gv = gv_fetchsv_nomg(left, GV_NOINIT, SVt_PVGV);
bb112e5a 157 if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) {
e26df76a
NC
158 /* Good. Create a new proxy constant subroutine in the target.
159 The gv becomes a(nother) reference to the constant. */
160 SV *const value = SvRV(cv);
161
ad64d0ec 162 SvUPGRADE(MUTABLE_SV(gv), SVt_IV);
1ccdb730 163 SvPCS_IMPORTED_on(gv);
e26df76a 164 SvRV_set(gv, value);
b37c2d43 165 SvREFCNT_inc_simple_void(value);
3e75a3c4 166 SETs(left);
e26df76a
NC
167 RETURN;
168 }
169 }
170
171 /* Need to fix things up. */
13be902c 172 if (!is_gv) {
e26df76a 173 /* Need to fix GV. */
3e75a3c4 174 left = MUTABLE_SV(gv_fetchsv_nomg(left,GV_ADD, SVt_PVGV));
e26df76a
NC
175 }
176
177 if (!got_coderef) {
178 /* We've been returned a constant rather than a full subroutine,
179 but they expect a subroutine reference to apply. */
53a42478 180 if (SvROK(cv)) {
d343c3ef 181 ENTER_with_name("sassign_coderef");
53a42478
NC
182 SvREFCNT_inc_void(SvRV(cv));
183 /* newCONSTSUB takes a reference count on the passed in SV
184 from us. We set the name to NULL, otherwise we get into
185 all sorts of fun as the reference to our new sub is
186 donated to the GV that we're about to assign to.
187 */
3e75a3c4 188 SvRV_set(right, MUTABLE_SV(newCONSTSUB(GvSTASH(left), NULL,
ad64d0ec 189 SvRV(cv))));
fc2b2dca 190 SvREFCNT_dec_NN(cv);
d343c3ef 191 LEAVE_with_name("sassign_coderef");
53a42478
NC
192 } else {
193 /* What can happen for the corner case *{"BONK"} = \&{"BONK"};
194 is that
195 First: ops for \&{"BONK"}; return us the constant in the
196 symbol table
197 Second: ops for *{"BONK"} cause that symbol table entry
198 (and our reference to it) to be upgraded from RV
199 to typeblob)
200 Thirdly: We get here. cv is actually PVGV now, and its
201 GvCV() is actually the subroutine we're looking for
202
203 So change the reference so that it points to the subroutine
204 of that typeglob, as that's what they were after all along.
205 */
159b6efe 206 GV *const upgraded = MUTABLE_GV(cv);
53a42478
NC
207 CV *const source = GvCV(upgraded);
208
209 assert(source);
210 assert(CvFLAGS(source) & CVf_CONST);
211
0ad694a7 212 SvREFCNT_inc_simple_void_NN(source);
fc2b2dca 213 SvREFCNT_dec_NN(upgraded);
3e75a3c4 214 SvRV_set(right, MUTABLE_SV(source));
53a42478 215 }
e26df76a 216 }
53a42478 217
e26df76a 218 }
8fe85e3f 219 if (
5d9574c1 220 UNLIKELY(SvTEMP(left)) && !SvSMAGICAL(left) && SvREFCNT(left) == 1 &&
3e75a3c4 221 (!isGV_with_GP(left) || SvFAKE(left)) && ckWARN(WARN_MISC)
8fe85e3f
FC
222 )
223 Perl_warner(aTHX_
224 packWARN(WARN_MISC), "Useless assignment to a temporary"
225 );
3e75a3c4
RU
226 SvSetMagicSV(left, right);
227 SETs(left);
a0d0e21e
LW
228 RETURN;
229}
230
231PP(pp_cond_expr)
232{
20b7effb 233 dSP;
f4c975aa
DM
234 SV *sv;
235
f410a211 236 PERL_ASYNC_CHECK();
f4c975aa
DM
237 sv = POPs;
238 RETURNOP(SvTRUE_NN(sv) ? cLOGOP->op_other : cLOGOP->op_next);
a0d0e21e
LW
239}
240
241PP(pp_unstack)
242{
f5319de9 243 PERL_CONTEXT *cx;
8f3964af 244 PERL_ASYNC_CHECK();
a0d0e21e 245 TAINT_NOT; /* Each statement is presumed innocent */
4ebe6e95 246 cx = CX_CUR();
f5319de9 247 PL_stack_sp = PL_stack_base + cx->blk_oldsp;
a0d0e21e 248 FREETMPS;
eae48c89 249 if (!(PL_op->op_flags & OPf_SPECIAL)) {
93661e56 250 assert(CxTYPE(cx) == CXt_BLOCK || CxTYPE_is_LOOP(cx));
dfe0f39b 251 CX_LEAVE_SCOPE(cx);
eae48c89 252 }
a0d0e21e
LW
253 return NORMAL;
254}
255
a0d0e21e
LW
256PP(pp_concat)
257{
20b7effb 258 dSP; dATARGET; tryAMAGICbin_MG(concat_amg, AMGf_assign);
748a9306
LW
259 {
260 dPOPTOPssrl;
8d6d96c1
HS
261 bool lbyte;
262 STRLEN rlen;
d4c19fe8 263 const char *rpv = NULL;
a6b599c7 264 bool rbyte = FALSE;
a9c4fd4e 265 bool rcopied = FALSE;
8d6d96c1 266
6f1401dc
DM
267 if (TARG == right && right != left) { /* $r = $l.$r */
268 rpv = SvPV_nomg_const(right, rlen);
c75ab21a 269 rbyte = !DO_UTF8(right);
59cd0e26 270 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
349d4f2f 271 rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */
db79b45b 272 rcopied = TRUE;
8d6d96c1 273 }
7889fe52 274
89734059 275 if (TARG != left) { /* not $l .= $r */
a9c4fd4e 276 STRLEN llen;
6f1401dc 277 const char* const lpv = SvPV_nomg_const(left, llen);
90f5826e 278 lbyte = !DO_UTF8(left);
8d6d96c1
HS
279 sv_setpvn(TARG, lpv, llen);
280 if (!lbyte)
281 SvUTF8_on(TARG);
282 else
283 SvUTF8_off(TARG);
284 }
18ea7bf2
SM
285 else { /* $l .= $r and left == TARG */
286 if (!SvOK(left)) {
51f69a24
AC
287 if ((left == right /* $l .= $l */
288 || (PL_op->op_private & OPpTARGET_MY)) /* $l = $l . $r */
289 && ckWARN(WARN_UNINITIALIZED)
290 )
291 report_uninit(left);
adf14ec6 292 SvPVCLEAR(left);
c75ab21a 293 }
18ea7bf2
SM
294 else {
295 SvPV_force_nomg_nolen(left);
296 }
583a5589 297 lbyte = !DO_UTF8(left);
90f5826e 298 if (IN_BYTES)
18ea7bf2 299 SvUTF8_off(left);
8d6d96c1 300 }
a12c0f56 301
c75ab21a 302 if (!rcopied) {
6f1401dc 303 rpv = SvPV_nomg_const(right, rlen);
c75ab21a
RH
304 rbyte = !DO_UTF8(right);
305 }
8d6d96c1
HS
306 if (lbyte != rbyte) {
307 if (lbyte)
308 sv_utf8_upgrade_nomg(TARG);
309 else {
db79b45b 310 if (!rcopied)
59cd0e26 311 right = newSVpvn_flags(rpv, rlen, SVs_TEMP);
8d6d96c1 312 sv_utf8_upgrade_nomg(right);
6f1401dc 313 rpv = SvPV_nomg_const(right, rlen);
69b47968 314 }
a0d0e21e 315 }
8d6d96c1 316 sv_catpvn_nomg(TARG, rpv, rlen);
43ebc500 317
a0d0e21e
LW
318 SETTARG;
319 RETURN;
748a9306 320 }
a0d0e21e
LW
321}
322
ea710183 323/* push the elements of av onto the stack */
d5524600 324
f9ae8fb6 325STATIC void
d5524600
DM
326S_pushav(pTHX_ AV* const av)
327{
328 dSP;
c70927a6 329 const SSize_t maxarg = AvFILL(av) + 1;
d5524600 330 EXTEND(SP, maxarg);
5d9574c1 331 if (UNLIKELY(SvRMAGICAL(av))) {
c70927a6
FC
332 PADOFFSET i;
333 for (i=0; i < (PADOFFSET)maxarg; i++) {
d5524600 334 SV ** const svp = av_fetch(av, i, FALSE);
ea710183 335 SP[i+1] = svp ? *svp : &PL_sv_undef;
d5524600
DM
336 }
337 }
338 else {
c70927a6
FC
339 PADOFFSET i;
340 for (i=0; i < (PADOFFSET)maxarg; i++) {
ce0d59fd 341 SV * const sv = AvARRAY(av)[i];
5d9574c1 342 SP[i+1] = LIKELY(sv) ? sv : &PL_sv_undef;
ce0d59fd 343 }
d5524600
DM
344 }
345 SP += maxarg;
346 PUTBACK;
347}
348
349
a7fd8ef6
DM
350/* ($lex1,@lex2,...) or my ($lex1,@lex2,...) */
351
352PP(pp_padrange)
353{
20b7effb 354 dSP;
a7fd8ef6
DM
355 PADOFFSET base = PL_op->op_targ;
356 int count = (int)(PL_op->op_private) & OPpPADRANGE_COUNTMASK;
d5524600
DM
357 if (PL_op->op_flags & OPf_SPECIAL) {
358 /* fake the RHS of my ($x,$y,..) = @_ */
359 PUSHMARK(SP);
360 S_pushav(aTHX_ GvAVn(PL_defgv));
361 SPAGAIN;
362 }
363
a7fd8ef6
DM
364 /* note, this is only skipped for compile-time-known void cxt */
365 if ((PL_op->op_flags & OPf_WANT) != OPf_WANT_VOID) {
19742f39
AL
366 int i;
367
a7fd8ef6
DM
368 EXTEND(SP, count);
369 PUSHMARK(SP);
370 for (i = 0; i <count; i++)
371 *++SP = PAD_SV(base+i);
372 }
373 if (PL_op->op_private & OPpLVAL_INTRO) {
4e09461c
DM
374 SV **svp = &(PAD_SVl(base));
375 const UV payload = (UV)(
376 (base << (OPpPADRANGE_COUNTSHIFT + SAVE_TIGHT_SHIFT))
377 | (count << SAVE_TIGHT_SHIFT)
378 | SAVEt_CLEARPADRANGE);
19742f39
AL
379 int i;
380
6d59e610 381 STATIC_ASSERT_STMT(OPpPADRANGE_COUNTMASK + 1 == (1 << OPpPADRANGE_COUNTSHIFT));
d081a355
DM
382 assert((payload >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
383 == (Size_t)base);
a3444cc5
DM
384 {
385 dSS_ADD;
386 SS_ADD_UV(payload);
387 SS_ADD_END(1);
388 }
4e09461c 389
a7fd8ef6 390 for (i = 0; i <count; i++)
4e09461c 391 SvPADSTALE_off(*svp++); /* mark lexical as active */
a7fd8ef6
DM
392 }
393 RETURN;
394}
395
396
a0d0e21e
LW
397PP(pp_padsv)
398{
20b7effb 399 dSP;
6c28b496
DD
400 EXTEND(SP, 1);
401 {
402 OP * const op = PL_op;
403 /* access PL_curpad once */
404 SV ** const padentry = &(PAD_SVl(op->op_targ));
405 {
406 dTARG;
407 TARG = *padentry;
408 PUSHs(TARG);
409 PUTBACK; /* no pop/push after this, TOPs ok */
8ec5e241 410 }
6c28b496
DD
411 if (op->op_flags & OPf_MOD) {
412 if (op->op_private & OPpLVAL_INTRO)
413 if (!(op->op_private & OPpPAD_STATE))
414 save_clearsv(padentry);
415 if (op->op_private & OPpDEREF) {
8f90a16d
FC
416 /* TOPs is equivalent to TARG here. Using TOPs (SP) rather
417 than TARG reduces the scope of TARG, so it does not
418 span the call to save_clearsv, resulting in smaller
419 machine code. */
6c28b496
DD
420 TOPs = vivify_ref(TOPs, op->op_private & OPpDEREF);
421 }
422 }
423 return op->op_next;
4633a7c4 424 }
a0d0e21e
LW
425}
426
427PP(pp_readline)
428{
30901a8a 429 dSP;
12dc5f94
DM
430 /* pp_coreargs pushes a NULL to indicate no args passed to
431 * CORE::readline() */
30901a8a
FC
432 if (TOPs) {
433 SvGETMAGIC(TOPs);
fc99edcf 434 tryAMAGICunTARGETlist(iter_amg, 0);
30901a8a
FC
435 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
436 }
437 else PL_last_in_gv = PL_argvgv, PL_stack_sp--;
6e592b3a
BM
438 if (!isGV_with_GP(PL_last_in_gv)) {
439 if (SvROK(PL_last_in_gv) && isGV_with_GP(SvRV(PL_last_in_gv)))
159b6efe 440 PL_last_in_gv = MUTABLE_GV(SvRV(PL_last_in_gv));
8efb3254 441 else {
f5284f61 442 dSP;
ad64d0ec 443 XPUSHs(MUTABLE_SV(PL_last_in_gv));
f5284f61 444 PUTBACK;
897d3989 445 Perl_pp_rv2gv(aTHX);
159b6efe 446 PL_last_in_gv = MUTABLE_GV(*PL_stack_sp--);
84ee769f
FC
447 if (PL_last_in_gv == (GV *)&PL_sv_undef)
448 PL_last_in_gv = NULL;
449 else
450 assert(isGV_with_GP(PL_last_in_gv));
f5284f61
IZ
451 }
452 }
a0d0e21e
LW
453 return do_readline();
454}
455
456PP(pp_eq)
457{
20b7effb 458 dSP;
33efebe6
DM
459 SV *left, *right;
460
a42d0242 461 tryAMAGICbin_MG(eq_amg, AMGf_set|AMGf_numeric);
33efebe6
DM
462 right = POPs;
463 left = TOPs;
464 SETs(boolSV(
465 (SvIOK_notUV(left) && SvIOK_notUV(right))
466 ? (SvIVX(left) == SvIVX(right))
467 : ( do_ncmp(left, right) == 0)
468 ));
469 RETURN;
a0d0e21e
LW
470}
471
b1c05ba5 472
4c2c3128 473/* also used for: pp_i_preinc() */
b1c05ba5 474
a0d0e21e
LW
475PP(pp_preinc)
476{
4c2c3128
DM
477 SV *sv = *PL_stack_sp;
478
479 if (LIKELY(((sv->sv_flags &
480 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
481 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
482 == SVf_IOK))
483 && SvIVX(sv) != IV_MAX)
484 {
485 SvIV_set(sv, SvIVX(sv) + 1);
486 }
487 else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_inc */
488 sv_inc(sv);
489 SvSETMAGIC(sv);
490 return NORMAL;
491}
492
493
494/* also used for: pp_i_predec() */
495
496PP(pp_predec)
497{
498 SV *sv = *PL_stack_sp;
499
500 if (LIKELY(((sv->sv_flags &
501 (SVf_THINKFIRST|SVs_GMG|SVf_IVisUV|
502 SVf_IOK|SVf_NOK|SVf_POK|SVp_NOK|SVp_POK|SVf_ROK))
503 == SVf_IOK))
504 && SvIVX(sv) != IV_MIN)
55497cff 505 {
4c2c3128 506 SvIV_set(sv, SvIVX(sv) - 1);
748a9306 507 }
4c2c3128
DM
508 else /* Do all the PERL_PRESERVE_IVUV and hard cases in sv_dec */
509 sv_dec(sv);
510 SvSETMAGIC(sv);
a0d0e21e
LW
511 return NORMAL;
512}
513
b1c05ba5
DM
514
515/* also used for: pp_orassign() */
516
a0d0e21e
LW
517PP(pp_or)
518{
20b7effb 519 dSP;
f4c975aa 520 SV *sv;
f410a211 521 PERL_ASYNC_CHECK();
f4c975aa
DM
522 sv = TOPs;
523 if (SvTRUE_NN(sv))
a0d0e21e
LW
524 RETURN;
525 else {
c960fc3b
SP
526 if (PL_op->op_type == OP_OR)
527 --SP;
a0d0e21e
LW
528 RETURNOP(cLOGOP->op_other);
529 }
530}
531
b1c05ba5
DM
532
533/* also used for: pp_dor() pp_dorassign() */
534
25a55bd7 535PP(pp_defined)
c963b151 536{
20b7effb 537 dSP;
eb578fdb 538 SV* sv;
6136c704 539 bool defined;
25a55bd7 540 const int op_type = PL_op->op_type;
ea5195b7 541 const bool is_dor = (op_type == OP_DOR || op_type == OP_DORASSIGN);
c963b151 542
6136c704 543 if (is_dor) {
f410a211 544 PERL_ASYNC_CHECK();
25a55bd7 545 sv = TOPs;
5d9574c1 546 if (UNLIKELY(!sv || !SvANY(sv))) {
2bd49cfc
NC
547 if (op_type == OP_DOR)
548 --SP;
25a55bd7
SP
549 RETURNOP(cLOGOP->op_other);
550 }
b7c44293
RGS
551 }
552 else {
553 /* OP_DEFINED */
25a55bd7 554 sv = POPs;
5d9574c1 555 if (UNLIKELY(!sv || !SvANY(sv)))
25a55bd7 556 RETPUSHNO;
b7c44293 557 }
25a55bd7 558
6136c704 559 defined = FALSE;
c963b151
BD
560 switch (SvTYPE(sv)) {
561 case SVt_PVAV:
562 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
25a55bd7 563 defined = TRUE;
c963b151
BD
564 break;
565 case SVt_PVHV:
566 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
25a55bd7 567 defined = TRUE;
c963b151
BD
568 break;
569 case SVt_PVCV:
570 if (CvROOT(sv) || CvXSUB(sv))
25a55bd7 571 defined = TRUE;
c963b151
BD
572 break;
573 default:
5b295bef 574 SvGETMAGIC(sv);
c963b151 575 if (SvOK(sv))
25a55bd7 576 defined = TRUE;
6136c704 577 break;
c963b151 578 }
6136c704
AL
579
580 if (is_dor) {
c960fc3b
SP
581 if(defined)
582 RETURN;
583 if(op_type == OP_DOR)
584 --SP;
25a55bd7 585 RETURNOP(cLOGOP->op_other);
25a55bd7 586 }
d9aa96a4
SP
587 /* assuming OP_DEFINED */
588 if(defined)
589 RETPUSHYES;
590 RETPUSHNO;
c963b151
BD
591}
592
230ee21f
DM
593
594
a0d0e21e
LW
595PP(pp_add)
596{
20b7effb 597 dSP; dATARGET; bool useleft; SV *svl, *svr;
230ee21f 598
6f1401dc
DM
599 tryAMAGICbin_MG(add_amg, AMGf_assign|AMGf_numeric);
600 svr = TOPs;
601 svl = TOPm1s;
602
28e5dec8 603#ifdef PERL_PRESERVE_IVUV
230ee21f
DM
604
605 /* special-case some simple common cases */
606 if (!((svl->sv_flags|svr->sv_flags) & (SVf_IVisUV|SVs_GMG))) {
607 IV il, ir;
608 U32 flags = (svl->sv_flags & svr->sv_flags);
609 if (flags & SVf_IOK) {
610 /* both args are simple IVs */
611 UV topl, topr;
612 il = SvIVX(svl);
613 ir = SvIVX(svr);
614 do_iv:
615 topl = ((UV)il) >> (UVSIZE * 8 - 2);
616 topr = ((UV)ir) >> (UVSIZE * 8 - 2);
617
618 /* if both are in a range that can't under/overflow, do a
619 * simple integer add: if the top of both numbers
620 * are 00 or 11, then it's safe */
621 if (!( ((topl+1) | (topr+1)) & 2)) {
622 SP--;
623 TARGi(il + ir, 0); /* args not GMG, so can't be tainted */
624 SETs(TARG);
625 RETURN;
626 }
627 goto generic;
628 }
629 else if (flags & SVf_NOK) {
630 /* both args are NVs */
631 NV nl = SvNVX(svl);
632 NV nr = SvNVX(svr);
633
3336af0b
DD
634 if (
635#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
636 !Perl_isnan(nl) && nl == (NV)(il = (IV)nl)
637 && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr)
638#else
639 nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr)
640#endif
641 )
230ee21f
DM
642 /* nothing was lost by converting to IVs */
643 goto do_iv;
644 SP--;
645 TARGn(nl + nr, 0); /* args not GMG, so can't be tainted */
646 SETs(TARG);
647 RETURN;
648 }
649 }
650
651 generic:
652
653 useleft = USE_LEFT(svl);
28e5dec8
JH
654 /* We must see if we can perform the addition with integers if possible,
655 as the integer code detects overflow while the NV code doesn't.
656 If either argument hasn't had a numeric conversion yet attempt to get
657 the IV. It's important to do this now, rather than just assuming that
658 it's not IOK as a PV of "9223372036854775806" may not take well to NV
659 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
660 integer in case the second argument is IV=9223372036854775806
661 We can (now) rely on sv_2iv to do the right thing, only setting the
662 public IOK flag if the value in the NV (or PV) slot is truly integer.
663
664 A side effect is that this also aggressively prefers integer maths over
7dca457a
NC
665 fp maths for integer values.
666
a00b5bd3 667 How to detect overflow?
7dca457a
NC
668
669 C 99 section 6.2.6.1 says
670
671 The range of nonnegative values of a signed integer type is a subrange
672 of the corresponding unsigned integer type, and the representation of
673 the same value in each type is the same. A computation involving
674 unsigned operands can never overflow, because a result that cannot be
675 represented by the resulting unsigned integer type is reduced modulo
676 the number that is one greater than the largest value that can be
677 represented by the resulting type.
678
679 (the 9th paragraph)
680
681 which I read as "unsigned ints wrap."
682
683 signed integer overflow seems to be classed as "exception condition"
684
685 If an exceptional condition occurs during the evaluation of an
686 expression (that is, if the result is not mathematically defined or not
687 in the range of representable values for its type), the behavior is
688 undefined.
689
690 (6.5, the 5th paragraph)
691
692 I had assumed that on 2s complement machines signed arithmetic would
693 wrap, hence coded pp_add and pp_subtract on the assumption that
694 everything perl builds on would be happy. After much wailing and
695 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
696 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
697 unsigned code below is actually shorter than the old code. :-)
698 */
699
01f91bf2 700 if (SvIV_please_nomg(svr)) {
28e5dec8
JH
701 /* Unless the left argument is integer in range we are going to have to
702 use NV maths. Hence only attempt to coerce the right argument if
703 we know the left is integer. */
eb578fdb 704 UV auv = 0;
9c5ffd7c 705 bool auvok = FALSE;
7dca457a
NC
706 bool a_valid = 0;
707
28e5dec8 708 if (!useleft) {
7dca457a
NC
709 auv = 0;
710 a_valid = auvok = 1;
711 /* left operand is undef, treat as zero. + 0 is identity,
712 Could SETi or SETu right now, but space optimise by not adding
713 lots of code to speed up what is probably a rarish case. */
714 } else {
715 /* Left operand is defined, so is it IV? */
01f91bf2 716 if (SvIV_please_nomg(svl)) {
800401ee
JH
717 if ((auvok = SvUOK(svl)))
718 auv = SvUVX(svl);
7dca457a 719 else {
eb578fdb 720 const IV aiv = SvIVX(svl);
7dca457a
NC
721 if (aiv >= 0) {
722 auv = aiv;
723 auvok = 1; /* Now acting as a sign flag. */
53e2bfb7
DM
724 } else {
725 auv = (aiv == IV_MIN) ? (UV)aiv : (UV)(-aiv);
7dca457a
NC
726 }
727 }
728 a_valid = 1;
28e5dec8
JH
729 }
730 }
7dca457a
NC
731 if (a_valid) {
732 bool result_good = 0;
733 UV result;
eb578fdb 734 UV buv;
800401ee 735 bool buvok = SvUOK(svr);
a00b5bd3 736
7dca457a 737 if (buvok)
800401ee 738 buv = SvUVX(svr);
7dca457a 739 else {
eb578fdb 740 const IV biv = SvIVX(svr);
7dca457a
NC
741 if (biv >= 0) {
742 buv = biv;
743 buvok = 1;
744 } else
53e2bfb7 745 buv = (biv == IV_MIN) ? (UV)biv : (UV)(-biv);
7dca457a
NC
746 }
747 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 748 else "IV" now, independent of how it came in.
7dca457a
NC
749 if a, b represents positive, A, B negative, a maps to -A etc
750 a + b => (a + b)
751 A + b => -(a - b)
752 a + B => (a - b)
753 A + B => -(a + b)
754 all UV maths. negate result if A negative.
755 add if signs same, subtract if signs differ. */
756
757 if (auvok ^ buvok) {
758 /* Signs differ. */
759 if (auv >= buv) {
760 result = auv - buv;
761 /* Must get smaller */
762 if (result <= auv)
763 result_good = 1;
764 } else {
765 result = buv - auv;
766 if (result <= buv) {
767 /* result really should be -(auv-buv). as its negation
768 of true value, need to swap our result flag */
769 auvok = !auvok;
770 result_good = 1;
28e5dec8
JH
771 }
772 }
7dca457a
NC
773 } else {
774 /* Signs same */
775 result = auv + buv;
776 if (result >= auv)
777 result_good = 1;
778 }
779 if (result_good) {
780 SP--;
781 if (auvok)
28e5dec8 782 SETu( result );
7dca457a
NC
783 else {
784 /* Negate result */
785 if (result <= (UV)IV_MIN)
53e2bfb7
DM
786 SETi(result == (UV)IV_MIN
787 ? IV_MIN : -(IV)result);
7dca457a
NC
788 else {
789 /* result valid, but out of range for IV. */
790 SETn( -(NV)result );
28e5dec8
JH
791 }
792 }
7dca457a
NC
793 RETURN;
794 } /* Overflow, drop through to NVs. */
28e5dec8
JH
795 }
796 }
230ee21f
DM
797
798#else
799 useleft = USE_LEFT(svl);
28e5dec8 800#endif
230ee21f 801
a0d0e21e 802 {
6f1401dc 803 NV value = SvNV_nomg(svr);
4efa5a16 804 (void)POPs;
28e5dec8
JH
805 if (!useleft) {
806 /* left operand is undef, treat as zero. + 0.0 is identity. */
807 SETn(value);
808 RETURN;
809 }
6f1401dc 810 SETn( value + SvNV_nomg(svl) );
28e5dec8 811 RETURN;
a0d0e21e
LW
812 }
813}
814
b1c05ba5
DM
815
816/* also used for: pp_aelemfast_lex() */
817
a0d0e21e
LW
818PP(pp_aelemfast)
819{
20b7effb 820 dSP;
93bad3fd 821 AV * const av = PL_op->op_type == OP_AELEMFAST_LEX
8f878375 822 ? MUTABLE_AV(PAD_SV(PL_op->op_targ)) : GvAVn(cGVOP_gv);
a3b680e6 823 const U32 lval = PL_op->op_flags & OPf_MOD;
7e169e84
DM
824 const I8 key = (I8)PL_op->op_private;
825 SV** svp;
826 SV *sv;
827
828 assert(SvTYPE(av) == SVt_PVAV);
829
f4484b87
DM
830 EXTEND(SP, 1);
831
7e169e84
DM
832 /* inlined av_fetch() for simple cases ... */
833 if (!SvRMAGICAL(av) && key >= 0 && key <= AvFILLp(av)) {
834 sv = AvARRAY(av)[key];
9fb994be 835 if (sv) {
7e169e84
DM
836 PUSHs(sv);
837 RETURN;
838 }
839 }
840
841 /* ... else do it the hard way */
842 svp = av_fetch(av, key, lval);
843 sv = (svp ? *svp : &PL_sv_undef);
b024352e
DM
844
845 if (UNLIKELY(!svp && lval))
7e169e84 846 DIE(aTHX_ PL_no_aelem, (int)key);
b024352e 847
39cf747a 848 if (!lval && SvRMAGICAL(av) && SvGMAGICAL(sv)) /* see note in pp_helem() */
fd69380d 849 mg_get(sv);
be6c24e0 850 PUSHs(sv);
a0d0e21e
LW
851 RETURN;
852}
853
854PP(pp_join)
855{
20b7effb 856 dSP; dMARK; dTARGET;
a0d0e21e
LW
857 MARK++;
858 do_join(TARG, *MARK, MARK, SP);
859 SP = MARK;
860 SETs(TARG);
861 RETURN;
862}
863
a0d0e21e
LW
864/* Oversized hot code. */
865
b1c05ba5
DM
866/* also used for: pp_say() */
867
a0d0e21e
LW
868PP(pp_print)
869{
20b7effb 870 dSP; dMARK; dORIGMARK;
eb578fdb 871 PerlIO *fp;
236988e4 872 MAGIC *mg;
159b6efe
NC
873 GV * const gv
874 = (PL_op->op_flags & OPf_STACKED) ? MUTABLE_GV(*++MARK) : PL_defoutgv;
9c9f25b8 875 IO *io = GvIO(gv);
5b468f54 876
9c9f25b8 877 if (io
ad64d0ec 878 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
5b468f54 879 {
01bb7c6d 880 had_magic:
68dc0745 881 if (MARK == ORIGMARK) {
1c846c1f 882 /* If using default handle then we need to make space to
a60c0954
NIS
883 * pass object as 1st arg, so move other args up ...
884 */
4352c267 885 MEXTEND(SP, 1);
68dc0745
PP
886 ++MARK;
887 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
888 ++SP;
889 }
3e0cb5de 890 return Perl_tied_method(aTHX_ SV_CONST(PRINT), mark - 1, MUTABLE_SV(io),
94bc412f
NC
891 mg,
892 (G_SCALAR | TIED_METHOD_ARGUMENTS_ON_STACK
893 | (PL_op->op_type == OP_SAY
894 ? TIED_METHOD_SAY : 0)), sp - mark);
236988e4 895 }
9c9f25b8 896 if (!io) {
68b590d9 897 if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv)))
ad64d0ec 898 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
01bb7c6d 899 goto had_magic;
51087808 900 report_evil_fh(gv);
93189314 901 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
902 goto just_say_no;
903 }
904 else if (!(fp = IoOFP(io))) {
7716c5c5
NC
905 if (IoIFP(io))
906 report_wrongway_fh(gv, '<');
51087808 907 else
7716c5c5 908 report_evil_fh(gv);
93189314 909 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
910 goto just_say_no;
911 }
912 else {
e23d9e2f 913 SV * const ofs = GvSV(PL_ofsgv); /* $, */
a0d0e21e 914 MARK++;
e23d9e2f 915 if (ofs && (SvGMAGICAL(ofs) || SvOK(ofs))) {
a0d0e21e
LW
916 while (MARK <= SP) {
917 if (!do_print(*MARK, fp))
918 break;
919 MARK++;
920 if (MARK <= SP) {
e23d9e2f
CS
921 /* don't use 'ofs' here - it may be invalidated by magic callbacks */
922 if (!do_print(GvSV(PL_ofsgv), fp)) {
a0d0e21e
LW
923 MARK--;
924 break;
925 }
926 }
927 }
928 }
929 else {
930 while (MARK <= SP) {
931 if (!do_print(*MARK, fp))
932 break;
933 MARK++;
934 }
935 }
936 if (MARK <= SP)
937 goto just_say_no;
938 else {
cfc4a7da
GA
939 if (PL_op->op_type == OP_SAY) {
940 if (PerlIO_write(fp, "\n", 1) == 0 || PerlIO_error(fp))
941 goto just_say_no;
942 }
943 else if (PL_ors_sv && SvOK(PL_ors_sv))
7889fe52 944 if (!do_print(PL_ors_sv, fp)) /* $\ */
a0d0e21e
LW
945 goto just_say_no;
946
947 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 948 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
949 goto just_say_no;
950 }
951 }
952 SP = ORIGMARK;
e52fd6f4 953 XPUSHs(&PL_sv_yes);
a0d0e21e
LW
954 RETURN;
955
956 just_say_no:
957 SP = ORIGMARK;
e52fd6f4 958 XPUSHs(&PL_sv_undef);
a0d0e21e
LW
959 RETURN;
960}
961
b1c05ba5 962
aa36782f
DM
963/* do the common parts of pp_padhv() and pp_rv2hv()
964 * It assumes the caller has done EXTEND(SP, 1) or equivalent.
af3b1cba 965 * 'is_keys' indicates the OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS flag is set.
e84e4286
DM
966 * 'has_targ' indicates that the op has a target - this should
967 * be a compile-time constant so that the code can constant-folded as
968 * appropriate
aa36782f
DM
969 * */
970
971PERL_STATIC_INLINE OP*
e84e4286 972S_padhv_rv2hv_common(pTHX_ HV *hv, U8 gimme, bool is_keys, bool has_targ)
aa36782f
DM
973{
974 bool tied;
975 dSP;
976
977 assert(PL_op->op_type == OP_PADHV || PL_op->op_type == OP_RV2HV);
978
979 if (gimme == G_ARRAY) {
af3b1cba
DM
980 hv_pushkv(hv);
981 return NORMAL;
aa36782f
DM
982 }
983
984 if (is_keys)
985 /* 'keys %h' masquerading as '%h': reset iterator */
986 (void)hv_iterinit(hv);
987
988 tied = SvRMAGICAL(hv) && mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied);
989
990 if ( ( PL_op->op_private & OPpTRUEBOOL
991 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
992 && block_gimme() == G_VOID)
993 )
994 && !tied
995 )
996 PUSHs(HvUSEDKEYS(hv) ? &PL_sv_yes : &PL_sv_zero);
997 else if (gimme == G_SCALAR) {
998 if (is_keys) {
999 IV i;
1000 if (tied) {
1001 i = 0;
1002 while (hv_iternext(hv))
1003 i++;
1004 }
1005 else
1006 i = HvUSEDKEYS(hv);
e84e4286
DM
1007 if (has_targ) {
1008 dTARGET;
1009 PUSHi(i);
1010 }
1011 else
1012 mPUSHi(i);
aa36782f
DM
1013 }
1014 else
1015 PUSHs(Perl_hv_scalar(aTHX_ hv));
1016 }
1017
1018 PUTBACK;
1019 return NORMAL;
1020}
1021
1022
e855b461
DM
1023/* This is also called directly by pp_lvavref. */
1024PP(pp_padav)
1025{
1026 dSP; dTARGET;
1027 U8 gimme;
1028 assert(SvTYPE(TARG) == SVt_PVAV);
1029 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
1030 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
1031 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
1032 EXTEND(SP, 1);
1033
1034 if (PL_op->op_flags & OPf_REF) {
1035 PUSHs(TARG);
1036 RETURN;
1037 }
1038 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
1039 const I32 flags = is_lvalue_sub();
1040 if (flags && !(flags & OPpENTERSUB_INARGS)) {
1041 if (GIMME_V == G_SCALAR)
1042 /* diag_listed_as: Can't return %s to lvalue scalar context */
1043 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
1044 PUSHs(TARG);
1045 RETURN;
1046 }
1047 }
1048
1049 gimme = GIMME_V;
1050 if (gimme == G_ARRAY) {
327c9b9e
DM
1051 S_pushav(aTHX_ (AV*)TARG);
1052 return NORMAL;
e855b461 1053 }
327c9b9e
DM
1054
1055 if (gimme == G_SCALAR) {
e855b461
DM
1056 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
1057 if (!maxarg)
1058 PUSHs(&PL_sv_zero);
1059 else if (PL_op->op_private & OPpTRUEBOOL)
1060 PUSHs(&PL_sv_yes);
1061 else
1062 mPUSHi(maxarg);
1063 }
1064 RETURN;
1065}
1066
1067
1068PP(pp_padhv)
1069{
1070 dSP; dTARGET;
1071 U8 gimme;
e855b461
DM
1072
1073 assert(SvTYPE(TARG) == SVt_PVHV);
e855b461
DM
1074 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
1075 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
1076 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
1077
aa36782f
DM
1078 EXTEND(SP, 1);
1079
1080 if (PL_op->op_flags & OPf_REF) {
1081 PUSHs(TARG);
e855b461 1082 RETURN;
aa36782f 1083 }
e855b461
DM
1084 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
1085 const I32 flags = is_lvalue_sub();
1086 if (flags && !(flags & OPpENTERSUB_INARGS)) {
1087 if (GIMME_V == G_SCALAR)
1088 /* diag_listed_as: Can't return %s to lvalue scalar context */
1089 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
aa36782f 1090 PUSHs(TARG);
e855b461
DM
1091 RETURN;
1092 }
1093 }
1094
1095 gimme = GIMME_V;
e855b461 1096
aa36782f 1097 return S_padhv_rv2hv_common(aTHX_ (HV*)TARG, gimme,
e84e4286
DM
1098 cBOOL(PL_op->op_private & OPpPADHV_ISKEYS),
1099 0 /* has_targ*/);
e855b461
DM
1100}
1101
1102
b1c05ba5 1103/* also used for: pp_rv2hv() */
bdaf10a5 1104/* also called directly by pp_lvavref */
b1c05ba5 1105
a0d0e21e
LW
1106PP(pp_rv2av)
1107{
20b7effb 1108 dSP; dTOPss;
1c23e2bd 1109 const U8 gimme = GIMME_V;
13c59d41
MH
1110 static const char an_array[] = "an ARRAY";
1111 static const char a_hash[] = "a HASH";
bdaf10a5
FC
1112 const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV
1113 || PL_op->op_type == OP_LVAVREF;
d83b45b8 1114 const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV;
a0d0e21e 1115
9026059d 1116 SvGETMAGIC(sv);
a0d0e21e 1117 if (SvROK(sv)) {
5d9574c1 1118 if (UNLIKELY(SvAMAGIC(sv))) {
93d7320b 1119 sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg);
93d7320b 1120 }
17ab7946 1121 sv = SvRV(sv);
5d9574c1 1122 if (UNLIKELY(SvTYPE(sv) != type))
dcbac5bb 1123 /* diag_listed_as: Not an ARRAY reference */
13c59d41 1124 DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash);
5d9574c1
DM
1125 else if (UNLIKELY(PL_op->op_flags & OPf_MOD
1126 && PL_op->op_private & OPpLVAL_INTRO))
3da99855 1127 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
a0d0e21e 1128 }
5d9574c1 1129 else if (UNLIKELY(SvTYPE(sv) != type)) {
67955e0c 1130 GV *gv;
1c846c1f 1131
6e592b3a 1132 if (!isGV_with_GP(sv)) {
13c59d41 1133 gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash,
dc3c76f8
NC
1134 type, &sp);
1135 if (!gv)
1136 RETURN;
35cd451c
GS
1137 }
1138 else {
159b6efe 1139 gv = MUTABLE_GV(sv);
a0d0e21e 1140 }
ad64d0ec 1141 sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv));
533c011a 1142 if (PL_op->op_private & OPpLVAL_INTRO)
ad64d0ec 1143 sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv));
9f527363
FC
1144 }
1145 if (PL_op->op_flags & OPf_REF) {
17ab7946 1146 SETs(sv);
a0d0e21e 1147 RETURN;
9f527363 1148 }
5d9574c1 1149 else if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
40c94d11
FC
1150 const I32 flags = is_lvalue_sub();
1151 if (flags && !(flags & OPpENTERSUB_INARGS)) {
cde874ca 1152 if (gimme != G_ARRAY)
042560a6 1153 goto croak_cant_return;
17ab7946 1154 SETs(sv);
78f9721b 1155 RETURN;
40c94d11 1156 }
a0d0e21e
LW
1157 }
1158
17ab7946 1159 if (is_pp_rv2av) {
502c6561 1160 AV *const av = MUTABLE_AV(sv);
636fe681 1161 /* The guts of pp_rv2av */
96913b52 1162 if (gimme == G_ARRAY) {
d5524600
DM
1163 SP--;
1164 PUTBACK;
1165 S_pushav(aTHX_ av);
1166 SPAGAIN;
1c846c1f 1167 }
96913b52 1168 else if (gimme == G_SCALAR) {
c70927a6 1169 const SSize_t maxarg = AvFILL(av) + 1;
7be75ccf
DM
1170 if (PL_op->op_private & OPpTRUEBOOL)
1171 SETs(maxarg ? &PL_sv_yes : &PL_sv_zero);
1172 else {
1173 dTARGET;
1174 SETi(maxarg);
1175 }
93965878 1176 }
7be75ccf
DM
1177 }
1178 else {
aa36782f
DM
1179 SP--; PUTBACK;
1180 return S_padhv_rv2hv_common(aTHX_ (HV*)sv, gimme,
e84e4286
DM
1181 cBOOL(PL_op->op_private & OPpRV2HV_ISKEYS),
1182 1 /* has_targ*/);
17ab7946 1183 }
be85d344 1184 RETURN;
042560a6
NC
1185
1186 croak_cant_return:
1187 Perl_croak(aTHX_ "Can't return %s to lvalue scalar context",
1188 is_pp_rv2av ? "array" : "hash");
77e217c6 1189 RETURN;
a0d0e21e
LW
1190}
1191
10c8fecd 1192STATIC void
fb8f4cf8 1193S_do_oddball(pTHX_ SV **oddkey, SV **firstkey)
10c8fecd 1194{
7918f24d
NC
1195 PERL_ARGS_ASSERT_DO_ODDBALL;
1196
fb8f4cf8 1197 if (*oddkey) {
6d822dc4 1198 if (ckWARN(WARN_MISC)) {
a3b680e6 1199 const char *err;
fb8f4cf8
RZ
1200 if (oddkey == firstkey &&
1201 SvROK(*oddkey) &&
1202 (SvTYPE(SvRV(*oddkey)) == SVt_PVAV ||
1203 SvTYPE(SvRV(*oddkey)) == SVt_PVHV))
10c8fecd 1204 {
a3b680e6 1205 err = "Reference found where even-sized list expected";
10c8fecd
GS
1206 }
1207 else
a3b680e6 1208 err = "Odd number of elements in hash assignment";
f1f66076 1209 Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err);
10c8fecd 1210 }
6d822dc4 1211
10c8fecd
GS
1212 }
1213}
1214
a5f48505
DM
1215
1216/* Do a mark and sweep with the SVf_BREAK flag to detect elements which
1217 * are common to both the LHS and RHS of an aassign, and replace them
1218 * with copies. All these copies are made before the actual list assign is
1219 * done.
1220 *
1221 * For example in ($a,$b) = ($b,$a), assigning the value of the first RHS
1222 * element ($b) to the first LH element ($a), modifies $a; when the
1223 * second assignment is done, the second RH element now has the wrong
1224 * value. So we initially replace the RHS with ($b, mortalcopy($a)).
1225 * Note that we don't need to make a mortal copy of $b.
1226 *
1227 * The algorithm below works by, for every RHS element, mark the
1228 * corresponding LHS target element with SVf_BREAK. Then if the RHS
1229 * element is found with SVf_BREAK set, it means it would have been
1230 * modified, so make a copy.
1231 * Note that by scanning both LHS and RHS in lockstep, we avoid
1232 * unnecessary copies (like $b above) compared with a naive
1233 * "mark all LHS; copy all marked RHS; unmark all LHS".
1234 *
1235 * If the LHS element is a 'my' declaration' and has a refcount of 1, then
1236 * it can't be common and can be skipped.
ebc643ce
DM
1237 *
1238 * On DEBUGGING builds it takes an extra boolean, fake. If true, it means
1239 * that we thought we didn't need to call S_aassign_copy_common(), but we
1240 * have anyway for sanity checking. If we find we need to copy, then panic.
a5f48505
DM
1241 */
1242
1243PERL_STATIC_INLINE void
1244S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem,
ebc643ce
DM
1245 SV **firstrelem, SV **lastrelem
1246#ifdef DEBUGGING
1247 , bool fake
1248#endif
1249)
a5f48505
DM
1250{
1251 dVAR;
1252 SV **relem;
1253 SV **lelem;
1254 SSize_t lcount = lastlelem - firstlelem + 1;
1255 bool marked = FALSE; /* have we marked any LHS with SVf_BREAK ? */
1256 bool const do_rc1 = cBOOL(PL_op->op_private & OPpASSIGN_COMMON_RC1);
beb08a1e 1257 bool copy_all = FALSE;
a5f48505
DM
1258
1259 assert(!PL_in_clean_all); /* SVf_BREAK not already in use */
1260 assert(firstlelem < lastlelem); /* at least 2 LH elements */
1261 assert(firstrelem < lastrelem); /* at least 2 RH elements */
1262
ebc643ce
DM
1263
1264 lelem = firstlelem;
a5f48505
DM
1265 /* we never have to copy the first RH element; it can't be corrupted
1266 * by assigning something to the corresponding first LH element.
1267 * So this scan does in a loop: mark LHS[N]; test RHS[N+1]
1268 */
ebc643ce 1269 relem = firstrelem + 1;
a5f48505
DM
1270
1271 for (; relem <= lastrelem; relem++) {
1272 SV *svr;
1273
1274 /* mark next LH element */
1275
1276 if (--lcount >= 0) {
1277 SV *svl = *lelem++;
1278
1279 if (UNLIKELY(!svl)) {/* skip AV alias marker */
1280 assert (lelem <= lastlelem);
1281 svl = *lelem++;
1282 lcount--;
1283 }
1284
1285 assert(svl);
beb08a1e
TC
1286 if (SvSMAGICAL(svl)) {
1287 copy_all = TRUE;
1288 }
a5f48505
DM
1289 if (SvTYPE(svl) == SVt_PVAV || SvTYPE(svl) == SVt_PVHV) {
1290 if (!marked)
1291 return;
1292 /* this LH element will consume all further args;
1293 * no need to mark any further LH elements (if any).
1294 * But we still need to scan any remaining RHS elements;
1295 * set lcount negative to distinguish from lcount == 0,
1296 * so the loop condition continues being true
1297 */
1298 lcount = -1;
1299 lelem--; /* no need to unmark this element */
1300 }
94a5f659 1301 else if (!(do_rc1 && SvREFCNT(svl) == 1) && !SvIMMORTAL(svl)) {
a5f48505
DM
1302 SvFLAGS(svl) |= SVf_BREAK;
1303 marked = TRUE;
1304 }
1305 else if (!marked) {
1306 /* don't check RH element if no SVf_BREAK flags set yet */
1307 if (!lcount)
1308 break;
1309 continue;
1310 }
1311 }
1312
1313 /* see if corresponding RH element needs copying */
1314
1315 assert(marked);
1316 svr = *relem;
1317 assert(svr);
1318
5c1db569 1319 if (UNLIKELY(SvFLAGS(svr) & (SVf_BREAK|SVs_GMG) || copy_all)) {
1050723f 1320 U32 brk = (SvFLAGS(svr) & SVf_BREAK);
a5f48505 1321
ebc643ce
DM
1322#ifdef DEBUGGING
1323 if (fake) {
9ae0115f 1324 /* op_dump(PL_op); */
ebc643ce
DM
1325 Perl_croak(aTHX_
1326 "panic: aassign skipped needed copy of common RH elem %"
1327 UVuf, (UV)(relem - firstrelem));
1328 }
1329#endif
1330
a5f48505
DM
1331 TAINT_NOT; /* Each item is independent */
1332
1333 /* Dear TODO test in t/op/sort.t, I love you.
1334 (It's relying on a panic, not a "semi-panic" from newSVsv()
1335 and then an assertion failure below.) */
1336 if (UNLIKELY(SvIS_FREED(svr))) {
1337 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p",
1338 (void*)svr);
1339 }
1340 /* avoid break flag while copying; otherwise COW etc
1341 * disabled... */
1342 SvFLAGS(svr) &= ~SVf_BREAK;
1343 /* Not newSVsv(), as it does not allow copy-on-write,
8c1e192f
DM
1344 resulting in wasteful copies.
1345 Also, we use SV_NOSTEAL in case the SV is used more than
1346 once, e.g. (...) = (f())[0,0]
1347 Where the same SV appears twice on the RHS without a ref
1348 count bump. (Although I suspect that the SV won't be
1349 stealable here anyway - DAPM).
1350 */
a5f48505
DM
1351 *relem = sv_mortalcopy_flags(svr,
1352 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
1353 /* ... but restore afterwards in case it's needed again,
1354 * e.g. ($a,$b,$c) = (1,$a,$a)
1355 */
1050723f 1356 SvFLAGS(svr) |= brk;
a5f48505
DM
1357 }
1358
1359 if (!lcount)
1360 break;
1361 }
1362
1363 if (!marked)
1364 return;
1365
1366 /*unmark LHS */
1367
1368 while (lelem > firstlelem) {
1369 SV * const svl = *(--lelem);
1370 if (svl)
1371 SvFLAGS(svl) &= ~SVf_BREAK;
1372 }
1373}
1374
1375
1376
a0d0e21e
LW
1377PP(pp_aassign)
1378{
27da23d5 1379 dVAR; dSP;
3280af22
NIS
1380 SV **lastlelem = PL_stack_sp;
1381 SV **lastrelem = PL_stack_base + POPMARK;
1382 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
1383 SV **firstlelem = lastrelem + 1;
1384
eb578fdb
KW
1385 SV **relem;
1386 SV **lelem;
1c23e2bd 1387 U8 gimme;
a68090fe
DM
1388 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
1389 * only need to save locally, not on the save stack */
1390 U16 old_delaymagic = PL_delaymagic;
ebc643ce
DM
1391#ifdef DEBUGGING
1392 bool fake = 0;
1393#endif
5637b936 1394
3280af22 1395 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
a0d0e21e
LW
1396
1397 /* If there's a common identifier on both sides we have to take
1398 * special care that assigning the identifier on the left doesn't
1399 * clobber a value on the right that's used later in the list.
1400 */
acdea6f0 1401
beb08a1e
TC
1402 /* at least 2 LH and RH elements, or commonality isn't an issue */
1403 if (firstlelem < lastlelem && firstrelem < lastrelem) {
5c1db569
TC
1404 for (relem = firstrelem+1; relem <= lastrelem; relem++) {
1405 if (SvGMAGICAL(*relem))
1406 goto do_scan;
1407 }
beb08a1e
TC
1408 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
1409 if (*lelem && SvSMAGICAL(*lelem))
1410 goto do_scan;
a5f48505 1411 }
beb08a1e
TC
1412 if ( PL_op->op_private & (OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1) ) {
1413 if (PL_op->op_private & OPpASSIGN_COMMON_RC1) {
1414 /* skip the scan if all scalars have a ref count of 1 */
1415 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
8b0c3377 1416 SV *sv = *lelem;
beb08a1e
TC
1417 if (!sv || SvREFCNT(sv) == 1)
1418 continue;
1419 if (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVAV)
1420 goto do_scan;
1421 break;
1422 }
1423 }
1424 else {
1425 do_scan:
1426 S_aassign_copy_common(aTHX_
1427 firstlelem, lastlelem, firstrelem, lastrelem
ebc643ce 1428#ifdef DEBUGGING
beb08a1e 1429 , fake
ebc643ce 1430#endif
beb08a1e
TC
1431 );
1432 }
a5f48505 1433 }
a0d0e21e 1434 }
ebc643ce
DM
1435#ifdef DEBUGGING
1436 else {
1437 /* on debugging builds, do the scan even if we've concluded we
1438 * don't need to, then panic if we find commonality. Note that the
1439 * scanner assumes at least 2 elements */
1440 if (firstlelem < lastlelem && firstrelem < lastrelem) {
1441 fake = 1;
1442 goto do_scan;
1443 }
1444 }
1445#endif
a0d0e21e 1446
a5f48505 1447 gimme = GIMME_V;
a0d0e21e
LW
1448 relem = firstrelem;
1449 lelem = firstlelem;
10c8fecd 1450
8b0c3377
DM
1451 if (relem > lastrelem)
1452 goto no_relems;
1453
1454 /* first lelem loop while there are still relems */
5d9574c1 1455 while (LIKELY(lelem <= lastlelem)) {
bdaf10a5 1456 bool alias = FALSE;
8b0c3377
DM
1457 SV *lsv = *lelem++;
1458
c73f612f
DM
1459 TAINT_NOT; /* Each item stands on its own, taintwise. */
1460
8b0c3377
DM
1461 assert(relem <= lastrelem);
1462 if (UNLIKELY(!lsv)) {
bdaf10a5 1463 alias = TRUE;
8b0c3377
DM
1464 lsv = *lelem++;
1465 ASSUME(SvTYPE(lsv) == SVt_PVAV);
bdaf10a5 1466 }
a5f48505 1467
8b0c3377
DM
1468 switch (SvTYPE(lsv)) {
1469 case SVt_PVAV: {
1470 SV **svp;
1471 SSize_t i;
1472 SSize_t tmps_base;
1473 SSize_t nelems = lastrelem - relem + 1;
b09ed995 1474 AV *ary = MUTABLE_AV(lsv);
8b0c3377
DM
1475
1476 /* Assigning to an aggregate is tricky. First there is the
1477 * issue of commonality, e.g. @a = ($a[0]). Since the
1478 * stack isn't refcounted, clearing @a prior to storing
1479 * elements will free $a[0]. Similarly with
1480 * sub FETCH { $status[$_[1]] } @status = @tied[0,1];
1481 *
1482 * The way to avoid these issues is to make the copy of each
1483 * SV (and we normally store a *copy* in the array) *before*
1484 * clearing the array. But this has a problem in that
1485 * if the code croaks during copying, the not-yet-stored copies
1486 * could leak. One way to avoid this is to make all the copies
1487 * mortal, but that's quite expensive.
1488 *
1489 * The current solution to these issues is to use a chunk
1490 * of the tmps stack as a temporary refcounted-stack. SVs
1491 * will be put on there during processing to avoid leaks,
1492 * but will be removed again before the end of this block,
1493 * so free_tmps() is never normally called. Also, the
1494 * sv_refcnt of the SVs doesn't have to be manipulated, since
1495 * the ownership of 1 reference count is transferred directly
1496 * from the tmps stack to the AV when the SV is stored.
1497 *
1498 * We disarm slots in the temps stack by storing PL_sv_undef
1499 * there: it doesn't matter if that SV's refcount is
1500 * repeatedly decremented during a croak. But usually this is
1501 * only an interim measure. By the end of this code block
1502 * we try where possible to not leave any PL_sv_undef's on the
1503 * tmps stack e.g. by shuffling newer entries down.
1504 *
1505 * There is one case where we don't copy: non-magical
1506 * SvTEMP(sv)'s with a ref count of 1. The only owner of these
1507 * is on the tmps stack, so its safe to directly steal the SV
1508 * rather than copying. This is common in things like function
1509 * returns, map etc, which all return a list of such SVs.
1510 *
1511 * Note however something like @a = (f())[0,0], where there is
1512 * a danger of the same SV being shared: this avoided because
1513 * when the SV is stored as $a[0], its ref count gets bumped,
1514 * so the RC==1 test fails and the second element is copied
1515 * instead.
1516 *
1517 * We also use one slot in the tmps stack to hold an extra
1518 * ref to the array, to ensure it doesn't get prematurely
1519 * freed. Again, this is removed before the end of this block.
1520 *
1521 * Note that OPpASSIGN_COMMON_AGG is used to flag a possible
1522 * @a = ($a[0]) case, but the current implementation uses the
1523 * same algorithm regardless, so ignores that flag. (It *is*
1524 * used in the hash branch below, however).
1525 */
1526
1527 /* Reserve slots for ary, plus the elems we're about to copy,
1528 * then protect ary and temporarily void the remaining slots
1529 * with &PL_sv_undef */
1530 EXTEND_MORTAL(nelems + 1);
1531 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(ary);
1532 tmps_base = PL_tmps_ix + 1;
1533 for (i = 0; i < nelems; i++)
1534 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1535 PL_tmps_ix += nelems;
1536
1537 /* Make a copy of each RHS elem and save on the tmps_stack
1538 * (or pass through where we can optimise away the copy) */
1539
1540 if (UNLIKELY(alias)) {
1541 U32 lval = (gimme == G_ARRAY)
1542 ? (PL_op->op_flags & OPf_MOD || LVRET) : 0;
a5f48505 1543 for (svp = relem; svp <= lastrelem; svp++) {
8b0c3377
DM
1544 SV *rsv = *svp;
1545
1546 SvGETMAGIC(rsv);
1547 if (!SvROK(rsv))
1548 DIE(aTHX_ "Assigned value is not a reference");
1549 if (SvTYPE(SvRV(rsv)) > SVt_PVLV)
1550 /* diag_listed_as: Assigned value is not %s reference */
1551 DIE(aTHX_
1552 "Assigned value is not a SCALAR reference");
1553 if (lval)
1554 *svp = rsv = sv_mortalcopy(rsv);
1555 /* XXX else check for weak refs? */
1556 rsv = SvREFCNT_inc_NN(SvRV(rsv));
1557 assert(tmps_base <= PL_tmps_max);
1558 PL_tmps_stack[tmps_base++] = rsv;
a5f48505 1559 }
a5f48505 1560 }
8b0c3377
DM
1561 else {
1562 for (svp = relem; svp <= lastrelem; svp++) {
1563 SV *rsv = *svp;
a5f48505 1564
8b0c3377
DM
1565 if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) {
1566 /* can skip the copy */
1567 SvREFCNT_inc_simple_void_NN(rsv);
1568 SvTEMP_off(rsv);
1569 }
a5f48505 1570 else {
8b0c3377
DM
1571 SV *nsv;
1572 /* do get before newSV, in case it dies and leaks */
1573 SvGETMAGIC(rsv);
1574 nsv = newSV(0);
8c1e192f
DM
1575 /* see comment in S_aassign_copy_common about
1576 * SV_NOSTEAL */
8b0c3377
DM
1577 sv_setsv_flags(nsv, rsv,
1578 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
1579 rsv = *svp = nsv;
a5f48505 1580 }
8b0c3377
DM
1581
1582 assert(tmps_base <= PL_tmps_max);
1583 PL_tmps_stack[tmps_base++] = rsv;
1584 }
1585 }
1586
1587 if (SvRMAGICAL(ary) || AvFILLp(ary) >= 0) /* may be non-empty */
1588 av_clear(ary);
1589
1590 /* store in the array, the SVs that are in the tmps stack */
1591
1592 tmps_base -= nelems;
1593
80c1439f 1594 if (SvMAGICAL(ary) || SvREADONLY(ary) || !AvREAL(ary)) {
8b0c3377
DM
1595 /* for arrays we can't cheat with, use the official API */
1596 av_extend(ary, nelems - 1);
1597 for (i = 0; i < nelems; i++) {
1598 SV **svp = &(PL_tmps_stack[tmps_base + i]);
1599 SV *rsv = *svp;
1600 /* A tied store won't take ownership of rsv, so keep
1601 * the 1 refcnt on the tmps stack; otherwise disarm
1602 * the tmps stack entry */
1603 if (av_store(ary, i, rsv))
1604 *svp = &PL_sv_undef;
1605 /* av_store() may have added set magic to rsv */;
1606 SvSETMAGIC(rsv);
1607 }
1608 /* disarm ary refcount: see comments below about leak */
1609 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
1610 }
1611 else {
1612 /* directly access/set the guts of the AV */
1613 SSize_t fill = nelems - 1;
1614 if (fill > AvMAX(ary))
1615 av_extend_guts(ary, fill, &AvMAX(ary), &AvALLOC(ary),
1616 &AvARRAY(ary));
1617 AvFILLp(ary) = fill;
1618 Copy(&(PL_tmps_stack[tmps_base]), AvARRAY(ary), nelems, SV*);
1619 /* Quietly remove all the SVs from the tmps stack slots,
1620 * since ary has now taken ownership of the refcnt.
1621 * Also remove ary: which will now leak if we die before
1622 * the SvREFCNT_dec_NN(ary) below */
1623 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
1624 Move(&PL_tmps_stack[tmps_base + nelems],
1625 &PL_tmps_stack[tmps_base - 1],
1626 PL_tmps_ix - (tmps_base + nelems) + 1,
1627 SV*);
1628 PL_tmps_ix -= (nelems + 1);
1629 }
1630
5d9574c1 1631 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
8b0c3377 1632 /* its assumed @ISA set magic can't die and leak ary */
ad64d0ec 1633 SvSETMAGIC(MUTABLE_SV(ary));
8b0c3377
DM
1634 SvREFCNT_dec_NN(ary);
1635
1636 relem = lastrelem + 1;
1637 goto no_relems;
a5f48505
DM
1638 }
1639
10c8fecd 1640 case SVt_PVHV: { /* normal hash */
8b0c3377
DM
1641
1642 SV **svp;
1643 bool dirty_tmps;
1644 SSize_t i;
1645 SSize_t tmps_base;
1646 SSize_t nelems = lastrelem - relem + 1;
b09ed995 1647 HV *hash = MUTABLE_HV(lsv);
8b0c3377
DM
1648
1649 if (UNLIKELY(nelems & 1)) {
1650 do_oddball(lastrelem, relem);
1651 /* we have firstlelem to reuse, it's not needed any more */
1652 *++lastrelem = &PL_sv_undef;
1653 nelems++;
1654 }
1655
1656 /* See the SVt_PVAV branch above for a long description of
1657 * how the following all works. The main difference for hashes
1658 * is that we treat keys and values separately (and have
1659 * separate loops for them): as for arrays, values are always
1660 * copied (except for the SvTEMP optimisation), since they
1661 * need to be stored in the hash; while keys are only
1662 * processed where they might get prematurely freed or
1663 * whatever. */
1664
1665 /* tmps stack slots:
1666 * * reserve a slot for the hash keepalive;
1667 * * reserve slots for the hash values we're about to copy;
1668 * * preallocate for the keys we'll possibly copy or refcount bump
1669 * later;
1670 * then protect hash and temporarily void the remaining
1671 * value slots with &PL_sv_undef */
1672 EXTEND_MORTAL(nelems + 1);
1673
1674 /* convert to number of key/value pairs */
1675 nelems >>= 1;
1676
1677 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hash);
1678 tmps_base = PL_tmps_ix + 1;
1679 for (i = 0; i < nelems; i++)
1680 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1681 PL_tmps_ix += nelems;
1682
1683 /* Make a copy of each RHS hash value and save on the tmps_stack
1684 * (or pass through where we can optimise away the copy) */
1685
1686 for (svp = relem + 1; svp <= lastrelem; svp += 2) {
1687 SV *rsv = *svp;
1688
1689 if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) {
1690 /* can skip the copy */
1691 SvREFCNT_inc_simple_void_NN(rsv);
1692 SvTEMP_off(rsv);
1693 }
1694 else {
1695 SV *nsv;
1696 /* do get before newSV, in case it dies and leaks */
1697 SvGETMAGIC(rsv);
1698 nsv = newSV(0);
1699 /* see comment in S_aassign_copy_common about
1700 * SV_NOSTEAL */
1701 sv_setsv_flags(nsv, rsv,
1702 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
1703 rsv = *svp = nsv;
1c4ea384
RZ
1704 }
1705
8b0c3377
DM
1706 assert(tmps_base <= PL_tmps_max);
1707 PL_tmps_stack[tmps_base++] = rsv;
1708 }
1709 tmps_base -= nelems;
a5f48505 1710
a5f48505 1711
8b0c3377
DM
1712 /* possibly protect keys */
1713
1714 if (UNLIKELY(gimme == G_ARRAY)) {
1715 /* handle e.g.
1716 * @a = ((%h = ($$r, 1)), $r = "x");
1717 * $_++ for %h = (1,2,3,4);
1718 */
1719 EXTEND_MORTAL(nelems);
1720 for (svp = relem; svp <= lastrelem; svp += 2)
1721 *svp = sv_mortalcopy_flags(*svp,
1722 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
1723 }
1724 else if (PL_op->op_private & OPpASSIGN_COMMON_AGG) {
1725 /* for possible commonality, e.g.
1726 * %h = ($h{a},1)
1727 * avoid premature freeing RHS keys by mortalising
1728 * them.
1729 * For a magic element, make a copy so that its magic is
1730 * called *before* the hash is emptied (which may affect
1731 * a tied value for example).
1732 * In theory we should check for magic keys in all
1733 * cases, not just under OPpASSIGN_COMMON_AGG, but in
1734 * practice, !OPpASSIGN_COMMON_AGG implies only
1735 * constants or padtmps on the RHS.
1736 */
1737 EXTEND_MORTAL(nelems);
1738 for (svp = relem; svp <= lastrelem; svp += 2) {
1739 SV *rsv = *svp;
1740 if (UNLIKELY(SvGMAGICAL(rsv))) {
1741 SSize_t n;
a5f48505
DM
1742 *svp = sv_mortalcopy_flags(*svp,
1743 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
8b0c3377
DM
1744 /* allow other branch to continue pushing
1745 * onto tmps stack without checking each time */
1746 n = (lastrelem - relem) >> 1;
1747 EXTEND_MORTAL(n);
a5f48505 1748 }
8b0c3377
DM
1749 else
1750 PL_tmps_stack[++PL_tmps_ix] =
1751 SvREFCNT_inc_simple_NN(rsv);
a5f48505 1752 }
8b0c3377 1753 }
a5f48505 1754
8b0c3377
DM
1755 if (SvRMAGICAL(hash) || HvUSEDKEYS(hash))
1756 hv_clear(hash);
a5f48505 1757
8b0c3377
DM
1758 /* now assign the keys and values to the hash */
1759
1760 dirty_tmps = FALSE;
1761
1762 if (UNLIKELY(gimme == G_ARRAY)) {
1763 /* @a = (%h = (...)) etc */
1764 SV **svp;
1765 SV **topelem = relem;
1766
1767 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
1768 SV *key = *svp++;
1769 SV *val = *svp;
1770 /* remove duplicates from list we return */
1771 if (!hv_exists_ent(hash, key, 0)) {
1772 /* copy key back: possibly to an earlier
1773 * stack location if we encountered dups earlier,
1774 * The values will be updated later
1775 */
1776 *topelem = key;
1777 topelem += 2;
632b9d6f 1778 }
8b0c3377
DM
1779 /* A tied store won't take ownership of val, so keep
1780 * the 1 refcnt on the tmps stack; otherwise disarm
1781 * the tmps stack entry */
1782 if (hv_store_ent(hash, key, val, 0))
1783 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1784 else
1785 dirty_tmps = TRUE;
1786 /* hv_store_ent() may have added set magic to val */;
1787 SvSETMAGIC(val);
1788 }
1789 if (topelem < svp) {
1c4ea384
RZ
1790 /* at this point we have removed the duplicate key/value
1791 * pairs from the stack, but the remaining values may be
1792 * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed
1793 * the (a 2), but the stack now probably contains
1794 * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) }
1795 * obliterates the earlier key. So refresh all values. */
8b0c3377
DM
1796 lastrelem = topelem - 1;
1797 while (relem < lastrelem) {
1c4ea384
RZ
1798 HE *he;
1799 he = hv_fetch_ent(hash, *relem++, 0, 0);
1800 *relem++ = (he ? HeVAL(he) : &PL_sv_undef);
1801 }
1802 }
8b0c3377
DM
1803 }
1804 else {
1805 SV **svp;
1806 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
1807 SV *key = *svp++;
1808 SV *val = *svp;
1809 if (hv_store_ent(hash, key, val, 0))
1810 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1811 else
1812 dirty_tmps = TRUE;
1813 /* hv_store_ent() may have added set magic to val */;
1814 SvSETMAGIC(val);
1815 }
1816 }
1817
1818 if (dirty_tmps) {
1819 /* there are still some 'live' recounts on the tmps stack
1820 * - usually caused by storing into a tied hash. So let
1821 * free_tmps() do the proper but slow job later.
1822 * Just disarm hash refcount: see comments below about leak
1823 */
1824 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
1825 }
1826 else {
1827 /* Quietly remove all the SVs from the tmps stack slots,
1828 * since hash has now taken ownership of the refcnt.
1829 * Also remove hash: which will now leak if we die before
1830 * the SvREFCNT_dec_NN(hash) below */
1831 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
1832 Move(&PL_tmps_stack[tmps_base + nelems],
1833 &PL_tmps_stack[tmps_base - 1],
1834 PL_tmps_ix - (tmps_base + nelems) + 1,
1835 SV*);
1836 PL_tmps_ix -= (nelems + 1);
1837 }
1838
1839 SvREFCNT_dec_NN(hash);
1840
1841 relem = lastrelem + 1;
1842 goto no_relems;
1843 }
1844
a0d0e21e 1845 default:
8b0c3377 1846 if (!SvIMMORTAL(lsv)) {
d24e3eb1
DM
1847 SV *ref;
1848
8b0c3377
DM
1849 if (UNLIKELY(
1850 SvTEMP(lsv) && !SvSMAGICAL(lsv) && SvREFCNT(lsv) == 1 &&
1851 (!isGV_with_GP(lsv) || SvFAKE(lsv)) && ckWARN(WARN_MISC)
1852 ))
1853 Perl_warner(aTHX_
1854 packWARN(WARN_MISC),
1855 "Useless assignment to a temporary"
1856 );
d24e3eb1
DM
1857
1858 /* avoid freeing $$lsv if it might be needed for further
1859 * elements, e.g. ($ref, $foo) = (1, $$ref) */
1860 if ( SvROK(lsv)
1861 && ( ((ref = SvRV(lsv)), SvREFCNT(ref)) == 1)
1862 && lelem <= lastlelem
1863 ) {
1864 SSize_t ix;
1865 SvREFCNT_inc_simple_void_NN(ref);
1866 /* an unrolled sv_2mortal */
1867 ix = ++PL_tmps_ix;
1868 if (UNLIKELY(ix >= PL_tmps_max))
1869 /* speculatively grow enough to cover other
1870 * possible refs */
67c3640a 1871 (void)tmps_grow_p(ix + (lastlelem - lelem));
d24e3eb1
DM
1872 PL_tmps_stack[ix] = ref;
1873 }
1874
8b0c3377
DM
1875 sv_setsv(lsv, *relem);
1876 *relem = lsv;
1877 SvSETMAGIC(lsv);
1878 }
1879 if (++relem > lastrelem)
1880 goto no_relems;
a0d0e21e 1881 break;
8b0c3377
DM
1882 } /* switch */
1883 } /* while */
1884
1885
1886 no_relems:
1887
1888 /* simplified lelem loop for when there are no relems left */
1889 while (LIKELY(lelem <= lastlelem)) {
1890 SV *lsv = *lelem++;
c73f612f
DM
1891
1892 TAINT_NOT; /* Each item stands on its own, taintwise. */
1893
8b0c3377
DM
1894 if (UNLIKELY(!lsv)) {
1895 lsv = *lelem++;
1896 ASSUME(SvTYPE(lsv) == SVt_PVAV);
a0d0e21e 1897 }
8b0c3377
DM
1898
1899 switch (SvTYPE(lsv)) {
1900 case SVt_PVAV:
b09ed995
DM
1901 if (SvRMAGICAL(lsv) || AvFILLp((SV*)lsv) >= 0) {
1902 av_clear((AV*)lsv);
8b0c3377 1903 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
b09ed995 1904 SvSETMAGIC(lsv);
8b0c3377
DM
1905 }
1906 break;
1907
1908 case SVt_PVHV:
b09ed995
DM
1909 if (SvRMAGICAL(lsv) || HvUSEDKEYS((HV*)lsv))
1910 hv_clear((HV*)lsv);
8b0c3377
DM
1911 break;
1912
1913 default:
1914 if (!SvIMMORTAL(lsv)) {
e03e82a0 1915 sv_set_undef(lsv);
8b0c3377 1916 SvSETMAGIC(lsv);
b09ed995 1917 *relem++ = lsv;
8b0c3377
DM
1918 }
1919 break;
1920 } /* switch */
1921 } /* while */
1922
c73f612f
DM
1923 TAINT_NOT; /* result of list assign isn't tainted */
1924
5d9574c1 1925 if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) {
985213f2 1926 /* Will be used to set PL_tainting below */
dfff4baf
BF
1927 Uid_t tmp_uid = PerlProc_getuid();
1928 Uid_t tmp_euid = PerlProc_geteuid();
1929 Gid_t tmp_gid = PerlProc_getgid();
1930 Gid_t tmp_egid = PerlProc_getegid();
985213f2 1931
b469f1e0 1932 /* XXX $> et al currently silently ignore failures */
3280af22 1933 if (PL_delaymagic & DM_UID) {
a0d0e21e 1934#ifdef HAS_SETRESUID
b469f1e0
JH
1935 PERL_UNUSED_RESULT(
1936 setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
1937 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
1938 (Uid_t)-1));
56febc5e
AD
1939#else
1940# ifdef HAS_SETREUID
b469f1e0
JH
1941 PERL_UNUSED_RESULT(
1942 setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
1943 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1));
56febc5e
AD
1944# else
1945# ifdef HAS_SETRUID
b28d0864 1946 if ((PL_delaymagic & DM_UID) == DM_RUID) {
b469f1e0 1947 PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid));
b28d0864 1948 PL_delaymagic &= ~DM_RUID;
a0d0e21e 1949 }
56febc5e
AD
1950# endif /* HAS_SETRUID */
1951# ifdef HAS_SETEUID
b28d0864 1952 if ((PL_delaymagic & DM_UID) == DM_EUID) {
b469f1e0 1953 PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid));
b28d0864 1954 PL_delaymagic &= ~DM_EUID;
a0d0e21e 1955 }
56febc5e 1956# endif /* HAS_SETEUID */
b28d0864 1957 if (PL_delaymagic & DM_UID) {
985213f2 1958 if (PL_delaymagic_uid != PL_delaymagic_euid)
cea2e8a9 1959 DIE(aTHX_ "No setreuid available");
b469f1e0 1960 PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
a0d0e21e 1961 }
56febc5e
AD
1962# endif /* HAS_SETREUID */
1963#endif /* HAS_SETRESUID */
04783dc7 1964
985213f2
AB
1965 tmp_uid = PerlProc_getuid();
1966 tmp_euid = PerlProc_geteuid();
a0d0e21e 1967 }
b469f1e0 1968 /* XXX $> et al currently silently ignore failures */
3280af22 1969 if (PL_delaymagic & DM_GID) {
a0d0e21e 1970#ifdef HAS_SETRESGID
b469f1e0
JH
1971 PERL_UNUSED_RESULT(
1972 setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
1973 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
1974 (Gid_t)-1));
56febc5e
AD
1975#else
1976# ifdef HAS_SETREGID
b469f1e0
JH
1977 PERL_UNUSED_RESULT(
1978 setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
1979 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1));
56febc5e
AD
1980# else
1981# ifdef HAS_SETRGID
b28d0864 1982 if ((PL_delaymagic & DM_GID) == DM_RGID) {
b469f1e0 1983 PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid));
b28d0864 1984 PL_delaymagic &= ~DM_RGID;
a0d0e21e 1985 }
56febc5e
AD
1986# endif /* HAS_SETRGID */
1987# ifdef HAS_SETEGID
b28d0864 1988 if ((PL_delaymagic & DM_GID) == DM_EGID) {
b469f1e0 1989 PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid));
b28d0864 1990 PL_delaymagic &= ~DM_EGID;
a0d0e21e 1991 }
56febc5e 1992# endif /* HAS_SETEGID */
b28d0864 1993 if (PL_delaymagic & DM_GID) {
985213f2 1994 if (PL_delaymagic_gid != PL_delaymagic_egid)
cea2e8a9 1995 DIE(aTHX_ "No setregid available");
b469f1e0 1996 PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
a0d0e21e 1997 }
56febc5e
AD
1998# endif /* HAS_SETREGID */
1999#endif /* HAS_SETRESGID */
04783dc7 2000
985213f2
AB
2001 tmp_gid = PerlProc_getgid();
2002 tmp_egid = PerlProc_getegid();
a0d0e21e 2003 }
284167a5 2004 TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
9a9b5ec9
DM
2005#ifdef NO_TAINT_SUPPORT
2006 PERL_UNUSED_VAR(tmp_uid);
2007 PERL_UNUSED_VAR(tmp_euid);
2008 PERL_UNUSED_VAR(tmp_gid);
2009 PERL_UNUSED_VAR(tmp_egid);
2010#endif
a0d0e21e 2011 }
a68090fe 2012 PL_delaymagic = old_delaymagic;
54310121 2013
54310121
PP
2014 if (gimme == G_VOID)
2015 SP = firstrelem - 1;
2016 else if (gimme == G_SCALAR) {
54310121 2017 SP = firstrelem;
b09ed995 2018 EXTEND(SP,1);
7b394f12
DM
2019 if (PL_op->op_private & OPpASSIGN_TRUEBOOL)
2020 SETs((firstlelem - firstrelem) ? &PL_sv_yes : &PL_sv_zero);
2021 else {
2022 dTARGET;
2023 SETi(firstlelem - firstrelem);
2024 }
54310121 2025 }
b09ed995
DM
2026 else
2027 SP = relem - 1;
08aeb9f7 2028
54310121 2029 RETURN;
a0d0e21e
LW
2030}
2031
8782bef2
GB
2032PP(pp_qr)
2033{
20b7effb 2034 dSP;
eb578fdb 2035 PMOP * const pm = cPMOP;
fe578d7f 2036 REGEXP * rx = PM_GETRE(pm);
196a02af
DM
2037 regexp *prog = ReANY(rx);
2038 SV * const pkg = RXp_ENGINE(prog)->qr_package(aTHX_ (rx));
c4420975 2039 SV * const rv = sv_newmortal();
d63c20f2
DM
2040 CV **cvp;
2041 CV *cv;
288b8c02
NC
2042
2043 SvUPGRADE(rv, SVt_IV);
c2123ae3
NC
2044 /* For a subroutine describing itself as "This is a hacky workaround" I'm
2045 loathe to use it here, but it seems to be the right fix. Or close.
2046 The key part appears to be that it's essential for pp_qr to return a new
2047 object (SV), which implies that there needs to be an effective way to
2048 generate a new SV from the existing SV that is pre-compiled in the
2049 optree. */
2050 SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx)));
288b8c02
NC
2051 SvROK_on(rv);
2052
8d919b0a 2053 cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv);
5d9574c1 2054 if (UNLIKELY((cv = *cvp) && CvCLONE(*cvp))) {
d63c20f2 2055 *cvp = cv_clone(cv);
fc2b2dca 2056 SvREFCNT_dec_NN(cv);
d63c20f2
DM
2057 }
2058
288b8c02 2059 if (pkg) {
f815daf2 2060 HV *const stash = gv_stashsv(pkg, GV_ADD);
fc2b2dca 2061 SvREFCNT_dec_NN(pkg);
288b8c02
NC
2062 (void)sv_bless(rv, stash);
2063 }
2064
196a02af 2065 if (UNLIKELY(RXp_ISTAINTED(prog))) {
e08e52cf 2066 SvTAINTED_on(rv);
9274aefd
DM
2067 SvTAINTED_on(SvRV(rv));
2068 }
c8c13c22 2069 XPUSHs(rv);
2070 RETURN;
8782bef2
GB
2071}
2072
a0d0e21e
LW
2073PP(pp_match)
2074{
20b7effb 2075 dSP; dTARG;
eb578fdb 2076 PMOP *pm = cPMOP;
d65afb4b 2077 PMOP *dynpm = pm;
eb578fdb 2078 const char *s;
5c144d81 2079 const char *strend;
99a90e59 2080 SSize_t curpos = 0; /* initial pos() or current $+[0] */
a0d0e21e 2081 I32 global;
7fadf4a7 2082 U8 r_flags = 0;
5c144d81 2083 const char *truebase; /* Start of string */
eb578fdb 2084 REGEXP *rx = PM_GETRE(pm);
196a02af 2085 regexp *prog = ReANY(rx);
b3eb6a9b 2086 bool rxtainted;
1c23e2bd 2087 const U8 gimme = GIMME_V;
a0d0e21e 2088 STRLEN len;
a3b680e6 2089 const I32 oldsave = PL_savestack_ix;
e60df1fa 2090 I32 had_zerolen = 0;
b1422d62 2091 MAGIC *mg = NULL;
a0d0e21e 2092
533c011a 2093 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
2094 TARG = POPs;
2095 else {
9399c607
DM
2096 if (ARGTARG)
2097 GETTARGET;
2098 else {
2099 TARG = DEFSV;
2100 }
a0d0e21e
LW
2101 EXTEND(SP,1);
2102 }
d9f424b2 2103
c277df42 2104 PUTBACK; /* EVAL blocks need stack_sp. */
69dc4b30
FC
2105 /* Skip get-magic if this is a qr// clone, because regcomp has
2106 already done it. */
196a02af 2107 truebase = prog->mother_re
69dc4b30
FC
2108 ? SvPV_nomg_const(TARG, len)
2109 : SvPV_const(TARG, len);
f1d31338 2110 if (!truebase)
2269b42e 2111 DIE(aTHX_ "panic: pp_match");
f1d31338 2112 strend = truebase + len;
196a02af 2113 rxtainted = (RXp_ISTAINTED(prog) ||
284167a5 2114 (TAINT_get && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 2115 TAINT_NOT;
a0d0e21e 2116
6c864ec2 2117 /* We need to know this in case we fail out early - pos() must be reset */
de0df3c0
MH
2118 global = dynpm->op_pmflags & PMf_GLOBAL;
2119
d65afb4b 2120 /* PMdf_USED is set after a ?? matches once */
c737faaf
YO
2121 if (
2122#ifdef USE_ITHREADS
2123 SvREADONLY(PL_regex_pad[pm->op_pmoffset])
2124#else
2125 pm->op_pmflags & PMf_USED
2126#endif
2127 ) {
e5dc5375 2128 DEBUG_r(PerlIO_printf(Perl_debug_log, "?? already matched once"));
de0df3c0 2129 goto nope;
a0d0e21e
LW
2130 }
2131
5585e758 2132 /* handle the empty pattern */
196a02af 2133 if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
5585e758
YO
2134 if (PL_curpm == PL_reg_curpm) {
2135 if (PL_curpm_under) {
2136 if (PL_curpm_under == PL_reg_curpm) {
2137 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
2138 } else {
2139 pm = PL_curpm_under;
2140 }
2141 }
2142 } else {
2143 pm = PL_curpm;
2144 }
2145 rx = PM_GETRE(pm);
196a02af 2146 prog = ReANY(rx);
a0d0e21e 2147 }
d65afb4b 2148
196a02af 2149 if (RXp_MINLEN(prog) >= 0 && (STRLEN)RXp_MINLEN(prog) > len) {
75d43e96 2150 DEBUG_r(PerlIO_printf(Perl_debug_log, "String shorter than min possible regex match (%"
147e3846 2151 UVuf " < %" IVdf ")\n",
196a02af 2152 (UV)len, (IV)RXp_MINLEN(prog)));
de0df3c0 2153 goto nope;
e5dc5375 2154 }
c277df42 2155
8ef97b0e 2156 /* get pos() if //g */
de0df3c0 2157 if (global) {
b1422d62 2158 mg = mg_find_mglob(TARG);
8ef97b0e 2159 if (mg && mg->mg_len >= 0) {
25fdce4a 2160 curpos = MgBYTEPOS(mg, TARG, truebase, len);
8ef97b0e
DM
2161 /* last time pos() was set, it was zero-length match */
2162 if (mg->mg_flags & MGf_MINMATCH)
2163 had_zerolen = 1;
2164 }
a0d0e21e 2165 }
8ef97b0e 2166
6e240d0b 2167#ifdef PERL_SAWAMPERSAND
196a02af 2168 if ( RXp_NPARENS(prog)
6502e081 2169 || PL_sawampersand
196a02af 2170 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
5b0e71e9 2171 || (dynpm->op_pmflags & PMf_KEEPCOPY)
6e240d0b
FC
2172 )
2173#endif
2174 {
6502e081
DM
2175 r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE);
2176 /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer
2177 * only on the first iteration. Therefore we need to copy $' as well
2178 * as $&, to make the rest of the string available for captures in
2179 * subsequent iterations */
2180 if (! (global && gimme == G_ARRAY))
2181 r_flags |= REXEC_COPY_SKIP_POST;
2182 };
5b0e71e9
DM
2183#ifdef PERL_SAWAMPERSAND
2184 if (dynpm->op_pmflags & PMf_KEEPCOPY)
2185 /* handle KEEPCOPY in pmop but not rx, eg $r=qr/a/; /$r/p */
2186 r_flags &= ~(REXEC_COPY_SKIP_PRE|REXEC_COPY_SKIP_POST);
2187#endif
22e551b9 2188
f1d31338
DM
2189 s = truebase;
2190
d7be1480 2191 play_it_again:
985afbc1 2192 if (global)
03c83e26 2193 s = truebase + curpos;
f722798b 2194
77da2310 2195 if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase,
03c83e26 2196 had_zerolen, TARG, NULL, r_flags))
03b6c93d 2197 goto nope;
77da2310
NC
2198
2199 PL_curpm = pm;
985afbc1 2200 if (dynpm->op_pmflags & PMf_ONCE)
c737faaf 2201#ifdef USE_ITHREADS
77da2310 2202 SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]);
c737faaf 2203#else
77da2310 2204 dynpm->op_pmflags |= PMf_USED;
c737faaf 2205#endif
a0d0e21e 2206
72311751 2207 if (rxtainted)
196a02af
DM
2208 RXp_MATCH_TAINTED_on(prog);
2209 TAINT_IF(RXp_MATCH_TAINTED(prog));
35c2ccc3
DM
2210
2211 /* update pos */
2212
2213 if (global && (gimme != G_ARRAY || (dynpm->op_pmflags & PMf_CONTINUE))) {
b1422d62 2214 if (!mg)
35c2ccc3 2215 mg = sv_magicext_mglob(TARG);
196a02af
DM
2216 MgBYTEPOS_set(mg, TARG, truebase, RXp_OFFS(prog)[0].end);
2217 if (RXp_ZERO_LEN(prog))
adf51885
DM
2218 mg->mg_flags |= MGf_MINMATCH;
2219 else
2220 mg->mg_flags &= ~MGf_MINMATCH;
35c2ccc3
DM
2221 }
2222
196a02af 2223 if ((!RXp_NPARENS(prog) && !global) || gimme != G_ARRAY) {
bf9dff51
DM
2224 LEAVE_SCOPE(oldsave);
2225 RETPUSHYES;
2226 }
2227
88ab22af
DM
2228 /* push captures on stack */
2229
bf9dff51 2230 {
196a02af 2231 const I32 nparens = RXp_NPARENS(prog);
a3b680e6 2232 I32 i = (global && !nparens) ? 1 : 0;
a0d0e21e 2233
c277df42 2234 SPAGAIN; /* EVAL blocks could move the stack. */
ffc61ed2
JH
2235 EXTEND(SP, nparens + i);
2236 EXTEND_MORTAL(nparens + i);
2237 for (i = !i; i <= nparens; i++) {
a0d0e21e 2238 PUSHs(sv_newmortal());
196a02af
DM
2239 if (LIKELY((RXp_OFFS(prog)[i].start != -1)
2240 && RXp_OFFS(prog)[i].end != -1 ))
5d9574c1 2241 {
196a02af
DM
2242 const I32 len = RXp_OFFS(prog)[i].end - RXp_OFFS(prog)[i].start;
2243 const char * const s = RXp_OFFS(prog)[i].start + truebase;
2244 if (UNLIKELY( RXp_OFFS(prog)[i].end < 0
2245 || RXp_OFFS(prog)[i].start < 0
2246 || len < 0
2247 || len > strend - s)
2248 )
5637ef5b 2249 DIE(aTHX_ "panic: pp_match start/end pointers, i=%ld, "
147e3846 2250 "start=%ld, end=%ld, s=%p, strend=%p, len=%" UVuf,
196a02af
DM
2251 (long) i, (long) RXp_OFFS(prog)[i].start,
2252 (long)RXp_OFFS(prog)[i].end, s, strend, (UV) len);
a0d0e21e 2253 sv_setpvn(*SP, s, len);
cce850e4 2254 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
a197cbdd 2255 SvUTF8_on(*SP);
a0d0e21e
LW
2256 }
2257 }
2258 if (global) {
196a02af
DM
2259 curpos = (UV)RXp_OFFS(prog)[0].end;
2260 had_zerolen = RXp_ZERO_LEN(prog);
c277df42 2261 PUTBACK; /* EVAL blocks may use stack */
cf93c79d 2262 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
2263 goto play_it_again;
2264 }
4633a7c4 2265 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2266 RETURN;
2267 }
e5964223 2268 NOT_REACHED; /* NOTREACHED */
a0d0e21e 2269
7b52d656 2270 nope:
d65afb4b 2271 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
b1422d62
DM
2272 if (!mg)
2273 mg = mg_find_mglob(TARG);
2274 if (mg)
2275 mg->mg_len = -1;
a0d0e21e 2276 }
4633a7c4 2277 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2278 if (gimme == G_ARRAY)
2279 RETURN;
2280 RETPUSHNO;
2281}
2282
2283OP *
864dbfa3 2284Perl_do_readline(pTHX)
a0d0e21e 2285{
20b7effb 2286 dSP; dTARGETSTACKED;
eb578fdb 2287 SV *sv;
a0d0e21e
LW
2288 STRLEN tmplen = 0;
2289 STRLEN offset;
760ac839 2290 PerlIO *fp;
eb578fdb
KW
2291 IO * const io = GvIO(PL_last_in_gv);
2292 const I32 type = PL_op->op_type;
1c23e2bd 2293 const U8 gimme = GIMME_V;
a0d0e21e 2294
6136c704 2295 if (io) {
50db69d8 2296 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
6136c704 2297 if (mg) {
3e0cb5de 2298 Perl_tied_method(aTHX_ SV_CONST(READLINE), SP, MUTABLE_SV(io), mg, gimme, 0);
6136c704 2299 if (gimme == G_SCALAR) {
50db69d8
NC
2300 SPAGAIN;
2301 SvSetSV_nosteal(TARG, TOPs);
2302 SETTARG;
6136c704 2303 }
50db69d8 2304 return NORMAL;
0b7c7b4f 2305 }
e79b0511 2306 }
4608196e 2307 fp = NULL;
a0d0e21e
LW
2308 if (io) {
2309 fp = IoIFP(io);
2310 if (!fp) {
2311 if (IoFLAGS(io) & IOf_ARGV) {
2312 if (IoFLAGS(io) & IOf_START) {
a0d0e21e 2313 IoLINES(io) = 0;
b9f2b683 2314 if (av_tindex(GvAVn(PL_last_in_gv)) < 0) {
1d7c1841 2315 IoFLAGS(io) &= ~IOf_START;
d5eb9a46 2316 do_open6(PL_last_in_gv, "-", 1, NULL, NULL, 0);
4bac9ae4 2317 SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */
76f68e9b 2318 sv_setpvs(GvSVn(PL_last_in_gv), "-");
3280af22 2319 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
2320 fp = IoIFP(io);
2321 goto have_fp;
a0d0e21e
LW
2322 }
2323 }
157fb5a1 2324 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
a0d0e21e 2325 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 2326 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
2327 }
2328 }
0d44d22b
NC
2329 else if (type == OP_GLOB)
2330 fp = Perl_start_glob(aTHX_ POPs, io);
a0d0e21e
LW
2331 }
2332 else if (type == OP_GLOB)
2333 SP--;
7716c5c5 2334 else if (IoTYPE(io) == IoTYPE_WRONLY) {
a5390457 2335 report_wrongway_fh(PL_last_in_gv, '>');
a00b5bd3 2336 }
a0d0e21e
LW
2337 }
2338 if (!fp) {
041457d9 2339 if ((!io || !(IoFLAGS(io) & IOf_START))
de7dabb6
TC
2340 && ckWARN(WARN_CLOSED)
2341 && type != OP_GLOB)
041457d9 2342 {
de7dabb6 2343 report_evil_fh(PL_last_in_gv);
3f4520fe 2344 }
54310121 2345 if (gimme == G_SCALAR) {
79628082 2346 /* undef TARG, and push that undefined value */
ba92458f 2347 if (type != OP_RCATLINE) {
3773545d 2348 sv_set_undef(TARG);
ba92458f 2349 }
a0d0e21e
LW
2350 PUSHTARG;
2351 }
2352 RETURN;
2353 }
a2008d6d 2354 have_fp:
54310121 2355 if (gimme == G_SCALAR) {
a0d0e21e 2356 sv = TARG;
0f722b55
RGS
2357 if (type == OP_RCATLINE && SvGMAGICAL(sv))
2358 mg_get(sv);
48de12d9
RGS
2359 if (SvROK(sv)) {
2360 if (type == OP_RCATLINE)
5668452f 2361 SvPV_force_nomg_nolen(sv);
48de12d9
RGS
2362 else
2363 sv_unref(sv);
2364 }
f7877b28 2365 else if (isGV_with_GP(sv)) {
5668452f 2366 SvPV_force_nomg_nolen(sv);
f7877b28 2367 }
862a34c6 2368 SvUPGRADE(sv, SVt_PV);
a0d0e21e 2369 tmplen = SvLEN(sv); /* remember if already alloced */
e3918bb7 2370 if (!tmplen && !SvREADONLY(sv) && !SvIsCOW(sv)) {
f72e8700
JJ
2371 /* try short-buffering it. Please update t/op/readline.t
2372 * if you change the growth length.
2373 */
2374 Sv_Grow(sv, 80);
2375 }
2b5e58c4
AMS
2376 offset = 0;
2377 if (type == OP_RCATLINE && SvOK(sv)) {
2378 if (!SvPOK(sv)) {
5668452f 2379 SvPV_force_nomg_nolen(sv);
2b5e58c4 2380 }
a0d0e21e 2381 offset = SvCUR(sv);
2b5e58c4 2382 }
a0d0e21e 2383 }
54310121 2384 else {
561b68a9 2385 sv = sv_2mortal(newSV(80));
54310121
PP
2386 offset = 0;
2387 }
fbad3eb5 2388
3887d568
AP
2389 /* This should not be marked tainted if the fp is marked clean */
2390#define MAYBE_TAINT_LINE(io, sv) \
2391 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
2392 TAINT; \
2393 SvTAINTED_on(sv); \
2394 }
2395
684bef36 2396/* delay EOF state for a snarfed empty file */
fbad3eb5 2397#define SNARF_EOF(gimme,rs,io,sv) \
684bef36 2398 (gimme != G_SCALAR || SvCUR(sv) \
b9fee9ba 2399 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
fbad3eb5 2400
a0d0e21e 2401 for (;;) {
09e8efcc 2402 PUTBACK;
fbad3eb5 2403 if (!sv_gets(sv, fp, offset)
2d726892
TF
2404 && (type == OP_GLOB
2405 || SNARF_EOF(gimme, PL_rs, io, sv)
2406 || PerlIO_error(fp)))
fbad3eb5 2407 {
760ac839 2408 PerlIO_clearerr(fp);
a0d0e21e 2409 if (IoFLAGS(io) & IOf_ARGV) {
157fb5a1 2410 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
a0d0e21e
LW
2411 if (fp)
2412 continue;
3280af22 2413 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
2414 }
2415 else if (type == OP_GLOB) {
a2a5de95
NC
2416 if (!do_close(PL_last_in_gv, FALSE)) {
2417 Perl_ck_warner(aTHX_ packWARN(WARN_GLOB),
2418 "glob failed (child exited with status %d%s)",
2419 (int)(STATUS_CURRENT >> 8),
2420 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 2421 }
a0d0e21e 2422 }
54310121 2423 if (gimme == G_SCALAR) {
ba92458f
AE
2424 if (type != OP_RCATLINE) {
2425 SV_CHECK_THINKFIRST_COW_DROP(TARG);
0c34ef67 2426 SvOK_off(TARG);
ba92458f 2427 }
09e8efcc 2428 SPAGAIN;
a0d0e21e
LW
2429 PUSHTARG;
2430 }
3887d568 2431 MAYBE_TAINT_LINE(io, sv);
a0d0e21e
LW
2432 RETURN;
2433 }
3887d568 2434 MAYBE_TAINT_LINE(io, sv);
a0d0e21e 2435 IoLINES(io)++;
b9fee9ba 2436 IoFLAGS(io) |= IOf_NOLINE;
71be2cbc 2437 SvSETMAGIC(sv);
09e8efcc 2438 SPAGAIN;
a0d0e21e 2439 XPUSHs(sv);
a0d0e21e 2440 if (type == OP_GLOB) {
349d4f2f 2441 const char *t1;
45a23732 2442 Stat_t statbuf;
a0d0e21e 2443
3280af22 2444 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
6136c704 2445 char * const tmps = SvEND(sv) - 1;
aa07b2f6 2446 if (*tmps == *SvPVX_const(PL_rs)) {
c07a80fd 2447 *tmps = '\0';
b162af07 2448 SvCUR_set(sv, SvCUR(sv) - 1);
c07a80fd
PP
2449 }
2450 }
349d4f2f 2451 for (t1 = SvPVX_const(sv); *t1; t1++)
b51c3e77
CB
2452#ifdef __VMS
2453 if (strchr("*%?", *t1))
2454#else
7ad1e72d 2455 if (strchr("$&*(){}[]'\";\\|?<>~`", *t1))
b51c3e77 2456#endif
a0d0e21e 2457 break;
45a23732 2458 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &statbuf) < 0) {
a0d0e21e
LW
2459 (void)POPs; /* Unmatched wildcard? Chuck it... */
2460 continue;
2461 }
2d79bf7f 2462 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
d4c19fe8
AL
2463 if (ckWARN(WARN_UTF8)) {
2464 const U8 * const s = (const U8*)SvPVX_const(sv) + offset;
2465 const STRLEN len = SvCUR(sv) - offset;
2466 const U8 *f;
2467
2468 if (!is_utf8_string_loc(s, len, &f))
2469 /* Emulate :encoding(utf8) warning in the same case. */
2470 Perl_warner(aTHX_ packWARN(WARN_UTF8),
2471 "utf8 \"\\x%02X\" does not map to Unicode",
2472 f < (U8*)SvEND(sv) ? *f : 0);
2473 }
a0d0e21e 2474 }
54310121 2475 if (gimme == G_ARRAY) {
a0d0e21e 2476 if (SvLEN(sv) - SvCUR(sv) > 20) {
1da4ca5f 2477 SvPV_shrink_to_cur(sv);
a0d0e21e 2478 }
561b68a9 2479 sv = sv_2mortal(newSV(80));
a0d0e21e
LW
2480 continue;
2481 }
54310121 2482 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e 2483 /* try to reclaim a bit of scalar space (only on 1st alloc) */
d5b5861b
NC
2484 const STRLEN new_len
2485 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1da4ca5f 2486 SvPV_renew(sv, new_len);
a0d0e21e
LW
2487 }
2488 RETURN;
2489 }
2490}
2491
a0d0e21e
LW
2492PP(pp_helem)
2493{
20b7effb 2494 dSP;
760ac839 2495 HE* he;
ae77835f 2496 SV **svp;
c445ea15 2497 SV * const keysv = POPs;
85fbaab2 2498 HV * const hv = MUTABLE_HV(POPs);
a3b680e6
AL
2499 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2500 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 2501 SV *sv;
92970b93 2502 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
d30e492c 2503 bool preeminent = TRUE;
a0d0e21e 2504
6dfc73ea
SM
2505 if (SvTYPE(hv) != SVt_PVHV)
2506 RETPUSHUNDEF;
d4c19fe8 2507
92970b93 2508 if (localizing) {
d4c19fe8
AL
2509 MAGIC *mg;
2510 HV *stash;
d30e492c
VP
2511
2512 /* If we can determine whether the element exist,
2513 * Try to preserve the existenceness of a tied hash
2514 * element by using EXISTS and DELETE if possible.
2515 * Fallback to FETCH and STORE otherwise. */
2c5f48c2 2516 if (SvCANEXISTDELETE(hv))
d30e492c 2517 preeminent = hv_exists_ent(hv, keysv, 0);
d4c19fe8 2518 }
d30e492c 2519
5f9d7e2b 2520 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
d4c19fe8 2521 svp = he ? &HeVAL(he) : NULL;
a0d0e21e 2522 if (lval) {
746f6409 2523 if (!svp || !*svp || *svp == &PL_sv_undef) {
68dc0745
PP
2524 SV* lv;
2525 SV* key2;
2d8e6c8d 2526 if (!defer) {
be2597df 2527 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2d8e6c8d 2528 }
68dc0745
PP
2529 lv = sv_newmortal();
2530 sv_upgrade(lv, SVt_PVLV);
2531 LvTYPE(lv) = 'y';
6136c704 2532 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0);
fc2b2dca 2533 SvREFCNT_dec_NN(key2); /* sv_magic() increments refcount */
0ad694a7 2534 LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
68dc0745
PP
2535 LvTARGLEN(lv) = 1;
2536 PUSHs(lv);
2537 RETURN;
2538 }
92970b93 2539 if (localizing) {
bfcb3514 2540 if (HvNAME_get(hv) && isGV(*svp))
159b6efe 2541 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
47cfc530
VP
2542 else if (preeminent)
2543 save_helem_flags(hv, keysv, svp,
2544 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
2545 else
2546 SAVEHDELETE(hv, keysv);
5f05dabc 2547 }
9026059d
GG
2548 else if (PL_op->op_private & OPpDEREF) {
2549 PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
2550 RETURN;
2551 }
a0d0e21e 2552 }
746f6409 2553 sv = (svp && *svp ? *svp : &PL_sv_undef);
fd69380d
DM
2554 /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this
2555 * was to make C<local $tied{foo} = $tied{foo}> possible.
2556 * However, it seems no longer to be needed for that purpose, and
2557 * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g>
2558 * would loop endlessly since the pos magic is getting set on the
2559 * mortal copy and lost. However, the copy has the effect of
2560 * triggering the get magic, and losing it altogether made things like
2561 * c<$tied{foo};> in void context no longer do get magic, which some
2562 * code relied on. Also, delayed triggering of magic on @+ and friends
2563 * meant the original regex may be out of scope by now. So as a
2564 * compromise, do the get magic here. (The MGf_GSKIP flag will stop it
2565 * being called too many times). */
39cf747a 2566 if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv))
fd69380d 2567 mg_get(sv);
be6c24e0 2568 PUSHs(sv);
a0d0e21e
LW
2569 RETURN;
2570}
2571
fedf30e1
DM
2572
2573/* a stripped-down version of Perl_softref2xv() for use by
2574 * pp_multideref(), which doesn't use PL_op->op_flags */
2575
f9db5646 2576STATIC GV *
fedf30e1
DM
2577S_softref2xv_lite(pTHX_ SV *const sv, const char *const what,
2578 const svtype type)
2579{
2580 if (PL_op->op_private & HINT_STRICT_REFS) {
2581 if (SvOK(sv))
2582 Perl_die(aTHX_ PL_no_symref_sv, sv,
2583 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
2584 else
2585 Perl_die(aTHX_ PL_no_usym, what);
2586 }
2587 if (!SvOK(sv))
2588 Perl_die(aTHX_ PL_no_usym, what);
2589 return gv_fetchsv_nomg(sv, GV_ADD, type);
2590}
2591
2592
79815f56
DM
2593/* Handle one or more aggregate derefs and array/hash indexings, e.g.
2594 * $h->{foo} or $a[0]{$key}[$i] or f()->[1]
fedf30e1
DM
2595 *
2596 * op_aux points to an array of unions of UV / IV / SV* / PADOFFSET.
79815f56
DM
2597 * Each of these either contains a set of actions, or an argument, such as
2598 * an IV to use as an array index, or a lexical var to retrieve.
2599 * Several actions re stored per UV; we keep shifting new actions off the
2600 * one UV, and only reload when it becomes zero.
fedf30e1
DM
2601 */
2602
2603PP(pp_multideref)
2604{
2605 SV *sv = NULL; /* init to avoid spurious 'may be used uninitialized' */
2606 UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux;
2607 UV actions = items->uv;
2608
2609 assert(actions);
2610 /* this tells find_uninit_var() where we're up to */
2611 PL_multideref_pc = items;
2612
2613 while (1) {
2614 /* there are three main classes of action; the first retrieve
2615 * the initial AV or HV from a variable or the stack; the second
2616 * does the equivalent of an unrolled (/DREFAV, rv2av, aelem),
2617 * the third an unrolled (/DREFHV, rv2hv, helem).
2618 */
2619 switch (actions & MDEREF_ACTION_MASK) {
2620
2621 case MDEREF_reload:
2622 actions = (++items)->uv;
2623 continue;
2624
2625 case MDEREF_AV_padav_aelem: /* $lex[...] */
2626 sv = PAD_SVl((++items)->pad_offset);
2627 goto do_AV_aelem;
2628
2629 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
2630 sv = UNOP_AUX_item_sv(++items);
2631 assert(isGV_with_GP(sv));
2632 sv = (SV*)GvAVn((GV*)sv);
2633 goto do_AV_aelem;
2634
2635 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
2636 {
2637 dSP;
2638 sv = POPs;
2639 PUTBACK;
2640 goto do_AV_rv2av_aelem;
2641 }
2642
2643 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
2644 sv = UNOP_AUX_item_sv(++items);
2645 assert(isGV_with_GP(sv));
2646 sv = GvSVn((GV*)sv);
2647 goto do_AV_vivify_rv2av_aelem;
2648
2649 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
2650 sv = PAD_SVl((++items)->pad_offset);
2651 /* FALLTHROUGH */
2652
2653 do_AV_vivify_rv2av_aelem:
2654 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
2655 /* this is the OPpDEREF action normally found at the end of
2656 * ops like aelem, helem, rv2sv */
2657 sv = vivify_ref(sv, OPpDEREF_AV);
2658 /* FALLTHROUGH */
2659
2660 do_AV_rv2av_aelem:
2661 /* this is basically a copy of pp_rv2av when it just has the
2662 * sKR/1 flags */
2663 SvGETMAGIC(sv);
2664 if (LIKELY(SvROK(sv))) {
2665 if (UNLIKELY(SvAMAGIC(sv))) {
2666 sv = amagic_deref_call(sv, to_av_amg);
2667 }
2668 sv = SvRV(sv);
2669 if (UNLIKELY(SvTYPE(sv) != SVt_PVAV))
2670 DIE(aTHX_ "Not an ARRAY reference");
2671 }
2672 else if (SvTYPE(sv) != SVt_PVAV) {
2673 if (!isGV_with_GP(sv))
2674 sv = (SV*)S_softref2xv_lite(aTHX_ sv, "an ARRAY", SVt_PVAV);
2675 sv = MUTABLE_SV(GvAVn((GV*)sv));
2676 }
2677 /* FALLTHROUGH */
2678
2679 do_AV_aelem:
2680 {
2681 /* retrieve the key; this may be either a lexical or package
2682 * var (whose index/ptr is stored as an item) or a signed
2683 * integer constant stored as an item.
2684 */
2685 SV *elemsv;
2686 IV elem = 0; /* to shut up stupid compiler warnings */
2687
2688
2689 assert(SvTYPE(sv) == SVt_PVAV);
2690
2691 switch (actions & MDEREF_INDEX_MASK) {
2692 case MDEREF_INDEX_none:
2693 goto finish;
2694 case MDEREF_INDEX_const:
2695 elem = (++items)->iv;
2696 break;
2697 case MDEREF_INDEX_padsv:
2698 elemsv = PAD_SVl((++items)->pad_offset);
2699 goto check_elem;
2700 case MDEREF_INDEX_gvsv:
2701 elemsv = UNOP_AUX_item_sv(++items);
2702 assert(isGV_with_GP(elemsv));
2703 elemsv = GvSVn((GV*)elemsv);
2704 check_elem:
2705 if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv)
2706 && ckWARN(WARN_MISC)))
2707 Perl_warner(aTHX_ packWARN(WARN_MISC),
147e3846 2708 "Use of reference \"%" SVf "\" as array index",
fedf30e1
DM
2709 SVfARG(elemsv));
2710 /* the only time that S_find_uninit_var() needs this
2711 * is to determine which index value triggered the
2712 * undef warning. So just update it here. Note that
2713 * since we don't save and restore this var (e.g. for
2714 * tie or overload execution), its value will be
2715 * meaningless apart from just here */
2716 PL_multideref_pc = items;
2717 elem = SvIV(elemsv);
2718 break;
2719 }
2720
2721
2722 /* this is basically a copy of pp_aelem with OPpDEREF skipped */
2723
2724 if (!(actions & MDEREF_FLAG_last)) {
2725 SV** svp = av_fetch((AV*)sv, elem, 1);
2726 if (!svp || ! (sv=*svp))
2727 DIE(aTHX_ PL_no_aelem, elem);
2728 break;
2729 }
2730
2731 if (PL_op->op_private &
2732 (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
2733 {
2734 if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
2735 sv = av_exists((AV*)sv, elem) ? &PL_sv_yes : &PL_sv_no;
2736 }
2737 else {
2738 I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
2739 sv = av_delete((AV*)sv, elem, discard);
2740 if (discard)
2741 return NORMAL;
2742 if (!sv)
2743 sv = &PL_sv_undef;
2744 }
2745 }
2746 else {
2747 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2748 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
2749 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
2750 bool preeminent = TRUE;
2751 AV *const av = (AV*)sv;
2752 SV** svp;
2753
2754 if (UNLIKELY(localizing)) {
2755 MAGIC *mg;
2756 HV *stash;
2757
2758 /* If we can determine whether the element exist,
2759 * Try to preserve the existenceness of a tied array
2760 * element by using EXISTS and DELETE if possible.
2761 * Fallback to FETCH and STORE otherwise. */
2762 if (SvCANEXISTDELETE(av))
2763 preeminent = av_exists(av, elem);
2764 }
2765
2766 svp = av_fetch(av, elem, lval && !defer);
2767
2768 if (lval) {
2769 if (!svp || !(sv = *svp)) {
2770 IV len;
2771 if (!defer)
2772 DIE(aTHX_ PL_no_aelem, elem);
2773 len = av_tindex(av);
2774 sv = sv_2mortal(newSVavdefelem(av,
2775 /* Resolve a negative index now, unless it points
2776 * before the beginning of the array, in which
2777 * case record it for error reporting in
2778 * magic_setdefelem. */
2779 elem < 0 && len + elem >= 0
2780 ? len + elem : elem, 1));
2781 }
2782 else {
2783 if (UNLIKELY(localizing)) {
2784 if (preeminent) {
2785 save_aelem(av, elem, svp);
2786 sv = *svp; /* may have changed */
2787 }
2788 else
2789 SAVEADELETE(av, elem);
2790 }
2791 }
2792 }
2793 else {
2794 sv = (svp ? *svp : &PL_sv_undef);
2795 /* see note in pp_helem() */
2796 if (SvRMAGICAL(av) && SvGMAGICAL(sv))
2797 mg_get(sv);
2798 }
2799 }
2800
2801 }
2802 finish:
2803 {
2804 dSP;
2805 XPUSHs(sv);
2806 RETURN;
2807 }
2808 /* NOTREACHED */
2809
2810
2811
2812
2813 case MDEREF_HV_padhv_helem: /* $lex{...} */
2814 sv = PAD_SVl((++items)->pad_offset);
2815 goto do_HV_helem;
2816
2817 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
2818 sv = UNOP_AUX_item_sv(++items);
2819 assert(isGV_with_GP(sv));
2820 sv = (SV*)GvHVn((GV*)sv);
2821 goto do_HV_helem;
2822
2823 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
2824 {
2825 dSP;
2826 sv = POPs;
2827 PUTBACK;
2828 goto do_HV_rv2hv_helem;
2829 }
2830
2831 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
2832 sv = UNOP_AUX_item_sv(++items);
2833 assert(isGV_with_GP(sv));
2834 sv = GvSVn((GV*)sv);
2835 goto do_HV_vivify_rv2hv_helem;
2836
2837 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
2838 sv = PAD_SVl((++items)->pad_offset);
2839 /* FALLTHROUGH */
2840
2841 do_HV_vivify_rv2hv_helem:
2842 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
2843 /* this is the OPpDEREF action normally found at the end of
2844 * ops like aelem, helem, rv2sv */
2845 sv = vivify_ref(sv, OPpDEREF_HV);
2846 /* FALLTHROUGH */
2847
2848 do_HV_rv2hv_helem:
2849 /* this is basically a copy of pp_rv2hv when it just has the
2850 * sKR/1 flags (and pp_rv2hv is aliased to pp_rv2av) */
2851
2852 SvGETMAGIC(sv);
2853 if (LIKELY(SvROK(sv))) {
2854 if (UNLIKELY(SvAMAGIC(sv))) {
2855 sv = amagic_deref_call(sv, to_hv_amg);
2856 }
2857 sv = SvRV(sv);
2858 if (UNLIKELY(SvTYPE(sv) != SVt_PVHV))
2859 DIE(aTHX_ "Not a HASH reference");
2860 }
2861 else if (SvTYPE(sv) != SVt_PVHV) {
2862 if (!isGV_with_GP(sv))
2863 sv = (SV*)S_softref2xv_lite(aTHX_ sv, "a HASH", SVt_PVHV);
2864 sv = MUTABLE_SV(GvHVn((GV*)sv));
2865 }
2866 /* FALLTHROUGH */
2867
2868 do_HV_helem:
2869 {
2870 /* retrieve the key; this may be either a lexical / package
2871 * var or a string constant, whose index/ptr is stored as an
2872 * item
2873 */
2874 SV *keysv = NULL; /* to shut up stupid compiler warnings */
2875
2876 assert(SvTYPE(sv) == SVt_PVHV);
2877
2878 switch (actions & MDEREF_INDEX_MASK) {
2879 case MDEREF_INDEX_none:
2880 goto finish;
2881
2882 case MDEREF_INDEX_const:
2883 keysv = UNOP_AUX_item_sv(++items);
2884 break;
2885
2886 case MDEREF_INDEX_padsv:
2887 keysv = PAD_SVl((++items)->pad_offset);
2888 break;
2889
2890 case MDEREF_INDEX_gvsv:
2891 keysv = UNOP_AUX_item_sv(++items);
2892 keysv = GvSVn((GV*)keysv);
2893 break;
2894 }
2895
2896 /* see comment above about setting this var */
2897 PL_multideref_pc = items;
2898
2899
2900 /* ensure that candidate CONSTs have been HEKified */
2901 assert( ((actions & MDEREF_INDEX_MASK) != MDEREF_INDEX_const)
2902 || SvTYPE(keysv) >= SVt_PVMG
2903 || !SvOK(keysv)
2904 || SvROK(keysv)
2905 || SvIsCOW_shared_hash(keysv));
2906
2907 /* this is basically a copy of pp_helem with OPpDEREF skipped */
2908
2909 if (!(actions & MDEREF_FLAG_last)) {
2910 HE *he = hv_fetch_ent((HV*)sv, keysv, 1, 0);
2911 if (!he || !(sv=HeVAL(he)) || sv == &PL_sv_undef)
2912 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2913 break;
2914 }
2915
2916 if (PL_op->op_private &
2917 (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
2918 {
2919 if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
2920 sv = hv_exists_ent((HV*)sv, keysv, 0)
2921 ? &PL_sv_yes : &PL_sv_no;
2922 }
2923 else {
2924 I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
2925 sv = hv_delete_ent((HV*)sv, keysv, discard, 0);
2926 if (discard)
2927 return NORMAL;
2928 if (!sv)
2929 sv = &PL_sv_undef;
2930 }
2931 }
2932 else {
2933 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2934 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
2935 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
2936 bool preeminent = TRUE;
2937 SV **svp;
2938 HV * const hv = (HV*)sv;
2939 HE* he;
2940
2941 if (UNLIKELY(localizing)) {
2942 MAGIC *mg;
2943 HV *stash;
2944
2945 /* If we can determine whether the element exist,
2946 * Try to preserve the existenceness of a tied hash
2947 * element by using EXISTS and DELETE if possible.
2948 * Fallback to FETCH and STORE otherwise. */
2949 if (SvCANEXISTDELETE(hv))
2950 preeminent = hv_exists_ent(hv, keysv, 0);
2951 }
2952
2953 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
2954 svp = he ? &HeVAL(he) : NULL;
2955
2956
2957 if (lval) {
2958 if (!svp || !(sv = *svp) || sv == &PL_sv_undef) {
2959 SV* lv;
2960 SV* key2;
2961 if (!defer)
2962 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2963 lv = sv_newmortal();
2964 sv_upgrade(lv, SVt_PVLV);
2965 LvTYPE(lv) = 'y';
2966 sv_magic(lv, key2 = newSVsv(keysv),
2967 PERL_MAGIC_defelem, NULL, 0);
2968 /* sv_magic() increments refcount */
2969 SvREFCNT_dec_NN(key2);
0ad694a7 2970 LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
fedf30e1
DM
2971 LvTARGLEN(lv) = 1;
2972 sv = lv;
2973 }
2974 else {
2975 if (localizing) {
2976 if (HvNAME_get(hv) && isGV(sv))
2977 save_gp(MUTABLE_GV(sv),
2978 !(PL_op->op_flags & OPf_SPECIAL));
2979 else if (preeminent) {
2980 save_helem_flags(hv, keysv, svp,
2981 (PL_op->op_flags & OPf_SPECIAL)
2982 ? 0 : SAVEf_SETMAGIC);
2983 sv = *svp; /* may have changed */
2984 }
2985 else
2986 SAVEHDELETE(hv, keysv);
2987 }
2988 }
2989 }
2990 else {
2991 sv = (svp && *svp ? *svp : &PL_sv_undef);
2992 /* see note in pp_helem() */
2993 if (SvRMAGICAL(hv) && SvGMAGICAL(sv))
2994 mg_get(sv);
2995 }
2996 }
2997 goto finish;
2998 }
2999
3000 } /* switch */
3001
3002 actions >>= MDEREF_SHIFT;
3003 } /* while */
3004 /* NOTREACHED */
3005}
3006
3007
a0d0e21e
LW
3008PP(pp_iter)
3009{
eb578fdb 3010 PERL_CONTEXT *cx;
7d6c2cef 3011 SV *oldsv;
1d7c1841 3012 SV **itersvp;
a0d0e21e 3013
84f05d57
JH
3014 SV *sv;
3015 AV *av;
3016 IV ix;
3017 IV inc;
3018
4ebe6e95 3019 cx = CX_CUR();
1d7c1841 3020 itersvp = CxITERVAR(cx);
4b5c941e 3021 assert(itersvp);
a48ce6be
DM
3022
3023 switch (CxTYPE(cx)) {
17c91640 3024
b552b52c
DM
3025 case CXt_LOOP_LAZYSV: /* string increment */
3026 {
3027 SV* cur = cx->blk_loop.state_u.lazysv.cur;
3028 SV *end = cx->blk_loop.state_u.lazysv.end;
3029 /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no.
3030 It has SvPVX of "" and SvCUR of 0, which is what we want. */
3031 STRLEN maxlen = 0;
3032 const char *max = SvPV_const(end, maxlen);
d6c970c7
AC
3033 if (DO_UTF8(end) && IN_UNI_8_BIT)
3034 maxlen = sv_len_utf8_nomg(end);
5d9574c1 3035 if (UNLIKELY(SvNIOK(cur) || SvCUR(cur) > maxlen))
8a1f10dd 3036 goto retno;
b552b52c
DM
3037
3038 oldsv = *itersvp;
6d3ca00e
DM
3039 /* NB: on the first iteration, oldsv will have a ref count of at
3040 * least 2 (one extra from blk_loop.itersave), so the GV or pad
3041 * slot will get localised; on subsequent iterations the RC==1
3042 * optimisation may kick in and the SV will be reused. */
3043 if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
b552b52c
DM
3044 /* safe to reuse old SV */
3045 sv_setsv(oldsv, cur);
a48ce6be 3046 }
b552b52c
DM
3047 else
3048 {
3049 /* we need a fresh SV every time so that loop body sees a
3050 * completely new SV for closures/references to work as
3051 * they used to */
3052 *itersvp = newSVsv(cur);
6d3ca00e 3053 SvREFCNT_dec(oldsv);
b552b52c
DM
3054 }
3055 if (strEQ(SvPVX_const(cur), max))
3056 sv_setiv(cur, 0); /* terminate next time */
3057 else
3058 sv_inc(cur);
3059 break;
3060 }
a48ce6be 3061
fcef60b4
DM
3062 case CXt_LOOP_LAZYIV: /* integer increment */
3063 {
3064 IV cur = cx->blk_loop.state_u.lazyiv.cur;
5d9574c1 3065 if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end))
8a1f10dd 3066 goto retno;
7f61b687 3067
fcef60b4 3068 oldsv = *itersvp;
6d3ca00e
DM
3069 /* see NB comment above */
3070 if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
eaa5c2d6 3071 /* safe to reuse old SV */
47b96a1e
DM
3072
3073 if ( (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV))
3074 == SVt_IV)
3075 {
3076 /* Cheap SvIOK_only().
3077 * Assert that flags which SvIOK_only() would test or
3078 * clear can't be set, because we're SVt_IV */
3079 assert(!(SvFLAGS(oldsv) &
3080 (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK)))));
3081 SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK);
3082 /* SvIV_set() where sv_any points to head */
3083 oldsv->sv_u.svu_iv = cur;
3084
3085 }
3086 else
3087 sv_setiv(oldsv, cur);
eaa5c2d6 3088 }
1c846c1f 3089 else
eaa5c2d6
GA
3090 {
3091 /* we need a fresh SV every time so that loop body sees a
3092 * completely new SV for closures/references to work as they
3093 * used to */
fcef60b4 3094 *itersvp = newSViv(cur);
6d3ca00e 3095 SvREFCNT_dec(oldsv);
eaa5c2d6 3096 }
a2309040 3097
5d9574c1 3098 if (UNLIKELY(cur == IV_MAX)) {
cdc1aa42
NC
3099 /* Handle end of range at IV_MAX */
3100 cx->blk_loop.state_u.lazyiv.end = IV_MIN;
3101 } else
3102 ++cx->blk_loop.state_u.lazyiv.cur;
a48ce6be 3103 break;
fcef60b4 3104 }
a48ce6be 3105
93661e56
DM
3106 case CXt_LOOP_LIST: /* for (1,2,3) */
3107
3108 assert(OPpITER_REVERSED == 2); /* so inc becomes -1 or 1 */
3109 inc = 1 - (PL_op->op_private & OPpITER_REVERSED);
3110 ix = (cx->blk_loop.state_u.stack.ix += inc);
3111 if (UNLIKELY(inc > 0
3112 ? ix > cx->blk_oldsp
3113 : ix <= cx->blk_loop.state_u.stack.basesp)
3114 )
8a1f10dd 3115 goto retno;
93661e56
DM
3116
3117 sv = PL_stack_base[ix];
3118 av = NULL;
3119 goto loop_ary_common;
3120
3121 case CXt_LOOP_ARY: /* for (@ary) */
3122
3123 av = cx->blk_loop.state_u.ary.ary;
3124 inc = 1 - (PL_op->op_private & OPpITER_REVERSED);
3125 ix = (cx->blk_loop.state_u.ary.ix += inc);
3126 if (UNLIKELY(inc > 0
3127 ? ix > AvFILL(av)
3128 : ix < 0)
3129 )
8a1f10dd 3130 goto retno;
de080daa 3131
9d1ee8e0 3132 if (UNLIKELY(SvRMAGICAL(av))) {
a8a20bb6
DM
3133 SV * const * const svp = av_fetch(av, ix, FALSE);
3134 sv = svp ? *svp : NULL;
3135 }
3136 else {
3137 sv = AvARRAY(av)[ix];
de080daa 3138 }
ef3e5ea9 3139
93661e56
DM
3140 loop_ary_common:
3141
d39c26a6
FC
3142 if (UNLIKELY(cx->cx_type & CXp_FOR_LVREF)) {
3143 SvSetMagicSV(*itersvp, sv);
3144 break;
3145 }
3146
5d9574c1
DM
3147 if (LIKELY(sv)) {
3148 if (UNLIKELY(SvIS_FREED(sv))) {
f38aa882
DM
3149 *itersvp = NULL;
3150 Perl_croak(aTHX_ "Use of freed value in iteration");
3151 }
60779a30 3152 if (SvPADTMP(sv)) {
8e079c2a 3153 sv = newSVsv(sv);
60779a30 3154 }
8e079c2a
FC
3155 else {
3156 SvTEMP_off(sv);
3157 SvREFCNT_inc_simple_void_NN(sv);
3158 }
de080daa 3159 }
93661e56 3160 else if (av) {
199f858d 3161 sv = newSVavdefelem(av, ix, 0);
de080daa 3162 }
a600f7e6
FC
3163 else
3164 sv = &PL_sv_undef;
a0d0e21e 3165
de080daa
DM
3166 oldsv = *itersvp;
3167 *itersvp = sv;
3168 SvREFCNT_dec(oldsv);
de080daa 3169 break;
a48ce6be
DM
3170
3171 default:
3172 DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx));
3173 }
8a1f10dd 3174
7c114860
DM
3175 /* Bypass pushing &PL_sv_yes and calling pp_and(); instead
3176 * jump straight to the AND op's op_other */
3177 assert(PL_op->op_next->op_type == OP_AND);
3178 assert(PL_op->op_next->op_ppaddr == Perl_pp_and);
3179 return cLOGOPx(PL_op->op_next)->op_other;
3180
3181 retno:
3182 /* Bypass pushing &PL_sv_no and calling pp_and(); instead
3183 * jump straight to the AND op's op_next */
3184 assert(PL_op->op_next->op_type == OP_AND);
3185 assert(PL_op->op_next->op_ppaddr == Perl_pp_and);
8a1f10dd 3186 /* pp_enteriter should have pre-extended the stack */
87058c31 3187 EXTEND_SKIP(PL_stack_sp, 1);
7c114860
DM
3188 /* we only need this for the rare case where the OP_AND isn't
3189 * in void context, e.g. $x = do { for (..) {...} };
3190 * but its cheaper to just push it rather than testing first
3191 */
3192 *++PL_stack_sp = &PL_sv_no;
3193 return PL_op->op_next->op_next;
a0d0e21e
LW
3194}
3195
7c114860 3196
ef07e810
DM
3197/*
3198A description of how taint works in pattern matching and substitution.
3199
284167a5
SM
3200This is all conditional on NO_TAINT_SUPPORT not being defined. Under
3201NO_TAINT_SUPPORT, taint-related operations should become no-ops.
3202
4e19c54b 3203While the pattern is being assembled/concatenated and then compiled,
284167a5
SM
3204PL_tainted will get set (via TAINT_set) if any component of the pattern
3205is tainted, e.g. /.*$tainted/. At the end of pattern compilation,
3206the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via
1738e041
DM
3207TAINT_get). It will also be set if any component of the pattern matches
3208based on locale-dependent behavior.
ef07e810 3209
0ab462a6
DM
3210When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to
3211the pattern is marked as tainted. This means that subsequent usage, such
284167a5
SM
3212as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED,
3213on the new pattern too.
ef07e810 3214
272d35c9 3215RXf_TAINTED_SEEN is used post-execution by the get magic code
ef07e810
DM
3216of $1 et al to indicate whether the returned value should be tainted.
3217It is the responsibility of the caller of the pattern (i.e. pp_match,
3218pp_subst etc) to set this flag for any other circumstances where $1 needs
3219to be tainted.
3220
3221The taint behaviour of pp_subst (and pp_substcont) is quite complex.
3222
3223There are three possible sources of taint
3224 * the source string
3225 * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN)
3226 * the replacement string (or expression under /e)
3227
3228There are four destinations of taint and they are affected by the sources
3229according to the rules below:
3230
3231 * the return value (not including /r):
3232 tainted by the source string and pattern, but only for the
3233 number-of-iterations case; boolean returns aren't tainted;
3234 * the modified string (or modified copy under /r):
3235 tainted by the source string, pattern, and replacement strings;
3236 * $1 et al:
3237 tainted by the pattern, and under 'use re "taint"', by the source
3238 string too;
3239 * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted:
3240 should always be unset before executing subsequent code.
3241
3242The overall action of pp_subst is:
3243
3244 * at the start, set bits in rxtainted indicating the taint status of
3245 the various sources.
3246
3247 * After each pattern execution, update the SUBST_TAINT_PAT bit in
3248 rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the
3249 pattern has subsequently become tainted via locale ops.
3250
3251 * If control is being passed to pp_substcont to execute a /e block,
3252 save rxtainted in the CXt_SUBST block, for future use by
3253 pp_substcont.
3254
3255 * Whenever control is being returned to perl code (either by falling
3256 off the "end" of pp_subst/pp_substcont, or by entering a /e block),
3257 use the flag bits in rxtainted to make all the appropriate types of
0ab462a6
DM
3258 destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1
3259 et al will appear tainted.
ef07e810
DM
3260
3261pp_match is just a simpler version of the above.
3262
3263*/
3264
a0d0e21e
LW
3265PP(pp_subst)
3266{
20b7effb 3267 dSP; dTARG;
eb578fdb 3268 PMOP *pm = cPMOP;
a0d0e21e 3269 PMOP *rpm = pm;
eb578fdb 3270 char *s;
a0d0e21e 3271 char *strend;
5c144d81 3272 const char *c;
a0d0e21e 3273 STRLEN clen;
3c6ef0a5
FC
3274 SSize_t iters = 0;
3275 SSize_t maxiters;
a0d0e21e 3276 bool once;
ef07e810
DM
3277 U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits.
3278 See "how taint works" above */
a0d0e21e 3279 char *orig;
1ed74d04 3280 U8 r_flags;
eb578fdb 3281 REGEXP *rx = PM_GETRE(pm);
196a02af 3282 regexp *prog = ReANY(rx);
a0d0e21e
LW
3283 STRLEN len;
3284 int force_on_match = 0;
0bcc34c2 3285 const I32 oldsave = PL_savestack_ix;
792b2c16 3286 STRLEN slen;
26a74523 3287 bool doutf8 = FALSE; /* whether replacement is in utf8 */
db2c6cb3 3288#ifdef PERL_ANY_COW
106d9a13 3289 bool was_cow;
ed252734 3290#endif
a0714e2c 3291 SV *nsv = NULL;
b770e143 3292 /* known replacement string? */
eb578fdb 3293 SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL;
a0d0e21e 3294
f410a211
NC
3295 PERL_ASYNC_CHECK();
3296
533c011a 3297 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
3298 TARG = POPs;
3299 else {
9399c607
DM
3300 if (ARGTARG)
3301 GETTARGET;
3302 else {
3303 TARG = DEFSV;
3304 }
a0d0e21e 3305 EXTEND(SP,1);
1c846c1f 3306 }
d9f424b2 3307
64534138 3308 SvGETMAGIC(TARG); /* must come before cow check */
db2c6cb3 3309#ifdef PERL_ANY_COW
106d9a13
DM
3310 /* note that a string might get converted to COW during matching */
3311 was_cow = cBOOL(SvIsCOW(TARG));
ed252734 3312#endif
d13a5d3b
TC
3313 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
3314#ifndef PERL_ANY_COW
3315 if (SvIsCOW(TARG))
3316 sv_force_normal_flags(TARG,0);
3317#endif
3318 if ((SvREADONLY(TARG)
3319 || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG))
3320 || SvTYPE(TARG) > SVt_PVLV)
3321 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
3322 Perl_croak_no_modify();
3323 }
8ec5e241
NIS
3324 PUTBACK;
3325
6ac6605d
DM
3326 orig = SvPV_nomg(TARG, len);
3327 /* note we don't (yet) force the var into being a string; if we fail
92711104 3328 * to match, we leave as-is; on successful match however, we *will*
6ac6605d 3329 * coerce into a string, then repeat the match */
4499db73 3330 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG))
a0d0e21e 3331 force_on_match = 1;
20be6587
DM
3332
3333 /* only replace once? */
3334 once = !(rpm->op_pmflags & PMf_GLOBAL);
3335
ef07e810 3336 /* See "how taint works" above */
284167a5 3337 if (TAINTING_get) {
20be6587
DM
3338 rxtainted = (
3339 (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0)
196a02af 3340 | (RXp_ISTAINTED(prog) ? SUBST_TAINT_PAT : 0)
20be6587
DM
3341 | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0)
3342 | ((once && !(rpm->op_pmflags & PMf_NONDESTRUCT))
3343 ? SUBST_TAINT_BOOLRET : 0));
3344 TAINT_NOT;
3345 }
a12c0f56 3346
a0d0e21e 3347 force_it:
6ac6605d
DM
3348 if (!pm || !orig)
3349 DIE(aTHX_ "panic: pp_subst, pm=%p, orig=%p", pm, orig);
a0d0e21e 3350
6ac6605d
DM
3351 strend = orig + len;
3352 slen = DO_UTF8(TARG) ? utf8_length((U8*)orig, (U8*)strend) : len;
792b2c16
JH
3353 maxiters = 2 * slen + 10; /* We can match twice at each
3354 position, once with zero-length,
3355 second time with non-zero. */
a0d0e21e 3356
794826f4 3357 /* handle the empty pattern */
196a02af 3358 if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
5585e758
YO
3359 if (PL_curpm == PL_reg_curpm) {
3360 if (PL_curpm_under) {
3361 if (PL_curpm_under == PL_reg_curpm) {
3362 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
3363 } else {
3364 pm = PL_curpm_under;
3365 }
3366 }
3367 } else {
3368 pm = PL_curpm;
3369 }
3370 rx = PM_GETRE(pm);
196a02af 3371 prog = ReANY(rx);
a0d0e21e 3372 }
6502e081 3373
6e240d0b 3374#ifdef PERL_SAWAMPERSAND
196a02af 3375 r_flags = ( RXp_NPARENS(prog)
6502e081 3376 || PL_sawampersand
196a02af 3377 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
5b0e71e9 3378 || (rpm->op_pmflags & PMf_KEEPCOPY)
6502e081
DM
3379 )
3380 ? REXEC_COPY_STR
3381 : 0;
6e240d0b
FC
3382#else
3383 r_flags = REXEC_COPY_STR;
3384#endif
7fba1cd6 3385
0395280b 3386 if (!CALLREGEXEC(rx, orig, strend, orig, 0, TARG, NULL, r_flags))
8b64c330 3387 {
5e79dfb9
DM
3388 SPAGAIN;
3389 PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no);
3390 LEAVE_SCOPE(oldsave);
3391 RETURN;
3392 }
1754320d
FC
3393 PL_curpm = pm;
3394
71be2cbc 3395 /* known replacement string? */
f272994b 3396 if (dstr) {
8514a05a
JH
3397 /* replacement needing upgrading? */
3398 if (DO_UTF8(TARG) && !doutf8) {
db79b45b 3399 nsv = sv_newmortal();
4a176938 3400 SvSetSV(nsv, dstr);
8df0e7a2 3401 sv_utf8_upgrade(nsv);
5c144d81 3402 c = SvPV_const(nsv, clen);
4a176938
JH
3403 doutf8 = TRUE;
3404 }
3405 else {
5c144d81 3406 c = SvPV_const(dstr, clen);
4a176938 3407 doutf8 = DO_UTF8(dstr);
8514a05a 3408 }
bb933b9b
FC
3409
3410 if (SvTAINTED(dstr))
3411 rxtainted |= SUBST_TAINT_REPL;
f272994b
A
3412 }
3413 else {
6136c704 3414 c = NULL;
f272994b
A
3415 doutf8 = FALSE;
3416 }
3417
71be2cbc 3418 /* can do inplace substitution? */
ed252734 3419 if (c
db2c6cb3 3420#ifdef PERL_ANY_COW
106d9a13 3421 && !was_cow
ed252734 3422#endif
196a02af 3423 && (I32)clen <= RXp_MINLENRET(prog)
9cefd268
FC
3424 && ( once
3425 || !(r_flags & REXEC_COPY_STR)
196a02af 3426 || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN))
9cefd268 3427 )
196a02af 3428 && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST)
8ca8a454
NC
3429 && (!doutf8 || SvUTF8(TARG))
3430 && !(rpm->op_pmflags & PMf_NONDESTRUCT))
8b030b38 3431 {
ec911639 3432
db2c6cb3 3433#ifdef PERL_ANY_COW
106d9a13 3434 /* string might have got converted to COW since we set was_cow */
ed252734 3435 if (SvIsCOW(TARG)) {
f7a8268c 3436 if (!force_on_match)
ed252734 3437 goto have_a_cow;
f7a8268c 3438 assert(SvVOK(TARG));
ed252734
NC
3439 }
3440#endif
71be2cbc 3441 if (force_on_match) {
6ac6605d
DM
3442 /* redo the first match, this time with the orig var
3443 * forced into being a string */
71be2cbc 3444 force_on_match = 0;
6ac6605d 3445 orig = SvPV_force_nomg(TARG, len);
71be2cbc
PP
3446 goto force_it;
3447 }
39b40493 3448
71be2cbc 3449 if (once) {
c67ab8f2 3450 char *d, *m;
196a02af 3451 if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
20be6587 3452 rxtainted |= SUBST_TAINT_PAT;
196a02af
DM
3453 m = orig + RXp_OFFS(prog)[0].start;
3454 d = orig + RXp_OFFS(prog)[0].end;
71be2cbc
PP
3455 s = orig;
3456 if (m - s > strend - d) { /* faster to shorten from end */
2ec7214c 3457 I32 i;
71be2cbc
PP
3458 if (clen) {
3459 Copy(c, m, clen, char);
3460 m += clen;
a0d0e21e 3461 }
71be2cbc
PP
3462 i = strend - d;
3463 if (i > 0) {
3464 Move(d, m, i, char);
3465 m += i;
a0d0e21e 3466 }
71be2cbc
PP
3467 *m = '\0';
3468 SvCUR_set(TARG, m - s);
3469 }
2ec7214c
DM
3470 else { /* faster from front */
3471 I32 i = m - s;
71be2cbc 3472 d -= clen;
2ec7214c
DM
3473 if (i > 0)
3474 Move(s, d - i, i, char);
71be2cbc 3475 sv_chop(TARG, d-i);
71be2cbc 3476 if (clen)
c947cd8d 3477 Copy(c, d, clen, char);
71be2cbc 3478 }
8ec5e241 3479 SPAGAIN;
8ca8a454 3480 PUSHs(&PL_sv_yes);
71be2cbc
PP
3481 }
3482 else {
c67ab8f2 3483 char *d, *m;
196a02af 3484 d = s = RXp_OFFS(prog)[0].start + orig;
71be2cbc 3485 do {
2b25edcf 3486 I32 i;
5d9574c1 3487 if (UNLIKELY(iters++ > maxiters))
cea2e8a9 3488 DIE(aTHX_ "Substitution loop");
196a02af
DM
3489 /* run time pattern taint, eg locale */
3490 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
20be6587 3491 rxtainted |= SUBST_TAINT_PAT;
196a02af 3492 m = RXp_OFFS(prog)[0].start + orig;
155aba94 3493 if ((i = m - s)) {
71be2cbc
PP
3494 if (s != d)
3495 Move(s, d, i, char);
3496 d += i;
a0d0e21e 3497 }
71be2cbc
PP
3498 if (clen) {
3499 Copy(c, d, clen, char);
3500 d += clen;
3501 }
196a02af 3502 s = RXp_OFFS(prog)[0].end + orig;
7ce41e5c
FC
3503 } while (CALLREGEXEC(rx, s, strend, orig,
3504 s == m, /* don't match same null twice */
f722798b 3505 TARG, NULL,
d5e7783a 3506 REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
71be2cbc 3507 if (s != d) {
2b25edcf 3508 I32 i = strend - s;
aa07b2f6 3509 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
71be2cbc 3510 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 3511 }
8ec5e241 3512 SPAGAIN;
7b394f12
DM
3513 if (PL_op->op_private & OPpTRUEBOOL)
3514 PUSHs(iters ? &PL_sv_yes : &PL_sv_zero);
3515 else
3516 mPUSHi(iters);
a0d0e21e
LW
3517 }
3518 }
ff6e92e8 3519 else {
1754320d 3520 bool first;
c67ab8f2 3521 char *m;
1754320d 3522 SV *repl;
a0d0e21e 3523 if (force_on_match) {
6ac6605d
DM
3524 /* redo the first match, this time with the orig var
3525 * forced into being a string */
a0d0e21e 3526 force_on_match = 0;
0c1438a1
NC
3527 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
3528 /* I feel that it should be possible to avoid this mortal copy
3529 given that the code below copies into a new destination.
3530 However, I suspect it isn't worth the complexity of
3531 unravelling the C<goto force_it> for the small number of
3532 cases where it would be viable to drop into the copy code. */
3533 TARG = sv_2mortal(newSVsv(TARG));
3534 }
6ac6605d 3535 orig = SvPV_force_nomg(TARG, len);
a0d0e21e
LW
3536 goto force_it;
3537 }
db2c6cb3 3538#ifdef PERL_ANY_COW
ed252734
NC
3539 have_a_cow:
3540#endif
196a02af 3541 if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
20be6587 3542 rxtainted |= SUBST_TAINT_PAT;
1754320d 3543 repl = dstr;
196a02af 3544 s = RXp_OFFS(prog)[0].start + orig;
0395280b
DM
3545 dstr = newSVpvn_flags(orig, s-orig,
3546 SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0));
a0d0e21e 3547 if (!c) {
eb578fdb 3548 PERL_CONTEXT *cx;
8ec5e241 3549 SPAGAIN;
0395280b 3550 m = orig;
20be6587
DM
3551 /* note that a whole bunch of local vars are saved here for
3552 * use by pp_substcont: here's a list of them in case you're
3553 * searching for places in this sub that uses a particular var:
3554 * iters maxiters r_flags oldsave rxtainted orig dstr targ
3555 * s m strend rx once */
490576d1 3556 CX_PUSHSUBST(cx);
20e98b0f 3557 RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot);
a0d0e21e 3558 }
1754320d 3559 first = TRUE;
a0d0e21e 3560 do {
5d9574c1 3561 if (UNLIKELY(iters++ > maxiters))
cea2e8a9 3562 DIE(aTHX_ "Substitution loop");
196a02af 3563 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
20be6587 3564 rxtainted |= SUBST_TAINT_PAT;
196a02af 3565 if (RXp_MATCH_COPIED(prog) && RXp_SUBBEG(prog) != orig) {
c67ab8f2
DM
3566 char *old_s = s;
3567 char *old_orig = orig;
196a02af 3568 assert(RXp_SUBOFFSET(prog) == 0);
c67ab8f2 3569
196a02af 3570 orig = RXp_SUBBEG(prog);
c67ab8f2
DM
3571 s = orig + (old_s - old_orig);
3572 strend = s + (strend - old_s);
a0d0e21e 3573 }
196a02af 3574 m = RXp_OFFS(prog)[0].start + orig;
64534138 3575 sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG));
196a02af 3576 s = RXp_OFFS(prog)[0].end + orig;
1754320d
FC
3577 if (first) {
3578 /* replacement already stringified */
3579 if (clen)
64534138 3580 sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8);
1754320d
FC
3581 first = FALSE;
3582 }
3583 else {
8df0e7a2 3584 sv_catsv(dstr, repl);
5d9574c1 3585 if (UNLIKELY(SvTAINTED(repl)))
bb933b9b 3586 rxtainted |= SUBST_TAINT_REPL;
1754320d 3587 }
a0d0e21e
LW
3588 if (once)
3589 break;
ff27773b
KW
3590 } while (CALLREGEXEC(rx, s, strend, orig,
3591 s == m, /* Yields minend of 0 or 1 */
d5e7783a
DM
3592 TARG, NULL,
3593 REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
aba224f7 3594 assert(strend >= s);
64534138 3595 sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG));
748a9306 3596
8ca8a454
NC
3597 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
3598 /* From here on down we're using the copy, and leaving the original
3599 untouched. */
3600 TARG = dstr;
3601 SPAGAIN;
3602 PUSHs(dstr);
3603 } else {
db2c6cb3 3604#ifdef PERL_ANY_COW
8ca8a454
NC
3605 /* The match may make the string COW. If so, brilliant, because
3606 that's just saved us one malloc, copy and free - the regexp has
3607 donated the old buffer, and we malloc an entirely new one, rather
3608 than the regexp malloc()ing a buffer and copying our original,
3609 only for us to throw it away here during the substitution. */
3610 if (SvIsCOW(TARG)) {
3611 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
3612 } else
ed252734 3613#endif
8ca8a454
NC
3614 {
3615 SvPV_free(TARG);
3616 }
3617 SvPV_set(TARG, SvPVX(dstr));
3618 SvCUR_set(TARG, SvCUR(dstr));
3619 SvLEN_set(TARG, SvLEN(dstr));
64534138 3620 SvFLAGS(TARG) |= SvUTF8(dstr);
8ca8a454 3621 SvPV_set(dstr, NULL);
748a9306 3622
8ca8a454 3623 SPAGAIN;
3c6ef0a5 3624 mPUSHi(iters);
8ca8a454
NC
3625 }
3626 }
3627
3628 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
3629 (void)SvPOK_only_UTF8(TARG);
a0d0e21e 3630 }
20be6587 3631
ef07e810 3632 /* See "how taint works" above */
284167a5 3633 if (TAINTING_get) {
20be6587
DM
3634 if ((rxtainted & SUBST_TAINT_PAT) ||
3635 ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) ==
3636 (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
3637 )
196a02af 3638 (RXp_MATCH_TAINTED_on(prog)); /* taint $1 et al */
20be6587
DM
3639
3640 if (!(rxtainted & SUBST_TAINT_BOOLRET)
3641 && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
3642 )
3643 SvTAINTED_on(TOPs); /* taint return value */
3644 else
3645 SvTAINTED_off(TOPs); /* may have got tainted earlier */
3646
3647 /* needed for mg_set below */
284167a5
SM
3648 TAINT_set(
3649 cBOOL(rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
3650 );
20be6587
DM
3651 SvTAINT(TARG);
3652 }
3653 SvSETMAGIC(TARG); /* PL_tainted must be correctly set for this mg_set */