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