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