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