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