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