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