This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
harmonise S_pushav() and pp_padav()
[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) {
1051 /* XXX see also S_pushav in pp_hot.c */
1052 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
1053 EXTEND(SP, maxarg);
ea710183 1054 if (SvRMAGICAL(TARG)) {
e855b461
DM
1055 SSize_t i;
1056 for (i=0; i < maxarg; i++) {
1057 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
1058 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
1059 }
1060 }
1061 else {
1062 SSize_t i;
1063 for (i=0; i < maxarg; i++) {
1064 SV * const sv = AvARRAY((const AV *)TARG)[i];
1065 SP[i+1] = sv ? sv : &PL_sv_undef;
1066 }
1067 }
1068 SP += maxarg;
1069 }
1070 else if (gimme == G_SCALAR) {
1071 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
1072 if (!maxarg)
1073 PUSHs(&PL_sv_zero);
1074 else if (PL_op->op_private & OPpTRUEBOOL)
1075 PUSHs(&PL_sv_yes);
1076 else
1077 mPUSHi(maxarg);
1078 }
1079 RETURN;
1080}
1081
1082
1083PP(pp_padhv)
1084{
1085 dSP; dTARGET;
1086 U8 gimme;
e855b461
DM
1087
1088 assert(SvTYPE(TARG) == SVt_PVHV);
e855b461
DM
1089 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
1090 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
1091 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
1092
aa36782f
DM
1093 EXTEND(SP, 1);
1094
1095 if (PL_op->op_flags & OPf_REF) {
1096 PUSHs(TARG);
e855b461 1097 RETURN;
aa36782f 1098 }
e855b461
DM
1099 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
1100 const I32 flags = is_lvalue_sub();
1101 if (flags && !(flags & OPpENTERSUB_INARGS)) {
1102 if (GIMME_V == G_SCALAR)
1103 /* diag_listed_as: Can't return %s to lvalue scalar context */
1104 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
aa36782f 1105 PUSHs(TARG);
e855b461
DM
1106 RETURN;
1107 }
1108 }
1109
1110 gimme = GIMME_V;
e855b461 1111
aa36782f 1112 return S_padhv_rv2hv_common(aTHX_ (HV*)TARG, gimme,
e84e4286
DM
1113 cBOOL(PL_op->op_private & OPpPADHV_ISKEYS),
1114 0 /* has_targ*/);
e855b461
DM
1115}
1116
1117
b1c05ba5 1118/* also used for: pp_rv2hv() */
bdaf10a5 1119/* also called directly by pp_lvavref */
b1c05ba5 1120
a0d0e21e
LW
1121PP(pp_rv2av)
1122{
20b7effb 1123 dSP; dTOPss;
1c23e2bd 1124 const U8 gimme = GIMME_V;
13c59d41
MH
1125 static const char an_array[] = "an ARRAY";
1126 static const char a_hash[] = "a HASH";
bdaf10a5
FC
1127 const bool is_pp_rv2av = PL_op->op_type == OP_RV2AV
1128 || PL_op->op_type == OP_LVAVREF;
d83b45b8 1129 const svtype type = is_pp_rv2av ? SVt_PVAV : SVt_PVHV;
a0d0e21e 1130
9026059d 1131 SvGETMAGIC(sv);
a0d0e21e 1132 if (SvROK(sv)) {
5d9574c1 1133 if (UNLIKELY(SvAMAGIC(sv))) {
93d7320b 1134 sv = amagic_deref_call(sv, is_pp_rv2av ? to_av_amg : to_hv_amg);
93d7320b 1135 }
17ab7946 1136 sv = SvRV(sv);
5d9574c1 1137 if (UNLIKELY(SvTYPE(sv) != type))
dcbac5bb 1138 /* diag_listed_as: Not an ARRAY reference */
13c59d41 1139 DIE(aTHX_ "Not %s reference", is_pp_rv2av ? an_array : a_hash);
5d9574c1
DM
1140 else if (UNLIKELY(PL_op->op_flags & OPf_MOD
1141 && PL_op->op_private & OPpLVAL_INTRO))
3da99855 1142 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
a0d0e21e 1143 }
5d9574c1 1144 else if (UNLIKELY(SvTYPE(sv) != type)) {
67955e0c 1145 GV *gv;
1c846c1f 1146
6e592b3a 1147 if (!isGV_with_GP(sv)) {
13c59d41 1148 gv = Perl_softref2xv(aTHX_ sv, is_pp_rv2av ? an_array : a_hash,
dc3c76f8
NC
1149 type, &sp);
1150 if (!gv)
1151 RETURN;
35cd451c
GS
1152 }
1153 else {
159b6efe 1154 gv = MUTABLE_GV(sv);
a0d0e21e 1155 }
ad64d0ec 1156 sv = is_pp_rv2av ? MUTABLE_SV(GvAVn(gv)) : MUTABLE_SV(GvHVn(gv));
533c011a 1157 if (PL_op->op_private & OPpLVAL_INTRO)
ad64d0ec 1158 sv = is_pp_rv2av ? MUTABLE_SV(save_ary(gv)) : MUTABLE_SV(save_hash(gv));
9f527363
FC
1159 }
1160 if (PL_op->op_flags & OPf_REF) {
17ab7946 1161 SETs(sv);
a0d0e21e 1162 RETURN;
9f527363 1163 }
5d9574c1 1164 else if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
40c94d11
FC
1165 const I32 flags = is_lvalue_sub();
1166 if (flags && !(flags & OPpENTERSUB_INARGS)) {
cde874ca 1167 if (gimme != G_ARRAY)
042560a6 1168 goto croak_cant_return;
17ab7946 1169 SETs(sv);
78f9721b 1170 RETURN;
40c94d11 1171 }
a0d0e21e
LW
1172 }
1173
17ab7946 1174 if (is_pp_rv2av) {
502c6561 1175 AV *const av = MUTABLE_AV(sv);
636fe681 1176 /* The guts of pp_rv2av */
96913b52 1177 if (gimme == G_ARRAY) {
d5524600
DM
1178 SP--;
1179 PUTBACK;
1180 S_pushav(aTHX_ av);
1181 SPAGAIN;
1c846c1f 1182 }
96913b52 1183 else if (gimme == G_SCALAR) {
c70927a6 1184 const SSize_t maxarg = AvFILL(av) + 1;
7be75ccf
DM
1185 if (PL_op->op_private & OPpTRUEBOOL)
1186 SETs(maxarg ? &PL_sv_yes : &PL_sv_zero);
1187 else {
1188 dTARGET;
1189 SETi(maxarg);
1190 }
93965878 1191 }
7be75ccf
DM
1192 }
1193 else {
aa36782f
DM
1194 SP--; PUTBACK;
1195 return S_padhv_rv2hv_common(aTHX_ (HV*)sv, gimme,
e84e4286
DM
1196 cBOOL(PL_op->op_private & OPpRV2HV_ISKEYS),
1197 1 /* has_targ*/);
17ab7946 1198 }
be85d344 1199 RETURN;
042560a6
NC
1200
1201 croak_cant_return:
1202 Perl_croak(aTHX_ "Can't return %s to lvalue scalar context",
1203 is_pp_rv2av ? "array" : "hash");
77e217c6 1204 RETURN;
a0d0e21e
LW
1205}
1206
10c8fecd 1207STATIC void
fb8f4cf8 1208S_do_oddball(pTHX_ SV **oddkey, SV **firstkey)
10c8fecd 1209{
7918f24d
NC
1210 PERL_ARGS_ASSERT_DO_ODDBALL;
1211
fb8f4cf8 1212 if (*oddkey) {
6d822dc4 1213 if (ckWARN(WARN_MISC)) {
a3b680e6 1214 const char *err;
fb8f4cf8
RZ
1215 if (oddkey == firstkey &&
1216 SvROK(*oddkey) &&
1217 (SvTYPE(SvRV(*oddkey)) == SVt_PVAV ||
1218 SvTYPE(SvRV(*oddkey)) == SVt_PVHV))
10c8fecd 1219 {
a3b680e6 1220 err = "Reference found where even-sized list expected";
10c8fecd
GS
1221 }
1222 else
a3b680e6 1223 err = "Odd number of elements in hash assignment";
f1f66076 1224 Perl_warner(aTHX_ packWARN(WARN_MISC), "%s", err);
10c8fecd 1225 }
6d822dc4 1226
10c8fecd
GS
1227 }
1228}
1229
a5f48505
DM
1230
1231/* Do a mark and sweep with the SVf_BREAK flag to detect elements which
1232 * are common to both the LHS and RHS of an aassign, and replace them
1233 * with copies. All these copies are made before the actual list assign is
1234 * done.
1235 *
1236 * For example in ($a,$b) = ($b,$a), assigning the value of the first RHS
1237 * element ($b) to the first LH element ($a), modifies $a; when the
1238 * second assignment is done, the second RH element now has the wrong
1239 * value. So we initially replace the RHS with ($b, mortalcopy($a)).
1240 * Note that we don't need to make a mortal copy of $b.
1241 *
1242 * The algorithm below works by, for every RHS element, mark the
1243 * corresponding LHS target element with SVf_BREAK. Then if the RHS
1244 * element is found with SVf_BREAK set, it means it would have been
1245 * modified, so make a copy.
1246 * Note that by scanning both LHS and RHS in lockstep, we avoid
1247 * unnecessary copies (like $b above) compared with a naive
1248 * "mark all LHS; copy all marked RHS; unmark all LHS".
1249 *
1250 * If the LHS element is a 'my' declaration' and has a refcount of 1, then
1251 * it can't be common and can be skipped.
ebc643ce
DM
1252 *
1253 * On DEBUGGING builds it takes an extra boolean, fake. If true, it means
1254 * that we thought we didn't need to call S_aassign_copy_common(), but we
1255 * have anyway for sanity checking. If we find we need to copy, then panic.
a5f48505
DM
1256 */
1257
1258PERL_STATIC_INLINE void
1259S_aassign_copy_common(pTHX_ SV **firstlelem, SV **lastlelem,
ebc643ce
DM
1260 SV **firstrelem, SV **lastrelem
1261#ifdef DEBUGGING
1262 , bool fake
1263#endif
1264)
a5f48505
DM
1265{
1266 dVAR;
1267 SV **relem;
1268 SV **lelem;
1269 SSize_t lcount = lastlelem - firstlelem + 1;
1270 bool marked = FALSE; /* have we marked any LHS with SVf_BREAK ? */
1271 bool const do_rc1 = cBOOL(PL_op->op_private & OPpASSIGN_COMMON_RC1);
beb08a1e 1272 bool copy_all = FALSE;
a5f48505
DM
1273
1274 assert(!PL_in_clean_all); /* SVf_BREAK not already in use */
1275 assert(firstlelem < lastlelem); /* at least 2 LH elements */
1276 assert(firstrelem < lastrelem); /* at least 2 RH elements */
1277
ebc643ce
DM
1278
1279 lelem = firstlelem;
a5f48505
DM
1280 /* we never have to copy the first RH element; it can't be corrupted
1281 * by assigning something to the corresponding first LH element.
1282 * So this scan does in a loop: mark LHS[N]; test RHS[N+1]
1283 */
ebc643ce 1284 relem = firstrelem + 1;
a5f48505
DM
1285
1286 for (; relem <= lastrelem; relem++) {
1287 SV *svr;
1288
1289 /* mark next LH element */
1290
1291 if (--lcount >= 0) {
1292 SV *svl = *lelem++;
1293
1294 if (UNLIKELY(!svl)) {/* skip AV alias marker */
1295 assert (lelem <= lastlelem);
1296 svl = *lelem++;
1297 lcount--;
1298 }
1299
1300 assert(svl);
beb08a1e
TC
1301 if (SvSMAGICAL(svl)) {
1302 copy_all = TRUE;
1303 }
a5f48505
DM
1304 if (SvTYPE(svl) == SVt_PVAV || SvTYPE(svl) == SVt_PVHV) {
1305 if (!marked)
1306 return;
1307 /* this LH element will consume all further args;
1308 * no need to mark any further LH elements (if any).
1309 * But we still need to scan any remaining RHS elements;
1310 * set lcount negative to distinguish from lcount == 0,
1311 * so the loop condition continues being true
1312 */
1313 lcount = -1;
1314 lelem--; /* no need to unmark this element */
1315 }
94a5f659 1316 else if (!(do_rc1 && SvREFCNT(svl) == 1) && !SvIMMORTAL(svl)) {
a5f48505
DM
1317 SvFLAGS(svl) |= SVf_BREAK;
1318 marked = TRUE;
1319 }
1320 else if (!marked) {
1321 /* don't check RH element if no SVf_BREAK flags set yet */
1322 if (!lcount)
1323 break;
1324 continue;
1325 }
1326 }
1327
1328 /* see if corresponding RH element needs copying */
1329
1330 assert(marked);
1331 svr = *relem;
1332 assert(svr);
1333
5c1db569 1334 if (UNLIKELY(SvFLAGS(svr) & (SVf_BREAK|SVs_GMG) || copy_all)) {
1050723f 1335 U32 brk = (SvFLAGS(svr) & SVf_BREAK);
a5f48505 1336
ebc643ce
DM
1337#ifdef DEBUGGING
1338 if (fake) {
9ae0115f 1339 /* op_dump(PL_op); */
ebc643ce
DM
1340 Perl_croak(aTHX_
1341 "panic: aassign skipped needed copy of common RH elem %"
1342 UVuf, (UV)(relem - firstrelem));
1343 }
1344#endif
1345
a5f48505
DM
1346 TAINT_NOT; /* Each item is independent */
1347
1348 /* Dear TODO test in t/op/sort.t, I love you.
1349 (It's relying on a panic, not a "semi-panic" from newSVsv()
1350 and then an assertion failure below.) */
1351 if (UNLIKELY(SvIS_FREED(svr))) {
1352 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p",
1353 (void*)svr);
1354 }
1355 /* avoid break flag while copying; otherwise COW etc
1356 * disabled... */
1357 SvFLAGS(svr) &= ~SVf_BREAK;
1358 /* Not newSVsv(), as it does not allow copy-on-write,
8c1e192f
DM
1359 resulting in wasteful copies.
1360 Also, we use SV_NOSTEAL in case the SV is used more than
1361 once, e.g. (...) = (f())[0,0]
1362 Where the same SV appears twice on the RHS without a ref
1363 count bump. (Although I suspect that the SV won't be
1364 stealable here anyway - DAPM).
1365 */
a5f48505
DM
1366 *relem = sv_mortalcopy_flags(svr,
1367 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
1368 /* ... but restore afterwards in case it's needed again,
1369 * e.g. ($a,$b,$c) = (1,$a,$a)
1370 */
1050723f 1371 SvFLAGS(svr) |= brk;
a5f48505
DM
1372 }
1373
1374 if (!lcount)
1375 break;
1376 }
1377
1378 if (!marked)
1379 return;
1380
1381 /*unmark LHS */
1382
1383 while (lelem > firstlelem) {
1384 SV * const svl = *(--lelem);
1385 if (svl)
1386 SvFLAGS(svl) &= ~SVf_BREAK;
1387 }
1388}
1389
1390
1391
a0d0e21e
LW
1392PP(pp_aassign)
1393{
27da23d5 1394 dVAR; dSP;
3280af22
NIS
1395 SV **lastlelem = PL_stack_sp;
1396 SV **lastrelem = PL_stack_base + POPMARK;
1397 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
1398 SV **firstlelem = lastrelem + 1;
1399
eb578fdb
KW
1400 SV **relem;
1401 SV **lelem;
1c23e2bd 1402 U8 gimme;
a68090fe
DM
1403 /* PL_delaymagic is restored by JUMPENV_POP on dieing, so we
1404 * only need to save locally, not on the save stack */
1405 U16 old_delaymagic = PL_delaymagic;
ebc643ce
DM
1406#ifdef DEBUGGING
1407 bool fake = 0;
1408#endif
5637b936 1409
3280af22 1410 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
a0d0e21e
LW
1411
1412 /* If there's a common identifier on both sides we have to take
1413 * special care that assigning the identifier on the left doesn't
1414 * clobber a value on the right that's used later in the list.
1415 */
acdea6f0 1416
beb08a1e
TC
1417 /* at least 2 LH and RH elements, or commonality isn't an issue */
1418 if (firstlelem < lastlelem && firstrelem < lastrelem) {
5c1db569
TC
1419 for (relem = firstrelem+1; relem <= lastrelem; relem++) {
1420 if (SvGMAGICAL(*relem))
1421 goto do_scan;
1422 }
beb08a1e
TC
1423 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
1424 if (*lelem && SvSMAGICAL(*lelem))
1425 goto do_scan;
a5f48505 1426 }
beb08a1e
TC
1427 if ( PL_op->op_private & (OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1) ) {
1428 if (PL_op->op_private & OPpASSIGN_COMMON_RC1) {
1429 /* skip the scan if all scalars have a ref count of 1 */
1430 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
8b0c3377 1431 SV *sv = *lelem;
beb08a1e
TC
1432 if (!sv || SvREFCNT(sv) == 1)
1433 continue;
1434 if (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVAV)
1435 goto do_scan;
1436 break;
1437 }
1438 }
1439 else {
1440 do_scan:
1441 S_aassign_copy_common(aTHX_
1442 firstlelem, lastlelem, firstrelem, lastrelem
ebc643ce 1443#ifdef DEBUGGING
beb08a1e 1444 , fake
ebc643ce 1445#endif
beb08a1e
TC
1446 );
1447 }
a5f48505 1448 }
a0d0e21e 1449 }
ebc643ce
DM
1450#ifdef DEBUGGING
1451 else {
1452 /* on debugging builds, do the scan even if we've concluded we
1453 * don't need to, then panic if we find commonality. Note that the
1454 * scanner assumes at least 2 elements */
1455 if (firstlelem < lastlelem && firstrelem < lastrelem) {
1456 fake = 1;
1457 goto do_scan;
1458 }
1459 }
1460#endif
a0d0e21e 1461
a5f48505 1462 gimme = GIMME_V;
a0d0e21e
LW
1463 relem = firstrelem;
1464 lelem = firstlelem;
10c8fecd 1465
8b0c3377
DM
1466 if (relem > lastrelem)
1467 goto no_relems;
1468
1469 /* first lelem loop while there are still relems */
5d9574c1 1470 while (LIKELY(lelem <= lastlelem)) {
bdaf10a5 1471 bool alias = FALSE;
8b0c3377
DM
1472 SV *lsv = *lelem++;
1473
c73f612f
DM
1474 TAINT_NOT; /* Each item stands on its own, taintwise. */
1475
8b0c3377
DM
1476 assert(relem <= lastrelem);
1477 if (UNLIKELY(!lsv)) {
bdaf10a5 1478 alias = TRUE;
8b0c3377
DM
1479 lsv = *lelem++;
1480 ASSUME(SvTYPE(lsv) == SVt_PVAV);
bdaf10a5 1481 }
a5f48505 1482
8b0c3377
DM
1483 switch (SvTYPE(lsv)) {
1484 case SVt_PVAV: {
1485 SV **svp;
1486 SSize_t i;
1487 SSize_t tmps_base;
1488 SSize_t nelems = lastrelem - relem + 1;
b09ed995 1489 AV *ary = MUTABLE_AV(lsv);
8b0c3377
DM
1490
1491 /* Assigning to an aggregate is tricky. First there is the
1492 * issue of commonality, e.g. @a = ($a[0]). Since the
1493 * stack isn't refcounted, clearing @a prior to storing
1494 * elements will free $a[0]. Similarly with
1495 * sub FETCH { $status[$_[1]] } @status = @tied[0,1];
1496 *
1497 * The way to avoid these issues is to make the copy of each
1498 * SV (and we normally store a *copy* in the array) *before*
1499 * clearing the array. But this has a problem in that
1500 * if the code croaks during copying, the not-yet-stored copies
1501 * could leak. One way to avoid this is to make all the copies
1502 * mortal, but that's quite expensive.
1503 *
1504 * The current solution to these issues is to use a chunk
1505 * of the tmps stack as a temporary refcounted-stack. SVs
1506 * will be put on there during processing to avoid leaks,
1507 * but will be removed again before the end of this block,
1508 * so free_tmps() is never normally called. Also, the
1509 * sv_refcnt of the SVs doesn't have to be manipulated, since
1510 * the ownership of 1 reference count is transferred directly
1511 * from the tmps stack to the AV when the SV is stored.
1512 *
1513 * We disarm slots in the temps stack by storing PL_sv_undef
1514 * there: it doesn't matter if that SV's refcount is
1515 * repeatedly decremented during a croak. But usually this is
1516 * only an interim measure. By the end of this code block
1517 * we try where possible to not leave any PL_sv_undef's on the
1518 * tmps stack e.g. by shuffling newer entries down.
1519 *
1520 * There is one case where we don't copy: non-magical
1521 * SvTEMP(sv)'s with a ref count of 1. The only owner of these
1522 * is on the tmps stack, so its safe to directly steal the SV
1523 * rather than copying. This is common in things like function
1524 * returns, map etc, which all return a list of such SVs.
1525 *
1526 * Note however something like @a = (f())[0,0], where there is
1527 * a danger of the same SV being shared: this avoided because
1528 * when the SV is stored as $a[0], its ref count gets bumped,
1529 * so the RC==1 test fails and the second element is copied
1530 * instead.
1531 *
1532 * We also use one slot in the tmps stack to hold an extra
1533 * ref to the array, to ensure it doesn't get prematurely
1534 * freed. Again, this is removed before the end of this block.
1535 *
1536 * Note that OPpASSIGN_COMMON_AGG is used to flag a possible
1537 * @a = ($a[0]) case, but the current implementation uses the
1538 * same algorithm regardless, so ignores that flag. (It *is*
1539 * used in the hash branch below, however).
1540 */
1541
1542 /* Reserve slots for ary, plus the elems we're about to copy,
1543 * then protect ary and temporarily void the remaining slots
1544 * with &PL_sv_undef */
1545 EXTEND_MORTAL(nelems + 1);
1546 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(ary);
1547 tmps_base = PL_tmps_ix + 1;
1548 for (i = 0; i < nelems; i++)
1549 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1550 PL_tmps_ix += nelems;
1551
1552 /* Make a copy of each RHS elem and save on the tmps_stack
1553 * (or pass through where we can optimise away the copy) */
1554
1555 if (UNLIKELY(alias)) {
1556 U32 lval = (gimme == G_ARRAY)
1557 ? (PL_op->op_flags & OPf_MOD || LVRET) : 0;
a5f48505 1558 for (svp = relem; svp <= lastrelem; svp++) {
8b0c3377
DM
1559 SV *rsv = *svp;
1560
1561 SvGETMAGIC(rsv);
1562 if (!SvROK(rsv))
1563 DIE(aTHX_ "Assigned value is not a reference");
1564 if (SvTYPE(SvRV(rsv)) > SVt_PVLV)
1565 /* diag_listed_as: Assigned value is not %s reference */
1566 DIE(aTHX_
1567 "Assigned value is not a SCALAR reference");
1568 if (lval)
1569 *svp = rsv = sv_mortalcopy(rsv);
1570 /* XXX else check for weak refs? */
1571 rsv = SvREFCNT_inc_NN(SvRV(rsv));
1572 assert(tmps_base <= PL_tmps_max);
1573 PL_tmps_stack[tmps_base++] = rsv;
a5f48505 1574 }
a5f48505 1575 }
8b0c3377
DM
1576 else {
1577 for (svp = relem; svp <= lastrelem; svp++) {
1578 SV *rsv = *svp;
a5f48505 1579
8b0c3377
DM
1580 if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) {
1581 /* can skip the copy */
1582 SvREFCNT_inc_simple_void_NN(rsv);
1583 SvTEMP_off(rsv);
1584 }
a5f48505 1585 else {
8b0c3377
DM
1586 SV *nsv;
1587 /* do get before newSV, in case it dies and leaks */
1588 SvGETMAGIC(rsv);
1589 nsv = newSV(0);
8c1e192f
DM
1590 /* see comment in S_aassign_copy_common about
1591 * SV_NOSTEAL */
8b0c3377
DM
1592 sv_setsv_flags(nsv, rsv,
1593 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
1594 rsv = *svp = nsv;
a5f48505 1595 }
8b0c3377
DM
1596
1597 assert(tmps_base <= PL_tmps_max);
1598 PL_tmps_stack[tmps_base++] = rsv;
1599 }
1600 }
1601
1602 if (SvRMAGICAL(ary) || AvFILLp(ary) >= 0) /* may be non-empty */
1603 av_clear(ary);
1604
1605 /* store in the array, the SVs that are in the tmps stack */
1606
1607 tmps_base -= nelems;
1608
80c1439f 1609 if (SvMAGICAL(ary) || SvREADONLY(ary) || !AvREAL(ary)) {
8b0c3377
DM
1610 /* for arrays we can't cheat with, use the official API */
1611 av_extend(ary, nelems - 1);
1612 for (i = 0; i < nelems; i++) {
1613 SV **svp = &(PL_tmps_stack[tmps_base + i]);
1614 SV *rsv = *svp;
1615 /* A tied store won't take ownership of rsv, so keep
1616 * the 1 refcnt on the tmps stack; otherwise disarm
1617 * the tmps stack entry */
1618 if (av_store(ary, i, rsv))
1619 *svp = &PL_sv_undef;
1620 /* av_store() may have added set magic to rsv */;
1621 SvSETMAGIC(rsv);
1622 }
1623 /* disarm ary refcount: see comments below about leak */
1624 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
1625 }
1626 else {
1627 /* directly access/set the guts of the AV */
1628 SSize_t fill = nelems - 1;
1629 if (fill > AvMAX(ary))
1630 av_extend_guts(ary, fill, &AvMAX(ary), &AvALLOC(ary),
1631 &AvARRAY(ary));
1632 AvFILLp(ary) = fill;
1633 Copy(&(PL_tmps_stack[tmps_base]), AvARRAY(ary), nelems, SV*);
1634 /* Quietly remove all the SVs from the tmps stack slots,
1635 * since ary has now taken ownership of the refcnt.
1636 * Also remove ary: which will now leak if we die before
1637 * the SvREFCNT_dec_NN(ary) below */
1638 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
1639 Move(&PL_tmps_stack[tmps_base + nelems],
1640 &PL_tmps_stack[tmps_base - 1],
1641 PL_tmps_ix - (tmps_base + nelems) + 1,
1642 SV*);
1643 PL_tmps_ix -= (nelems + 1);
1644 }
1645
5d9574c1 1646 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
8b0c3377 1647 /* its assumed @ISA set magic can't die and leak ary */
ad64d0ec 1648 SvSETMAGIC(MUTABLE_SV(ary));
8b0c3377
DM
1649 SvREFCNT_dec_NN(ary);
1650
1651 relem = lastrelem + 1;
1652 goto no_relems;
a5f48505
DM
1653 }
1654
10c8fecd 1655 case SVt_PVHV: { /* normal hash */
8b0c3377
DM
1656
1657 SV **svp;
1658 bool dirty_tmps;
1659 SSize_t i;
1660 SSize_t tmps_base;
1661 SSize_t nelems = lastrelem - relem + 1;
b09ed995 1662 HV *hash = MUTABLE_HV(lsv);
8b0c3377
DM
1663
1664 if (UNLIKELY(nelems & 1)) {
1665 do_oddball(lastrelem, relem);
1666 /* we have firstlelem to reuse, it's not needed any more */
1667 *++lastrelem = &PL_sv_undef;
1668 nelems++;
1669 }
1670
1671 /* See the SVt_PVAV branch above for a long description of
1672 * how the following all works. The main difference for hashes
1673 * is that we treat keys and values separately (and have
1674 * separate loops for them): as for arrays, values are always
1675 * copied (except for the SvTEMP optimisation), since they
1676 * need to be stored in the hash; while keys are only
1677 * processed where they might get prematurely freed or
1678 * whatever. */
1679
1680 /* tmps stack slots:
1681 * * reserve a slot for the hash keepalive;
1682 * * reserve slots for the hash values we're about to copy;
1683 * * preallocate for the keys we'll possibly copy or refcount bump
1684 * later;
1685 * then protect hash and temporarily void the remaining
1686 * value slots with &PL_sv_undef */
1687 EXTEND_MORTAL(nelems + 1);
1688
1689 /* convert to number of key/value pairs */
1690 nelems >>= 1;
1691
1692 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple_NN(hash);
1693 tmps_base = PL_tmps_ix + 1;
1694 for (i = 0; i < nelems; i++)
1695 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1696 PL_tmps_ix += nelems;
1697
1698 /* Make a copy of each RHS hash value and save on the tmps_stack
1699 * (or pass through where we can optimise away the copy) */
1700
1701 for (svp = relem + 1; svp <= lastrelem; svp += 2) {
1702 SV *rsv = *svp;
1703
1704 if (SvTEMP(rsv) && !SvGMAGICAL(rsv) && SvREFCNT(rsv) == 1) {
1705 /* can skip the copy */
1706 SvREFCNT_inc_simple_void_NN(rsv);
1707 SvTEMP_off(rsv);
1708 }
1709 else {
1710 SV *nsv;
1711 /* do get before newSV, in case it dies and leaks */
1712 SvGETMAGIC(rsv);
1713 nsv = newSV(0);
1714 /* see comment in S_aassign_copy_common about
1715 * SV_NOSTEAL */
1716 sv_setsv_flags(nsv, rsv,
1717 (SV_DO_COW_SVSETSV|SV_NOSTEAL));
1718 rsv = *svp = nsv;
1c4ea384
RZ
1719 }
1720
8b0c3377
DM
1721 assert(tmps_base <= PL_tmps_max);
1722 PL_tmps_stack[tmps_base++] = rsv;
1723 }
1724 tmps_base -= nelems;
a5f48505 1725
a5f48505 1726
8b0c3377
DM
1727 /* possibly protect keys */
1728
1729 if (UNLIKELY(gimme == G_ARRAY)) {
1730 /* handle e.g.
1731 * @a = ((%h = ($$r, 1)), $r = "x");
1732 * $_++ for %h = (1,2,3,4);
1733 */
1734 EXTEND_MORTAL(nelems);
1735 for (svp = relem; svp <= lastrelem; svp += 2)
1736 *svp = sv_mortalcopy_flags(*svp,
1737 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
1738 }
1739 else if (PL_op->op_private & OPpASSIGN_COMMON_AGG) {
1740 /* for possible commonality, e.g.
1741 * %h = ($h{a},1)
1742 * avoid premature freeing RHS keys by mortalising
1743 * them.
1744 * For a magic element, make a copy so that its magic is
1745 * called *before* the hash is emptied (which may affect
1746 * a tied value for example).
1747 * In theory we should check for magic keys in all
1748 * cases, not just under OPpASSIGN_COMMON_AGG, but in
1749 * practice, !OPpASSIGN_COMMON_AGG implies only
1750 * constants or padtmps on the RHS.
1751 */
1752 EXTEND_MORTAL(nelems);
1753 for (svp = relem; svp <= lastrelem; svp += 2) {
1754 SV *rsv = *svp;
1755 if (UNLIKELY(SvGMAGICAL(rsv))) {
1756 SSize_t n;
a5f48505
DM
1757 *svp = sv_mortalcopy_flags(*svp,
1758 SV_GMAGIC|SV_DO_COW_SVSETSV|SV_NOSTEAL);
8b0c3377
DM
1759 /* allow other branch to continue pushing
1760 * onto tmps stack without checking each time */
1761 n = (lastrelem - relem) >> 1;
1762 EXTEND_MORTAL(n);
a5f48505 1763 }
8b0c3377
DM
1764 else
1765 PL_tmps_stack[++PL_tmps_ix] =
1766 SvREFCNT_inc_simple_NN(rsv);
a5f48505 1767 }
8b0c3377 1768 }
a5f48505 1769
8b0c3377
DM
1770 if (SvRMAGICAL(hash) || HvUSEDKEYS(hash))
1771 hv_clear(hash);
a5f48505 1772
8b0c3377
DM
1773 /* now assign the keys and values to the hash */
1774
1775 dirty_tmps = FALSE;
1776
1777 if (UNLIKELY(gimme == G_ARRAY)) {
1778 /* @a = (%h = (...)) etc */
1779 SV **svp;
1780 SV **topelem = relem;
1781
1782 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
1783 SV *key = *svp++;
1784 SV *val = *svp;
1785 /* remove duplicates from list we return */
1786 if (!hv_exists_ent(hash, key, 0)) {
1787 /* copy key back: possibly to an earlier
1788 * stack location if we encountered dups earlier,
1789 * The values will be updated later
1790 */
1791 *topelem = key;
1792 topelem += 2;
632b9d6f 1793 }
8b0c3377
DM
1794 /* A tied store won't take ownership of val, so keep
1795 * the 1 refcnt on the tmps stack; otherwise disarm
1796 * the tmps stack entry */
1797 if (hv_store_ent(hash, key, val, 0))
1798 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1799 else
1800 dirty_tmps = TRUE;
1801 /* hv_store_ent() may have added set magic to val */;
1802 SvSETMAGIC(val);
1803 }
1804 if (topelem < svp) {
1c4ea384
RZ
1805 /* at this point we have removed the duplicate key/value
1806 * pairs from the stack, but the remaining values may be
1807 * wrong; i.e. with (a 1 a 2 b 3) on the stack we've removed
1808 * the (a 2), but the stack now probably contains
1809 * (a <freed> b 3), because { hv_save(a,1); hv_save(a,2) }
1810 * obliterates the earlier key. So refresh all values. */
8b0c3377
DM
1811 lastrelem = topelem - 1;
1812 while (relem < lastrelem) {
1c4ea384
RZ
1813 HE *he;
1814 he = hv_fetch_ent(hash, *relem++, 0, 0);
1815 *relem++ = (he ? HeVAL(he) : &PL_sv_undef);
1816 }
1817 }
8b0c3377
DM
1818 }
1819 else {
1820 SV **svp;
1821 for (i = 0, svp = relem; svp <= lastrelem; i++, svp++) {
1822 SV *key = *svp++;
1823 SV *val = *svp;
1824 if (hv_store_ent(hash, key, val, 0))
1825 PL_tmps_stack[tmps_base + i] = &PL_sv_undef;
1826 else
1827 dirty_tmps = TRUE;
1828 /* hv_store_ent() may have added set magic to val */;
1829 SvSETMAGIC(val);
1830 }
1831 }
1832
1833 if (dirty_tmps) {
1834 /* there are still some 'live' recounts on the tmps stack
1835 * - usually caused by storing into a tied hash. So let
1836 * free_tmps() do the proper but slow job later.
1837 * Just disarm hash refcount: see comments below about leak
1838 */
1839 PL_tmps_stack[tmps_base - 1] = &PL_sv_undef;
1840 }
1841 else {
1842 /* Quietly remove all the SVs from the tmps stack slots,
1843 * since hash has now taken ownership of the refcnt.
1844 * Also remove hash: which will now leak if we die before
1845 * the SvREFCNT_dec_NN(hash) below */
1846 if (UNLIKELY(PL_tmps_ix >= tmps_base + nelems))
1847 Move(&PL_tmps_stack[tmps_base + nelems],
1848 &PL_tmps_stack[tmps_base - 1],
1849 PL_tmps_ix - (tmps_base + nelems) + 1,
1850 SV*);
1851 PL_tmps_ix -= (nelems + 1);
1852 }
1853
1854 SvREFCNT_dec_NN(hash);
1855
1856 relem = lastrelem + 1;
1857 goto no_relems;
1858 }
1859
a0d0e21e 1860 default:
8b0c3377 1861 if (!SvIMMORTAL(lsv)) {
d24e3eb1
DM
1862 SV *ref;
1863
8b0c3377
DM
1864 if (UNLIKELY(
1865 SvTEMP(lsv) && !SvSMAGICAL(lsv) && SvREFCNT(lsv) == 1 &&
1866 (!isGV_with_GP(lsv) || SvFAKE(lsv)) && ckWARN(WARN_MISC)
1867 ))
1868 Perl_warner(aTHX_
1869 packWARN(WARN_MISC),
1870 "Useless assignment to a temporary"
1871 );
d24e3eb1
DM
1872
1873 /* avoid freeing $$lsv if it might be needed for further
1874 * elements, e.g. ($ref, $foo) = (1, $$ref) */
1875 if ( SvROK(lsv)
1876 && ( ((ref = SvRV(lsv)), SvREFCNT(ref)) == 1)
1877 && lelem <= lastlelem
1878 ) {
1879 SSize_t ix;
1880 SvREFCNT_inc_simple_void_NN(ref);
1881 /* an unrolled sv_2mortal */
1882 ix = ++PL_tmps_ix;
1883 if (UNLIKELY(ix >= PL_tmps_max))
1884 /* speculatively grow enough to cover other
1885 * possible refs */
67c3640a 1886 (void)tmps_grow_p(ix + (lastlelem - lelem));
d24e3eb1
DM
1887 PL_tmps_stack[ix] = ref;
1888 }
1889
8b0c3377
DM
1890 sv_setsv(lsv, *relem);
1891 *relem = lsv;
1892 SvSETMAGIC(lsv);
1893 }
1894 if (++relem > lastrelem)
1895 goto no_relems;
a0d0e21e 1896 break;
8b0c3377
DM
1897 } /* switch */
1898 } /* while */
1899
1900
1901 no_relems:
1902
1903 /* simplified lelem loop for when there are no relems left */
1904 while (LIKELY(lelem <= lastlelem)) {
1905 SV *lsv = *lelem++;
c73f612f
DM
1906
1907 TAINT_NOT; /* Each item stands on its own, taintwise. */
1908
8b0c3377
DM
1909 if (UNLIKELY(!lsv)) {
1910 lsv = *lelem++;
1911 ASSUME(SvTYPE(lsv) == SVt_PVAV);
a0d0e21e 1912 }
8b0c3377
DM
1913
1914 switch (SvTYPE(lsv)) {
1915 case SVt_PVAV:
b09ed995
DM
1916 if (SvRMAGICAL(lsv) || AvFILLp((SV*)lsv) >= 0) {
1917 av_clear((AV*)lsv);
8b0c3377 1918 if (UNLIKELY(PL_delaymagic & DM_ARRAY_ISA))
b09ed995 1919 SvSETMAGIC(lsv);
8b0c3377
DM
1920 }
1921 break;
1922
1923 case SVt_PVHV:
b09ed995
DM
1924 if (SvRMAGICAL(lsv) || HvUSEDKEYS((HV*)lsv))
1925 hv_clear((HV*)lsv);
8b0c3377
DM
1926 break;
1927
1928 default:
1929 if (!SvIMMORTAL(lsv)) {
e03e82a0 1930 sv_set_undef(lsv);
8b0c3377 1931 SvSETMAGIC(lsv);
b09ed995 1932 *relem++ = lsv;
8b0c3377
DM
1933 }
1934 break;
1935 } /* switch */
1936 } /* while */
1937
c73f612f
DM
1938 TAINT_NOT; /* result of list assign isn't tainted */
1939
5d9574c1 1940 if (UNLIKELY(PL_delaymagic & ~DM_DELAY)) {
985213f2 1941 /* Will be used to set PL_tainting below */
dfff4baf
BF
1942 Uid_t tmp_uid = PerlProc_getuid();
1943 Uid_t tmp_euid = PerlProc_geteuid();
1944 Gid_t tmp_gid = PerlProc_getgid();
1945 Gid_t tmp_egid = PerlProc_getegid();
985213f2 1946
b469f1e0 1947 /* XXX $> et al currently silently ignore failures */
3280af22 1948 if (PL_delaymagic & DM_UID) {
a0d0e21e 1949#ifdef HAS_SETRESUID
b469f1e0
JH
1950 PERL_UNUSED_RESULT(
1951 setresuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
1952 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1,
1953 (Uid_t)-1));
56febc5e
AD
1954#else
1955# ifdef HAS_SETREUID
b469f1e0
JH
1956 PERL_UNUSED_RESULT(
1957 setreuid((PL_delaymagic & DM_RUID) ? PL_delaymagic_uid : (Uid_t)-1,
1958 (PL_delaymagic & DM_EUID) ? PL_delaymagic_euid : (Uid_t)-1));
56febc5e
AD
1959# else
1960# ifdef HAS_SETRUID
b28d0864 1961 if ((PL_delaymagic & DM_UID) == DM_RUID) {
b469f1e0 1962 PERL_UNUSED_RESULT(setruid(PL_delaymagic_uid));
b28d0864 1963 PL_delaymagic &= ~DM_RUID;
a0d0e21e 1964 }
56febc5e
AD
1965# endif /* HAS_SETRUID */
1966# ifdef HAS_SETEUID
b28d0864 1967 if ((PL_delaymagic & DM_UID) == DM_EUID) {
b469f1e0 1968 PERL_UNUSED_RESULT(seteuid(PL_delaymagic_euid));
b28d0864 1969 PL_delaymagic &= ~DM_EUID;
a0d0e21e 1970 }
56febc5e 1971# endif /* HAS_SETEUID */
b28d0864 1972 if (PL_delaymagic & DM_UID) {
985213f2 1973 if (PL_delaymagic_uid != PL_delaymagic_euid)
cea2e8a9 1974 DIE(aTHX_ "No setreuid available");
b469f1e0 1975 PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
a0d0e21e 1976 }
56febc5e
AD
1977# endif /* HAS_SETREUID */
1978#endif /* HAS_SETRESUID */
04783dc7 1979
985213f2
AB
1980 tmp_uid = PerlProc_getuid();
1981 tmp_euid = PerlProc_geteuid();
a0d0e21e 1982 }
b469f1e0 1983 /* XXX $> et al currently silently ignore failures */
3280af22 1984 if (PL_delaymagic & DM_GID) {
a0d0e21e 1985#ifdef HAS_SETRESGID
b469f1e0
JH
1986 PERL_UNUSED_RESULT(
1987 setresgid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
1988 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1,
1989 (Gid_t)-1));
56febc5e
AD
1990#else
1991# ifdef HAS_SETREGID
b469f1e0
JH
1992 PERL_UNUSED_RESULT(
1993 setregid((PL_delaymagic & DM_RGID) ? PL_delaymagic_gid : (Gid_t)-1,
1994 (PL_delaymagic & DM_EGID) ? PL_delaymagic_egid : (Gid_t)-1));
56febc5e
AD
1995# else
1996# ifdef HAS_SETRGID
b28d0864 1997 if ((PL_delaymagic & DM_GID) == DM_RGID) {
b469f1e0 1998 PERL_UNUSED_RESULT(setrgid(PL_delaymagic_gid));
b28d0864 1999 PL_delaymagic &= ~DM_RGID;
a0d0e21e 2000 }
56febc5e
AD
2001# endif /* HAS_SETRGID */
2002# ifdef HAS_SETEGID
b28d0864 2003 if ((PL_delaymagic & DM_GID) == DM_EGID) {
b469f1e0 2004 PERL_UNUSED_RESULT(setegid(PL_delaymagic_egid));
b28d0864 2005 PL_delaymagic &= ~DM_EGID;
a0d0e21e 2006 }
56febc5e 2007# endif /* HAS_SETEGID */
b28d0864 2008 if (PL_delaymagic & DM_GID) {
985213f2 2009 if (PL_delaymagic_gid != PL_delaymagic_egid)
cea2e8a9 2010 DIE(aTHX_ "No setregid available");
b469f1e0 2011 PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
a0d0e21e 2012 }
56febc5e
AD
2013# endif /* HAS_SETREGID */
2014#endif /* HAS_SETRESGID */
04783dc7 2015
985213f2
AB
2016 tmp_gid = PerlProc_getgid();
2017 tmp_egid = PerlProc_getegid();
a0d0e21e 2018 }
284167a5 2019 TAINTING_set( TAINTING_get | (tmp_uid && (tmp_euid != tmp_uid || tmp_egid != tmp_gid)) );
9a9b5ec9
DM
2020#ifdef NO_TAINT_SUPPORT
2021 PERL_UNUSED_VAR(tmp_uid);
2022 PERL_UNUSED_VAR(tmp_euid);
2023 PERL_UNUSED_VAR(tmp_gid);
2024 PERL_UNUSED_VAR(tmp_egid);
2025#endif
a0d0e21e 2026 }
a68090fe 2027 PL_delaymagic = old_delaymagic;
54310121 2028
54310121
PP
2029 if (gimme == G_VOID)
2030 SP = firstrelem - 1;
2031 else if (gimme == G_SCALAR) {
54310121 2032 SP = firstrelem;
b09ed995 2033 EXTEND(SP,1);
7b394f12
DM
2034 if (PL_op->op_private & OPpASSIGN_TRUEBOOL)
2035 SETs((firstlelem - firstrelem) ? &PL_sv_yes : &PL_sv_zero);
2036 else {
2037 dTARGET;
2038 SETi(firstlelem - firstrelem);
2039 }
54310121 2040 }
b09ed995
DM
2041 else
2042 SP = relem - 1;
08aeb9f7 2043
54310121 2044 RETURN;
a0d0e21e
LW
2045}
2046
8782bef2
GB
2047PP(pp_qr)
2048{
20b7effb 2049 dSP;
eb578fdb 2050 PMOP * const pm = cPMOP;
fe578d7f 2051 REGEXP * rx = PM_GETRE(pm);
196a02af
DM
2052 regexp *prog = ReANY(rx);
2053 SV * const pkg = RXp_ENGINE(prog)->qr_package(aTHX_ (rx));
c4420975 2054 SV * const rv = sv_newmortal();
d63c20f2
DM
2055 CV **cvp;
2056 CV *cv;
288b8c02
NC
2057
2058 SvUPGRADE(rv, SVt_IV);
c2123ae3
NC
2059 /* For a subroutine describing itself as "This is a hacky workaround" I'm
2060 loathe to use it here, but it seems to be the right fix. Or close.
2061 The key part appears to be that it's essential for pp_qr to return a new
2062 object (SV), which implies that there needs to be an effective way to
2063 generate a new SV from the existing SV that is pre-compiled in the
2064 optree. */
2065 SvRV_set(rv, MUTABLE_SV(reg_temp_copy(NULL, rx)));
288b8c02
NC
2066 SvROK_on(rv);
2067
8d919b0a 2068 cvp = &( ReANY((REGEXP *)SvRV(rv))->qr_anoncv);
5d9574c1 2069 if (UNLIKELY((cv = *cvp) && CvCLONE(*cvp))) {
d63c20f2 2070 *cvp = cv_clone(cv);
fc2b2dca 2071 SvREFCNT_dec_NN(cv);
d63c20f2
DM
2072 }
2073
288b8c02 2074 if (pkg) {
f815daf2 2075 HV *const stash = gv_stashsv(pkg, GV_ADD);
fc2b2dca 2076 SvREFCNT_dec_NN(pkg);
288b8c02
NC
2077 (void)sv_bless(rv, stash);
2078 }
2079
196a02af 2080 if (UNLIKELY(RXp_ISTAINTED(prog))) {
e08e52cf 2081 SvTAINTED_on(rv);
9274aefd
DM
2082 SvTAINTED_on(SvRV(rv));
2083 }
c8c13c22 2084 XPUSHs(rv);
2085 RETURN;
8782bef2
GB
2086}
2087
a0d0e21e
LW
2088PP(pp_match)
2089{
20b7effb 2090 dSP; dTARG;
eb578fdb 2091 PMOP *pm = cPMOP;
d65afb4b 2092 PMOP *dynpm = pm;
eb578fdb 2093 const char *s;
5c144d81 2094 const char *strend;
99a90e59 2095 SSize_t curpos = 0; /* initial pos() or current $+[0] */
a0d0e21e 2096 I32 global;
7fadf4a7 2097 U8 r_flags = 0;
5c144d81 2098 const char *truebase; /* Start of string */
eb578fdb 2099 REGEXP *rx = PM_GETRE(pm);
196a02af 2100 regexp *prog = ReANY(rx);
b3eb6a9b 2101 bool rxtainted;
1c23e2bd 2102 const U8 gimme = GIMME_V;
a0d0e21e 2103 STRLEN len;
a3b680e6 2104 const I32 oldsave = PL_savestack_ix;
e60df1fa 2105 I32 had_zerolen = 0;
b1422d62 2106 MAGIC *mg = NULL;
a0d0e21e 2107
533c011a 2108 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
2109 TARG = POPs;
2110 else {
9399c607
DM
2111 if (ARGTARG)
2112 GETTARGET;
2113 else {
2114 TARG = DEFSV;
2115 }
a0d0e21e
LW
2116 EXTEND(SP,1);
2117 }
d9f424b2 2118
c277df42 2119 PUTBACK; /* EVAL blocks need stack_sp. */
69dc4b30
FC
2120 /* Skip get-magic if this is a qr// clone, because regcomp has
2121 already done it. */
196a02af 2122 truebase = prog->mother_re
69dc4b30
FC
2123 ? SvPV_nomg_const(TARG, len)
2124 : SvPV_const(TARG, len);
f1d31338 2125 if (!truebase)
2269b42e 2126 DIE(aTHX_ "panic: pp_match");
f1d31338 2127 strend = truebase + len;
196a02af 2128 rxtainted = (RXp_ISTAINTED(prog) ||
284167a5 2129 (TAINT_get && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 2130 TAINT_NOT;
a0d0e21e 2131
6c864ec2 2132 /* We need to know this in case we fail out early - pos() must be reset */
de0df3c0
MH
2133 global = dynpm->op_pmflags & PMf_GLOBAL;
2134
d65afb4b 2135 /* PMdf_USED is set after a ?? matches once */
c737faaf
YO
2136 if (
2137#ifdef USE_ITHREADS
2138 SvREADONLY(PL_regex_pad[pm->op_pmoffset])
2139#else
2140 pm->op_pmflags & PMf_USED
2141#endif
2142 ) {
e5dc5375 2143 DEBUG_r(PerlIO_printf(Perl_debug_log, "?? already matched once"));
de0df3c0 2144 goto nope;
a0d0e21e
LW
2145 }
2146
5585e758 2147 /* handle the empty pattern */
196a02af 2148 if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
5585e758
YO
2149 if (PL_curpm == PL_reg_curpm) {
2150 if (PL_curpm_under) {
2151 if (PL_curpm_under == PL_reg_curpm) {
2152 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
2153 } else {
2154 pm = PL_curpm_under;
2155 }
2156 }
2157 } else {
2158 pm = PL_curpm;
2159 }
2160 rx = PM_GETRE(pm);
196a02af 2161 prog = ReANY(rx);
a0d0e21e 2162 }
d65afb4b 2163
196a02af 2164 if (RXp_MINLEN(prog) >= 0 && (STRLEN)RXp_MINLEN(prog) > len) {
75d43e96 2165 DEBUG_r(PerlIO_printf(Perl_debug_log, "String shorter than min possible regex match (%"
147e3846 2166 UVuf " < %" IVdf ")\n",
196a02af 2167 (UV)len, (IV)RXp_MINLEN(prog)));
de0df3c0 2168 goto nope;
e5dc5375 2169 }
c277df42 2170
8ef97b0e 2171 /* get pos() if //g */
de0df3c0 2172 if (global) {
b1422d62 2173 mg = mg_find_mglob(TARG);
8ef97b0e 2174 if (mg && mg->mg_len >= 0) {
25fdce4a 2175 curpos = MgBYTEPOS(mg, TARG, truebase, len);
8ef97b0e
DM
2176 /* last time pos() was set, it was zero-length match */
2177 if (mg->mg_flags & MGf_MINMATCH)
2178 had_zerolen = 1;
2179 }
a0d0e21e 2180 }
8ef97b0e 2181
6e240d0b 2182#ifdef PERL_SAWAMPERSAND
196a02af 2183 if ( RXp_NPARENS(prog)
6502e081 2184 || PL_sawampersand
196a02af 2185 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
5b0e71e9 2186 || (dynpm->op_pmflags & PMf_KEEPCOPY)
6e240d0b
FC
2187 )
2188#endif
2189 {
6502e081
DM
2190 r_flags |= (REXEC_COPY_STR|REXEC_COPY_SKIP_PRE);
2191 /* in @a =~ /(.)/g, we iterate multiple times, but copy the buffer
2192 * only on the first iteration. Therefore we need to copy $' as well
2193 * as $&, to make the rest of the string available for captures in
2194 * subsequent iterations */
2195 if (! (global && gimme == G_ARRAY))
2196 r_flags |= REXEC_COPY_SKIP_POST;
2197 };
5b0e71e9
DM
2198#ifdef PERL_SAWAMPERSAND
2199 if (dynpm->op_pmflags & PMf_KEEPCOPY)
2200 /* handle KEEPCOPY in pmop but not rx, eg $r=qr/a/; /$r/p */
2201 r_flags &= ~(REXEC_COPY_SKIP_PRE|REXEC_COPY_SKIP_POST);
2202#endif
22e551b9 2203
f1d31338
DM
2204 s = truebase;
2205
d7be1480 2206 play_it_again:
985afbc1 2207 if (global)
03c83e26 2208 s = truebase + curpos;
f722798b 2209
77da2310 2210 if (!CALLREGEXEC(rx, (char*)s, (char *)strend, (char*)truebase,
03c83e26 2211 had_zerolen, TARG, NULL, r_flags))
03b6c93d 2212 goto nope;
77da2310
NC
2213
2214 PL_curpm = pm;
985afbc1 2215 if (dynpm->op_pmflags & PMf_ONCE)
c737faaf 2216#ifdef USE_ITHREADS
77da2310 2217 SvREADONLY_on(PL_regex_pad[dynpm->op_pmoffset]);
c737faaf 2218#else
77da2310 2219 dynpm->op_pmflags |= PMf_USED;
c737faaf 2220#endif
a0d0e21e 2221
72311751 2222 if (rxtainted)
196a02af
DM
2223 RXp_MATCH_TAINTED_on(prog);
2224 TAINT_IF(RXp_MATCH_TAINTED(prog));
35c2ccc3
DM
2225
2226 /* update pos */
2227
2228 if (global && (gimme != G_ARRAY || (dynpm->op_pmflags & PMf_CONTINUE))) {
b1422d62 2229 if (!mg)
35c2ccc3 2230 mg = sv_magicext_mglob(TARG);
196a02af
DM
2231 MgBYTEPOS_set(mg, TARG, truebase, RXp_OFFS(prog)[0].end);
2232 if (RXp_ZERO_LEN(prog))
adf51885
DM
2233 mg->mg_flags |= MGf_MINMATCH;
2234 else
2235 mg->mg_flags &= ~MGf_MINMATCH;
35c2ccc3
DM
2236 }
2237
196a02af 2238 if ((!RXp_NPARENS(prog) && !global) || gimme != G_ARRAY) {
bf9dff51
DM
2239 LEAVE_SCOPE(oldsave);
2240 RETPUSHYES;
2241 }
2242
88ab22af
DM
2243 /* push captures on stack */
2244
bf9dff51 2245 {
196a02af 2246 const I32 nparens = RXp_NPARENS(prog);
a3b680e6 2247 I32 i = (global && !nparens) ? 1 : 0;
a0d0e21e 2248
c277df42 2249 SPAGAIN; /* EVAL blocks could move the stack. */
ffc61ed2
JH
2250 EXTEND(SP, nparens + i);
2251 EXTEND_MORTAL(nparens + i);
2252 for (i = !i; i <= nparens; i++) {
a0d0e21e 2253 PUSHs(sv_newmortal());
196a02af
DM
2254 if (LIKELY((RXp_OFFS(prog)[i].start != -1)
2255 && RXp_OFFS(prog)[i].end != -1 ))
5d9574c1 2256 {
196a02af
DM
2257 const I32 len = RXp_OFFS(prog)[i].end - RXp_OFFS(prog)[i].start;
2258 const char * const s = RXp_OFFS(prog)[i].start + truebase;
2259 if (UNLIKELY( RXp_OFFS(prog)[i].end < 0
2260 || RXp_OFFS(prog)[i].start < 0
2261 || len < 0
2262 || len > strend - s)
2263 )
5637ef5b 2264 DIE(aTHX_ "panic: pp_match start/end pointers, i=%ld, "
147e3846 2265 "start=%ld, end=%ld, s=%p, strend=%p, len=%" UVuf,
196a02af
DM
2266 (long) i, (long) RXp_OFFS(prog)[i].start,
2267 (long)RXp_OFFS(prog)[i].end, s, strend, (UV) len);
a0d0e21e 2268 sv_setpvn(*SP, s, len);
cce850e4 2269 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
a197cbdd 2270 SvUTF8_on(*SP);
a0d0e21e
LW
2271 }
2272 }
2273 if (global) {
196a02af
DM
2274 curpos = (UV)RXp_OFFS(prog)[0].end;
2275 had_zerolen = RXp_ZERO_LEN(prog);
c277df42 2276 PUTBACK; /* EVAL blocks may use stack */
cf93c79d 2277 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
2278 goto play_it_again;
2279 }
4633a7c4 2280 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2281 RETURN;
2282 }
e5964223 2283 NOT_REACHED; /* NOTREACHED */
a0d0e21e 2284
7b52d656 2285 nope:
d65afb4b 2286 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
b1422d62
DM
2287 if (!mg)
2288 mg = mg_find_mglob(TARG);
2289 if (mg)
2290 mg->mg_len = -1;
a0d0e21e 2291 }
4633a7c4 2292 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2293 if (gimme == G_ARRAY)
2294 RETURN;
2295 RETPUSHNO;
2296}
2297
2298OP *
864dbfa3 2299Perl_do_readline(pTHX)
a0d0e21e 2300{
20b7effb 2301 dSP; dTARGETSTACKED;
eb578fdb 2302 SV *sv;
a0d0e21e
LW
2303 STRLEN tmplen = 0;
2304 STRLEN offset;
760ac839 2305 PerlIO *fp;
eb578fdb
KW
2306 IO * const io = GvIO(PL_last_in_gv);
2307 const I32 type = PL_op->op_type;
1c23e2bd 2308 const U8 gimme = GIMME_V;
a0d0e21e 2309
6136c704 2310 if (io) {
50db69d8 2311 const MAGIC *const mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar);
6136c704 2312 if (mg) {
3e0cb5de 2313 Perl_tied_method(aTHX_ SV_CONST(READLINE), SP, MUTABLE_SV(io), mg, gimme, 0);
6136c704 2314 if (gimme == G_SCALAR) {
50db69d8
NC
2315 SPAGAIN;
2316 SvSetSV_nosteal(TARG, TOPs);
2317 SETTARG;
6136c704 2318 }
50db69d8 2319 return NORMAL;
0b7c7b4f 2320 }
e79b0511 2321 }
4608196e 2322 fp = NULL;
a0d0e21e
LW
2323 if (io) {
2324 fp = IoIFP(io);
2325 if (!fp) {
2326 if (IoFLAGS(io) & IOf_ARGV) {
2327 if (IoFLAGS(io) & IOf_START) {
a0d0e21e 2328 IoLINES(io) = 0;
b9f2b683 2329 if (av_tindex(GvAVn(PL_last_in_gv)) < 0) {
1d7c1841 2330 IoFLAGS(io) &= ~IOf_START;
d5eb9a46 2331 do_open6(PL_last_in_gv, "-", 1, NULL, NULL, 0);
4bac9ae4 2332 SvTAINTED_off(GvSVn(PL_last_in_gv)); /* previous tainting irrelevant */
76f68e9b 2333 sv_setpvs(GvSVn(PL_last_in_gv), "-");
3280af22 2334 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
2335 fp = IoIFP(io);
2336 goto have_fp;
a0d0e21e
LW
2337 }
2338 }
157fb5a1 2339 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
a0d0e21e 2340 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 2341 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
2342 }
2343 }
0d44d22b
NC
2344 else if (type == OP_GLOB)
2345 fp = Perl_start_glob(aTHX_ POPs, io);
a0d0e21e
LW
2346 }
2347 else if (type == OP_GLOB)
2348 SP--;
7716c5c5 2349 else if (IoTYPE(io) == IoTYPE_WRONLY) {
a5390457 2350 report_wrongway_fh(PL_last_in_gv, '>');
a00b5bd3 2351 }
a0d0e21e
LW
2352 }
2353 if (!fp) {
041457d9 2354 if ((!io || !(IoFLAGS(io) & IOf_START))
de7dabb6
TC
2355 && ckWARN(WARN_CLOSED)
2356 && type != OP_GLOB)
041457d9 2357 {
de7dabb6 2358 report_evil_fh(PL_last_in_gv);
3f4520fe 2359 }
54310121 2360 if (gimme == G_SCALAR) {
79628082 2361 /* undef TARG, and push that undefined value */
ba92458f 2362 if (type != OP_RCATLINE) {
3773545d 2363 sv_set_undef(TARG);
ba92458f 2364 }
a0d0e21e
LW
2365 PUSHTARG;
2366 }
2367 RETURN;
2368 }
a2008d6d 2369 have_fp:
54310121 2370 if (gimme == G_SCALAR) {
a0d0e21e 2371 sv = TARG;
0f722b55
RGS
2372 if (type == OP_RCATLINE && SvGMAGICAL(sv))
2373 mg_get(sv);
48de12d9
RGS
2374 if (SvROK(sv)) {
2375 if (type == OP_RCATLINE)
5668452f 2376 SvPV_force_nomg_nolen(sv);
48de12d9
RGS
2377 else
2378 sv_unref(sv);
2379 }
f7877b28 2380 else if (isGV_with_GP(sv)) {
5668452f 2381 SvPV_force_nomg_nolen(sv);
f7877b28 2382 }
862a34c6 2383 SvUPGRADE(sv, SVt_PV);
a0d0e21e 2384 tmplen = SvLEN(sv); /* remember if already alloced */
e3918bb7 2385 if (!tmplen && !SvREADONLY(sv) && !SvIsCOW(sv)) {
f72e8700
JJ
2386 /* try short-buffering it. Please update t/op/readline.t
2387 * if you change the growth length.
2388 */
2389 Sv_Grow(sv, 80);
2390 }
2b5e58c4
AMS
2391 offset = 0;
2392 if (type == OP_RCATLINE && SvOK(sv)) {
2393 if (!SvPOK(sv)) {
5668452f 2394 SvPV_force_nomg_nolen(sv);
2b5e58c4 2395 }
a0d0e21e 2396 offset = SvCUR(sv);
2b5e58c4 2397 }
a0d0e21e 2398 }
54310121 2399 else {
561b68a9 2400 sv = sv_2mortal(newSV(80));
54310121
PP
2401 offset = 0;
2402 }
fbad3eb5 2403
3887d568
AP
2404 /* This should not be marked tainted if the fp is marked clean */
2405#define MAYBE_TAINT_LINE(io, sv) \
2406 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
2407 TAINT; \
2408 SvTAINTED_on(sv); \
2409 }
2410
684bef36 2411/* delay EOF state for a snarfed empty file */
fbad3eb5 2412#define SNARF_EOF(gimme,rs,io,sv) \
684bef36 2413 (gimme != G_SCALAR || SvCUR(sv) \
b9fee9ba 2414 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
fbad3eb5 2415
a0d0e21e 2416 for (;;) {
09e8efcc 2417 PUTBACK;
fbad3eb5 2418 if (!sv_gets(sv, fp, offset)
2d726892
TF
2419 && (type == OP_GLOB
2420 || SNARF_EOF(gimme, PL_rs, io, sv)
2421 || PerlIO_error(fp)))
fbad3eb5 2422 {
760ac839 2423 PerlIO_clearerr(fp);
a0d0e21e 2424 if (IoFLAGS(io) & IOf_ARGV) {
157fb5a1 2425 fp = nextargv(PL_last_in_gv, PL_op->op_flags & OPf_SPECIAL);
a0d0e21e
LW
2426 if (fp)
2427 continue;
3280af22 2428 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
2429 }
2430 else if (type == OP_GLOB) {
a2a5de95
NC
2431 if (!do_close(PL_last_in_gv, FALSE)) {
2432 Perl_ck_warner(aTHX_ packWARN(WARN_GLOB),
2433 "glob failed (child exited with status %d%s)",
2434 (int)(STATUS_CURRENT >> 8),
2435 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 2436 }
a0d0e21e 2437 }
54310121 2438 if (gimme == G_SCALAR) {
ba92458f
AE
2439 if (type != OP_RCATLINE) {
2440 SV_CHECK_THINKFIRST_COW_DROP(TARG);
0c34ef67 2441 SvOK_off(TARG);
ba92458f 2442 }
09e8efcc 2443 SPAGAIN;
a0d0e21e
LW
2444 PUSHTARG;
2445 }
3887d568 2446 MAYBE_TAINT_LINE(io, sv);
a0d0e21e
LW
2447 RETURN;
2448 }
3887d568 2449 MAYBE_TAINT_LINE(io, sv);
a0d0e21e 2450 IoLINES(io)++;
b9fee9ba 2451 IoFLAGS(io) |= IOf_NOLINE;
71be2cbc 2452 SvSETMAGIC(sv);
09e8efcc 2453 SPAGAIN;
a0d0e21e 2454 XPUSHs(sv);
a0d0e21e 2455 if (type == OP_GLOB) {
349d4f2f 2456 const char *t1;
45a23732 2457 Stat_t statbuf;
a0d0e21e 2458
3280af22 2459 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
6136c704 2460 char * const tmps = SvEND(sv) - 1;
aa07b2f6 2461 if (*tmps == *SvPVX_const(PL_rs)) {
c07a80fd 2462 *tmps = '\0';
b162af07 2463 SvCUR_set(sv, SvCUR(sv) - 1);
c07a80fd
PP
2464 }
2465 }
349d4f2f 2466 for (t1 = SvPVX_const(sv); *t1; t1++)
b51c3e77
CB
2467#ifdef __VMS
2468 if (strchr("*%?", *t1))
2469#else
7ad1e72d 2470 if (strchr("$&*(){}[]'\";\\|?<>~`", *t1))
b51c3e77 2471#endif
a0d0e21e 2472 break;
45a23732 2473 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &statbuf) < 0) {
a0d0e21e
LW
2474 (void)POPs; /* Unmatched wildcard? Chuck it... */
2475 continue;
2476 }
2d79bf7f 2477 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
d4c19fe8
AL
2478 if (ckWARN(WARN_UTF8)) {
2479 const U8 * const s = (const U8*)SvPVX_const(sv) + offset;
2480 const STRLEN len = SvCUR(sv) - offset;
2481 const U8 *f;
2482
2483 if (!is_utf8_string_loc(s, len, &f))
2484 /* Emulate :encoding(utf8) warning in the same case. */
2485 Perl_warner(aTHX_ packWARN(WARN_UTF8),
2486 "utf8 \"\\x%02X\" does not map to Unicode",
2487 f < (U8*)SvEND(sv) ? *f : 0);
2488 }
a0d0e21e 2489 }
54310121 2490 if (gimme == G_ARRAY) {
a0d0e21e 2491 if (SvLEN(sv) - SvCUR(sv) > 20) {
1da4ca5f 2492 SvPV_shrink_to_cur(sv);
a0d0e21e 2493 }
561b68a9 2494 sv = sv_2mortal(newSV(80));
a0d0e21e
LW
2495 continue;
2496 }
54310121 2497 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e 2498 /* try to reclaim a bit of scalar space (only on 1st alloc) */
d5b5861b
NC
2499 const STRLEN new_len
2500 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1da4ca5f 2501 SvPV_renew(sv, new_len);
a0d0e21e
LW
2502 }
2503 RETURN;
2504 }
2505}
2506
a0d0e21e
LW
2507PP(pp_helem)
2508{
20b7effb 2509 dSP;
760ac839 2510 HE* he;
ae77835f 2511 SV **svp;
c445ea15 2512 SV * const keysv = POPs;
85fbaab2 2513 HV * const hv = MUTABLE_HV(POPs);
a3b680e6
AL
2514 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2515 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 2516 SV *sv;
92970b93 2517 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
d30e492c 2518 bool preeminent = TRUE;
a0d0e21e 2519
6dfc73ea
SM
2520 if (SvTYPE(hv) != SVt_PVHV)
2521 RETPUSHUNDEF;
d4c19fe8 2522
92970b93 2523 if (localizing) {
d4c19fe8
AL
2524 MAGIC *mg;
2525 HV *stash;
d30e492c
VP
2526
2527 /* If we can determine whether the element exist,
2528 * Try to preserve the existenceness of a tied hash
2529 * element by using EXISTS and DELETE if possible.
2530 * Fallback to FETCH and STORE otherwise. */
2c5f48c2 2531 if (SvCANEXISTDELETE(hv))
d30e492c 2532 preeminent = hv_exists_ent(hv, keysv, 0);
d4c19fe8 2533 }
d30e492c 2534
5f9d7e2b 2535 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
d4c19fe8 2536 svp = he ? &HeVAL(he) : NULL;
a0d0e21e 2537 if (lval) {
746f6409 2538 if (!svp || !*svp || *svp == &PL_sv_undef) {
68dc0745
PP
2539 SV* lv;
2540 SV* key2;
2d8e6c8d 2541 if (!defer) {
be2597df 2542 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2d8e6c8d 2543 }
68dc0745
PP
2544 lv = sv_newmortal();
2545 sv_upgrade(lv, SVt_PVLV);
2546 LvTYPE(lv) = 'y';
6136c704 2547 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, NULL, 0);
fc2b2dca 2548 SvREFCNT_dec_NN(key2); /* sv_magic() increments refcount */
0ad694a7 2549 LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
68dc0745
PP
2550 LvTARGLEN(lv) = 1;
2551 PUSHs(lv);
2552 RETURN;
2553 }
92970b93 2554 if (localizing) {
bfcb3514 2555 if (HvNAME_get(hv) && isGV(*svp))
159b6efe 2556 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
47cfc530
VP
2557 else if (preeminent)
2558 save_helem_flags(hv, keysv, svp,
2559 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
2560 else
2561 SAVEHDELETE(hv, keysv);
5f05dabc 2562 }
9026059d
GG
2563 else if (PL_op->op_private & OPpDEREF) {
2564 PUSHs(vivify_ref(*svp, PL_op->op_private & OPpDEREF));
2565 RETURN;
2566 }
a0d0e21e 2567 }
746f6409 2568 sv = (svp && *svp ? *svp : &PL_sv_undef);
fd69380d
DM
2569 /* Originally this did a conditional C<sv = sv_mortalcopy(sv)>; this
2570 * was to make C<local $tied{foo} = $tied{foo}> possible.
2571 * However, it seems no longer to be needed for that purpose, and
2572 * introduced a new bug: stuff like C<while ($hash{taintedval} =~ /.../g>
2573 * would loop endlessly since the pos magic is getting set on the
2574 * mortal copy and lost. However, the copy has the effect of
2575 * triggering the get magic, and losing it altogether made things like
2576 * c<$tied{foo};> in void context no longer do get magic, which some
2577 * code relied on. Also, delayed triggering of magic on @+ and friends
2578 * meant the original regex may be out of scope by now. So as a
2579 * compromise, do the get magic here. (The MGf_GSKIP flag will stop it
2580 * being called too many times). */
39cf747a 2581 if (!lval && SvRMAGICAL(hv) && SvGMAGICAL(sv))
fd69380d 2582 mg_get(sv);
be6c24e0 2583 PUSHs(sv);
a0d0e21e
LW
2584 RETURN;
2585}
2586
fedf30e1
DM
2587
2588/* a stripped-down version of Perl_softref2xv() for use by
2589 * pp_multideref(), which doesn't use PL_op->op_flags */
2590
f9db5646 2591STATIC GV *
fedf30e1
DM
2592S_softref2xv_lite(pTHX_ SV *const sv, const char *const what,
2593 const svtype type)
2594{
2595 if (PL_op->op_private & HINT_STRICT_REFS) {
2596 if (SvOK(sv))
2597 Perl_die(aTHX_ PL_no_symref_sv, sv,
2598 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
2599 else
2600 Perl_die(aTHX_ PL_no_usym, what);
2601 }
2602 if (!SvOK(sv))
2603 Perl_die(aTHX_ PL_no_usym, what);
2604 return gv_fetchsv_nomg(sv, GV_ADD, type);
2605}
2606
2607
79815f56
DM
2608/* Handle one or more aggregate derefs and array/hash indexings, e.g.
2609 * $h->{foo} or $a[0]{$key}[$i] or f()->[1]
fedf30e1
DM
2610 *
2611 * op_aux points to an array of unions of UV / IV / SV* / PADOFFSET.
79815f56
DM
2612 * Each of these either contains a set of actions, or an argument, such as
2613 * an IV to use as an array index, or a lexical var to retrieve.
2614 * Several actions re stored per UV; we keep shifting new actions off the
2615 * one UV, and only reload when it becomes zero.
fedf30e1
DM
2616 */
2617
2618PP(pp_multideref)
2619{
2620 SV *sv = NULL; /* init to avoid spurious 'may be used uninitialized' */
2621 UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux;
2622 UV actions = items->uv;
2623
2624 assert(actions);
2625 /* this tells find_uninit_var() where we're up to */
2626 PL_multideref_pc = items;
2627
2628 while (1) {
2629 /* there are three main classes of action; the first retrieve
2630 * the initial AV or HV from a variable or the stack; the second
2631 * does the equivalent of an unrolled (/DREFAV, rv2av, aelem),
2632 * the third an unrolled (/DREFHV, rv2hv, helem).
2633 */
2634 switch (actions & MDEREF_ACTION_MASK) {
2635
2636 case MDEREF_reload:
2637 actions = (++items)->uv;
2638 continue;
2639
2640 case MDEREF_AV_padav_aelem: /* $lex[...] */
2641 sv = PAD_SVl((++items)->pad_offset);
2642 goto do_AV_aelem;
2643
2644 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
2645 sv = UNOP_AUX_item_sv(++items);
2646 assert(isGV_with_GP(sv));
2647 sv = (SV*)GvAVn((GV*)sv);
2648 goto do_AV_aelem;
2649
2650 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
2651 {
2652 dSP;
2653 sv = POPs;
2654 PUTBACK;
2655 goto do_AV_rv2av_aelem;
2656 }
2657
2658 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
2659 sv = UNOP_AUX_item_sv(++items);
2660 assert(isGV_with_GP(sv));
2661 sv = GvSVn((GV*)sv);
2662 goto do_AV_vivify_rv2av_aelem;
2663
2664 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
2665 sv = PAD_SVl((++items)->pad_offset);
2666 /* FALLTHROUGH */
2667
2668 do_AV_vivify_rv2av_aelem:
2669 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
2670 /* this is the OPpDEREF action normally found at the end of
2671 * ops like aelem, helem, rv2sv */
2672 sv = vivify_ref(sv, OPpDEREF_AV);
2673 /* FALLTHROUGH */
2674
2675 do_AV_rv2av_aelem:
2676 /* this is basically a copy of pp_rv2av when it just has the
2677 * sKR/1 flags */
2678 SvGETMAGIC(sv);
2679 if (LIKELY(SvROK(sv))) {
2680 if (UNLIKELY(SvAMAGIC(sv))) {
2681 sv = amagic_deref_call(sv, to_av_amg);
2682 }
2683 sv = SvRV(sv);
2684 if (UNLIKELY(SvTYPE(sv) != SVt_PVAV))
2685 DIE(aTHX_ "Not an ARRAY reference");
2686 }
2687 else if (SvTYPE(sv) != SVt_PVAV) {
2688 if (!isGV_with_GP(sv))
2689 sv = (SV*)S_softref2xv_lite(aTHX_ sv, "an ARRAY", SVt_PVAV);
2690 sv = MUTABLE_SV(GvAVn((GV*)sv));
2691 }
2692 /* FALLTHROUGH */
2693
2694 do_AV_aelem:
2695 {
2696 /* retrieve the key; this may be either a lexical or package
2697 * var (whose index/ptr is stored as an item) or a signed
2698 * integer constant stored as an item.
2699 */
2700 SV *elemsv;
2701 IV elem = 0; /* to shut up stupid compiler warnings */
2702
2703
2704 assert(SvTYPE(sv) == SVt_PVAV);
2705
2706 switch (actions & MDEREF_INDEX_MASK) {
2707 case MDEREF_INDEX_none:
2708 goto finish;
2709 case MDEREF_INDEX_const:
2710 elem = (++items)->iv;
2711 break;
2712 case MDEREF_INDEX_padsv:
2713 elemsv = PAD_SVl((++items)->pad_offset);
2714 goto check_elem;
2715 case MDEREF_INDEX_gvsv:
2716 elemsv = UNOP_AUX_item_sv(++items);
2717 assert(isGV_with_GP(elemsv));
2718 elemsv = GvSVn((GV*)elemsv);
2719 check_elem:
2720 if (UNLIKELY(SvROK(elemsv) && !SvGAMAGIC(elemsv)
2721 && ckWARN(WARN_MISC)))
2722 Perl_warner(aTHX_ packWARN(WARN_MISC),
147e3846 2723 "Use of reference \"%" SVf "\" as array index",
fedf30e1
DM
2724 SVfARG(elemsv));
2725 /* the only time that S_find_uninit_var() needs this
2726 * is to determine which index value triggered the
2727 * undef warning. So just update it here. Note that
2728 * since we don't save and restore this var (e.g. for
2729 * tie or overload execution), its value will be
2730 * meaningless apart from just here */
2731 PL_multideref_pc = items;
2732 elem = SvIV(elemsv);
2733 break;
2734 }
2735
2736
2737 /* this is basically a copy of pp_aelem with OPpDEREF skipped */
2738
2739 if (!(actions & MDEREF_FLAG_last)) {
2740 SV** svp = av_fetch((AV*)sv, elem, 1);
2741 if (!svp || ! (sv=*svp))
2742 DIE(aTHX_ PL_no_aelem, elem);
2743 break;
2744 }
2745
2746 if (PL_op->op_private &
2747 (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
2748 {
2749 if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
2750 sv = av_exists((AV*)sv, elem) ? &PL_sv_yes : &PL_sv_no;
2751 }
2752 else {
2753 I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
2754 sv = av_delete((AV*)sv, elem, discard);
2755 if (discard)
2756 return NORMAL;
2757 if (!sv)
2758 sv = &PL_sv_undef;
2759 }
2760 }
2761 else {
2762 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2763 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
2764 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
2765 bool preeminent = TRUE;
2766 AV *const av = (AV*)sv;
2767 SV** svp;
2768
2769 if (UNLIKELY(localizing)) {
2770 MAGIC *mg;
2771 HV *stash;
2772
2773 /* If we can determine whether the element exist,
2774 * Try to preserve the existenceness of a tied array
2775 * element by using EXISTS and DELETE if possible.
2776 * Fallback to FETCH and STORE otherwise. */
2777 if (SvCANEXISTDELETE(av))
2778 preeminent = av_exists(av, elem);
2779 }
2780
2781 svp = av_fetch(av, elem, lval && !defer);
2782
2783 if (lval) {
2784 if (!svp || !(sv = *svp)) {
2785 IV len;
2786 if (!defer)
2787 DIE(aTHX_ PL_no_aelem, elem);
2788 len = av_tindex(av);
2789 sv = sv_2mortal(newSVavdefelem(av,
2790 /* Resolve a negative index now, unless it points
2791 * before the beginning of the array, in which
2792 * case record it for error reporting in
2793 * magic_setdefelem. */
2794 elem < 0 && len + elem >= 0
2795 ? len + elem : elem, 1));
2796 }
2797 else {
2798 if (UNLIKELY(localizing)) {
2799 if (preeminent) {
2800 save_aelem(av, elem, svp);
2801 sv = *svp; /* may have changed */
2802 }
2803 else
2804 SAVEADELETE(av, elem);
2805 }
2806 }
2807 }
2808 else {
2809 sv = (svp ? *svp : &PL_sv_undef);
2810 /* see note in pp_helem() */
2811 if (SvRMAGICAL(av) && SvGMAGICAL(sv))
2812 mg_get(sv);
2813 }
2814 }
2815
2816 }
2817 finish:
2818 {
2819 dSP;
2820 XPUSHs(sv);
2821 RETURN;
2822 }
2823 /* NOTREACHED */
2824
2825
2826
2827
2828 case MDEREF_HV_padhv_helem: /* $lex{...} */
2829 sv = PAD_SVl((++items)->pad_offset);
2830 goto do_HV_helem;
2831
2832 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
2833 sv = UNOP_AUX_item_sv(++items);
2834 assert(isGV_with_GP(sv));
2835 sv = (SV*)GvHVn((GV*)sv);
2836 goto do_HV_helem;
2837
2838 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
2839 {
2840 dSP;
2841 sv = POPs;
2842 PUTBACK;
2843 goto do_HV_rv2hv_helem;
2844 }
2845
2846 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
2847 sv = UNOP_AUX_item_sv(++items);
2848 assert(isGV_with_GP(sv));
2849 sv = GvSVn((GV*)sv);
2850 goto do_HV_vivify_rv2hv_helem;
2851
2852 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
2853 sv = PAD_SVl((++items)->pad_offset);
2854 /* FALLTHROUGH */
2855
2856 do_HV_vivify_rv2hv_helem:
2857 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
2858 /* this is the OPpDEREF action normally found at the end of
2859 * ops like aelem, helem, rv2sv */
2860 sv = vivify_ref(sv, OPpDEREF_HV);
2861 /* FALLTHROUGH */
2862
2863 do_HV_rv2hv_helem:
2864 /* this is basically a copy of pp_rv2hv when it just has the
2865 * sKR/1 flags (and pp_rv2hv is aliased to pp_rv2av) */
2866
2867 SvGETMAGIC(sv);
2868 if (LIKELY(SvROK(sv))) {
2869 if (UNLIKELY(SvAMAGIC(sv))) {
2870 sv = amagic_deref_call(sv, to_hv_amg);
2871 }
2872 sv = SvRV(sv);
2873 if (UNLIKELY(SvTYPE(sv) != SVt_PVHV))
2874 DIE(aTHX_ "Not a HASH reference");
2875 }
2876 else if (SvTYPE(sv) != SVt_PVHV) {
2877 if (!isGV_with_GP(sv))
2878 sv = (SV*)S_softref2xv_lite(aTHX_ sv, "a HASH", SVt_PVHV);
2879 sv = MUTABLE_SV(GvHVn((GV*)sv));
2880 }
2881 /* FALLTHROUGH */
2882
2883 do_HV_helem:
2884 {
2885 /* retrieve the key; this may be either a lexical / package
2886 * var or a string constant, whose index/ptr is stored as an
2887 * item
2888 */
2889 SV *keysv = NULL; /* to shut up stupid compiler warnings */
2890
2891 assert(SvTYPE(sv) == SVt_PVHV);
2892
2893 switch (actions & MDEREF_INDEX_MASK) {
2894 case MDEREF_INDEX_none:
2895 goto finish;
2896
2897 case MDEREF_INDEX_const:
2898 keysv = UNOP_AUX_item_sv(++items);
2899 break;
2900
2901 case MDEREF_INDEX_padsv:
2902 keysv = PAD_SVl((++items)->pad_offset);
2903 break;
2904
2905 case MDEREF_INDEX_gvsv:
2906 keysv = UNOP_AUX_item_sv(++items);
2907 keysv = GvSVn((GV*)keysv);
2908 break;
2909 }
2910
2911 /* see comment above about setting this var */
2912 PL_multideref_pc = items;
2913
2914
2915 /* ensure that candidate CONSTs have been HEKified */
2916 assert( ((actions & MDEREF_INDEX_MASK) != MDEREF_INDEX_const)
2917 || SvTYPE(keysv) >= SVt_PVMG
2918 || !SvOK(keysv)
2919 || SvROK(keysv)
2920 || SvIsCOW_shared_hash(keysv));
2921
2922 /* this is basically a copy of pp_helem with OPpDEREF skipped */
2923
2924 if (!(actions & MDEREF_FLAG_last)) {
2925 HE *he = hv_fetch_ent((HV*)sv, keysv, 1, 0);
2926 if (!he || !(sv=HeVAL(he)) || sv == &PL_sv_undef)
2927 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2928 break;
2929 }
2930
2931 if (PL_op->op_private &
2932 (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE))
2933 {
2934 if (PL_op->op_private & OPpMULTIDEREF_EXISTS) {
2935 sv = hv_exists_ent((HV*)sv, keysv, 0)
2936 ? &PL_sv_yes : &PL_sv_no;
2937 }
2938 else {
2939 I32 discard = (GIMME_V == G_VOID) ? G_DISCARD : 0;
2940 sv = hv_delete_ent((HV*)sv, keysv, discard, 0);
2941 if (discard)
2942 return NORMAL;
2943 if (!sv)
2944 sv = &PL_sv_undef;
2945 }
2946 }
2947 else {
2948 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2949 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
2950 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
2951 bool preeminent = TRUE;
2952 SV **svp;
2953 HV * const hv = (HV*)sv;
2954 HE* he;
2955
2956 if (UNLIKELY(localizing)) {
2957 MAGIC *mg;
2958 HV *stash;
2959
2960 /* If we can determine whether the element exist,
2961 * Try to preserve the existenceness of a tied hash
2962 * element by using EXISTS and DELETE if possible.
2963 * Fallback to FETCH and STORE otherwise. */
2964 if (SvCANEXISTDELETE(hv))
2965 preeminent = hv_exists_ent(hv, keysv, 0);
2966 }
2967
2968 he = hv_fetch_ent(hv, keysv, lval && !defer, 0);
2969 svp = he ? &HeVAL(he) : NULL;
2970
2971
2972 if (lval) {
2973 if (!svp || !(sv = *svp) || sv == &PL_sv_undef) {
2974 SV* lv;
2975 SV* key2;
2976 if (!defer)
2977 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
2978 lv = sv_newmortal();
2979 sv_upgrade(lv, SVt_PVLV);
2980 LvTYPE(lv) = 'y';
2981 sv_magic(lv, key2 = newSVsv(keysv),
2982 PERL_MAGIC_defelem, NULL, 0);
2983 /* sv_magic() increments refcount */
2984 SvREFCNT_dec_NN(key2);
0ad694a7 2985 LvTARG(lv) = SvREFCNT_inc_simple_NN(hv);
fedf30e1
DM
2986 LvTARGLEN(lv) = 1;
2987 sv = lv;
2988 }
2989 else {
2990 if (localizing) {
2991 if (HvNAME_get(hv) && isGV(sv))
2992 save_gp(MUTABLE_GV(sv),
2993 !(PL_op->op_flags & OPf_SPECIAL));
2994 else if (preeminent) {
2995 save_helem_flags(hv, keysv, svp,
2996 (PL_op->op_flags & OPf_SPECIAL)
2997 ? 0 : SAVEf_SETMAGIC);
2998 sv = *svp; /* may have changed */
2999 }
3000 else
3001 SAVEHDELETE(hv, keysv);
3002 }
3003 }
3004 }
3005 else {
3006 sv = (svp && *svp ? *svp : &PL_sv_undef);
3007 /* see note in pp_helem() */
3008 if (SvRMAGICAL(hv) && SvGMAGICAL(sv))
3009 mg_get(sv);
3010 }
3011 }
3012 goto finish;
3013 }
3014
3015 } /* switch */
3016
3017 actions >>= MDEREF_SHIFT;
3018 } /* while */
3019 /* NOTREACHED */
3020}
3021
3022
a0d0e21e
LW
3023PP(pp_iter)
3024{
eb578fdb 3025 PERL_CONTEXT *cx;
7d6c2cef 3026 SV *oldsv;
1d7c1841 3027 SV **itersvp;
a0d0e21e 3028
84f05d57
JH
3029 SV *sv;
3030 AV *av;
3031 IV ix;
3032 IV inc;
3033
4ebe6e95 3034 cx = CX_CUR();
1d7c1841 3035 itersvp = CxITERVAR(cx);
4b5c941e 3036 assert(itersvp);
a48ce6be
DM
3037
3038 switch (CxTYPE(cx)) {
17c91640 3039
b552b52c
DM
3040 case CXt_LOOP_LAZYSV: /* string increment */
3041 {
3042 SV* cur = cx->blk_loop.state_u.lazysv.cur;
3043 SV *end = cx->blk_loop.state_u.lazysv.end;
3044 /* If the maximum is !SvOK(), pp_enteriter substitutes PL_sv_no.
3045 It has SvPVX of "" and SvCUR of 0, which is what we want. */
3046 STRLEN maxlen = 0;
3047 const char *max = SvPV_const(end, maxlen);
d6c970c7
AC
3048 if (DO_UTF8(end) && IN_UNI_8_BIT)
3049 maxlen = sv_len_utf8_nomg(end);
5d9574c1 3050 if (UNLIKELY(SvNIOK(cur) || SvCUR(cur) > maxlen))
8a1f10dd 3051 goto retno;
b552b52c
DM
3052
3053 oldsv = *itersvp;
6d3ca00e
DM
3054 /* NB: on the first iteration, oldsv will have a ref count of at
3055 * least 2 (one extra from blk_loop.itersave), so the GV or pad
3056 * slot will get localised; on subsequent iterations the RC==1
3057 * optimisation may kick in and the SV will be reused. */
3058 if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
b552b52c
DM
3059 /* safe to reuse old SV */
3060 sv_setsv(oldsv, cur);
a48ce6be 3061 }
b552b52c
DM
3062 else
3063 {
3064 /* we need a fresh SV every time so that loop body sees a
3065 * completely new SV for closures/references to work as
3066 * they used to */
3067 *itersvp = newSVsv(cur);
6d3ca00e 3068 SvREFCNT_dec(oldsv);
b552b52c
DM
3069 }
3070 if (strEQ(SvPVX_const(cur), max))
3071 sv_setiv(cur, 0); /* terminate next time */
3072 else
3073 sv_inc(cur);
3074 break;
3075 }
a48ce6be 3076
fcef60b4
DM
3077 case CXt_LOOP_LAZYIV: /* integer increment */
3078 {
3079 IV cur = cx->blk_loop.state_u.lazyiv.cur;
5d9574c1 3080 if (UNLIKELY(cur > cx->blk_loop.state_u.lazyiv.end))
8a1f10dd 3081 goto retno;
7f61b687 3082
fcef60b4 3083 oldsv = *itersvp;
6d3ca00e
DM
3084 /* see NB comment above */
3085 if (oldsv && LIKELY(SvREFCNT(oldsv) == 1 && !SvMAGICAL(oldsv))) {
eaa5c2d6 3086 /* safe to reuse old SV */
47b96a1e
DM
3087
3088 if ( (SvFLAGS(oldsv) & (SVTYPEMASK|SVf_THINKFIRST|SVf_IVisUV))
3089 == SVt_IV)
3090 {
3091 /* Cheap SvIOK_only().
3092 * Assert that flags which SvIOK_only() would test or
3093 * clear can't be set, because we're SVt_IV */
3094 assert(!(SvFLAGS(oldsv) &
3095 (SVf_OOK|SVf_UTF8|(SVf_OK & ~(SVf_IOK|SVp_IOK)))));
3096 SvFLAGS(oldsv) |= (SVf_IOK|SVp_IOK);
3097 /* SvIV_set() where sv_any points to head */
3098 oldsv->sv_u.svu_iv = cur;
3099
3100 }
3101 else
3102 sv_setiv(oldsv, cur);
eaa5c2d6 3103 }
1c846c1f 3104 else
eaa5c2d6
GA
3105 {
3106 /* we need a fresh SV every time so that loop body sees a
3107 * completely new SV for closures/references to work as they
3108 * used to */
fcef60b4 3109 *itersvp = newSViv(cur);
6d3ca00e 3110 SvREFCNT_dec(oldsv);
eaa5c2d6 3111 }
a2309040 3112
5d9574c1 3113 if (UNLIKELY(cur == IV_MAX)) {
cdc1aa42
NC
3114 /* Handle end of range at IV_MAX */
3115 cx->blk_loop.state_u.lazyiv.end = IV_MIN;
3116 } else
3117 ++cx->blk_loop.state_u.lazyiv.cur;
a48ce6be 3118 break;
fcef60b4 3119 }
a48ce6be 3120
93661e56
DM
3121 case CXt_LOOP_LIST: /* for (1,2,3) */
3122
3123 assert(OPpITER_REVERSED == 2); /* so inc becomes -1 or 1 */
3124 inc = 1 - (PL_op->op_private & OPpITER_REVERSED);
3125 ix = (cx->blk_loop.state_u.stack.ix += inc);
3126 if (UNLIKELY(inc > 0
3127 ? ix > cx->blk_oldsp
3128 : ix <= cx->blk_loop.state_u.stack.basesp)
3129 )
8a1f10dd 3130 goto retno;
93661e56
DM
3131
3132 sv = PL_stack_base[ix];
3133 av = NULL;
3134 goto loop_ary_common;
3135
3136 case CXt_LOOP_ARY: /* for (@ary) */
3137
3138 av = cx->blk_loop.state_u.ary.ary;
3139 inc = 1 - (PL_op->op_private & OPpITER_REVERSED);
3140 ix = (cx->blk_loop.state_u.ary.ix += inc);
3141 if (UNLIKELY(inc > 0
3142 ? ix > AvFILL(av)
3143 : ix < 0)
3144 )
8a1f10dd 3145 goto retno;
de080daa 3146
9d1ee8e0 3147 if (UNLIKELY(SvRMAGICAL(av))) {
a8a20bb6
DM
3148 SV * const * const svp = av_fetch(av, ix, FALSE);
3149 sv = svp ? *svp : NULL;
3150 }
3151 else {
3152 sv = AvARRAY(av)[ix];
de080daa 3153 }
ef3e5ea9 3154
93661e56
DM
3155 loop_ary_common:
3156
d39c26a6
FC
3157 if (UNLIKELY(cx->cx_type & CXp_FOR_LVREF)) {
3158 SvSetMagicSV(*itersvp, sv);
3159 break;
3160 }
3161
5d9574c1
DM
3162 if (LIKELY(sv)) {
3163 if (UNLIKELY(SvIS_FREED(sv))) {
f38aa882
DM
3164 *itersvp = NULL;
3165 Perl_croak(aTHX_ "Use of freed value in iteration");
3166 }
60779a30 3167 if (SvPADTMP(sv)) {
8e079c2a 3168 sv = newSVsv(sv);
60779a30 3169 }
8e079c2a
FC
3170 else {
3171 SvTEMP_off(sv);
3172 SvREFCNT_inc_simple_void_NN(sv);
3173 }
de080daa 3174 }
93661e56 3175 else if (av) {
199f858d 3176 sv = newSVavdefelem(av, ix, 0);
de080daa 3177 }
a600f7e6
FC
3178 else
3179 sv = &PL_sv_undef;
a0d0e21e 3180
de080daa
DM
3181 oldsv = *itersvp;
3182 *itersvp = sv;
3183 SvREFCNT_dec(oldsv);
de080daa 3184 break;
a48ce6be
DM
3185
3186 default:
3187 DIE(aTHX_ "panic: pp_iter, type=%u", CxTYPE(cx));
3188 }
8a1f10dd 3189
7c114860
DM
3190 /* Bypass pushing &PL_sv_yes and calling pp_and(); instead
3191 * jump straight to the AND op's op_other */
3192 assert(PL_op->op_next->op_type == OP_AND);
3193 assert(PL_op->op_next->op_ppaddr == Perl_pp_and);
3194 return cLOGOPx(PL_op->op_next)->op_other;
3195
3196 retno:
3197 /* Bypass pushing &PL_sv_no and calling pp_and(); instead
3198 * jump straight to the AND op's op_next */
3199 assert(PL_op->op_next->op_type == OP_AND);
3200 assert(PL_op->op_next->op_ppaddr == Perl_pp_and);
8a1f10dd 3201 /* pp_enteriter should have pre-extended the stack */
87058c31 3202 EXTEND_SKIP(PL_stack_sp, 1);
7c114860
DM
3203 /* we only need this for the rare case where the OP_AND isn't
3204 * in void context, e.g. $x = do { for (..) {...} };
3205 * but its cheaper to just push it rather than testing first
3206 */
3207 *++PL_stack_sp = &PL_sv_no;
3208 return PL_op->op_next->op_next;
a0d0e21e
LW
3209}
3210
7c114860 3211
ef07e810
DM
3212/*
3213A description of how taint works in pattern matching and substitution.
3214
284167a5
SM
3215This is all conditional on NO_TAINT_SUPPORT not being defined. Under
3216NO_TAINT_SUPPORT, taint-related operations should become no-ops.
3217
4e19c54b 3218While the pattern is being assembled/concatenated and then compiled,
284167a5
SM
3219PL_tainted will get set (via TAINT_set) if any component of the pattern
3220is tainted, e.g. /.*$tainted/. At the end of pattern compilation,
3221the RXf_TAINTED flag is set on the pattern if PL_tainted is set (via
1738e041
DM
3222TAINT_get). It will also be set if any component of the pattern matches
3223based on locale-dependent behavior.
ef07e810 3224
0ab462a6
DM
3225When the pattern is copied, e.g. $r = qr/..../, the SV holding the ref to
3226the pattern is marked as tainted. This means that subsequent usage, such
284167a5
SM
3227as /x$r/, will set PL_tainted using TAINT_set, and thus RXf_TAINTED,
3228on the new pattern too.
ef07e810 3229
272d35c9 3230RXf_TAINTED_SEEN is used post-execution by the get magic code
ef07e810
DM
3231of $1 et al to indicate whether the returned value should be tainted.
3232It is the responsibility of the caller of the pattern (i.e. pp_match,
3233pp_subst etc) to set this flag for any other circumstances where $1 needs
3234to be tainted.
3235
3236The taint behaviour of pp_subst (and pp_substcont) is quite complex.
3237
3238There are three possible sources of taint
3239 * the source string
3240 * the pattern (both compile- and run-time, RXf_TAINTED / RXf_TAINTED_SEEN)
3241 * the replacement string (or expression under /e)
3242
3243There are four destinations of taint and they are affected by the sources
3244according to the rules below:
3245
3246 * the return value (not including /r):
3247 tainted by the source string and pattern, but only for the
3248 number-of-iterations case; boolean returns aren't tainted;
3249 * the modified string (or modified copy under /r):
3250 tainted by the source string, pattern, and replacement strings;
3251 * $1 et al:
3252 tainted by the pattern, and under 'use re "taint"', by the source
3253 string too;
3254 * PL_taint - i.e. whether subsequent code (e.g. in a /e block) is tainted:
3255 should always be unset before executing subsequent code.
3256
3257The overall action of pp_subst is:
3258
3259 * at the start, set bits in rxtainted indicating the taint status of
3260 the various sources.
3261
3262 * After each pattern execution, update the SUBST_TAINT_PAT bit in
3263 rxtainted if RXf_TAINTED_SEEN has been set, to indicate that the
3264 pattern has subsequently become tainted via locale ops.
3265
3266 * If control is being passed to pp_substcont to execute a /e block,
3267 save rxtainted in the CXt_SUBST block, for future use by
3268 pp_substcont.
3269
3270 * Whenever control is being returned to perl code (either by falling
3271 off the "end" of pp_subst/pp_substcont, or by entering a /e block),
3272 use the flag bits in rxtainted to make all the appropriate types of
0ab462a6
DM
3273 destination taint visible; e.g. set RXf_TAINTED_SEEN so that $1
3274 et al will appear tainted.
ef07e810
DM
3275
3276pp_match is just a simpler version of the above.
3277
3278*/
3279
a0d0e21e
LW
3280PP(pp_subst)
3281{
20b7effb 3282 dSP; dTARG;
eb578fdb 3283 PMOP *pm = cPMOP;
a0d0e21e 3284 PMOP *rpm = pm;
eb578fdb 3285 char *s;
a0d0e21e 3286 char *strend;
5c144d81 3287 const char *c;
a0d0e21e 3288 STRLEN clen;
3c6ef0a5
FC
3289 SSize_t iters = 0;
3290 SSize_t maxiters;
a0d0e21e 3291 bool once;
ef07e810
DM
3292 U8 rxtainted = 0; /* holds various SUBST_TAINT_* flag bits.
3293 See "how taint works" above */
a0d0e21e 3294 char *orig;
1ed74d04 3295 U8 r_flags;
eb578fdb 3296 REGEXP *rx = PM_GETRE(pm);
196a02af 3297 regexp *prog = ReANY(rx);
a0d0e21e
LW
3298 STRLEN len;
3299 int force_on_match = 0;
0bcc34c2 3300 const I32 oldsave = PL_savestack_ix;
792b2c16 3301 STRLEN slen;
26a74523 3302 bool doutf8 = FALSE; /* whether replacement is in utf8 */
db2c6cb3 3303#ifdef PERL_ANY_COW
106d9a13 3304 bool was_cow;
ed252734 3305#endif
a0714e2c 3306 SV *nsv = NULL;
b770e143 3307 /* known replacement string? */
eb578fdb 3308 SV *dstr = (pm->op_pmflags & PMf_CONST) ? POPs : NULL;
a0d0e21e 3309
f410a211
NC
3310 PERL_ASYNC_CHECK();
3311
533c011a 3312 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
3313 TARG = POPs;
3314 else {
9399c607
DM
3315 if (ARGTARG)
3316 GETTARGET;
3317 else {
3318 TARG = DEFSV;
3319 }
a0d0e21e 3320 EXTEND(SP,1);
1c846c1f 3321 }
d9f424b2 3322
64534138 3323 SvGETMAGIC(TARG); /* must come before cow check */
db2c6cb3 3324#ifdef PERL_ANY_COW
106d9a13
DM
3325 /* note that a string might get converted to COW during matching */
3326 was_cow = cBOOL(SvIsCOW(TARG));
ed252734 3327#endif
d13a5d3b
TC
3328 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
3329#ifndef PERL_ANY_COW
3330 if (SvIsCOW(TARG))
3331 sv_force_normal_flags(TARG,0);
3332#endif
3333 if ((SvREADONLY(TARG)
3334 || ( ((SvTYPE(TARG) == SVt_PVGV && isGV_with_GP(TARG))
3335 || SvTYPE(TARG) > SVt_PVLV)
3336 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
3337 Perl_croak_no_modify();
3338 }
8ec5e241
NIS
3339 PUTBACK;
3340
6ac6605d
DM
3341 orig = SvPV_nomg(TARG, len);
3342 /* note we don't (yet) force the var into being a string; if we fail
92711104 3343 * to match, we leave as-is; on successful match however, we *will*
6ac6605d 3344 * coerce into a string, then repeat the match */
4499db73 3345 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV || SvVOK(TARG))
a0d0e21e 3346 force_on_match = 1;
20be6587
DM
3347
3348 /* only replace once? */
3349 once = !(rpm->op_pmflags & PMf_GLOBAL);
3350
ef07e810 3351 /* See "how taint works" above */
284167a5 3352 if (TAINTING_get) {
20be6587
DM
3353 rxtainted = (
3354 (SvTAINTED(TARG) ? SUBST_TAINT_STR : 0)
196a02af 3355 | (RXp_ISTAINTED(prog) ? SUBST_TAINT_PAT : 0)
20be6587
DM
3356 | ((pm->op_pmflags & PMf_RETAINT) ? SUBST_TAINT_RETAINT : 0)
3357 | ((once && !(rpm->op_pmflags & PMf_NONDESTRUCT))
3358 ? SUBST_TAINT_BOOLRET : 0));
3359 TAINT_NOT;
3360 }
a12c0f56 3361
a0d0e21e 3362 force_it:
6ac6605d
DM
3363 if (!pm || !orig)
3364 DIE(aTHX_ "panic: pp_subst, pm=%p, orig=%p", pm, orig);
a0d0e21e 3365
6ac6605d
DM
3366 strend = orig + len;
3367 slen = DO_UTF8(TARG) ? utf8_length((U8*)orig, (U8*)strend) : len;
792b2c16
JH
3368 maxiters = 2 * slen + 10; /* We can match twice at each
3369 position, once with zero-length,
3370 second time with non-zero. */
a0d0e21e 3371
794826f4 3372 /* handle the empty pattern */
196a02af 3373 if (!RX_PRELEN(rx) && PL_curpm && !prog->mother_re) {
5585e758
YO
3374 if (PL_curpm == PL_reg_curpm) {
3375 if (PL_curpm_under) {
3376 if (PL_curpm_under == PL_reg_curpm) {
3377 Perl_croak(aTHX_ "Infinite recursion via empty pattern");
3378 } else {
3379 pm = PL_curpm_under;
3380 }
3381 }
3382 } else {
3383 pm = PL_curpm;
3384 }
3385 rx = PM_GETRE(pm);
196a02af 3386 prog = ReANY(rx);
a0d0e21e 3387 }
6502e081 3388
6e240d0b 3389#ifdef PERL_SAWAMPERSAND
196a02af 3390 r_flags = ( RXp_NPARENS(prog)
6502e081 3391 || PL_sawampersand
196a02af 3392 || (RXp_EXTFLAGS(prog) & (RXf_EVAL_SEEN|RXf_PMf_KEEPCOPY))
5b0e71e9 3393 || (rpm->op_pmflags & PMf_KEEPCOPY)
6502e081
DM
3394 )
3395 ? REXEC_COPY_STR
3396 : 0;
6e240d0b
FC
3397#else
3398 r_flags = REXEC_COPY_STR;
3399#endif
7fba1cd6 3400
0395280b 3401 if (!CALLREGEXEC(rx, orig, strend, orig, 0, TARG, NULL, r_flags))
8b64c330 3402 {
5e79dfb9
DM
3403 SPAGAIN;
3404 PUSHs(rpm->op_pmflags & PMf_NONDESTRUCT ? TARG : &PL_sv_no);
3405 LEAVE_SCOPE(oldsave);
3406 RETURN;
3407 }
1754320d
FC
3408 PL_curpm = pm;
3409
71be2cbc 3410 /* known replacement string? */
f272994b 3411 if (dstr) {
8514a05a
JH
3412 /* replacement needing upgrading? */
3413 if (DO_UTF8(TARG) && !doutf8) {
db79b45b 3414 nsv = sv_newmortal();
4a176938 3415 SvSetSV(nsv, dstr);
8df0e7a2 3416 sv_utf8_upgrade(nsv);
5c144d81 3417 c = SvPV_const(nsv, clen);
4a176938
JH
3418 doutf8 = TRUE;
3419 }
3420 else {
5c144d81 3421 c = SvPV_const(dstr, clen);
4a176938 3422 doutf8 = DO_UTF8(dstr);
8514a05a 3423 }
bb933b9b
FC
3424
3425 if (SvTAINTED(dstr))
3426 rxtainted |= SUBST_TAINT_REPL;
f272994b
A
3427 }
3428 else {
6136c704 3429 c = NULL;
f272994b
A
3430 doutf8 = FALSE;
3431 }
3432
71be2cbc 3433 /* can do inplace substitution? */
ed252734 3434 if (c
db2c6cb3 3435#ifdef PERL_ANY_COW
106d9a13 3436 && !was_cow
ed252734 3437#endif
196a02af 3438 && (I32)clen <= RXp_MINLENRET(prog)
9cefd268
FC
3439 && ( once
3440 || !(r_flags & REXEC_COPY_STR)
196a02af 3441 || (!SvGMAGICAL(dstr) && !(RXp_EXTFLAGS(prog) & RXf_EVAL_SEEN))
9cefd268 3442 )
196a02af 3443 && !(RXp_EXTFLAGS(prog) & RXf_NO_INPLACE_SUBST)
8ca8a454
NC
3444 && (!doutf8 || SvUTF8(TARG))
3445 && !(rpm->op_pmflags & PMf_NONDESTRUCT))
8b030b38 3446 {
ec911639 3447
db2c6cb3 3448#ifdef PERL_ANY_COW
106d9a13 3449 /* string might have got converted to COW since we set was_cow */
ed252734 3450 if (SvIsCOW(TARG)) {
f7a8268c 3451 if (!force_on_match)
ed252734 3452 goto have_a_cow;
f7a8268c 3453 assert(SvVOK(TARG));
ed252734
NC
3454 }
3455#endif
71be2cbc 3456 if (force_on_match) {
6ac6605d
DM
3457 /* redo the first match, this time with the orig var
3458 * forced into being a string */
71be2cbc 3459 force_on_match = 0;
6ac6605d 3460 orig = SvPV_force_nomg(TARG, len);
71be2cbc
PP
3461 goto force_it;
3462 }
39b40493 3463
71be2cbc 3464 if (once) {
c67ab8f2 3465 char *d, *m;
196a02af 3466 if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
20be6587 3467 rxtainted |= SUBST_TAINT_PAT;
196a02af
DM
3468 m = orig + RXp_OFFS(prog)[0].start;
3469 d = orig + RXp_OFFS(prog)[0].end;
71be2cbc
PP
3470 s = orig;
3471 if (m - s > strend - d) { /* faster to shorten from end */
2ec7214c 3472 I32 i;
71be2cbc
PP
3473 if (clen) {
3474 Copy(c, m, clen, char);
3475 m += clen;
a0d0e21e 3476 }
71be2cbc
PP
3477 i = strend - d;
3478 if (i > 0) {
3479 Move(d, m, i, char);
3480 m += i;
a0d0e21e 3481 }
71be2cbc
PP
3482 *m = '\0';
3483 SvCUR_set(TARG, m - s);
3484 }
2ec7214c
DM
3485 else { /* faster from front */
3486 I32 i = m - s;
71be2cbc 3487 d -= clen;
2ec7214c
DM
3488 if (i > 0)
3489 Move(s, d - i, i, char);
71be2cbc 3490 sv_chop(TARG, d-i);
71be2cbc 3491 if (clen)
c947cd8d 3492 Copy(c, d, clen, char);
71be2cbc 3493 }
8ec5e241 3494 SPAGAIN;
8ca8a454 3495 PUSHs(&PL_sv_yes);
71be2cbc
PP
3496 }
3497 else {
c67ab8f2 3498 char *d, *m;
196a02af 3499 d = s = RXp_OFFS(prog)[0].start + orig;
71be2cbc 3500 do {
2b25edcf 3501 I32 i;
5d9574c1 3502 if (UNLIKELY(iters++ > maxiters))
cea2e8a9 3503 DIE(aTHX_ "Substitution loop");
196a02af
DM
3504 /* run time pattern taint, eg locale */
3505 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
20be6587 3506 rxtainted |= SUBST_TAINT_PAT;
196a02af 3507 m = RXp_OFFS(prog)[0].start + orig;
155aba94 3508 if ((i = m - s)) {
71be2cbc
PP
3509 if (s != d)
3510 Move(s, d, i, char);
3511 d += i;
a0d0e21e 3512 }
71be2cbc
PP
3513 if (clen) {
3514 Copy(c, d, clen, char);
3515 d += clen;
3516 }
196a02af 3517 s = RXp_OFFS(prog)[0].end + orig;
7ce41e5c
FC
3518 } while (CALLREGEXEC(rx, s, strend, orig,
3519 s == m, /* don't match same null twice */
f722798b 3520 TARG, NULL,
d5e7783a 3521 REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
71be2cbc 3522 if (s != d) {
2b25edcf 3523 I32 i = strend - s;
aa07b2f6 3524 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
71be2cbc 3525 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 3526 }
8ec5e241 3527 SPAGAIN;
7b394f12
DM
3528 if (PL_op->op_private & OPpTRUEBOOL)
3529 PUSHs(iters ? &PL_sv_yes : &PL_sv_zero);
3530 else
3531 mPUSHi(iters);
a0d0e21e
LW
3532 }
3533 }
ff6e92e8 3534 else {
1754320d 3535 bool first;
c67ab8f2 3536 char *m;
1754320d 3537 SV *repl;
a0d0e21e 3538 if (force_on_match) {
6ac6605d
DM
3539 /* redo the first match, this time with the orig var
3540 * forced into being a string */
a0d0e21e 3541 force_on_match = 0;
0c1438a1
NC
3542 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
3543 /* I feel that it should be possible to avoid this mortal copy
3544 given that the code below copies into a new destination.
3545 However, I suspect it isn't worth the complexity of
3546 unravelling the C<goto force_it> for the small number of
3547 cases where it would be viable to drop into the copy code. */
3548 TARG = sv_2mortal(newSVsv(TARG));
3549 }
6ac6605d 3550 orig = SvPV_force_nomg(TARG, len);
a0d0e21e
LW
3551 goto force_it;
3552 }
db2c6cb3 3553#ifdef PERL_ANY_COW
ed252734
NC
3554 have_a_cow:
3555#endif
196a02af 3556 if (RXp_MATCH_TAINTED(prog)) /* run time pattern taint, eg locale */
20be6587 3557 rxtainted |= SUBST_TAINT_PAT;
1754320d 3558 repl = dstr;
196a02af 3559 s = RXp_OFFS(prog)[0].start + orig;
0395280b
DM
3560 dstr = newSVpvn_flags(orig, s-orig,
3561 SVs_TEMP | (DO_UTF8(TARG) ? SVf_UTF8 : 0));
a0d0e21e 3562 if (!c) {
eb578fdb 3563 PERL_CONTEXT *cx;
8ec5e241 3564 SPAGAIN;
0395280b 3565 m = orig;
20be6587
DM
3566 /* note that a whole bunch of local vars are saved here for
3567 * use by pp_substcont: here's a list of them in case you're
3568 * searching for places in this sub that uses a particular var:
3569 * iters maxiters r_flags oldsave rxtainted orig dstr targ
3570 * s m strend rx once */
490576d1 3571 CX_PUSHSUBST(cx);
20e98b0f 3572 RETURNOP(cPMOP->op_pmreplrootu.op_pmreplroot);
a0d0e21e 3573 }
1754320d 3574 first = TRUE;
a0d0e21e 3575 do {
5d9574c1 3576 if (UNLIKELY(iters++ > maxiters))
cea2e8a9 3577 DIE(aTHX_ "Substitution loop");
196a02af 3578 if (UNLIKELY(RXp_MATCH_TAINTED(prog)))
20be6587 3579 rxtainted |= SUBST_TAINT_PAT;
196a02af 3580 if (RXp_MATCH_COPIED(prog) && RXp_SUBBEG(prog) != orig) {
c67ab8f2
DM
3581 char *old_s = s;
3582 char *old_orig = orig;
196a02af 3583 assert(RXp_SUBOFFSET(prog) == 0);
c67ab8f2 3584
196a02af 3585 orig = RXp_SUBBEG(prog);
c67ab8f2
DM
3586 s = orig + (old_s - old_orig);
3587 strend = s + (strend - old_s);
a0d0e21e 3588 }
196a02af 3589 m = RXp_OFFS(prog)[0].start + orig;
64534138 3590 sv_catpvn_nomg_maybeutf8(dstr, s, m - s, DO_UTF8(TARG));
196a02af 3591 s = RXp_OFFS(prog)[0].end + orig;
1754320d
FC
3592 if (first) {
3593 /* replacement already stringified */
3594 if (clen)
64534138 3595 sv_catpvn_nomg_maybeutf8(dstr, c, clen, doutf8);
1754320d
FC
3596 first = FALSE;
3597 }
3598 else {
8df0e7a2 3599 sv_catsv(dstr, repl);
5d9574c1 3600 if (UNLIKELY(SvTAINTED(repl)))
bb933b9b 3601 rxtainted |= SUBST_TAINT_REPL;
1754320d 3602 }
a0d0e21e
LW
3603 if (once)
3604 break;
ff27773b
KW
3605 } while (CALLREGEXEC(rx, s, strend, orig,
3606 s == m, /* Yields minend of 0 or 1 */
d5e7783a
DM
3607 TARG, NULL,
3608 REXEC_NOT_FIRST|REXEC_IGNOREPOS|REXEC_FAIL_ON_UNDERFLOW));
aba224f7 3609 assert(strend >= s);
64534138 3610 sv_catpvn_nomg_maybeutf8(dstr, s, strend - s, DO_UTF8(TARG));
748a9306 3611
8ca8a454
NC
3612 if (rpm->op_pmflags & PMf_NONDESTRUCT) {
3613 /* From here on down we're using the copy, and leaving the original
3614 untouched. */
3615 TARG = dstr;
3616 SPAGAIN;
3617 PUSHs(dstr);
3618 } else {
db2c6cb3 3619#ifdef PERL_ANY_COW
8ca8a454
NC
3620 /* The match may make the string COW. If so, brilliant, because
3621 that's just saved us one malloc, copy and free - the regexp has
3622 donated the old buffer, and we malloc an entirely new one, rather
3623 than the regexp malloc()ing a buffer and copying our original,
3624 only for us to throw it away here during the substitution. */
3625 if (SvIsCOW(TARG)) {
3626 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
3627 } else
ed252734 3628#endif
8ca8a454
NC
3629 {
3630 SvPV_free(TARG);
3631 }
3632 SvPV_set(TARG, SvPVX(dstr));
3633 SvCUR_set(TARG, SvCUR(dstr));
3634 SvLEN_set(TARG, SvLEN(dstr));
64534138 3635 SvFLAGS(TARG) |= SvUTF8(dstr);
8ca8a454 3636 SvPV_set(dstr, NULL);
748a9306 3637
8ca8a454 3638 SPAGAIN;
3c6ef0a5 3639 mPUSHi(iters);
8ca8a454
NC
3640 }
3641 }
3642
3643 if (!(rpm->op_pmflags & PMf_NONDESTRUCT)) {
3644 (void)SvPOK_only_UTF8(TARG);
a0d0e21e 3645 }
20be6587 3646
ef07e810 3647 /* See "how taint works" above */
284167a5 3648 if (TAINTING_get) {
20be6587
DM
3649 if ((rxtainted & SUBST_TAINT_PAT) ||
3650 ((rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT)) ==
3651 (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
3652 )
196a02af 3653 (RXp_MATCH_TAINTED_on(prog)); /* taint $1 et al */
20be6587
DM
3654
3655 if (!(rxtainted & SUBST_TAINT_BOOLRET)
3656 && (rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
3657 )