This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add PERL_NO_GET_CONTEXT to Math::BigInit::FastCalc.
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.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/*
4ac71550
TC
12 * Now far ahead the Road has gone,
13 * And I must follow, if I can,
14 * Pursuing it with eager feet,
15 * Until it joins some larger way
16 * Where many paths and errands meet.
17 * And whither then? I cannot say.
18 *
19 * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
a0d0e21e
LW
20 */
21
166f8a29
DM
22/* This file contains control-oriented pp ("push/pop") functions that
23 * execute the opcodes that make up a perl program. A typical pp function
24 * expects to find its arguments on the stack, and usually pushes its
25 * results onto the stack, hence the 'pp' terminology. Each OP structure
26 * contains a pointer to the relevant pp_foo() function.
27 *
28 * Control-oriented means things like pp_enteriter() and pp_next(), which
29 * alter the flow of control of the program.
30 */
31
32
a0d0e21e 33#include "EXTERN.h"
864dbfa3 34#define PERL_IN_PP_CTL_C
a0d0e21e
LW
35#include "perl.h"
36
54310121 37#define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
1e422769 38
94fcd414
NC
39#define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
40
a0d0e21e
LW
41PP(pp_wantarray)
42{
97aff369 43 dVAR;
39644a26 44 dSP;
a0d0e21e
LW
45 I32 cxix;
46 EXTEND(SP, 1);
47
48 cxix = dopoptosub(cxstack_ix);
49 if (cxix < 0)
50 RETPUSHUNDEF;
51
54310121 52 switch (cxstack[cxix].blk_gimme) {
53 case G_ARRAY:
a0d0e21e 54 RETPUSHYES;
54310121 55 case G_SCALAR:
a0d0e21e 56 RETPUSHNO;
54310121 57 default:
58 RETPUSHUNDEF;
59 }
a0d0e21e
LW
60}
61
2cd61cdb
IZ
62PP(pp_regcreset)
63{
97aff369 64 dVAR;
2cd61cdb
IZ
65 /* XXXX Should store the old value to allow for tie/overload - and
66 restore in regcomp, where marked with XXXX. */
3280af22 67 PL_reginterp_cnt = 0;
0b4182de 68 TAINT_NOT;
2cd61cdb
IZ
69 return NORMAL;
70}
71
b3eb6a9b
GS
72PP(pp_regcomp)
73{
97aff369 74 dVAR;
39644a26 75 dSP;
a0d0e21e 76 register PMOP *pm = (PMOP*)cLOGOP->op_other;
a0d0e21e 77 SV *tmpstr;
84679df5 78 REGEXP *re = NULL;
bfed75c6 79
4b5a0d1c 80 /* prevent recompiling under /o and ithreads. */
3db8f154 81#if defined(USE_ITHREADS)
131b3ad0
DM
82 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
83 if (PL_op->op_flags & OPf_STACKED) {
84 dMARK;
85 SP = MARK;
86 }
87 else
88 (void)POPs;
89 RETURN;
90 }
513629ba 91#endif
d4b87e75
BM
92
93#define tryAMAGICregexp(rx) \
94 STMT_START { \
6f1401dc 95 SvGETMAGIC(rx); \
d4b87e75 96 if (SvROK(rx) && SvAMAGIC(rx)) { \
31d632c3 97 SV *sv = AMG_CALLunary(rx, regexp_amg); \
d4b87e75
BM
98 if (sv) { \
99 if (SvROK(sv)) \
100 sv = SvRV(sv); \
101 if (SvTYPE(sv) != SVt_REGEXP) \
102 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
103 rx = sv; \
104 } \
105 } \
106 } STMT_END
107
108
131b3ad0 109 if (PL_op->op_flags & OPf_STACKED) {
486ec47a 110 /* multiple args; concatenate them */
131b3ad0
DM
111 dMARK; dORIGMARK;
112 tmpstr = PAD_SV(ARGTARG);
76f68e9b 113 sv_setpvs(tmpstr, "");
131b3ad0 114 while (++MARK <= SP) {
d4b87e75 115 SV *msv = *MARK;
79a8d529 116 SV *sv;
d4b87e75 117
79a8d529 118 tryAMAGICregexp(msv);
d4b87e75 119
79a8d529
DM
120 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
121 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
122 {
123 sv_setsv(tmpstr, sv);
124 continue;
131b3ad0 125 }
a9984b10 126 sv_catsv_nomg(tmpstr, msv);
131b3ad0
DM
127 }
128 SvSETMAGIC(tmpstr);
129 SP = ORIGMARK;
130 }
d4b87e75 131 else {
131b3ad0 132 tmpstr = POPs;
d4b87e75
BM
133 tryAMAGICregexp(tmpstr);
134 }
135
136#undef tryAMAGICregexp
513629ba 137
b3eb6a9b 138 if (SvROK(tmpstr)) {
d8f6592e 139 SV * const sv = SvRV(tmpstr);
5c35adbb 140 if (SvTYPE(sv) == SVt_REGEXP)
d2f13c59 141 re = (REGEXP*) sv;
c277df42 142 }
d4b87e75
BM
143 else if (SvTYPE(tmpstr) == SVt_REGEXP)
144 re = (REGEXP*) tmpstr;
145
5c35adbb 146 if (re) {
69dc4b30
FC
147 /* The match's LHS's get-magic might need to access this op's reg-
148 exp (as is sometimes the case with $'; see bug 70764). So we
149 must call get-magic now before we replace the regexp. Hopeful-
150 ly this hack can be replaced with the approach described at
151 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
152 /msg122415.html some day. */
455d9033
FC
153 if(pm->op_type == OP_MATCH) {
154 SV *lhs;
155 const bool was_tainted = PL_tainted;
156 if (pm->op_flags & OPf_STACKED)
69dc4b30 157 lhs = TOPs;
455d9033
FC
158 else if (pm->op_private & OPpTARGET_MY)
159 lhs = PAD_SV(pm->op_targ);
160 else lhs = DEFSV;
161 SvGETMAGIC(lhs);
162 /* Restore the previous value of PL_tainted (which may have been
163 modified by get-magic), to avoid incorrectly setting the
164 RXf_TAINTED flag further down. */
165 PL_tainted = was_tainted;
166 }
69dc4b30 167
f0826785 168 re = reg_temp_copy(NULL, re);
aaa362c4 169 ReREFCNT_dec(PM_GETRE(pm));
28d8d7f4 170 PM_SETRE(pm, re);
c277df42
IZ
171 }
172 else {
f3ec07c7
DM
173 STRLEN len = 0;
174 const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : "";
175
c737faaf 176 re = PM_GETRE(pm);
14a49a24 177 assert (re != (REGEXP*) &PL_sv_undef);
c277df42 178
20408e3c 179 /* Check against the last compiled regexp. */
a11c8683 180 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
220fc49f 181 memNE(RX_PRECOMP(re), t, len))
85aff577 182 {
07bc277f 183 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
73134a2e 184 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
d8f6592e
AL
185 if (re) {
186 ReREFCNT_dec(re);
14a49a24
NC
187#ifdef USE_ITHREADS
188 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
189#else
4608196e 190 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
14a49a24 191#endif
1e2e3d02 192 } else if (PL_curcop->cop_hints_hash) {
20439bc7 193 SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0);
1e2e3d02
YO
194 if (ptr && SvIOK(ptr) && SvIV(ptr))
195 eng = INT2PTR(regexp_engine*,SvIV(ptr));
c277df42 196 }
664e119d 197
533c011a 198 if (PL_op->op_flags & OPf_SPECIAL)
3280af22 199 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
a0d0e21e 200
b9ad30b4
NC
201 if (DO_UTF8(tmpstr)) {
202 assert (SvUTF8(tmpstr));
203 } else if (SvUTF8(tmpstr)) {
204 /* Not doing UTF-8, despite what the SV says. Is this only if
205 we're trapped in use 'bytes'? */
206 /* Make a copy of the octet sequence, but without the flag on,
207 as the compiler now honours the SvUTF8 flag on tmpstr. */
208 STRLEN len;
209 const char *const p = SvPV(tmpstr, len);
210 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
211 }
f3ec07c7
DM
212 else if (SvAMAGIC(tmpstr)) {
213 /* make a copy to avoid extra stringifies */
0479a84a 214 tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
f3ec07c7 215 }
c737faaf 216
a9984b10
FC
217 /* If it is gmagical, create a mortal copy, but without calling
218 get-magic, as we have already done that. */
219 if(SvGMAGICAL(tmpstr)) {
220 SV *mortalcopy = sv_newmortal();
221 sv_setsv_flags(mortalcopy, tmpstr, 0);
222 tmpstr = mortalcopy;
223 }
224
5a8697a7 225 if (eng)
3ab4a224 226 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
5a8697a7 227 else
3ab4a224 228 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
c737faaf 229
f86aaa29 230 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
2cd61cdb 231 inside tie/overload accessors. */
c277df42 232 }
4633a7c4 233 }
c737faaf
YO
234
235 re = PM_GETRE(pm);
a0d0e21e 236
72311751 237#ifndef INCOMPLETE_TAINTS
3280af22 238 if (PL_tainting) {
9274aefd
DM
239 if (PL_tainted) {
240 SvTAINTED_on((SV*)re);
07bc277f 241 RX_EXTFLAGS(re) |= RXf_TAINTED;
9274aefd 242 }
72311751
GS
243 }
244#endif
245
220fc49f 246 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
3280af22 247 pm = PL_curpm;
a0d0e21e 248
c737faaf
YO
249
250#if !defined(USE_ITHREADS)
251 /* can't change the optree at runtime either */
252 /* PMf_KEEP is handled differently under threads to avoid these problems */
a0d0e21e 253 if (pm->op_pmflags & PMf_KEEP) {
c90c0ff4 254 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
533c011a 255 cLOGOP->op_first->op_next = PL_op->op_next;
a0d0e21e 256 }
c737faaf 257#endif
a0d0e21e
LW
258 RETURN;
259}
260
261PP(pp_substcont)
262{
97aff369 263 dVAR;
39644a26 264 dSP;
c09156bb 265 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
901017d6
AL
266 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
267 register SV * const dstr = cx->sb_dstr;
a0d0e21e
LW
268 register char *s = cx->sb_s;
269 register char *m = cx->sb_m;
270 char *orig = cx->sb_orig;
901017d6 271 register REGEXP * const rx = cx->sb_rx;
c445ea15 272 SV *nsv = NULL;
988e6e7e 273 REGEXP *old = PM_GETRE(pm);
f410a211
NC
274
275 PERL_ASYNC_CHECK();
276
988e6e7e 277 if(old != rx) {
bfed75c6 278 if(old)
988e6e7e 279 ReREFCNT_dec(old);
d6106309 280 PM_SETRE(pm,ReREFCNT_inc(rx));
d8f2cf8a
AB
281 }
282
d9f97599 283 rxres_restore(&cx->sb_rxres, rx);
01b35787 284 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
c90c0ff4 285
a0d0e21e 286 if (cx->sb_iters++) {
a3b680e6 287 const I32 saviters = cx->sb_iters;
a0d0e21e 288 if (cx->sb_iters > cx->sb_maxiters)
cea2e8a9 289 DIE(aTHX_ "Substitution loop");
a0d0e21e 290
447ee134
DM
291 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
292
ef07e810 293 /* See "how taint works" above pp_subst() */
20be6587
DM
294 if (SvTAINTED(TOPs))
295 cx->sb_rxtainted |= SUBST_TAINT_REPL;
447ee134 296 sv_catsv_nomg(dstr, POPs);
2c296965
YO
297 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
298 s -= RX_GOFS(rx);
a0d0e21e
LW
299
300 /* Are we done */
2c296965
YO
301 if (CxONCE(cx) || s < orig ||
302 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
303 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
304 ((cx->sb_rflags & REXEC_COPY_STR)
305 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
306 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
a0d0e21e 307 {
823a54a3 308 SV * const targ = cx->sb_targ;
748a9306 309
078c425b
JH
310 assert(cx->sb_strend >= s);
311 if(cx->sb_strend > s) {
312 if (DO_UTF8(dstr) && !SvUTF8(targ))
313 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
314 else
315 sv_catpvn(dstr, s, cx->sb_strend - s);
316 }
20be6587
DM
317 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
318 cx->sb_rxtainted |= SUBST_TAINT_PAT;
9212bbba 319
f8c7b90f 320#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
321 if (SvIsCOW(targ)) {
322 sv_force_normal_flags(targ, SV_COW_DROP_PV);
323 } else
324#endif
325 {
8bd4d4c5 326 SvPV_free(targ);
ed252734 327 }
f880fe2f 328 SvPV_set(targ, SvPVX(dstr));
748a9306
LW
329 SvCUR_set(targ, SvCUR(dstr));
330 SvLEN_set(targ, SvLEN(dstr));
1aa99e6b
IH
331 if (DO_UTF8(dstr))
332 SvUTF8_on(targ);
6136c704 333 SvPV_set(dstr, NULL);
48c036b1 334
4f4d7508
DC
335 if (pm->op_pmflags & PMf_NONDESTRUCT)
336 PUSHs(targ);
337 else
338 mPUSHi(saviters - 1);
48c036b1 339
ffc61ed2 340 (void)SvPOK_only_UTF8(targ);
5cd24f17 341
20be6587 342 /* update the taint state of various various variables in
ef07e810
DM
343 * preparation for final exit.
344 * See "how taint works" above pp_subst() */
20be6587
DM
345 if (PL_tainting) {
346 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
347 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
348 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
349 )
350 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
351
352 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
353 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
354 )
355 SvTAINTED_on(TOPs); /* taint return value */
356 /* needed for mg_set below */
357 PL_tainted = cBOOL(cx->sb_rxtainted &
358 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL));
359 SvTAINT(TARG);
360 }
361 /* PL_tainted must be correctly set for this mg_set */
362 SvSETMAGIC(TARG);
363 TAINT_NOT;
4633a7c4 364 LEAVE_SCOPE(cx->sb_oldsave);
a0d0e21e
LW
365 POPSUBST(cx);
366 RETURNOP(pm->op_next);
20be6587 367 /* NOTREACHED */
a0d0e21e 368 }
8e5e9ebe 369 cx->sb_iters = saviters;
a0d0e21e 370 }
07bc277f 371 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
372 m = s;
373 s = orig;
07bc277f 374 cx->sb_orig = orig = RX_SUBBEG(rx);
a0d0e21e
LW
375 s = orig + (m - s);
376 cx->sb_strend = s + (cx->sb_strend - m);
377 }
07bc277f 378 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
db79b45b 379 if (m > s) {
bfed75c6 380 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
db79b45b
JH
381 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
382 else
383 sv_catpvn(dstr, s, m-s);
384 }
07bc277f 385 cx->sb_s = RX_OFFS(rx)[0].end + orig;
084916e3 386 { /* Update the pos() information. */
44f8325f 387 SV * const sv = cx->sb_targ;
084916e3 388 MAGIC *mg;
7a7f3e45 389 SvUPGRADE(sv, SVt_PVMG);
14befaf4 390 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
d83f0a82 391#ifdef PERL_OLD_COPY_ON_WRITE
51a9ea20 392 if (SvIsCOW(sv))
d83f0a82
NC
393 sv_force_normal_flags(sv, 0);
394#endif
395 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
396 NULL, 0);
084916e3 397 }
ce474962 398 mg->mg_len = m - orig;
084916e3 399 }
988e6e7e 400 if (old != rx)
d6106309 401 (void)ReREFCNT_inc(rx);
20be6587 402 /* update the taint state of various various variables in preparation
ef07e810
DM
403 * for calling the code block.
404 * See "how taint works" above pp_subst() */
20be6587
DM
405 if (PL_tainting) {
406 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
407 cx->sb_rxtainted |= SUBST_TAINT_PAT;
408
409 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
410 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
411 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
412 )
413 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
414
415 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
416 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
417 SvTAINTED_on(cx->sb_targ);
418 TAINT_NOT;
419 }
d9f97599 420 rxres_save(&cx->sb_rxres, rx);
af9838cc 421 PL_curpm = pm;
29f2e912 422 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
a0d0e21e
LW
423}
424
c90c0ff4 425void
864dbfa3 426Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4 427{
428 UV *p = (UV*)*rsp;
429 U32 i;
7918f24d
NC
430
431 PERL_ARGS_ASSERT_RXRES_SAVE;
96a5add6 432 PERL_UNUSED_CONTEXT;
c90c0ff4 433
07bc277f 434 if (!p || p[1] < RX_NPARENS(rx)) {
f8c7b90f 435#ifdef PERL_OLD_COPY_ON_WRITE
07bc277f 436 i = 7 + RX_NPARENS(rx) * 2;
ed252734 437#else
07bc277f 438 i = 6 + RX_NPARENS(rx) * 2;
ed252734 439#endif
c90c0ff4 440 if (!p)
a02a5408 441 Newx(p, i, UV);
c90c0ff4 442 else
443 Renew(p, i, UV);
444 *rsp = (void*)p;
445 }
446
07bc277f 447 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
cf93c79d 448 RX_MATCH_COPIED_off(rx);
c90c0ff4 449
f8c7b90f 450#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
451 *p++ = PTR2UV(RX_SAVED_COPY(rx));
452 RX_SAVED_COPY(rx) = NULL;
ed252734
NC
453#endif
454
07bc277f 455 *p++ = RX_NPARENS(rx);
c90c0ff4 456
07bc277f
NC
457 *p++ = PTR2UV(RX_SUBBEG(rx));
458 *p++ = (UV)RX_SUBLEN(rx);
459 for (i = 0; i <= RX_NPARENS(rx); ++i) {
460 *p++ = (UV)RX_OFFS(rx)[i].start;
461 *p++ = (UV)RX_OFFS(rx)[i].end;
c90c0ff4 462 }
463}
464
9c105995
NC
465static void
466S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4 467{
468 UV *p = (UV*)*rsp;
469 U32 i;
7918f24d
NC
470
471 PERL_ARGS_ASSERT_RXRES_RESTORE;
96a5add6 472 PERL_UNUSED_CONTEXT;
c90c0ff4 473
ed252734 474 RX_MATCH_COPY_FREE(rx);
cf93c79d 475 RX_MATCH_COPIED_set(rx, *p);
c90c0ff4 476 *p++ = 0;
477
f8c7b90f 478#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
479 if (RX_SAVED_COPY(rx))
480 SvREFCNT_dec (RX_SAVED_COPY(rx));
481 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
ed252734
NC
482 *p++ = 0;
483#endif
484
07bc277f 485 RX_NPARENS(rx) = *p++;
c90c0ff4 486
07bc277f
NC
487 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
488 RX_SUBLEN(rx) = (I32)(*p++);
489 for (i = 0; i <= RX_NPARENS(rx); ++i) {
490 RX_OFFS(rx)[i].start = (I32)(*p++);
491 RX_OFFS(rx)[i].end = (I32)(*p++);
c90c0ff4 492 }
493}
494
9c105995
NC
495static void
496S_rxres_free(pTHX_ void **rsp)
c90c0ff4 497{
44f8325f 498 UV * const p = (UV*)*rsp;
7918f24d
NC
499
500 PERL_ARGS_ASSERT_RXRES_FREE;
96a5add6 501 PERL_UNUSED_CONTEXT;
c90c0ff4 502
503 if (p) {
94010e71
NC
504#ifdef PERL_POISON
505 void *tmp = INT2PTR(char*,*p);
506 Safefree(tmp);
507 if (*p)
7e337ee0 508 PoisonFree(*p, 1, sizeof(*p));
94010e71 509#else
56431972 510 Safefree(INT2PTR(char*,*p));
94010e71 511#endif
f8c7b90f 512#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
513 if (p[1]) {
514 SvREFCNT_dec (INT2PTR(SV*,p[1]));
515 }
516#endif
c90c0ff4 517 Safefree(p);
4608196e 518 *rsp = NULL;
c90c0ff4 519 }
520}
521
a701009a
DM
522#define FORM_NUM_BLANK (1<<30)
523#define FORM_NUM_POINT (1<<29)
524
a0d0e21e
LW
525PP(pp_formline)
526{
97aff369 527 dVAR; dSP; dMARK; dORIGMARK;
823a54a3 528 register SV * const tmpForm = *++MARK;
086b26f3
DM
529 SV *formsv; /* contains text of original format */
530 register U32 *fpc; /* format ops program counter */
531 register char *t; /* current append position in target string */
532 const char *f; /* current position in format string */
a0d0e21e 533 register I32 arg;
086b26f3
DM
534 register SV *sv = NULL; /* current item */
535 const char *item = NULL;/* string value of current item */
536 I32 itemsize = 0; /* length of current item, possibly truncated */
537 I32 fieldsize = 0; /* width of current field */
538 I32 lines = 0; /* number of lines that have been output */
539 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
540 const char *chophere = NULL; /* where to chop current item */
f5ada144 541 STRLEN linemark = 0; /* pos of start of line in output */
65202027 542 NV value;
086b26f3 543 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
a0d0e21e 544 STRLEN len;
26e935cf 545 STRLEN linemax; /* estimate of output size in bytes */
1bd51a4c
IH
546 bool item_is_utf8 = FALSE;
547 bool targ_is_utf8 = FALSE;
bfed75c6 548 const char *fmt;
74e0ddf7 549 MAGIC *mg = NULL;
4ff700b9
DM
550 U8 *source; /* source of bytes to append */
551 STRLEN to_copy; /* how may bytes to append */
ea60cfe8 552 char trans; /* what chars to translate */
74e0ddf7 553
3808a683 554 mg = doparseform(tmpForm);
a0d0e21e 555
74e0ddf7 556 fpc = (U32*)mg->mg_ptr;
3808a683
DM
557 /* the actual string the format was compiled from.
558 * with overload etc, this may not match tmpForm */
559 formsv = mg->mg_obj;
560
74e0ddf7 561
3280af22 562 SvPV_force(PL_formtarget, len);
3808a683 563 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
125b9982 564 SvTAINTED_on(PL_formtarget);
1bd51a4c
IH
565 if (DO_UTF8(PL_formtarget))
566 targ_is_utf8 = TRUE;
26e935cf
DM
567 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
568 t = SvGROW(PL_formtarget, len + linemax + 1);
569 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
a0d0e21e 570 t += len;
3808a683 571 f = SvPV_const(formsv, len);
a0d0e21e
LW
572
573 for (;;) {
574 DEBUG_f( {
bfed75c6 575 const char *name = "???";
a0d0e21e
LW
576 arg = -1;
577 switch (*fpc) {
578 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
579 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
580 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
581 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
582 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
583
584 case FF_CHECKNL: name = "CHECKNL"; break;
585 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
586 case FF_SPACE: name = "SPACE"; break;
587 case FF_HALFSPACE: name = "HALFSPACE"; break;
588 case FF_ITEM: name = "ITEM"; break;
589 case FF_CHOP: name = "CHOP"; break;
590 case FF_LINEGLOB: name = "LINEGLOB"; break;
591 case FF_NEWLINE: name = "NEWLINE"; break;
592 case FF_MORE: name = "MORE"; break;
593 case FF_LINEMARK: name = "LINEMARK"; break;
594 case FF_END: name = "END"; break;
bfed75c6 595 case FF_0DECIMAL: name = "0DECIMAL"; break;
a1b95068 596 case FF_LINESNGL: name = "LINESNGL"; break;
a0d0e21e
LW
597 }
598 if (arg >= 0)
bf49b057 599 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
a0d0e21e 600 else
bf49b057 601 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
5f80b19c 602 } );
a0d0e21e
LW
603 switch (*fpc++) {
604 case FF_LINEMARK:
f5ada144 605 linemark = t - SvPVX(PL_formtarget);
a0d0e21e
LW
606 lines++;
607 gotsome = FALSE;
608 break;
609
610 case FF_LITERAL:
ea60cfe8
DM
611 to_copy = *fpc++;
612 source = (U8 *)f;
613 f += to_copy;
614 trans = '~';
75645721 615 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
ea60cfe8 616 goto append;
a0d0e21e
LW
617
618 case FF_SKIP:
619 f += *fpc++;
620 break;
621
622 case FF_FETCH:
623 arg = *fpc++;
624 f += arg;
625 fieldsize = arg;
626
627 if (MARK < SP)
628 sv = *++MARK;
629 else {
3280af22 630 sv = &PL_sv_no;
a2a5de95 631 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
a0d0e21e 632 }
125b9982
NT
633 if (SvTAINTED(sv))
634 SvTAINTED_on(PL_formtarget);
a0d0e21e
LW
635 break;
636
637 case FF_CHECKNL:
5a34cab7
NC
638 {
639 const char *send;
640 const char *s = item = SvPV_const(sv, len);
641 itemsize = len;
642 if (DO_UTF8(sv)) {
643 itemsize = sv_len_utf8(sv);
644 if (itemsize != (I32)len) {
645 I32 itembytes;
646 if (itemsize > fieldsize) {
647 itemsize = fieldsize;
648 itembytes = itemsize;
649 sv_pos_u2b(sv, &itembytes, 0);
650 }
651 else
652 itembytes = len;
653 send = chophere = s + itembytes;
654 while (s < send) {
655 if (*s & ~31)
656 gotsome = TRUE;
657 else if (*s == '\n')
658 break;
659 s++;
660 }
661 item_is_utf8 = TRUE;
662 itemsize = s - item;
663 sv_pos_b2u(sv, &itemsize);
664 break;
a0ed51b3 665 }
a0ed51b3 666 }
5a34cab7
NC
667 item_is_utf8 = FALSE;
668 if (itemsize > fieldsize)
669 itemsize = fieldsize;
670 send = chophere = s + itemsize;
671 while (s < send) {
672 if (*s & ~31)
673 gotsome = TRUE;
674 else if (*s == '\n')
675 break;
676 s++;
677 }
678 itemsize = s - item;
679 break;
a0ed51b3 680 }
a0d0e21e
LW
681
682 case FF_CHECKCHOP:
5a34cab7
NC
683 {
684 const char *s = item = SvPV_const(sv, len);
685 itemsize = len;
686 if (DO_UTF8(sv)) {
687 itemsize = sv_len_utf8(sv);
688 if (itemsize != (I32)len) {
689 I32 itembytes;
690 if (itemsize <= fieldsize) {
691 const char *send = chophere = s + itemsize;
692 while (s < send) {
693 if (*s == '\r') {
694 itemsize = s - item;
a0ed51b3 695 chophere = s;
a0ed51b3 696 break;
5a34cab7
NC
697 }
698 if (*s++ & ~31)
a0ed51b3 699 gotsome = TRUE;
a0ed51b3 700 }
a0ed51b3 701 }
5a34cab7
NC
702 else {
703 const char *send;
704 itemsize = fieldsize;
705 itembytes = itemsize;
706 sv_pos_u2b(sv, &itembytes, 0);
707 send = chophere = s + itembytes;
708 while (s < send || (s == send && isSPACE(*s))) {
709 if (isSPACE(*s)) {
710 if (chopspace)
711 chophere = s;
712 if (*s == '\r')
713 break;
714 }
715 else {
716 if (*s & ~31)
717 gotsome = TRUE;
718 if (strchr(PL_chopset, *s))
719 chophere = s + 1;
720 }
721 s++;
722 }
723 itemsize = chophere - item;
724 sv_pos_b2u(sv, &itemsize);
725 }
726 item_is_utf8 = TRUE;
a0d0e21e
LW
727 break;
728 }
a0d0e21e 729 }
5a34cab7
NC
730 item_is_utf8 = FALSE;
731 if (itemsize <= fieldsize) {
732 const char *const send = chophere = s + itemsize;
733 while (s < send) {
734 if (*s == '\r') {
735 itemsize = s - item;
a0d0e21e 736 chophere = s;
a0d0e21e 737 break;
5a34cab7
NC
738 }
739 if (*s++ & ~31)
a0d0e21e 740 gotsome = TRUE;
a0d0e21e 741 }
a0d0e21e 742 }
5a34cab7
NC
743 else {
744 const char *send;
745 itemsize = fieldsize;
746 send = chophere = s + itemsize;
747 while (s < send || (s == send && isSPACE(*s))) {
748 if (isSPACE(*s)) {
749 if (chopspace)
750 chophere = s;
751 if (*s == '\r')
752 break;
753 }
754 else {
755 if (*s & ~31)
756 gotsome = TRUE;
757 if (strchr(PL_chopset, *s))
758 chophere = s + 1;
759 }
760 s++;
761 }
762 itemsize = chophere - item;
763 }
764 break;
a0d0e21e 765 }
a0d0e21e
LW
766
767 case FF_SPACE:
768 arg = fieldsize - itemsize;
769 if (arg) {
770 fieldsize -= arg;
771 while (arg-- > 0)
772 *t++ = ' ';
773 }
774 break;
775
776 case FF_HALFSPACE:
777 arg = fieldsize - itemsize;
778 if (arg) {
779 arg /= 2;
780 fieldsize -= arg;
781 while (arg-- > 0)
782 *t++ = ' ';
783 }
784 break;
785
786 case FF_ITEM:
8aa7beb6
DM
787 to_copy = itemsize;
788 source = (U8 *)item;
789 trans = 1;
790 if (item_is_utf8) {
791 /* convert to_copy from chars to bytes */
792 U8 *s = source;
793 while (to_copy--)
794 s += UTF8SKIP(s);
795 to_copy = s - source;
a0d0e21e 796 }
8aa7beb6 797 goto append;
a0d0e21e
LW
798
799 case FF_CHOP:
5a34cab7
NC
800 {
801 const char *s = chophere;
802 if (chopspace) {
af68e756 803 while (isSPACE(*s))
5a34cab7
NC
804 s++;
805 }
806 sv_chop(sv,s);
807 SvSETMAGIC(sv);
808 break;
a0d0e21e 809 }
a0d0e21e 810
a1b95068
WL
811 case FF_LINESNGL:
812 chopspace = 0;
a0d0e21e 813 case FF_LINEGLOB:
5a34cab7 814 {
e32383e2 815 const bool oneline = fpc[-1] == FF_LINESNGL;
5a34cab7 816 const char *s = item = SvPV_const(sv, len);
7440a75b 817 const char *const send = s + len;
7440a75b 818
f3f2f1a3 819 item_is_utf8 = DO_UTF8(sv);
a1137ee5 820 if (!len)
7440a75b 821 break;
ea60cfe8 822 trans = 0;
0d21cefe 823 gotsome = TRUE;
a1137ee5 824 chophere = s + len;
4ff700b9
DM
825 source = (U8 *) s;
826 to_copy = len;
0d21cefe
DM
827 while (s < send) {
828 if (*s++ == '\n') {
829 if (oneline) {
830 to_copy = s - SvPVX_const(sv) - 1;
831 chophere = s;
832 break;
833 } else {
834 if (s == send) {
0d21cefe
DM
835 to_copy--;
836 } else
837 lines++;
1bd51a4c 838 }
a0d0e21e 839 }
0d21cefe 840 }
a2c0032b
DM
841 }
842
ea60cfe8
DM
843 append:
844 /* append to_copy bytes from source to PL_formstring.
845 * item_is_utf8 implies source is utf8.
846 * if trans, translate certain characters during the copy */
a2c0032b
DM
847 {
848 U8 *tmp = NULL;
26e935cf 849 STRLEN grow = 0;
0325ce87
DM
850
851 SvCUR_set(PL_formtarget,
852 t - SvPVX_const(PL_formtarget));
853
0d21cefe
DM
854 if (targ_is_utf8 && !item_is_utf8) {
855 source = tmp = bytes_to_utf8(source, &to_copy);
0d21cefe
DM
856 } else {
857 if (item_is_utf8 && !targ_is_utf8) {
f5ada144 858 U8 *s;
0d21cefe 859 /* Upgrade targ to UTF8, and then we reduce it to
0325ce87
DM
860 a problem we have a simple solution for.
861 Don't need get magic. */
0d21cefe 862 sv_utf8_upgrade_nomg(PL_formtarget);
0325ce87 863 targ_is_utf8 = TRUE;
f5ada144
DM
864 /* re-calculate linemark */
865 s = (U8*)SvPVX(PL_formtarget);
26e935cf
DM
866 /* the bytes we initially allocated to append the
867 * whole line may have been gobbled up during the
868 * upgrade, so allocate a whole new line's worth
869 * for safety */
870 grow = linemax;
f5ada144
DM
871 while (linemark--)
872 s += UTF8SKIP(s);
873 linemark = s - (U8*)SvPVX(PL_formtarget);
e8e72d41 874 }
0d21cefe
DM
875 /* Easy. They agree. */
876 assert (item_is_utf8 == targ_is_utf8);
877 }
26e935cf
DM
878 if (!trans)
879 /* @* and ^* are the only things that can exceed
880 * the linemax, so grow by the output size, plus
881 * a whole new form's worth in case of any further
882 * output */
883 grow = linemax + to_copy;
884 if (grow)
885 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
0d21cefe
DM
886 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
887
888 Copy(source, t, to_copy, char);
ea60cfe8 889 if (trans) {
8aa7beb6
DM
890 /* blank out ~ or control chars, depending on trans.
891 * works on bytes not chars, so relies on not
892 * matching utf8 continuation bytes */
ea60cfe8
DM
893 U8 *s = (U8*)t;
894 U8 *send = s + to_copy;
895 while (s < send) {
8aa7beb6
DM
896 const int ch = *s;
897 if (trans == '~' ? (ch == '~') :
898#ifdef EBCDIC
899 iscntrl(ch)
900#else
901 (!(ch & ~31))
902#endif
903 )
ea60cfe8
DM
904 *s = ' ';
905 s++;
906 }
907 }
908
0d21cefe
DM
909 t += to_copy;
910 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
a1137ee5 911 if (tmp)
0d21cefe 912 Safefree(tmp);
5a34cab7 913 break;
a0d0e21e 914 }
a0d0e21e 915
a1b95068
WL
916 case FF_0DECIMAL:
917 arg = *fpc++;
918#if defined(USE_LONG_DOUBLE)
10edeb5d 919 fmt = (const char *)
a701009a 920 ((arg & FORM_NUM_POINT) ?
10edeb5d 921 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
a1b95068 922#else
10edeb5d 923 fmt = (const char *)
a701009a 924 ((arg & FORM_NUM_POINT) ?
10edeb5d 925 "%#0*.*f" : "%0*.*f");
a1b95068
WL
926#endif
927 goto ff_dec;
a0d0e21e 928 case FF_DECIMAL:
a0d0e21e 929 arg = *fpc++;
65202027 930#if defined(USE_LONG_DOUBLE)
10edeb5d 931 fmt = (const char *)
a701009a 932 ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
65202027 933#else
10edeb5d 934 fmt = (const char *)
a701009a 935 ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f");
65202027 936#endif
a1b95068 937 ff_dec:
784707d5
JP
938 /* If the field is marked with ^ and the value is undefined,
939 blank it out. */
a701009a 940 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
784707d5
JP
941 arg = fieldsize;
942 while (arg--)
943 *t++ = ' ';
944 break;
945 }
946 gotsome = TRUE;
947 value = SvNV(sv);
a1b95068 948 /* overflow evidence */
bfed75c6 949 if (num_overflow(value, fieldsize, arg)) {
a1b95068
WL
950 arg = fieldsize;
951 while (arg--)
952 *t++ = '#';
953 break;
954 }
784707d5
JP
955 /* Formats aren't yet marked for locales, so assume "yes". */
956 {
957 STORE_NUMERIC_STANDARD_SET_LOCAL();
a701009a
DM
958 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
959 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value);
784707d5
JP
960 RESTORE_NUMERIC_STANDARD();
961 }
962 t += fieldsize;
963 break;
a1b95068 964
a0d0e21e
LW
965 case FF_NEWLINE:
966 f++;
f5ada144 967 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
a0d0e21e
LW
968 t++;
969 *t++ = '\n';
970 break;
971
972 case FF_BLANK:
973 arg = *fpc++;
974 if (gotsome) {
975 if (arg) { /* repeat until fields exhausted? */
11f9eeaf
DM
976 fpc--;
977 goto end;
a0d0e21e
LW
978 }
979 }
980 else {
f5ada144 981 t = SvPVX(PL_formtarget) + linemark;
a0d0e21e
LW
982 lines--;
983 }
984 break;
985
986 case FF_MORE:
5a34cab7
NC
987 {
988 const char *s = chophere;
989 const char *send = item + len;
990 if (chopspace) {
af68e756 991 while (isSPACE(*s) && (s < send))
5a34cab7 992 s++;
a0d0e21e 993 }
5a34cab7
NC
994 if (s < send) {
995 char *s1;
996 arg = fieldsize - itemsize;
997 if (arg) {
998 fieldsize -= arg;
999 while (arg-- > 0)
1000 *t++ = ' ';
1001 }
1002 s1 = t - 3;
1003 if (strnEQ(s1," ",3)) {
1004 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1005 s1--;
1006 }
1007 *s1++ = '.';
1008 *s1++ = '.';
1009 *s1++ = '.';
a0d0e21e 1010 }
5a34cab7 1011 break;
a0d0e21e 1012 }
a0d0e21e 1013 case FF_END:
11f9eeaf 1014 end:
bf2bec63 1015 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
a0d0e21e 1016 *t = '\0';
b15aece3 1017 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
1018 if (targ_is_utf8)
1019 SvUTF8_on(PL_formtarget);
3280af22 1020 FmLINES(PL_formtarget) += lines;
a0d0e21e 1021 SP = ORIGMARK;
11f9eeaf
DM
1022 if (fpc[-1] == FF_BLANK)
1023 RETURNOP(cLISTOP->op_first);
1024 else
1025 RETPUSHYES;
a0d0e21e
LW
1026 }
1027 }
1028}
1029
1030PP(pp_grepstart)
1031{
27da23d5 1032 dVAR; dSP;
a0d0e21e
LW
1033 SV *src;
1034
3280af22 1035 if (PL_stack_base + *PL_markstack_ptr == SP) {
a0d0e21e 1036 (void)POPMARK;
54310121 1037 if (GIMME_V == G_SCALAR)
6e449a3a 1038 mXPUSHi(0);
533c011a 1039 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 1040 }
3280af22 1041 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
897d3989
NC
1042 Perl_pp_pushmark(aTHX); /* push dst */
1043 Perl_pp_pushmark(aTHX); /* push src */
d343c3ef 1044 ENTER_with_name("grep"); /* enter outer scope */
a0d0e21e
LW
1045
1046 SAVETMPS;
59f00321
RGS
1047 if (PL_op->op_private & OPpGREP_LEX)
1048 SAVESPTR(PAD_SVl(PL_op->op_targ));
1049 else
1050 SAVE_DEFSV;
d343c3ef 1051 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1052 SAVEVPTR(PL_curpm);
a0d0e21e 1053
3280af22 1054 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1055 SvTEMP_off(src);
59f00321
RGS
1056 if (PL_op->op_private & OPpGREP_LEX)
1057 PAD_SVl(PL_op->op_targ) = src;
1058 else
414bf5ae 1059 DEFSV_set(src);
a0d0e21e
LW
1060
1061 PUTBACK;
533c011a 1062 if (PL_op->op_type == OP_MAPSTART)
897d3989 1063 Perl_pp_pushmark(aTHX); /* push top */
533c011a 1064 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
1065}
1066
a0d0e21e
LW
1067PP(pp_mapwhile)
1068{
27da23d5 1069 dVAR; dSP;
f54cb97a 1070 const I32 gimme = GIMME_V;
544f3153 1071 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
a0d0e21e
LW
1072 I32 count;
1073 I32 shift;
1074 SV** src;
ac27b0f5 1075 SV** dst;
a0d0e21e 1076
544f3153 1077 /* first, move source pointer to the next item in the source list */
3280af22 1078 ++PL_markstack_ptr[-1];
544f3153
GS
1079
1080 /* if there are new items, push them into the destination list */
4c90a460 1081 if (items && gimme != G_VOID) {
544f3153
GS
1082 /* might need to make room back there first */
1083 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1084 /* XXX this implementation is very pessimal because the stack
1085 * is repeatedly extended for every set of items. Is possible
1086 * to do this without any stack extension or copying at all
1087 * by maintaining a separate list over which the map iterates
18ef8bea 1088 * (like foreach does). --gsar */
544f3153
GS
1089
1090 /* everything in the stack after the destination list moves
1091 * towards the end the stack by the amount of room needed */
1092 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1093
1094 /* items to shift up (accounting for the moved source pointer) */
1095 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
18ef8bea
BT
1096
1097 /* This optimization is by Ben Tilly and it does
1098 * things differently from what Sarathy (gsar)
1099 * is describing. The downside of this optimization is
1100 * that leaves "holes" (uninitialized and hopefully unused areas)
1101 * to the Perl stack, but on the other hand this
1102 * shouldn't be a problem. If Sarathy's idea gets
1103 * implemented, this optimization should become
1104 * irrelevant. --jhi */
1105 if (shift < count)
1106 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1107
924508f0
GS
1108 EXTEND(SP,shift);
1109 src = SP;
1110 dst = (SP += shift);
3280af22
NIS
1111 PL_markstack_ptr[-1] += shift;
1112 *PL_markstack_ptr += shift;
544f3153 1113 while (count--)
a0d0e21e
LW
1114 *dst-- = *src--;
1115 }
544f3153 1116 /* copy the new items down to the destination list */
ac27b0f5 1117 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
22023b26 1118 if (gimme == G_ARRAY) {
b2a2a901
DM
1119 /* add returned items to the collection (making mortal copies
1120 * if necessary), then clear the current temps stack frame
1121 * *except* for those items. We do this splicing the items
1122 * into the start of the tmps frame (so some items may be on
59d53fd6 1123 * the tmps stack twice), then moving PL_tmps_floor above
b2a2a901
DM
1124 * them, then freeing the frame. That way, the only tmps that
1125 * accumulate over iterations are the return values for map.
1126 * We have to do to this way so that everything gets correctly
1127 * freed if we die during the map.
1128 */
1129 I32 tmpsbase;
1130 I32 i = items;
1131 /* make space for the slice */
1132 EXTEND_MORTAL(items);
1133 tmpsbase = PL_tmps_floor + 1;
1134 Move(PL_tmps_stack + tmpsbase,
1135 PL_tmps_stack + tmpsbase + items,
1136 PL_tmps_ix - PL_tmps_floor,
1137 SV*);
1138 PL_tmps_ix += items;
1139
1140 while (i-- > 0) {
1141 SV *sv = POPs;
1142 if (!SvTEMP(sv))
1143 sv = sv_mortalcopy(sv);
1144 *dst-- = sv;
1145 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1146 }
1147 /* clear the stack frame except for the items */
1148 PL_tmps_floor += items;
1149 FREETMPS;
1150 /* FREETMPS may have cleared the TEMP flag on some of the items */
1151 i = items;
1152 while (i-- > 0)
1153 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
22023b26 1154 }
bfed75c6 1155 else {
22023b26
TP
1156 /* scalar context: we don't care about which values map returns
1157 * (we use undef here). And so we certainly don't want to do mortal
1158 * copies of meaningless values. */
1159 while (items-- > 0) {
b988aa42 1160 (void)POPs;
22023b26
TP
1161 *dst-- = &PL_sv_undef;
1162 }
b2a2a901 1163 FREETMPS;
22023b26 1164 }
a0d0e21e 1165 }
b2a2a901
DM
1166 else {
1167 FREETMPS;
1168 }
d343c3ef 1169 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
1170
1171 /* All done yet? */
3280af22 1172 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
a0d0e21e
LW
1173
1174 (void)POPMARK; /* pop top */
d343c3ef 1175 LEAVE_with_name("grep"); /* exit outer scope */
a0d0e21e 1176 (void)POPMARK; /* pop src */
3280af22 1177 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1178 (void)POPMARK; /* pop dst */
3280af22 1179 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1180 if (gimme == G_SCALAR) {
7cc47870
RGS
1181 if (PL_op->op_private & OPpGREP_LEX) {
1182 SV* sv = sv_newmortal();
1183 sv_setiv(sv, items);
1184 PUSHs(sv);
1185 }
1186 else {
1187 dTARGET;
1188 XPUSHi(items);
1189 }
a0d0e21e 1190 }
54310121 1191 else if (gimme == G_ARRAY)
1192 SP += items;
a0d0e21e
LW
1193 RETURN;
1194 }
1195 else {
1196 SV *src;
1197
d343c3ef 1198 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1199 SAVEVPTR(PL_curpm);
a0d0e21e 1200
544f3153 1201 /* set $_ to the new source item */
3280af22 1202 src = PL_stack_base[PL_markstack_ptr[-1]];
a0d0e21e 1203 SvTEMP_off(src);
59f00321
RGS
1204 if (PL_op->op_private & OPpGREP_LEX)
1205 PAD_SVl(PL_op->op_targ) = src;
1206 else
414bf5ae 1207 DEFSV_set(src);
a0d0e21e
LW
1208
1209 RETURNOP(cLOGOP->op_other);
1210 }
1211}
1212
a0d0e21e
LW
1213/* Range stuff. */
1214
1215PP(pp_range)
1216{
97aff369 1217 dVAR;
a0d0e21e 1218 if (GIMME == G_ARRAY)
1a67a97c 1219 return NORMAL;
538573f7 1220 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1a67a97c 1221 return cLOGOP->op_other;
538573f7 1222 else
1a67a97c 1223 return NORMAL;
a0d0e21e
LW
1224}
1225
1226PP(pp_flip)
1227{
97aff369 1228 dVAR;
39644a26 1229 dSP;
a0d0e21e
LW
1230
1231 if (GIMME == G_ARRAY) {
1a67a97c 1232 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1233 }
1234 else {
1235 dTOPss;
44f8325f 1236 SV * const targ = PAD_SV(PL_op->op_targ);
bfed75c6 1237 int flip = 0;
790090df 1238
bfed75c6 1239 if (PL_op->op_private & OPpFLIP_LINENUM) {
4e3399f9
YST
1240 if (GvIO(PL_last_in_gv)) {
1241 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1242 }
1243 else {
fafc274c 1244 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
44f8325f
AL
1245 if (gv && GvSV(gv))
1246 flip = SvIV(sv) == SvIV(GvSV(gv));
4e3399f9 1247 }
bfed75c6
AL
1248 } else {
1249 flip = SvTRUE(sv);
1250 }
1251 if (flip) {
a0d0e21e 1252 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 1253 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1254 sv_setiv(targ, 1);
3e3baf6d 1255 SETs(targ);
a0d0e21e
LW
1256 RETURN;
1257 }
1258 else {
1259 sv_setiv(targ, 0);
924508f0 1260 SP--;
1a67a97c 1261 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1262 }
1263 }
76f68e9b 1264 sv_setpvs(TARG, "");
a0d0e21e
LW
1265 SETs(targ);
1266 RETURN;
1267 }
1268}
1269
8e9bbdb9
RGS
1270/* This code tries to decide if "$left .. $right" should use the
1271 magical string increment, or if the range is numeric (we make
1272 an exception for .."0" [#18165]). AMS 20021031. */
1273
1274#define RANGE_IS_NUMERIC(left,right) ( \
b0e74086
RGS
1275 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1276 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
e0ab1c0e 1277 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
b15aece3 1278 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
e0ab1c0e 1279 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1280
a0d0e21e
LW
1281PP(pp_flop)
1282{
97aff369 1283 dVAR; dSP;
a0d0e21e
LW
1284
1285 if (GIMME == G_ARRAY) {
1286 dPOPPOPssrl;
86cb7173 1287
5b295bef
RD
1288 SvGETMAGIC(left);
1289 SvGETMAGIC(right);
a0d0e21e 1290
8e9bbdb9 1291 if (RANGE_IS_NUMERIC(left,right)) {
901017d6
AL
1292 register IV i, j;
1293 IV max;
4fe3f0fa
MHM
1294 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1295 (SvOK(right) && SvNV(right) > IV_MAX))
d470f89e 1296 DIE(aTHX_ "Range iterator outside integer range");
a0d0e21e
LW
1297 i = SvIV(left);
1298 max = SvIV(right);
bbce6d69 1299 if (max >= i) {
c1ab3db2
AK
1300 j = max - i + 1;
1301 EXTEND_MORTAL(j);
1302 EXTEND(SP, j);
bbce6d69 1303 }
c1ab3db2
AK
1304 else
1305 j = 0;
1306 while (j--) {
901017d6 1307 SV * const sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
1308 PUSHs(sv);
1309 }
1310 }
1311 else {
44f8325f 1312 SV * const final = sv_mortalcopy(right);
13c5b33c 1313 STRLEN len;
823a54a3 1314 const char * const tmps = SvPV_const(final, len);
a0d0e21e 1315
901017d6 1316 SV *sv = sv_mortalcopy(left);
13c5b33c 1317 SvPV_force_nolen(sv);
89ea2908 1318 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 1319 XPUSHs(sv);
b15aece3 1320 if (strEQ(SvPVX_const(sv),tmps))
89ea2908 1321 break;
a0d0e21e
LW
1322 sv = sv_2mortal(newSVsv(sv));
1323 sv_inc(sv);
1324 }
a0d0e21e
LW
1325 }
1326 }
1327 else {
1328 dTOPss;
901017d6 1329 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
4e3399f9 1330 int flop = 0;
a0d0e21e 1331 sv_inc(targ);
4e3399f9
YST
1332
1333 if (PL_op->op_private & OPpFLIP_LINENUM) {
1334 if (GvIO(PL_last_in_gv)) {
1335 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1336 }
1337 else {
fafc274c 1338 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
4e3399f9
YST
1339 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1340 }
1341 }
1342 else {
1343 flop = SvTRUE(sv);
1344 }
1345
1346 if (flop) {
a0d0e21e 1347 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
396482e1 1348 sv_catpvs(targ, "E0");
a0d0e21e
LW
1349 }
1350 SETs(targ);
1351 }
1352
1353 RETURN;
1354}
1355
1356/* Control. */
1357
27da23d5 1358static const char * const context_name[] = {
515afda2 1359 "pseudo-block",
f31522f3 1360 NULL, /* CXt_WHEN never actually needs "block" */
76753e7f 1361 NULL, /* CXt_BLOCK never actually needs "block" */
f31522f3 1362 NULL, /* CXt_GIVEN never actually needs "block" */
76753e7f
NC
1363 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1364 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1365 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1366 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
515afda2 1367 "subroutine",
76753e7f 1368 "format",
515afda2 1369 "eval",
515afda2 1370 "substitution",
515afda2
NC
1371};
1372
76e3520e 1373STATIC I32
06b5626a 1374S_dopoptolabel(pTHX_ const char *label)
a0d0e21e 1375{
97aff369 1376 dVAR;
a0d0e21e 1377 register I32 i;
a0d0e21e 1378
7918f24d
NC
1379 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1380
a0d0e21e 1381 for (i = cxstack_ix; i >= 0; i--) {
901017d6 1382 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1383 switch (CxTYPE(cx)) {
a0d0e21e 1384 case CXt_SUBST:
a0d0e21e 1385 case CXt_SUB:
7766f137 1386 case CXt_FORMAT:
a0d0e21e 1387 case CXt_EVAL:
0a753a76 1388 case CXt_NULL:
a2a5de95
NC
1389 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1390 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1391 if (CxTYPE(cx) == CXt_NULL)
1392 return -1;
1393 break;
c6fdafd0 1394 case CXt_LOOP_LAZYIV:
d01136d6 1395 case CXt_LOOP_LAZYSV:
3b719c58
NC
1396 case CXt_LOOP_FOR:
1397 case CXt_LOOP_PLAIN:
7e8f1eac
AD
1398 {
1399 const char *cx_label = CxLABEL(cx);
1400 if (!cx_label || strNE(label, cx_label) ) {
1c98cc53 1401 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
7e8f1eac 1402 (long)i, cx_label));
a0d0e21e
LW
1403 continue;
1404 }
1c98cc53 1405 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
a0d0e21e 1406 return i;
7e8f1eac 1407 }
a0d0e21e
LW
1408 }
1409 }
1410 return i;
1411}
1412
0d863452
RH
1413
1414
e50aee73 1415I32
864dbfa3 1416Perl_dowantarray(pTHX)
e50aee73 1417{
97aff369 1418 dVAR;
f54cb97a 1419 const I32 gimme = block_gimme();
54310121 1420 return (gimme == G_VOID) ? G_SCALAR : gimme;
1421}
1422
1423I32
864dbfa3 1424Perl_block_gimme(pTHX)
54310121 1425{
97aff369 1426 dVAR;
06b5626a 1427 const I32 cxix = dopoptosub(cxstack_ix);
e50aee73 1428 if (cxix < 0)
46fc3d4c 1429 return G_VOID;
e50aee73 1430
54310121 1431 switch (cxstack[cxix].blk_gimme) {
d2719217
GS
1432 case G_VOID:
1433 return G_VOID;
54310121 1434 case G_SCALAR:
e50aee73 1435 return G_SCALAR;
54310121 1436 case G_ARRAY:
1437 return G_ARRAY;
1438 default:
cea2e8a9 1439 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
d2719217
GS
1440 /* NOTREACHED */
1441 return 0;
54310121 1442 }
e50aee73
AD
1443}
1444
78f9721b
SM
1445I32
1446Perl_is_lvalue_sub(pTHX)
1447{
97aff369 1448 dVAR;
06b5626a 1449 const I32 cxix = dopoptosub(cxstack_ix);
78f9721b
SM
1450 assert(cxix >= 0); /* We should only be called from inside subs */
1451
bafb2adc
NC
1452 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1453 return CxLVAL(cxstack + cxix);
78f9721b
SM
1454 else
1455 return 0;
1456}
1457
76e3520e 1458STATIC I32
901017d6 1459S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
2c375eb9 1460{
97aff369 1461 dVAR;
a0d0e21e 1462 I32 i;
7918f24d
NC
1463
1464 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1465
a0d0e21e 1466 for (i = startingblock; i >= 0; i--) {
901017d6 1467 register const PERL_CONTEXT * const cx = &cxstk[i];
6b35e009 1468 switch (CxTYPE(cx)) {
a0d0e21e
LW
1469 default:
1470 continue;
1471 case CXt_EVAL:
1472 case CXt_SUB:
7766f137 1473 case CXt_FORMAT:
1c98cc53 1474 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
a0d0e21e
LW
1475 return i;
1476 }
1477 }
1478 return i;
1479}
1480
76e3520e 1481STATIC I32
cea2e8a9 1482S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e 1483{
97aff369 1484 dVAR;
a0d0e21e 1485 I32 i;
a0d0e21e 1486 for (i = startingblock; i >= 0; i--) {
06b5626a 1487 register const PERL_CONTEXT *cx = &cxstack[i];
6b35e009 1488 switch (CxTYPE(cx)) {
a0d0e21e
LW
1489 default:
1490 continue;
1491 case CXt_EVAL:
1c98cc53 1492 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
a0d0e21e
LW
1493 return i;
1494 }
1495 }
1496 return i;
1497}
1498
76e3520e 1499STATIC I32
cea2e8a9 1500S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e 1501{
97aff369 1502 dVAR;
a0d0e21e 1503 I32 i;
a0d0e21e 1504 for (i = startingblock; i >= 0; i--) {
901017d6 1505 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1506 switch (CxTYPE(cx)) {
a0d0e21e 1507 case CXt_SUBST:
a0d0e21e 1508 case CXt_SUB:
7766f137 1509 case CXt_FORMAT:
a0d0e21e 1510 case CXt_EVAL:
0a753a76 1511 case CXt_NULL:
a2a5de95
NC
1512 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1513 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1514 if ((CxTYPE(cx)) == CXt_NULL)
1515 return -1;
1516 break;
c6fdafd0 1517 case CXt_LOOP_LAZYIV:
d01136d6 1518 case CXt_LOOP_LAZYSV:
3b719c58
NC
1519 case CXt_LOOP_FOR:
1520 case CXt_LOOP_PLAIN:
1c98cc53 1521 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
a0d0e21e
LW
1522 return i;
1523 }
1524 }
1525 return i;
1526}
1527
0d863452
RH
1528STATIC I32
1529S_dopoptogiven(pTHX_ I32 startingblock)
1530{
97aff369 1531 dVAR;
0d863452
RH
1532 I32 i;
1533 for (i = startingblock; i >= 0; i--) {
1534 register const PERL_CONTEXT *cx = &cxstack[i];
1535 switch (CxTYPE(cx)) {
1536 default:
1537 continue;
1538 case CXt_GIVEN:
1c98cc53 1539 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
0d863452 1540 return i;
3b719c58
NC
1541 case CXt_LOOP_PLAIN:
1542 assert(!CxFOREACHDEF(cx));
1543 break;
c6fdafd0 1544 case CXt_LOOP_LAZYIV:
d01136d6 1545 case CXt_LOOP_LAZYSV:
3b719c58 1546 case CXt_LOOP_FOR:
0d863452 1547 if (CxFOREACHDEF(cx)) {
1c98cc53 1548 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
0d863452
RH
1549 return i;
1550 }
1551 }
1552 }
1553 return i;
1554}
1555
1556STATIC I32
1557S_dopoptowhen(pTHX_ I32 startingblock)
1558{
97aff369 1559 dVAR;
0d863452
RH
1560 I32 i;
1561 for (i = startingblock; i >= 0; i--) {
1562 register const PERL_CONTEXT *cx = &cxstack[i];
1563 switch (CxTYPE(cx)) {
1564 default:
1565 continue;
1566 case CXt_WHEN:
1c98cc53 1567 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
0d863452
RH
1568 return i;
1569 }
1570 }
1571 return i;
1572}
1573
a0d0e21e 1574void
864dbfa3 1575Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1576{
97aff369 1577 dVAR;
a0d0e21e
LW
1578 I32 optype;
1579
f144f1e3
DM
1580 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1581 return;
1582
a0d0e21e 1583 while (cxstack_ix > cxix) {
b0d9ce38 1584 SV *sv;
06b5626a 1585 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1c98cc53 1586 DEBUG_CX("UNWIND"); \
a0d0e21e 1587 /* Note: we don't need to restore the base context info till the end. */
6b35e009 1588 switch (CxTYPE(cx)) {
c90c0ff4 1589 case CXt_SUBST:
1590 POPSUBST(cx);
1591 continue; /* not break */
a0d0e21e 1592 case CXt_SUB:
b0d9ce38
GS
1593 POPSUB(cx,sv);
1594 LEAVESUB(sv);
a0d0e21e
LW
1595 break;
1596 case CXt_EVAL:
1597 POPEVAL(cx);
1598 break;
c6fdafd0 1599 case CXt_LOOP_LAZYIV:
d01136d6 1600 case CXt_LOOP_LAZYSV:
3b719c58
NC
1601 case CXt_LOOP_FOR:
1602 case CXt_LOOP_PLAIN:
a0d0e21e
LW
1603 POPLOOP(cx);
1604 break;
0a753a76 1605 case CXt_NULL:
a0d0e21e 1606 break;
7766f137
GS
1607 case CXt_FORMAT:
1608 POPFORMAT(cx);
1609 break;
a0d0e21e 1610 }
c90c0ff4 1611 cxstack_ix--;
a0d0e21e 1612 }
1b6737cc 1613 PERL_UNUSED_VAR(optype);
a0d0e21e
LW
1614}
1615
5a844595
GS
1616void
1617Perl_qerror(pTHX_ SV *err)
1618{
97aff369 1619 dVAR;
7918f24d
NC
1620
1621 PERL_ARGS_ASSERT_QERROR;
1622
6b2fb389
DM
1623 if (PL_in_eval) {
1624 if (PL_in_eval & EVAL_KEEPERR) {
1625 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1626 SvPV_nolen_const(err));
1627 }
1628 else
1629 sv_catsv(ERRSV, err);
1630 }
5a844595
GS
1631 else if (PL_errors)
1632 sv_catsv(PL_errors, err);
1633 else
be2597df 1634 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
13765c85
DM
1635 if (PL_parser)
1636 ++PL_parser->error_count;
5a844595
GS
1637}
1638
bb4c52e0 1639void
c5df3096 1640Perl_die_unwind(pTHX_ SV *msv)
a0d0e21e 1641{
27da23d5 1642 dVAR;
c5df3096 1643 SV *exceptsv = sv_mortalcopy(msv);
96d9b9cd 1644 U8 in_eval = PL_in_eval;
c5df3096 1645 PERL_ARGS_ASSERT_DIE_UNWIND;
87582a92 1646
96d9b9cd 1647 if (in_eval) {
a0d0e21e 1648 I32 cxix;
a0d0e21e 1649 I32 gimme;
a0d0e21e 1650
22a30693
Z
1651 /*
1652 * Historically, perl used to set ERRSV ($@) early in the die
1653 * process and rely on it not getting clobbered during unwinding.
1654 * That sucked, because it was liable to get clobbered, so the
1655 * setting of ERRSV used to emit the exception from eval{} has
1656 * been moved to much later, after unwinding (see just before
1657 * JMPENV_JUMP below). However, some modules were relying on the
1658 * early setting, by examining $@ during unwinding to use it as
1659 * a flag indicating whether the current unwinding was caused by
1660 * an exception. It was never a reliable flag for that purpose,
1661 * being totally open to false positives even without actual
1662 * clobberage, but was useful enough for production code to
1663 * semantically rely on it.
1664 *
1665 * We'd like to have a proper introspective interface that
1666 * explicitly describes the reason for whatever unwinding
1667 * operations are currently in progress, so that those modules
1668 * work reliably and $@ isn't further overloaded. But we don't
1669 * have one yet. In its absence, as a stopgap measure, ERRSV is
1670 * now *additionally* set here, before unwinding, to serve as the
1671 * (unreliable) flag that it used to.
1672 *
1673 * This behaviour is temporary, and should be removed when a
1674 * proper way to detect exceptional unwinding has been developed.
1675 * As of 2010-12, the authors of modules relying on the hack
1676 * are aware of the issue, because the modules failed on
1677 * perls 5.13.{1..7} which had late setting of $@ without this
1678 * early-setting hack.
1679 */
1680 if (!(in_eval & EVAL_KEEPERR)) {
1681 SvTEMP_off(exceptsv);
1682 sv_setsv(ERRSV, exceptsv);
1683 }
1684
5a844595
GS
1685 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1686 && PL_curstackinfo->si_prev)
1687 {
bac4b2ad 1688 dounwind(-1);
d3acc0f7 1689 POPSTACK;
bac4b2ad 1690 }
e336de0d 1691
a0d0e21e
LW
1692 if (cxix >= 0) {
1693 I32 optype;
b6494f15 1694 SV *namesv;
35a4481c 1695 register PERL_CONTEXT *cx;
901017d6 1696 SV **newsp;
8f89e5a9
Z
1697 COP *oldcop;
1698 JMPENV *restartjmpenv;
1699 OP *restartop;
a0d0e21e
LW
1700
1701 if (cxix < cxstack_ix)
1702 dounwind(cxix);
1703
3280af22 1704 POPBLOCK(cx,PL_curpm);
6b35e009 1705 if (CxTYPE(cx) != CXt_EVAL) {
7d0994e0 1706 STRLEN msglen;
96d9b9cd 1707 const char* message = SvPVx_const(exceptsv, msglen);
10edeb5d 1708 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
bf49b057 1709 PerlIO_write(Perl_error_log, message, msglen);
a0d0e21e
LW
1710 my_exit(1);
1711 }
1712 POPEVAL(cx);
b6494f15 1713 namesv = cx->blk_eval.old_namesv;
8f89e5a9
Z
1714 oldcop = cx->blk_oldcop;
1715 restartjmpenv = cx->blk_eval.cur_top_env;
1716 restartop = cx->blk_eval.retop;
a0d0e21e
LW
1717
1718 if (gimme == G_SCALAR)
3280af22
NIS
1719 *++newsp = &PL_sv_undef;
1720 PL_stack_sp = newsp;
a0d0e21e
LW
1721
1722 LEAVE;
748a9306 1723
7fb6a879
GS
1724 /* LEAVE could clobber PL_curcop (see save_re_context())
1725 * XXX it might be better to find a way to avoid messing with
1726 * PL_curcop in save_re_context() instead, but this is a more
1727 * minimal fix --GSAR */
8f89e5a9 1728 PL_curcop = oldcop;
7fb6a879 1729
7a2e2cd6 1730 if (optype == OP_REQUIRE) {
96d9b9cd 1731 const char* const msg = SvPVx_nolen_const(exceptsv);
b6494f15
VP
1732 (void)hv_store(GvHVn(PL_incgv),
1733 SvPVX_const(namesv), SvCUR(namesv),
27bcc0a7 1734 &PL_sv_undef, 0);
27e90453
DM
1735 /* note that unlike pp_entereval, pp_require isn't
1736 * supposed to trap errors. So now that we've popped the
1737 * EVAL that pp_require pushed, and processed the error
1738 * message, rethrow the error */
9fed9930
NC
1739 Perl_croak(aTHX_ "%sCompilation failed in require",
1740 *msg ? msg : "Unknown error\n");
7a2e2cd6 1741 }
c5df3096 1742 if (in_eval & EVAL_KEEPERR) {
7ce09284
Z
1743 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1744 SvPV_nolen_const(exceptsv));
96d9b9cd
Z
1745 }
1746 else {
1747 sv_setsv(ERRSV, exceptsv);
1748 }
8f89e5a9
Z
1749 PL_restartjmpenv = restartjmpenv;
1750 PL_restartop = restartop;
bb4c52e0
GG
1751 JMPENV_JUMP(3);
1752 /* NOTREACHED */
a0d0e21e
LW
1753 }
1754 }
87582a92 1755
96d9b9cd 1756 write_to_stderr(exceptsv);
f86702cc 1757 my_failure_exit();
1758 /* NOTREACHED */
a0d0e21e
LW
1759}
1760
1761PP(pp_xor)
1762{
97aff369 1763 dVAR; dSP; dPOPTOPssrl;
a0d0e21e
LW
1764 if (SvTRUE(left) != SvTRUE(right))
1765 RETSETYES;
1766 else
1767 RETSETNO;
1768}
1769
8dff4fc5
BM
1770/*
1771=for apidoc caller_cx
1772
1773The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1774returned C<PERL_CONTEXT> structure can be interrogated to find all the
1775information returned to Perl by C<caller>. Note that XSUBs don't get a
1776stack frame, so C<caller_cx(0, NULL)> will return information for the
1777immediately-surrounding Perl code.
1778
1779This function skips over the automatic calls to C<&DB::sub> made on the
1780behalf of the debugger. If the stack frame requested was a sub called by
1781C<DB::sub>, the return value will be the frame for the call to
1782C<DB::sub>, since that has the correct line number/etc. for the call
1783site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1784frame for the sub call itself.
1785
1786=cut
1787*/
1788
1789const PERL_CONTEXT *
1790Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
a0d0e21e 1791{
a0d0e21e 1792 register I32 cxix = dopoptosub(cxstack_ix);
901017d6
AL
1793 register const PERL_CONTEXT *cx;
1794 register const PERL_CONTEXT *ccstack = cxstack;
1795 const PERL_SI *top_si = PL_curstackinfo;
27d41816 1796
a0d0e21e 1797 for (;;) {
2c375eb9
GS
1798 /* we may be in a higher stacklevel, so dig down deeper */
1799 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1800 top_si = top_si->si_prev;
1801 ccstack = top_si->si_cxstack;
1802 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1803 }
8dff4fc5
BM
1804 if (cxix < 0)
1805 return NULL;
f2a7f298
DG
1806 /* caller() should not report the automatic calls to &DB::sub */
1807 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1808 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1809 count++;
1810 if (!count--)
1811 break;
2c375eb9 1812 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1813 }
2c375eb9
GS
1814
1815 cx = &ccstack[cxix];
8dff4fc5
BM
1816 if (dbcxp) *dbcxp = cx;
1817
7766f137 1818 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1819 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1820 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1821 field below is defined for any cx. */
f2a7f298
DG
1822 /* caller() should not report the automatic calls to &DB::sub */
1823 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1824 cx = &ccstack[dbcxix];
06a5b730 1825 }
1826
8dff4fc5
BM
1827 return cx;
1828}
1829
1830PP(pp_caller)
1831{
1832 dVAR;
1833 dSP;
1834 register const PERL_CONTEXT *cx;
1835 const PERL_CONTEXT *dbcx;
1836 I32 gimme;
1837 const char *stashname;
1838 I32 count = 0;
1839
1840 if (MAXARG)
1841 count = POPi;
1842
1843 cx = caller_cx(count, &dbcx);
1844 if (!cx) {
1845 if (GIMME != G_ARRAY) {
1846 EXTEND(SP, 1);
1847 RETPUSHUNDEF;
1848 }
1849 RETURN;
1850 }
1851
ed094faf 1852 stashname = CopSTASHPV(cx->blk_oldcop);
a0d0e21e 1853 if (GIMME != G_ARRAY) {
27d41816 1854 EXTEND(SP, 1);
ed094faf 1855 if (!stashname)
3280af22 1856 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1857 else {
1858 dTARGET;
ed094faf 1859 sv_setpv(TARG, stashname);
49d8d3a1
MB
1860 PUSHs(TARG);
1861 }
a0d0e21e
LW
1862 RETURN;
1863 }
a0d0e21e 1864
b3ca2e83 1865 EXTEND(SP, 11);
27d41816 1866
ed094faf 1867 if (!stashname)
3280af22 1868 PUSHs(&PL_sv_undef);
49d8d3a1 1869 else
6e449a3a
MHM
1870 mPUSHs(newSVpv(stashname, 0));
1871 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1872 mPUSHi((I32)CopLINE(cx->blk_oldcop));
a0d0e21e
LW
1873 if (!MAXARG)
1874 RETURN;
7766f137 1875 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
8dff4fc5 1876 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
7766f137 1877 /* So is ccstack[dbcxix]. */
07b8c804 1878 if (isGV(cvgv)) {
561b68a9 1879 SV * const sv = newSV(0);
c445ea15 1880 gv_efullname3(sv, cvgv, NULL);
6e449a3a 1881 mPUSHs(sv);
bf38a478 1882 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1883 }
1884 else {
84bafc02 1885 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1886 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1887 }
a0d0e21e
LW
1888 }
1889 else {
84bafc02 1890 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
6e449a3a 1891 mPUSHi(0);
a0d0e21e 1892 }
54310121 1893 gimme = (I32)cx->blk_gimme;
1894 if (gimme == G_VOID)
3280af22 1895 PUSHs(&PL_sv_undef);
54310121 1896 else
98625aca 1897 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1898 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1899 /* eval STRING */
85a64632 1900 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
4633a7c4 1901 PUSHs(cx->blk_eval.cur_text);
3280af22 1902 PUSHs(&PL_sv_no);
0f79a09d 1903 }
811a4de9 1904 /* require */
0f79a09d 1905 else if (cx->blk_eval.old_namesv) {
6e449a3a 1906 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1907 PUSHs(&PL_sv_yes);
06a5b730 1908 }
811a4de9
GS
1909 /* eval BLOCK (try blocks have old_namesv == 0) */
1910 else {
1911 PUSHs(&PL_sv_undef);
1912 PUSHs(&PL_sv_undef);
1913 }
4633a7c4 1914 }
a682de96
GS
1915 else {
1916 PUSHs(&PL_sv_undef);
1917 PUSHs(&PL_sv_undef);
1918 }
bafb2adc 1919 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1920 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1921 {
66a1b24b
AL
1922 AV * const ary = cx->blk_sub.argarray;
1923 const int off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1924
5b235299
NC
1925 if (!PL_dbargs)
1926 Perl_init_dbargs(aTHX);
a0d0e21e 1927
3280af22
NIS
1928 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1929 av_extend(PL_dbargs, AvFILLp(ary) + off);
1930 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1931 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1932 }
f3aa04c2
GS
1933 /* XXX only hints propagated via op_private are currently
1934 * visible (others are not easily accessible, since they
1935 * use the global PL_hints) */
6e449a3a 1936 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1937 {
1938 SV * mask ;
72dc9ed5 1939 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 1940
ac27b0f5 1941 if (old_warnings == pWARN_NONE ||
114bafba 1942 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
e476b1b5 1943 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
ac27b0f5 1944 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
1945 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1946 /* Get the bit mask for $warnings::Bits{all}, because
1947 * it could have been extended by warnings::register */
1948 SV **bits_all;
6673a63c 1949 HV * const bits = get_hv("warnings::Bits", 0);
017a3ce5 1950 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
1951 mask = newSVsv(*bits_all);
1952 }
1953 else {
1954 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1955 }
1956 }
e476b1b5 1957 else
72dc9ed5 1958 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 1959 mPUSHs(mask);
e476b1b5 1960 }
b3ca2e83 1961
c28fe1ec 1962 PUSHs(cx->blk_oldcop->cop_hints_hash ?
20439bc7 1963 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
b3ca2e83 1964 : &PL_sv_undef);
a0d0e21e
LW
1965 RETURN;
1966}
1967
a0d0e21e
LW
1968PP(pp_reset)
1969{
97aff369 1970 dVAR;
39644a26 1971 dSP;
10edeb5d 1972 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
11faa288 1973 sv_reset(tmps, CopSTASH(PL_curcop));
3280af22 1974 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1975 RETURN;
1976}
1977
dd2155a4
DM
1978/* like pp_nextstate, but used instead when the debugger is active */
1979
a0d0e21e
LW
1980PP(pp_dbstate)
1981{
27da23d5 1982 dVAR;
533c011a 1983 PL_curcop = (COP*)PL_op;
a0d0e21e 1984 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 1985 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1986 FREETMPS;
1987
f410a211
NC
1988 PERL_ASYNC_CHECK();
1989
5df8de69
DM
1990 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1991 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 1992 {
39644a26 1993 dSP;
c09156bb 1994 register PERL_CONTEXT *cx;
f54cb97a 1995 const I32 gimme = G_ARRAY;
eb160463 1996 U8 hasargs;
0bd48802
AL
1997 GV * const gv = PL_DBgv;
1998 register CV * const cv = GvCV(gv);
a0d0e21e 1999
a0d0e21e 2000 if (!cv)
cea2e8a9 2001 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 2002
aea4f609
DM
2003 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2004 /* don't do recursive DB::DB call */
a0d0e21e 2005 return NORMAL;
748a9306 2006
a57c6685 2007 ENTER;
4633a7c4
LW
2008 SAVETMPS;
2009
3280af22 2010 SAVEI32(PL_debug);
55497cff 2011 SAVESTACK_POS();
3280af22 2012 PL_debug = 0;
748a9306 2013 hasargs = 0;
924508f0 2014 SPAGAIN;
748a9306 2015
aed2304a 2016 if (CvISXSUB(cv)) {
c127bd3a
SF
2017 CvDEPTH(cv)++;
2018 PUSHMARK(SP);
2019 (void)(*CvXSUB(cv))(aTHX_ cv);
2020 CvDEPTH(cv)--;
2021 FREETMPS;
a57c6685 2022 LEAVE;
c127bd3a
SF
2023 return NORMAL;
2024 }
2025 else {
2026 PUSHBLOCK(cx, CXt_SUB, SP);
2027 PUSHSUB_DB(cx);
2028 cx->blk_sub.retop = PL_op->op_next;
2029 CvDEPTH(cv)++;
2030 SAVECOMPPAD();
2031 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2032 RETURNOP(CvSTART(cv));
2033 }
a0d0e21e
LW
2034 }
2035 else
2036 return NORMAL;
2037}
2038
a0d0e21e
LW
2039PP(pp_enteriter)
2040{
27da23d5 2041 dVAR; dSP; dMARK;
c09156bb 2042 register PERL_CONTEXT *cx;
f54cb97a 2043 const I32 gimme = GIMME_V;
df530c37 2044 void *itervar; /* location of the iteration variable */
840fe433 2045 U8 cxtype = CXt_LOOP_FOR;
a0d0e21e 2046
d343c3ef 2047 ENTER_with_name("loop1");
4633a7c4
LW
2048 SAVETMPS;
2049
aafca525
DM
2050 if (PL_op->op_targ) { /* "my" variable */
2051 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
14f338dc
DM
2052 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2053 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2054 SVs_PADSTALE, SVs_PADSTALE);
2055 }
09edbca0 2056 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
89e00a7c 2057#ifdef USE_ITHREADS
df530c37 2058 itervar = PL_comppad;
89e00a7c 2059#else
aafca525 2060 itervar = &PAD_SVl(PL_op->op_targ);
7766f137 2061#endif
54b9620d 2062 }
aafca525 2063 else { /* symbol table variable */
159b6efe 2064 GV * const gv = MUTABLE_GV(POPs);
f83b46a0
DM
2065 SV** svp = &GvSV(gv);
2066 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
561b68a9 2067 *svp = newSV(0);
df530c37 2068 itervar = (void *)gv;
54b9620d 2069 }
4633a7c4 2070
0d863452
RH
2071 if (PL_op->op_private & OPpITER_DEF)
2072 cxtype |= CXp_FOR_DEF;
2073
d343c3ef 2074 ENTER_with_name("loop2");
a0d0e21e 2075
7766f137 2076 PUSHBLOCK(cx, cxtype, SP);
df530c37 2077 PUSHLOOP_FOR(cx, itervar, MARK);
533c011a 2078 if (PL_op->op_flags & OPf_STACKED) {
d01136d6
BS
2079 SV *maybe_ary = POPs;
2080 if (SvTYPE(maybe_ary) != SVt_PVAV) {
89ea2908 2081 dPOPss;
d01136d6 2082 SV * const right = maybe_ary;
984a4bea
RD
2083 SvGETMAGIC(sv);
2084 SvGETMAGIC(right);
4fe3f0fa 2085 if (RANGE_IS_NUMERIC(sv,right)) {
d01136d6 2086 cx->cx_type &= ~CXTYPEMASK;
c6fdafd0
NC
2087 cx->cx_type |= CXt_LOOP_LAZYIV;
2088 /* Make sure that no-one re-orders cop.h and breaks our
2089 assumptions */
2090 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
a2309040
JH
2091#ifdef NV_PRESERVES_UV
2092 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2093 (SvNV(sv) > (NV)IV_MAX)))
2094 ||
2095 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2096 (SvNV(right) < (NV)IV_MIN))))
2097#else
2098 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2099 ||
2100 ((SvNV(sv) > 0) &&
2101 ((SvUV(sv) > (UV)IV_MAX) ||
2102 (SvNV(sv) > (NV)UV_MAX)))))
2103 ||
2104 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2105 ||
2106 ((SvNV(right) > 0) &&
2107 ((SvUV(right) > (UV)IV_MAX) ||
2108 (SvNV(right) > (NV)UV_MAX))))))
2109#endif
076d9a11 2110 DIE(aTHX_ "Range iterator outside integer range");
d01136d6
BS
2111 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2112 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
d4665a05
DM
2113#ifdef DEBUGGING
2114 /* for correct -Dstv display */
2115 cx->blk_oldsp = sp - PL_stack_base;
2116#endif
89ea2908 2117 }
3f63a782 2118 else {
d01136d6
BS
2119 cx->cx_type &= ~CXTYPEMASK;
2120 cx->cx_type |= CXt_LOOP_LAZYSV;
2121 /* Make sure that no-one re-orders cop.h and breaks our
2122 assumptions */
2123 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2124 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2125 cx->blk_loop.state_u.lazysv.end = right;
2126 SvREFCNT_inc(right);
2127 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
2128 /* This will do the upgrade to SVt_PV, and warn if the value
2129 is uninitialised. */
10516c54 2130 (void) SvPV_nolen_const(right);
267cc4a8
NC
2131 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2132 to replace !SvOK() with a pointer to "". */
2133 if (!SvOK(right)) {
2134 SvREFCNT_dec(right);
d01136d6 2135 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 2136 }
3f63a782 2137 }
89ea2908 2138 }
d01136d6 2139 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
502c6561 2140 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
d01136d6
BS
2141 SvREFCNT_inc(maybe_ary);
2142 cx->blk_loop.state_u.ary.ix =
2143 (PL_op->op_private & OPpITER_REVERSED) ?
2144 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2145 -1;
ef3e5ea9 2146 }
89ea2908 2147 }
d01136d6
BS
2148 else { /* iterating over items on the stack */
2149 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
ef3e5ea9 2150 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6 2151 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
ef3e5ea9
NC
2152 }
2153 else {
d01136d6 2154 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
ef3e5ea9 2155 }
4633a7c4 2156 }
a0d0e21e
LW
2157
2158 RETURN;
2159}
2160
2161PP(pp_enterloop)
2162{
27da23d5 2163 dVAR; dSP;
c09156bb 2164 register PERL_CONTEXT *cx;
f54cb97a 2165 const I32 gimme = GIMME_V;
a0d0e21e 2166
d343c3ef 2167 ENTER_with_name("loop1");
a0d0e21e 2168 SAVETMPS;
d343c3ef 2169 ENTER_with_name("loop2");
a0d0e21e 2170
3b719c58
NC
2171 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2172 PUSHLOOP_PLAIN(cx, SP);
a0d0e21e
LW
2173
2174 RETURN;
2175}
2176
2177PP(pp_leaveloop)
2178{
27da23d5 2179 dVAR; dSP;
c09156bb 2180 register PERL_CONTEXT *cx;
a0d0e21e
LW
2181 I32 gimme;
2182 SV **newsp;
2183 PMOP *newpm;
2184 SV **mark;
2185
2186 POPBLOCK(cx,newpm);
3b719c58 2187 assert(CxTYPE_is_LOOP(cx));
4fdae800 2188 mark = newsp;
a8bba7fa 2189 newsp = PL_stack_base + cx->blk_loop.resetsp;
f86702cc 2190
a1f49e72 2191 TAINT_NOT;
54310121 2192 if (gimme == G_VOID)
6f207bd3 2193 NOOP;
54310121 2194 else if (gimme == G_SCALAR) {
2195 if (mark < SP)
2196 *++newsp = sv_mortalcopy(*SP);
2197 else
3280af22 2198 *++newsp = &PL_sv_undef;
a0d0e21e
LW
2199 }
2200 else {
a1f49e72 2201 while (mark < SP) {
a0d0e21e 2202 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
2203 TAINT_NOT; /* Each item is independent */
2204 }
a0d0e21e 2205 }
f86702cc 2206 SP = newsp;
2207 PUTBACK;
2208
a8bba7fa 2209 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
3280af22 2210 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2211
d343c3ef
GG
2212 LEAVE_with_name("loop2");
2213 LEAVE_with_name("loop1");
a0d0e21e 2214
f86702cc 2215 return NORMAL;
a0d0e21e
LW
2216}
2217
3bdf583b
FC
2218STATIC void
2219S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
2220 PERL_CONTEXT *cx)
2221{
2222 if (gimme == G_SCALAR) {
2223 if (MARK < SP) {
2224 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2225 *++newsp = SvREFCNT_inc(*SP);
2226 FREETMPS;
2227 sv_2mortal(*newsp);
2228 }
2229 else
e08be60b
FC
2230 *++newsp =
2231 (!CxLVAL(cx) || CxLVAL(cx) & OPpENTERSUB_INARGS) &&
2232 !SvTEMP(*SP)
2233 ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP))
2234 : *SP;
3bdf583b
FC
2235 }
2236 else
2237 *++newsp = &PL_sv_undef;
767eda44
FC
2238 if (CxLVAL(cx) & OPpENTERSUB_DEREF) {
2239 SvGETMAGIC(TOPs);
2240 if (!SvOK(TOPs)) {
2241 U8 deref_type;
2242 if (cx->blk_sub.retop->op_type == OP_RV2SV)
2243 deref_type = OPpDEREF_SV;
2244 else if (cx->blk_sub.retop->op_type == OP_RV2AV)
2245 deref_type = OPpDEREF_AV;
2246 else {
2247 assert(cx->blk_sub.retop->op_type == OP_RV2HV);
2248 deref_type = OPpDEREF_HV;
2249 }
2250 vivify_ref(TOPs, deref_type);
2251 }
2252 }
3bdf583b
FC
2253 }
2254 else if (gimme == G_ARRAY) {
767eda44 2255 assert (!(CxLVAL(cx) & OPpENTERSUB_DEREF));
e08be60b
FC
2256 if (!CxLVAL(cx) || CxLVAL(cx) & OPpENTERSUB_INARGS)
2257 while (++MARK <= SP)
2258 *++newsp =
2259 SvTEMP(*MARK)
2260 ? *MARK
2261 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2262 else while (++MARK <= SP) {
3bdf583b 2263 *++newsp = *MARK;
3bdf583b
FC
2264 }
2265 }
2266 PL_stack_sp = newsp;
2267}
2268
a0d0e21e
LW
2269PP(pp_return)
2270{
27da23d5 2271 dVAR; dSP; dMARK;
c09156bb 2272 register PERL_CONTEXT *cx;
f86702cc 2273 bool popsub2 = FALSE;
b45de488 2274 bool clear_errsv = FALSE;
fa1e92c4 2275 bool lval = FALSE;
767eda44 2276 bool gmagic = FALSE;
a0d0e21e
LW
2277 I32 gimme;
2278 SV **newsp;
2279 PMOP *newpm;
2280 I32 optype = 0;
b6494f15 2281 SV *namesv;
b0d9ce38 2282 SV *sv;
b263a1ad 2283 OP *retop = NULL;
a0d0e21e 2284
0bd48802
AL
2285 const I32 cxix = dopoptosub(cxstack_ix);
2286
9850bf21
RH
2287 if (cxix < 0) {
2288 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2289 * sort block, which is a CXt_NULL
2290 * not a CXt_SUB */
2291 dounwind(0);
d7507f74
RH
2292 PL_stack_base[1] = *PL_stack_sp;
2293 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
2294 return 0;
2295 }
9850bf21
RH
2296 else
2297 DIE(aTHX_ "Can't return outside a subroutine");
a0d0e21e 2298 }
a0d0e21e
LW
2299 if (cxix < cxstack_ix)
2300 dounwind(cxix);
2301
d7507f74
RH
2302 if (CxMULTICALL(&cxstack[cxix])) {
2303 gimme = cxstack[cxix].blk_gimme;
2304 if (gimme == G_VOID)
2305 PL_stack_sp = PL_stack_base;
2306 else if (gimme == G_SCALAR) {
2307 PL_stack_base[1] = *PL_stack_sp;
2308 PL_stack_sp = PL_stack_base + 1;
2309 }
9850bf21 2310 return 0;
d7507f74 2311 }
9850bf21 2312
a0d0e21e 2313 POPBLOCK(cx,newpm);
6b35e009 2314 switch (CxTYPE(cx)) {
a0d0e21e 2315 case CXt_SUB:
f86702cc 2316 popsub2 = TRUE;
fa1e92c4 2317 lval = !!CvLVALUE(cx->blk_sub.cv);
f39bc417 2318 retop = cx->blk_sub.retop;
767eda44 2319 gmagic = CxLVAL(cx) & OPpENTERSUB_DEREF;
5dd42e15 2320 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
a0d0e21e
LW
2321 break;
2322 case CXt_EVAL:
b45de488
GS
2323 if (!(PL_in_eval & EVAL_KEEPERR))
2324 clear_errsv = TRUE;
a0d0e21e 2325 POPEVAL(cx);
b6494f15 2326 namesv = cx->blk_eval.old_namesv;
f39bc417 2327 retop = cx->blk_eval.retop;
1d76a5c3
GS
2328 if (CxTRYBLOCK(cx))
2329 break;
748a9306
LW
2330 if (optype == OP_REQUIRE &&
2331 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2332 {
54310121 2333 /* Unassume the success we assumed earlier. */
b6494f15
VP
2334 (void)hv_delete(GvHVn(PL_incgv),
2335 SvPVX_const(namesv), SvCUR(namesv),
2336 G_DISCARD);
2337 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
748a9306 2338 }
a0d0e21e 2339 break;
7766f137
GS
2340 case CXt_FORMAT:
2341 POPFORMAT(cx);
f39bc417 2342 retop = cx->blk_sub.retop;
7766f137 2343 break;
a0d0e21e 2344 default:
cea2e8a9 2345 DIE(aTHX_ "panic: return");
a0d0e21e
LW
2346 }
2347
a1f49e72 2348 TAINT_NOT;
3bdf583b
FC
2349 if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx);
2350 else {
2351 if (gimme == G_SCALAR) {
a29cdaf0
IZ
2352 if (MARK < SP) {
2353 if (popsub2) {
a8bba7fa 2354 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
3ed94dc0 2355 if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1) {
a29cdaf0
IZ
2356 *++newsp = SvREFCNT_inc(*SP);
2357 FREETMPS;
2358 sv_2mortal(*newsp);
959e3673
GS
2359 }
2360 else {
2361 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
a29cdaf0 2362 FREETMPS;
959e3673
GS
2363 *++newsp = sv_mortalcopy(sv);
2364 SvREFCNT_dec(sv);
767eda44 2365 if (gmagic) SvGETMAGIC(sv);
a29cdaf0 2366 }
959e3673 2367 }
3ed94dc0 2368 else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1) {
767eda44
FC
2369 *++newsp = *SP;
2370 if (gmagic) SvGETMAGIC(*SP);
2371 }
959e3673 2372 else
767eda44 2373 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2374 }
2375 else
a29cdaf0 2376 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2377 }
2378 else
3280af22 2379 *++newsp = &PL_sv_undef;
3bdf583b
FC
2380 }
2381 else if (gimme == G_ARRAY) {
a1f49e72 2382 while (++MARK <= SP) {
3ed94dc0 2383 *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1
f86702cc 2384 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2385 TAINT_NOT; /* Each item is independent */
2386 }
3bdf583b
FC
2387 }
2388 PL_stack_sp = newsp;
a0d0e21e 2389 }
a0d0e21e 2390
5dd42e15 2391 LEAVE;
f86702cc 2392 /* Stack values are safe: */
2393 if (popsub2) {
5dd42e15 2394 cxstack_ix--;
b0d9ce38 2395 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2396 }
b0d9ce38 2397 else
c445ea15 2398 sv = NULL;
3280af22 2399 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2400
b0d9ce38 2401 LEAVESUB(sv);
8433848b 2402 if (clear_errsv) {
ab69dbc2 2403 CLEAR_ERRSV();
8433848b 2404 }
f39bc417 2405 return retop;
a0d0e21e
LW
2406}
2407
4f443c3d
FC
2408/* This duplicates parts of pp_leavesub, so that it can share code with
2409 * pp_return */
2410PP(pp_leavesublv)
2411{
2412 dVAR; dSP;
2413 SV **mark;
2414 SV **newsp;
2415 PMOP *newpm;
2416 I32 gimme;
2417 register PERL_CONTEXT *cx;
2418 SV *sv;
2419
2420 if (CxMULTICALL(&cxstack[cxstack_ix]))
2421 return 0;
2422
2423 POPBLOCK(cx,newpm);
2424 cxstack_ix++; /* temporarily protect top context */
2425 assert(CvLVALUE(cx->blk_sub.cv));
2426
2427 TAINT_NOT;
2428
2429 if (CxLVAL(cx) & OPpENTERSUB_INARGS) {
2430 /* We are an argument to a function or grep().
2431 * This kind of lvalueness was legal before lvalue
2432 * subroutines too, so be backward compatible:
2433 * cannot report errors. */
2434
2435 /* Scalar context *is* possible, on the LHS of ->. */
2436 if (gimme == G_SCALAR)
2437 goto rvalue;
2438 if (gimme == G_ARRAY) {
2439 mark = newsp + 1;
4f443c3d
FC
2440 EXTEND_MORTAL(SP - newsp);
2441 for (mark = newsp + 1; mark <= SP; mark++) {
2442 if (SvTEMP(*mark))
2443 NOOP;
2444 else if (SvFLAGS(*mark) & SVs_PADTMP)
2445 *mark = sv_mortalcopy(*mark);
2446 else {
2447 /* Can be a localized value subject to deletion. */
2448 PL_tmps_stack[++PL_tmps_ix] = *mark;
2449 SvREFCNT_inc_void(*mark);
2450 }
2451 }
2452 }
2453 }
2454 else if (CxLVAL(cx)) { /* Leave it as it is if we can. */
2455 if (gimme == G_SCALAR) {
2456 MARK = newsp + 1;
2457 EXTEND_MORTAL(1);
2458 if (MARK == SP) {
2459 if ((SvPADTMP(TOPs) ||
2460 (SvFLAGS(TOPs) & (SVf_READONLY | SVf_FAKE))
2461 == SVf_READONLY
2462 ) &&
2463 !SvSMAGICAL(TOPs)) {
2464 LEAVE;
2465 cxstack_ix--;
2466 POPSUB(cx,sv);
2467 PL_curpm = newpm;
2468 LEAVESUB(sv);
2469 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2470 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2471 : "a readonly value" : "a temporary");
2472 }
2473 else { /* Can be a localized value
2474 * subject to deletion. */
2475 PL_tmps_stack[++PL_tmps_ix] = *mark;
2476 SvREFCNT_inc_void(*mark);
2477 }
2478 }
2479 else {
2480 /* sub:lvalue{} will take us here.
2481 Presumably the case of a non-empty array never happens.
2482 */
2483 LEAVE;
2484 cxstack_ix--;
2485 POPSUB(cx,sv);
2486 PL_curpm = newpm;
2487 LEAVESUB(sv);
2488 DIE(aTHX_ "%s",
2489 (MARK > SP
2490 ? "Can't return undef from lvalue subroutine"
2491 : "Array returned from lvalue subroutine in scalar "
2492 "context"
2493 )
2494 );
2495 }
2496 SP = MARK;
2497 }
2498 else if (gimme == G_ARRAY) {
2499 EXTEND_MORTAL(SP - newsp);
2500 for (mark = newsp + 1; mark <= SP; mark++) {
2501 if (*mark != &PL_sv_undef
2502 && (SvPADTMP(*mark)
2503 || (SvFLAGS(*mark) & (SVf_READONLY|SVf_FAKE))
2504 == SVf_READONLY
2505 )
2506 ) {
2507 /* Might be flattened array after $#array = */
2508 PUTBACK;
2509 LEAVE;
2510 cxstack_ix--;
2511 POPSUB(cx,sv);
2512 PL_curpm = newpm;
2513 LEAVESUB(sv);
2514 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2515 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2516 }
2517 else {
2518 /* Can be a localized value subject to deletion. */
2519 PL_tmps_stack[++PL_tmps_ix] = *mark;
2520 SvREFCNT_inc_void(*mark);
2521 }
2522 }
2523 }
2524 }
2525 else {
2526 if (gimme == G_SCALAR) {
2527 rvalue:
2528 MARK = newsp + 1;
2529 if (MARK <= SP) {
2530 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2531 *MARK = SvREFCNT_inc(TOPs);
2532 FREETMPS;
2533 sv_2mortal(*MARK);
2534 }
2535 else
2536 *MARK = SvTEMP(TOPs)
2537 ? TOPs
2538 : sv_2mortal(SvREFCNT_inc_simple_NN(TOPs));
2539 }
2540 else {
2541 MEXTEND(MARK, 0);
2542 *MARK = &PL_sv_undef;
2543 }
2544 SP = MARK;
2545 }
2546 else if (gimme == G_ARRAY) {
2547 rvalue_array:
2548 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2549 if (!SvTEMP(*MARK))
2550 *MARK = sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2551 }
2552 }
2553 }
2554
2555 if (CxLVAL(cx) & OPpENTERSUB_DEREF) {
2556 assert(gimme == G_SCALAR);
2557 SvGETMAGIC(TOPs);
2558 if (!SvOK(TOPs)) {
2559 U8 deref_type;
2560 if (cx->blk_sub.retop->op_type == OP_RV2SV)
2561 deref_type = OPpDEREF_SV;
2562 else if (cx->blk_sub.retop->op_type == OP_RV2AV)
2563 deref_type = OPpDEREF_AV;
2564 else {
2565 assert(cx->blk_sub.retop->op_type == OP_RV2HV);
2566 deref_type = OPpDEREF_HV;
2567 }
2568 vivify_ref(TOPs, deref_type);
2569 }
2570 }
2571
2572 PUTBACK;
2573
2574 LEAVE;
2575 cxstack_ix--;
2576 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2577 PL_curpm = newpm; /* ... and pop $1 et al */
2578
2579 LEAVESUB(sv);
2580 return cx->blk_sub.retop;
2581}
2582
a0d0e21e
LW
2583PP(pp_last)
2584{
27da23d5 2585 dVAR; dSP;
a0d0e21e 2586 I32 cxix;
c09156bb 2587 register PERL_CONTEXT *cx;
f86702cc 2588 I32 pop2 = 0;
a0d0e21e 2589 I32 gimme;
8772537c 2590 I32 optype;
b263a1ad 2591 OP *nextop = NULL;
a0d0e21e
LW
2592 SV **newsp;
2593 PMOP *newpm;
a8bba7fa 2594 SV **mark;
c445ea15 2595 SV *sv = NULL;
9d4ba2ae 2596
a0d0e21e 2597
533c011a 2598 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2599 cxix = dopoptoloop(cxstack_ix);
2600 if (cxix < 0)
a651a37d 2601 DIE(aTHX_ "Can't \"last\" outside a loop block");
a0d0e21e
LW
2602 }
2603 else {
2604 cxix = dopoptolabel(cPVOP->op_pv);
2605 if (cxix < 0)
cea2e8a9 2606 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
a0d0e21e
LW
2607 }
2608 if (cxix < cxstack_ix)
2609 dounwind(cxix);
2610
2611 POPBLOCK(cx,newpm);
5dd42e15 2612 cxstack_ix++; /* temporarily protect top context */
a8bba7fa 2613 mark = newsp;
6b35e009 2614 switch (CxTYPE(cx)) {
c6fdafd0 2615 case CXt_LOOP_LAZYIV:
d01136d6 2616 case CXt_LOOP_LAZYSV:
3b719c58
NC
2617 case CXt_LOOP_FOR:
2618 case CXt_LOOP_PLAIN:
2619 pop2 = CxTYPE(cx);
a8bba7fa 2620 newsp = PL_stack_base + cx->blk_loop.resetsp;
022eaa24 2621 nextop = cx->blk_loop.my_op->op_lastop->op_next;
a0d0e21e 2622 break;
f86702cc 2623 case CXt_SUB:
f86702cc 2624 pop2 = CXt_SUB;
f39bc417 2625 nextop = cx->blk_sub.retop;
a0d0e21e 2626 break;
f86702cc 2627 case CXt_EVAL:
2628 POPEVAL(cx);
f39bc417 2629 nextop = cx->blk_eval.retop;
a0d0e21e 2630 break;
7766f137
GS
2631 case CXt_FORMAT:
2632 POPFORMAT(cx);
f39bc417 2633 nextop = cx->blk_sub.retop;
7766f137 2634 break;
a0d0e21e 2635 default:
cea2e8a9 2636 DIE(aTHX_ "panic: last");
a0d0e21e
LW
2637 }
2638
a1f49e72 2639 TAINT_NOT;
a0d0e21e 2640 if (gimme == G_SCALAR) {
f86702cc 2641 if (MARK < SP)
2642 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2643 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 2644 else
3280af22 2645 *++newsp = &PL_sv_undef;
a0d0e21e 2646 }
54310121 2647 else if (gimme == G_ARRAY) {
a1f49e72 2648 while (++MARK <= SP) {
f86702cc 2649 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2650 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2651 TAINT_NOT; /* Each item is independent */
2652 }
f86702cc 2653 }
2654 SP = newsp;
2655 PUTBACK;
2656
5dd42e15
DM
2657 LEAVE;
2658 cxstack_ix--;
f86702cc 2659 /* Stack values are safe: */
2660 switch (pop2) {
c6fdafd0 2661 case CXt_LOOP_LAZYIV:
3b719c58 2662 case CXt_LOOP_PLAIN:
d01136d6 2663 case CXt_LOOP_LAZYSV:
3b719c58 2664 case CXt_LOOP_FOR:
a8bba7fa 2665 POPLOOP(cx); /* release loop vars ... */
4fdae800 2666 LEAVE;
f86702cc 2667 break;
2668 case CXt_SUB:
b0d9ce38 2669 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2670 break;
a0d0e21e 2671 }
3280af22 2672 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2673
b0d9ce38 2674 LEAVESUB(sv);
9d4ba2ae
AL
2675 PERL_UNUSED_VAR(optype);
2676 PERL_UNUSED_VAR(gimme);
f86702cc 2677 return nextop;
a0d0e21e
LW
2678}
2679
2680PP(pp_next)
2681{
27da23d5 2682 dVAR;
a0d0e21e 2683 I32 cxix;
c09156bb 2684 register PERL_CONTEXT *cx;
85538317 2685 I32 inner;
a0d0e21e 2686
533c011a 2687 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2688 cxix = dopoptoloop(cxstack_ix);
2689 if (cxix < 0)
a651a37d 2690 DIE(aTHX_ "Can't \"next\" outside a loop block");
a0d0e21e
LW
2691 }
2692 else {
2693 cxix = dopoptolabel(cPVOP->op_pv);
2694 if (cxix < 0)
cea2e8a9 2695 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
a0d0e21e
LW
2696 }
2697 if (cxix < cxstack_ix)
2698 dounwind(cxix);
2699
85538317
GS
2700 /* clear off anything above the scope we're re-entering, but
2701 * save the rest until after a possible continue block */
2702 inner = PL_scopestack_ix;
1ba6ee2b 2703 TOPBLOCK(cx);
85538317
GS
2704 if (PL_scopestack_ix < inner)
2705 leave_scope(PL_scopestack[PL_scopestack_ix]);
3a1b2b9e 2706 PL_curcop = cx->blk_oldcop;
d57ce4df 2707 return (cx)->blk_loop.my_op->op_nextop;
a0d0e21e
LW
2708}
2709
2710PP(pp_redo)
2711{
27da23d5 2712 dVAR;
a0d0e21e 2713 I32 cxix;
c09156bb 2714 register PERL_CONTEXT *cx;
a0d0e21e 2715 I32 oldsave;
a034e688 2716 OP* redo_op;
a0d0e21e 2717
533c011a 2718 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2719 cxix = dopoptoloop(cxstack_ix);
2720 if (cxix < 0)
a651a37d 2721 DIE(aTHX_ "Can't \"redo\" outside a loop block");
a0d0e21e
LW
2722 }
2723 else {
2724 cxix = dopoptolabel(cPVOP->op_pv);
2725 if (cxix < 0)
cea2e8a9 2726 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
a0d0e21e
LW
2727 }
2728 if (cxix < cxstack_ix)
2729 dounwind(cxix);
2730
022eaa24 2731 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
a034e688
DM
2732 if (redo_op->op_type == OP_ENTER) {
2733 /* pop one less context to avoid $x being freed in while (my $x..) */
2734 cxstack_ix++;
2735 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2736 redo_op = redo_op->op_next;
2737 }
2738
a0d0e21e 2739 TOPBLOCK(cx);
3280af22 2740 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e 2741 LEAVE_SCOPE(oldsave);
936c78b5 2742 FREETMPS;
3a1b2b9e 2743 PL_curcop = cx->blk_oldcop;
a034e688 2744 return redo_op;
a0d0e21e
LW
2745}
2746
0824fdcb 2747STATIC OP *
bfed75c6 2748S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
a0d0e21e 2749{
97aff369 2750 dVAR;
a0d0e21e 2751 OP **ops = opstack;
bfed75c6 2752 static const char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 2753
7918f24d
NC
2754 PERL_ARGS_ASSERT_DOFINDLABEL;
2755
fc36a67e 2756 if (ops >= oplimit)
cea2e8a9 2757 Perl_croak(aTHX_ too_deep);
11343788
MB
2758 if (o->op_type == OP_LEAVE ||
2759 o->op_type == OP_SCOPE ||
2760 o->op_type == OP_LEAVELOOP ||
33d34e4c 2761 o->op_type == OP_LEAVESUB ||
11343788 2762 o->op_type == OP_LEAVETRY)
fc36a67e 2763 {
5dc0d613 2764 *ops++ = cUNOPo->op_first;
fc36a67e 2765 if (ops >= oplimit)
cea2e8a9 2766 Perl_croak(aTHX_ too_deep);
fc36a67e 2767 }
c4aa4e48 2768 *ops = 0;
11343788 2769 if (o->op_flags & OPf_KIDS) {
aec46f14 2770 OP *kid;
a0d0e21e 2771 /* First try all the kids at this level, since that's likeliest. */
11343788 2772 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
7e8f1eac
AD
2773 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2774 const char *kid_label = CopLABEL(kCOP);
2775 if (kid_label && strEQ(kid_label, label))
2776 return kid;
2777 }
a0d0e21e 2778 }
11343788 2779 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 2780 if (kid == PL_lastgotoprobe)
a0d0e21e 2781 continue;
ed8d0fe2
SM
2782 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2783 if (ops == opstack)
2784 *ops++ = kid;
2785 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2786 ops[-1]->op_type == OP_DBSTATE)
2787 ops[-1] = kid;
2788 else
2789 *ops++ = kid;
2790 }
155aba94 2791 if ((o = dofindlabel(kid, label, ops, oplimit)))
11343788 2792 return o;
a0d0e21e
LW
2793 }
2794 }
c4aa4e48 2795 *ops = 0;
a0d0e21e
LW
2796 return 0;
2797}
2798
a0d0e21e
LW
2799PP(pp_goto)
2800{
27da23d5 2801 dVAR; dSP;
cbbf8932 2802 OP *retop = NULL;
a0d0e21e 2803 I32 ix;
c09156bb 2804 register PERL_CONTEXT *cx;
fc36a67e 2805#define GOTO_DEPTH 64
2806 OP *enterops[GOTO_DEPTH];
cbbf8932 2807 const char *label = NULL;
bfed75c6
AL
2808 const bool do_dump = (PL_op->op_type == OP_DUMP);
2809 static const char must_have_label[] = "goto must have label";
a0d0e21e 2810
533c011a 2811 if (PL_op->op_flags & OPf_STACKED) {
9d4ba2ae 2812 SV * const sv = POPs;
a0d0e21e
LW
2813
2814 /* This egregious kludge implements goto &subroutine */
2815 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2816 I32 cxix;
c09156bb 2817 register PERL_CONTEXT *cx;
ea726b52 2818 CV *cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2819 SV** mark;
2820 I32 items = 0;
2821 I32 oldsave;
b1464ded 2822 bool reified = 0;
a0d0e21e 2823
e8f7dd13 2824 retry:
4aa0a1f7 2825 if (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2826 const GV * const gv = CvGV(cv);
e8f7dd13 2827 if (gv) {
7fc63493 2828 GV *autogv;
e8f7dd13
GS
2829 SV *tmpstr;
2830 /* autoloaded stub? */
2831 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2832 goto retry;
2833 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2834 GvNAMELEN(gv), FALSE);
2835 if (autogv && (cv = GvCV(autogv)))
2836 goto retry;
2837 tmpstr = sv_newmortal();
c445ea15 2838 gv_efullname3(tmpstr, gv, NULL);
be2597df 2839 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
4aa0a1f7 2840 }
cea2e8a9 2841 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2842 }
2843
a0d0e21e 2844 /* First do some returnish stuff. */
b37c2d43 2845 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
71fc2216 2846 FREETMPS;
a0d0e21e
LW
2847 cxix = dopoptosub(cxstack_ix);
2848 if (cxix < 0)
cea2e8a9 2849 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
a0d0e21e
LW
2850 if (cxix < cxstack_ix)
2851 dounwind(cxix);
2852 TOPBLOCK(cx);
2d43a17f 2853 SPAGAIN;
564abe23 2854 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2855 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89
DM
2856 if (CxREALEVAL(cx))
2857 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2858 else
2859 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2860 }
9850bf21
RH
2861 else if (CxMULTICALL(cx))
2862 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
bafb2adc 2863 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
d8b46c1b 2864 /* put @_ back onto stack */
a0d0e21e 2865 AV* av = cx->blk_sub.argarray;
bfed75c6 2866
93965878 2867 items = AvFILLp(av) + 1;
a45cdc79
DM
2868 EXTEND(SP, items+1); /* @_ could have been extended. */
2869 Copy(AvARRAY(av), SP + 1, items, SV*);
3280af22
NIS
2870 SvREFCNT_dec(GvAV(PL_defgv));
2871 GvAV(PL_defgv) = cx->blk_sub.savearray;
b1464ded 2872 CLEAR_ARGARRAY(av);
d8b46c1b 2873 /* abandon @_ if it got reified */
62b1ebc2 2874 if (AvREAL(av)) {
b1464ded
DM
2875 reified = 1;
2876 SvREFCNT_dec(av);
d8b46c1b
GS
2877 av = newAV();
2878 av_extend(av, items-1);
11ca45c0 2879 AvREIFY_only(av);
ad64d0ec 2880 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
62b1ebc2 2881 }
a0d0e21e 2882 }
aed2304a 2883 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
890ce7af 2884 AV* const av = GvAV(PL_defgv);
1fa4e549 2885 items = AvFILLp(av) + 1;
a45cdc79
DM
2886 EXTEND(SP, items+1); /* @_ could have been extended. */
2887 Copy(AvARRAY(av), SP + 1, items, SV*);
1fa4e549 2888 }
a45cdc79
DM
2889 mark = SP;
2890 SP += items;
6b35e009 2891 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 2892 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 2893 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 2894 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
2895 LEAVE_SCOPE(oldsave);
2896
2897 /* Now do some callish stuff. */
2898 SAVETMPS;
5023d17a 2899 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
aed2304a 2900 if (CvISXSUB(cv)) {
b37c2d43 2901 OP* const retop = cx->blk_sub.retop;
9410e98d
RB
2902 SV **newsp __attribute__unused__;
2903 I32 gimme __attribute__unused__;
b1464ded
DM
2904 if (reified) {
2905 I32 index;
2906 for (index=0; index<items; index++)
2907 sv_2mortal(SP[-index]);
2908 }
1fa4e549 2909
b37c2d43
AL
2910 /* XS subs don't have a CxSUB, so pop it */
2911 POPBLOCK(cx, PL_curpm);
2912 /* Push a mark for the start of arglist */
2913 PUSHMARK(mark);
2914 PUTBACK;
2915 (void)(*CvXSUB(cv))(aTHX_ cv);
a57c6685 2916 LEAVE;
5eff7df7 2917 return retop;
a0d0e21e
LW
2918 }
2919 else {
b37c2d43 2920 AV* const padlist = CvPADLIST(cv);
6b35e009 2921 if (CxTYPE(cx) == CXt_EVAL) {
85a64632 2922 PL_in_eval = CxOLD_IN_EVAL(cx);
3280af22 2923 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22 2924 cx->cx_type = CXt_SUB;
b150fb22 2925 }
a0d0e21e 2926 cx->blk_sub.cv = cv;
1a5b3db4 2927 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2928
a0d0e21e
LW
2929 CvDEPTH(cv)++;
2930 if (CvDEPTH(cv) < 2)
74c765eb 2931 SvREFCNT_inc_simple_void_NN(cv);
dd2155a4 2932 else {
2b9dff67 2933 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2934 sub_crush_depth(cv);
26019298 2935 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2936 }
fd617465
DM
2937 SAVECOMPPAD();
2938 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 2939 if (CxHASARGS(cx))
6d4ff0d2 2940 {
502c6561 2941 AV *const av = MUTABLE_AV(PAD_SVl(0));
a0d0e21e 2942
3280af22 2943 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 2944 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 2945 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2946 cx->blk_sub.argarray = av;
a0d0e21e
LW
2947
2948 if (items >= AvMAX(av) + 1) {
b37c2d43 2949 SV **ary = AvALLOC(av);
a0d0e21e
LW
2950 if (AvARRAY(av) != ary) {
2951 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 2952 AvARRAY(av) = ary;
a0d0e21e
LW
2953 }
2954 if (items >= AvMAX(av) + 1) {
2955 AvMAX(av) = items - 1;
2956 Renew(ary,items+1,SV*);
2957 AvALLOC(av) = ary;
9c6bc640 2958 AvARRAY(av) = ary;
a0d0e21e
LW
2959 }
2960 }
a45cdc79 2961 ++mark;
a0d0e21e 2962 Copy(mark,AvARRAY(av),items,SV*);
93965878 2963 AvFILLp(av) = items - 1;
d8b46c1b 2964 assert(!AvREAL(av));
b1464ded
DM
2965 if (reified) {
2966 /* transfer 'ownership' of refcnts to new @_ */
2967 AvREAL_on(av);
2968 AvREIFY_off(av);
2969 }
a0d0e21e
LW
2970 while (items--) {
2971 if (*mark)
2972 SvTEMP_off(*mark);
2973 mark++;
2974 }
2975 }
491527d0 2976 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 2977 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43 2978 if (PERLDB_GOTO) {
b96d8cd9 2979 CV * const gotocv = get_cvs("DB::goto", 0);
b37c2d43
AL
2980 if (gotocv) {
2981 PUSHMARK( PL_stack_sp );
ad64d0ec 2982 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
2983 PL_stack_sp--;
2984 }
491527d0 2985 }
1ce6579f 2986 }
a0d0e21e
LW
2987 RETURNOP(CvSTART(cv));
2988 }
2989 }
1614b0e3 2990 else {
0510663f 2991 label = SvPV_nolen_const(sv);
1614b0e3 2992 if (!(do_dump || *label))
cea2e8a9 2993 DIE(aTHX_ must_have_label);
1614b0e3 2994 }
a0d0e21e 2995 }
533c011a 2996 else if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 2997 if (! do_dump)
cea2e8a9 2998 DIE(aTHX_ must_have_label);
a0d0e21e
LW
2999 }
3000 else
3001 label = cPVOP->op_pv;
3002
f410a211
NC
3003 PERL_ASYNC_CHECK();
3004
a0d0e21e 3005 if (label && *label) {
cbbf8932 3006 OP *gotoprobe = NULL;
3b2447bc 3007 bool leaving_eval = FALSE;
33d34e4c 3008 bool in_block = FALSE;
cbbf8932 3009 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
3010
3011 /* find label */
3012
d4c19fe8 3013 PL_lastgotoprobe = NULL;
a0d0e21e
LW
3014 *enterops = 0;
3015 for (ix = cxstack_ix; ix >= 0; ix--) {
3016 cx = &cxstack[ix];
6b35e009 3017 switch (CxTYPE(cx)) {
a0d0e21e 3018 case CXt_EVAL:
3b2447bc 3019 leaving_eval = TRUE;
971ecbe6 3020 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
3021 gotoprobe = (last_eval_cx ?
3022 last_eval_cx->blk_eval.old_eval_root :
3023 PL_eval_root);
3024 last_eval_cx = cx;
9c5794fe
RH
3025 break;
3026 }
3027 /* else fall through */
c6fdafd0 3028 case CXt_LOOP_LAZYIV:
d01136d6 3029 case CXt_LOOP_LAZYSV:
3b719c58
NC
3030 case CXt_LOOP_FOR:
3031 case CXt_LOOP_PLAIN:
bb5aedc1
VP
3032 case CXt_GIVEN:
3033 case CXt_WHEN:
a0d0e21e
LW
3034 gotoprobe = cx->blk_oldcop->op_sibling;
3035 break;
3036 case CXt_SUBST:
3037 continue;
3038 case CXt_BLOCK:
33d34e4c 3039 if (ix) {
a0d0e21e 3040 gotoprobe = cx->blk_oldcop->op_sibling;
33d34e4c
AE
3041 in_block = TRUE;
3042 } else
3280af22 3043 gotoprobe = PL_main_root;
a0d0e21e 3044 break;
b3933176 3045 case CXt_SUB:
9850bf21 3046 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
b3933176
CS
3047 gotoprobe = CvROOT(cx->blk_sub.cv);
3048 break;
3049 }
3050 /* FALL THROUGH */
7766f137 3051 case CXt_FORMAT:
0a753a76 3052 case CXt_NULL:
a651a37d 3053 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
3054 default:
3055 if (ix)
cea2e8a9 3056 DIE(aTHX_ "panic: goto");
3280af22 3057 gotoprobe = PL_main_root;
a0d0e21e
LW
3058 break;
3059 }
2b597662
GS
3060 if (gotoprobe) {
3061 retop = dofindlabel(gotoprobe, label,
3062 enterops, enterops + GOTO_DEPTH);
3063 if (retop)
3064 break;
eae48c89
Z
3065 if (gotoprobe->op_sibling &&
3066 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
3067 gotoprobe->op_sibling->op_sibling) {
3068 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
3069 label, enterops, enterops + GOTO_DEPTH);
3070 if (retop)
3071 break;
3072 }
2b597662 3073 }
3280af22 3074 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
3075 }
3076 if (!retop)
cea2e8a9 3077 DIE(aTHX_ "Can't find label %s", label);
a0d0e21e 3078
3b2447bc
RH
3079 /* if we're leaving an eval, check before we pop any frames
3080 that we're not going to punt, otherwise the error
3081 won't be caught */
3082
3083 if (leaving_eval && *enterops && enterops[1]) {
3084 I32 i;
3085 for (i = 1; enterops[i]; i++)
3086 if (enterops[i]->op_type == OP_ENTERITER)
3087 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3088 }
3089
b500e03b
GG
3090 if (*enterops && enterops[1]) {
3091 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3092 if (enterops[i])
3093 deprecate("\"goto\" to jump into a construct");
3094 }
3095
a0d0e21e
LW
3096 /* pop unwanted frames */
3097
3098 if (ix < cxstack_ix) {
3099 I32 oldsave;
3100
3101 if (ix < 0)
3102 ix = 0;
3103 dounwind(ix);
3104 TOPBLOCK(cx);
3280af22 3105 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
3106 LEAVE_SCOPE(oldsave);
3107 }
3108
3109 /* push wanted frames */
3110
748a9306 3111 if (*enterops && enterops[1]) {
0bd48802 3112 OP * const oldop = PL_op;
33d34e4c
AE
3113 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3114 for (; enterops[ix]; ix++) {
533c011a 3115 PL_op = enterops[ix];
84902520
TB
3116 /* Eventually we may want to stack the needed arguments
3117 * for each op. For now, we punt on the hard ones. */
533c011a 3118 if (PL_op->op_type == OP_ENTERITER)
894356b3 3119 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
16c91539 3120 PL_op->op_ppaddr(aTHX);
a0d0e21e 3121 }
533c011a 3122 PL_op = oldop;
a0d0e21e
LW
3123 }
3124 }
3125
3126 if (do_dump) {
a5f75d66 3127#ifdef VMS
6b88bc9c 3128 if (!retop) retop = PL_main_start;
a5f75d66 3129#endif
3280af22
NIS
3130 PL_restartop = retop;
3131 PL_do_undump = TRUE;
a0d0e21e
LW
3132
3133 my_unexec();
3134
3280af22
NIS
3135 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3136 PL_do_undump = FALSE;
a0d0e21e
LW
3137 }
3138
3139 RETURNOP(retop);
3140}
3141
3142PP(pp_exit)
3143{
97aff369 3144 dVAR;
39644a26 3145 dSP;
a0d0e21e
LW
3146 I32 anum;
3147
3148 if (MAXARG < 1)
3149 anum = 0;
ff0cee69 3150 else {
a0d0e21e 3151 anum = SvIVx(POPs);
d98f61e7
GS
3152#ifdef VMS
3153 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
ff0cee69 3154 anum = 0;
96e176bf 3155 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
ff0cee69 3156#endif
3157 }
cc3604b1 3158 PL_exit_flags |= PERL_EXIT_EXPECTED;
81d86705
NC
3159#ifdef PERL_MAD
3160 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
3161 if (anum || !(PL_minus_c && PL_madskills))
3162 my_exit(anum);
3163#else
a0d0e21e 3164 my_exit(anum);
81d86705 3165#endif
3280af22 3166 PUSHs(&PL_sv_undef);
a0d0e21e
LW
3167 RETURN;
3168}
3169
a0d0e21e
LW
3170/* Eval. */
3171
0824fdcb 3172STATIC void
cea2e8a9 3173S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 3174{
504618e9 3175 const char *s = SvPVX_const(sv);
890ce7af 3176 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 3177 I32 line = 1;
a0d0e21e 3178
7918f24d
NC
3179 PERL_ARGS_ASSERT_SAVE_LINES;
3180
a0d0e21e 3181 while (s && s < send) {
f54cb97a 3182 const char *t;
b9f83d2f 3183 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 3184
1d963ff3 3185 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
3186 if (t)
3187 t++;
3188 else
3189 t = send;
3190
3191 sv_setpvn(tmpstr, s, t - s);
3192 av_store(array, line++, tmpstr);
3193 s = t;
3194 }
3195}
3196
22f16304
RU
3197/*
3198=for apidoc docatch
3199
3200Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3201
32020 is used as continue inside eval,
3203
32043 is used for a die caught by an inner eval - continue inner loop
3205
3206See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3207establish a local jmpenv to handle exception traps.
3208
3209=cut
3210*/
0824fdcb 3211STATIC OP *
cea2e8a9 3212S_docatch(pTHX_ OP *o)
1e422769 3213{
97aff369 3214 dVAR;
6224f72b 3215 int ret;
06b5626a 3216 OP * const oldop = PL_op;
db36c5a1 3217 dJMPENV;
1e422769 3218
1e422769 3219#ifdef DEBUGGING
54310121 3220 assert(CATCH_GET == TRUE);
1e422769 3221#endif
312caa8e 3222 PL_op = o;
8bffa5f8 3223
14dd3ad8 3224 JMPENV_PUSH(ret);
6224f72b 3225 switch (ret) {
312caa8e 3226 case 0:
abd70938
DM
3227 assert(cxstack_ix >= 0);
3228 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3229 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
14dd3ad8 3230 redo_body:
85aaa934 3231 CALLRUNOPS(aTHX);
312caa8e
CS
3232 break;
3233 case 3:
8bffa5f8 3234 /* die caught by an inner eval - continue inner loop */
febb3a6d
Z
3235 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3236 PL_restartjmpenv = NULL;
312caa8e
CS
3237 PL_op = PL_restartop;
3238 PL_restartop = 0;
3239 goto redo_body;
3240 }
3241 /* FALL THROUGH */
3242 default:
14dd3ad8 3243 JMPENV_POP;
533c011a 3244 PL_op = oldop;
6224f72b 3245 JMPENV_JUMP(ret);
1e422769 3246 /* NOTREACHED */
1e422769 3247 }
14dd3ad8 3248 JMPENV_POP;
533c011a 3249 PL_op = oldop;
5f66b61c 3250 return NULL;
1e422769 3251}
3252
ee23ad3b
NC
3253/* James Bond: Do you expect me to talk?
3254 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3255
3256 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3257 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3258
3259 Currently it is not used outside the core code. Best if it stays that way.
d59a8b3e
NC
3260
3261 Hence it's now deprecated, and will be removed.
ee23ad3b 3262*/
c277df42 3263OP *
bfed75c6 3264Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
c277df42
IZ
3265/* sv Text to convert to OP tree. */
3266/* startop op_free() this to undo. */
3267/* code Short string id of the caller. */
3268{
d59a8b3e
NC
3269 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3270 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3271}
3272
3273/* Don't use this. It will go away without warning once the regexp engine is
3274 refactored not to use it. */
3275OP *
3276Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3277 PAD **padp)
3278{
27da23d5 3279 dVAR; dSP; /* Make POPBLOCK work. */
c277df42
IZ
3280 PERL_CONTEXT *cx;
3281 SV **newsp;
b094c71d 3282 I32 gimme = G_VOID;
c277df42
IZ
3283 I32 optype;
3284 OP dummy;
83ee9e09
GS
3285 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3286 char *tmpbuf = tbuf;
c277df42 3287 char *safestr;
a3985cdc 3288 int runtime;
601f1833 3289 CV* runcv = NULL; /* initialise to avoid compiler warnings */
f7997f86 3290 STRLEN len;
634d6919 3291 bool need_catch;
c277df42 3292
d59a8b3e 3293 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
7918f24d 3294
d343c3ef 3295 ENTER_with_name("eval");
27fcb6ee 3296 lex_start(sv, NULL, LEX_START_SAME_FILTER);
c277df42
IZ
3297 SAVETMPS;
3298 /* switch to eval mode */
3299
923e4eb5 3300 if (IN_PERL_COMPILETIME) {
f4dd75d9 3301 SAVECOPSTASH_FREE(&PL_compiling);
11faa288 3302 CopSTASH_set(&PL_compiling, PL_curstash);
cbce877f 3303 }
83ee9e09 3304 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
9d4ba2ae 3305 SV * const sv = sv_newmortal();
83ee9e09
GS
3306 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3307 code, (unsigned long)++PL_evalseq,
3308 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3309 tmpbuf = SvPVX(sv);
fc009855 3310 len = SvCUR(sv);
83ee9e09
GS
3311 }
3312 else
d9fad198
JH
3313 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3314 (unsigned long)++PL_evalseq);
f4dd75d9 3315 SAVECOPFILE_FREE(&PL_compiling);
57843af0 3316 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 3317 SAVECOPLINE(&PL_compiling);
57843af0 3318 CopLINE_set(&PL_compiling, 1);
c277df42
IZ
3319 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3320 deleting the eval's FILEGV from the stash before gv_check() runs
3321 (i.e. before run-time proper). To work around the coredump that
3322 ensues, we always turn GvMULTI_on for any globals that were
3323 introduced within evals. See force_ident(). GSAR 96-10-12 */
f7997f86
NC
3324 safestr = savepvn(tmpbuf, len);
3325 SAVEDELETE(PL_defstash, safestr, len);
b3ac6de7 3326 SAVEHINTS();
d1ca3daa 3327#ifdef OP_IN_REGISTER
6b88bc9c 3328 PL_opsave = op;
d1ca3daa 3329#else
7766f137 3330 SAVEVPTR(PL_op);
d1ca3daa 3331#endif
c277df42 3332
a3985cdc 3333 /* we get here either during compilation, or via pp_regcomp at runtime */
923e4eb5 3334 runtime = IN_PERL_RUNTIME;
a3985cdc 3335 if (runtime)
558b4424 3336 {
d819b83a 3337 runcv = find_runcv(NULL);
a3985cdc 3338
558b4424
FC
3339 /* At run time, we have to fetch the hints from PL_curcop. */
3340 PL_hints = PL_curcop->cop_hints;
3341 if (PL_hints & HINT_LOCALIZE_HH) {
3342 /* SAVEHINTS created a new HV in PL_hintgv, which we
3343 need to GC */
3344 SvREFCNT_dec(GvHV(PL_hintgv));
3345 GvHV(PL_hintgv) =
3346 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3347 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3348 }
3349 SAVECOMPILEWARNINGS();
3350 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3351 cophh_free(CopHINTHASH_get(&PL_compiling));
3352 /* XXX Does this need to avoid copying a label? */
3353 PL_compiling.cop_hints_hash
3354 = cophh_copy(PL_curcop->cop_hints_hash);
3355 }
3356
533c011a 3357 PL_op = &dummy;
13b51b79 3358 PL_op->op_type = OP_ENTEREVAL;
533c011a 3359 PL_op->op_flags = 0; /* Avoid uninit warning. */
923e4eb5 3360 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
6b75f042 3361 PUSHEVAL(cx, 0);
634d6919
GG
3362 need_catch = CATCH_GET;
3363 CATCH_SET(TRUE);
a3985cdc
DM
3364
3365 if (runtime)
410be5db 3366 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
a3985cdc 3367 else
410be5db 3368 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
634d6919 3369 CATCH_SET(need_catch);
13b51b79 3370 POPBLOCK(cx,PL_curpm);
e84b9f1f 3371 POPEVAL(cx);
c277df42
IZ
3372
3373 (*startop)->op_type = OP_NULL;
22c35a8c 3374 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
f3548bdc 3375 /* XXX DAPM do this properly one year */
502c6561 3376 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
d343c3ef 3377 LEAVE_with_name("eval");
923e4eb5 3378 if (IN_PERL_COMPILETIME)
623e6609 3379 CopHINTS_set(&PL_compiling, PL_hints);
d1ca3daa 3380#ifdef OP_IN_REGISTER
6b88bc9c 3381 op = PL_opsave;
d1ca3daa 3382#endif
9d4ba2ae
AL
3383 PERL_UNUSED_VAR(newsp);
3384 PERL_UNUSED_VAR(optype);
3385
410be5db 3386 return PL_eval_start;
c277df42
IZ
3387}
3388
a3985cdc
DM
3389
3390/*
3391=for apidoc find_runcv
3392
3393Locate the CV corresponding to the currently executing sub or eval.
d819b83a
DM
3394If db_seqp is non_null, skip CVs that are in the DB package and populate
3395*db_seqp with the cop sequence number at the point that the DB:: code was
3396entered. (allows debuggers to eval in the scope of the breakpoint rather
cf525c36 3397than in the scope of the debugger itself).
a3985cdc
DM
3398
3399=cut
3400*/
3401
3402CV*
d819b83a 3403Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3404{
97aff369 3405 dVAR;
a3985cdc 3406 PERL_SI *si;
a3985cdc 3407
d819b83a
DM
3408 if (db_seqp)
3409 *db_seqp = PL_curcop->cop_seq;
a3985cdc 3410 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3411 I32 ix;
a3985cdc 3412 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 3413 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
d819b83a 3414 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1b6737cc 3415 CV * const cv = cx->blk_sub.cv;
d819b83a
DM
3416 /* skip DB:: code */
3417 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3418 *db_seqp = cx->blk_oldcop->cop_seq;
3419 continue;
3420 }
3421 return cv;
3422 }
a3985cdc
DM
3423 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3424 return PL_compcv;
3425 }
3426 }
3427 return PL_main_cv;
3428}
3429
3430
27e90453
DM
3431/* Run yyparse() in a setjmp wrapper. Returns:
3432 * 0: yyparse() successful
3433 * 1: yyparse() failed
3434 * 3: yyparse() died
3435 */
3436STATIC int
28ac2b49 3437S_try_yyparse(pTHX_ int gramtype)
27e90453
DM
3438{
3439 int ret;
3440 dJMPENV;
3441
3442 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3443 JMPENV_PUSH(ret);
3444 switch (ret) {
3445 case 0:
28ac2b49 3446 ret = yyparse(gramtype) ? 1 : 0;
27e90453
DM
3447 break;
3448 case 3:
3449 break;
3450 default:
3451 JMPENV_POP;
3452 JMPENV_JUMP(ret);
3453 /* NOTREACHED */
3454 }
3455 JMPENV_POP;
3456 return ret;
3457}
3458
3459
a3985cdc
DM
3460/* Compile a require/do, an eval '', or a /(?{...})/.
3461 * In the last case, startop is non-null, and contains the address of
3462 * a pointer that should be set to the just-compiled code.
3463 * outside is the lexically enclosing CV (if any) that invoked us.
410be5db
DM
3464 * Returns a bool indicating whether the compile was successful; if so,
3465 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3466 * pushes undef (also croaks if startop != NULL).
a3985cdc
DM
3467 */
3468
410be5db 3469STATIC bool
a3985cdc 3470S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
a0d0e21e 3471{
27da23d5 3472 dVAR; dSP;
46c461b5 3473 OP * const saveop = PL_op;
27e90453
DM
3474 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3475 int yystatus;
a0d0e21e 3476
27e90453 3477 PL_in_eval = (in_require
6dc8a9e4
IZ
3478 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3479 : EVAL_INEVAL);
a0d0e21e 3480
1ce6579f 3481 PUSHMARK(SP);
3482
3280af22 3483 SAVESPTR(PL_compcv);
ea726b52 3484 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
1aff0e91 3485 CvEVAL_on(PL_compcv);
2090ab20
JH
3486 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3487 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3488
a3985cdc 3489 CvOUTSIDE_SEQ(PL_compcv) = seq;
ea726b52 3490 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3491
d