This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_formline: keep linemark consistent
[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 */
f5ada144 538 STRLEN linemark = 0; /* 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:
f5ada144 601 linemark = t - SvPVX(PL_formtarget);
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) {
f5ada144 853 U8 *s;
0d21cefe 854 /* Upgrade targ to UTF8, and then we reduce it to
0325ce87
DM
855 a problem we have a simple solution for.
856 Don't need get magic. */
0d21cefe 857 sv_utf8_upgrade_nomg(PL_formtarget);
0325ce87 858 targ_is_utf8 = TRUE;
f5ada144
DM
859 /* re-calculate linemark */
860 s = (U8*)SvPVX(PL_formtarget);
861 while (linemark--)
862 s += UTF8SKIP(s);
863 linemark = s - (U8*)SvPVX(PL_formtarget);
e8e72d41 864 }
0d21cefe
DM
865 /* Easy. They agree. */
866 assert (item_is_utf8 == targ_is_utf8);
867 }
868 SvGROW(PL_formtarget,
bf2bec63 869 SvCUR(PL_formtarget) + to_copy + 1);
0d21cefe
DM
870 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
871
872 Copy(source, t, to_copy, char);
ea60cfe8 873 if (trans) {
8aa7beb6
DM
874 /* blank out ~ or control chars, depending on trans.
875 * works on bytes not chars, so relies on not
876 * matching utf8 continuation bytes */
ea60cfe8
DM
877 U8 *s = (U8*)t;
878 U8 *send = s + to_copy;
879 while (s < send) {
8aa7beb6
DM
880 const int ch = *s;
881 if (trans == '~' ? (ch == '~') :
882#ifdef EBCDIC
883 iscntrl(ch)
884#else
885 (!(ch & ~31))
886#endif
887 )
ea60cfe8
DM
888 *s = ' ';
889 s++;
890 }
891 }
892
0d21cefe
DM
893 t += to_copy;
894 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
a1137ee5 895 if (tmp)
0d21cefe 896 Safefree(tmp);
5a34cab7 897 break;
a0d0e21e 898 }
a0d0e21e 899
a1b95068
LW
900 case FF_0DECIMAL:
901 arg = *fpc++;
902#if defined(USE_LONG_DOUBLE)
10edeb5d
JH
903 fmt = (const char *)
904 ((arg & 256) ?
905 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
a1b95068 906#else
10edeb5d
JH
907 fmt = (const char *)
908 ((arg & 256) ?
909 "%#0*.*f" : "%0*.*f");
a1b95068
LW
910#endif
911 goto ff_dec;
a0d0e21e 912 case FF_DECIMAL:
a0d0e21e 913 arg = *fpc++;
65202027 914#if defined(USE_LONG_DOUBLE)
10edeb5d
JH
915 fmt = (const char *)
916 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
65202027 917#else
10edeb5d
JH
918 fmt = (const char *)
919 ((arg & 256) ? "%#*.*f" : "%*.*f");
65202027 920#endif
a1b95068 921 ff_dec:
784707d5
JP
922 /* If the field is marked with ^ and the value is undefined,
923 blank it out. */
784707d5
JP
924 if ((arg & 512) && !SvOK(sv)) {
925 arg = fieldsize;
926 while (arg--)
927 *t++ = ' ';
928 break;
929 }
930 gotsome = TRUE;
931 value = SvNV(sv);
a1b95068 932 /* overflow evidence */
bfed75c6 933 if (num_overflow(value, fieldsize, arg)) {
a1b95068
LW
934 arg = fieldsize;
935 while (arg--)
936 *t++ = '#';
937 break;
938 }
784707d5
JP
939 /* Formats aren't yet marked for locales, so assume "yes". */
940 {
941 STORE_NUMERIC_STANDARD_SET_LOCAL();
d9fad198 942 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
784707d5
JP
943 RESTORE_NUMERIC_STANDARD();
944 }
945 t += fieldsize;
946 break;
a1b95068 947
a0d0e21e
LW
948 case FF_NEWLINE:
949 f++;
f5ada144 950 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
a0d0e21e
LW
951 t++;
952 *t++ = '\n';
953 break;
954
955 case FF_BLANK:
956 arg = *fpc++;
957 if (gotsome) {
958 if (arg) { /* repeat until fields exhausted? */
11f9eeaf
DM
959 fpc--;
960 goto end;
a0d0e21e
LW
961 }
962 }
963 else {
f5ada144 964 t = SvPVX(PL_formtarget) + linemark;
a0d0e21e
LW
965 lines--;
966 }
967 break;
968
969 case FF_MORE:
5a34cab7
NC
970 {
971 const char *s = chophere;
972 const char *send = item + len;
973 if (chopspace) {
af68e756 974 while (isSPACE(*s) && (s < send))
5a34cab7 975 s++;
a0d0e21e 976 }
5a34cab7
NC
977 if (s < send) {
978 char *s1;
979 arg = fieldsize - itemsize;
980 if (arg) {
981 fieldsize -= arg;
982 while (arg-- > 0)
983 *t++ = ' ';
984 }
985 s1 = t - 3;
986 if (strnEQ(s1," ",3)) {
987 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
988 s1--;
989 }
990 *s1++ = '.';
991 *s1++ = '.';
992 *s1++ = '.';
a0d0e21e 993 }
5a34cab7 994 break;
a0d0e21e 995 }
a0d0e21e 996 case FF_END:
11f9eeaf 997 end:
bf2bec63 998 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
a0d0e21e 999 *t = '\0';
b15aece3 1000 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
1001 if (targ_is_utf8)
1002 SvUTF8_on(PL_formtarget);
3280af22 1003 FmLINES(PL_formtarget) += lines;
a0d0e21e 1004 SP = ORIGMARK;
11f9eeaf
DM
1005 if (fpc[-1] == FF_BLANK)
1006 RETURNOP(cLISTOP->op_first);
1007 else
1008 RETPUSHYES;
a0d0e21e
LW
1009 }
1010 }
1011}
1012
1013PP(pp_grepstart)
1014{
27da23d5 1015 dVAR; dSP;
a0d0e21e
LW
1016 SV *src;
1017
3280af22 1018 if (PL_stack_base + *PL_markstack_ptr == SP) {
a0d0e21e 1019 (void)POPMARK;
54310121 1020 if (GIMME_V == G_SCALAR)
6e449a3a 1021 mXPUSHi(0);
533c011a 1022 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 1023 }
3280af22 1024 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
897d3989
NC
1025 Perl_pp_pushmark(aTHX); /* push dst */
1026 Perl_pp_pushmark(aTHX); /* push src */
d343c3ef 1027 ENTER_with_name("grep"); /* enter outer scope */
a0d0e21e
LW
1028
1029 SAVETMPS;
59f00321
RGS
1030 if (PL_op->op_private & OPpGREP_LEX)
1031 SAVESPTR(PAD_SVl(PL_op->op_targ));
1032 else
1033 SAVE_DEFSV;
d343c3ef 1034 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1035 SAVEVPTR(PL_curpm);
a0d0e21e 1036
3280af22 1037 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1038 SvTEMP_off(src);
59f00321
RGS
1039 if (PL_op->op_private & OPpGREP_LEX)
1040 PAD_SVl(PL_op->op_targ) = src;
1041 else
414bf5ae 1042 DEFSV_set(src);
a0d0e21e
LW
1043
1044 PUTBACK;
533c011a 1045 if (PL_op->op_type == OP_MAPSTART)
897d3989 1046 Perl_pp_pushmark(aTHX); /* push top */
533c011a 1047 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
1048}
1049
a0d0e21e
LW
1050PP(pp_mapwhile)
1051{
27da23d5 1052 dVAR; dSP;
f54cb97a 1053 const I32 gimme = GIMME_V;
544f3153 1054 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
a0d0e21e
LW
1055 I32 count;
1056 I32 shift;
1057 SV** src;
ac27b0f5 1058 SV** dst;
a0d0e21e 1059
544f3153 1060 /* first, move source pointer to the next item in the source list */
3280af22 1061 ++PL_markstack_ptr[-1];
544f3153
GS
1062
1063 /* if there are new items, push them into the destination list */
4c90a460 1064 if (items && gimme != G_VOID) {
544f3153
GS
1065 /* might need to make room back there first */
1066 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1067 /* XXX this implementation is very pessimal because the stack
1068 * is repeatedly extended for every set of items. Is possible
1069 * to do this without any stack extension or copying at all
1070 * by maintaining a separate list over which the map iterates
18ef8bea 1071 * (like foreach does). --gsar */
544f3153
GS
1072
1073 /* everything in the stack after the destination list moves
1074 * towards the end the stack by the amount of room needed */
1075 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1076
1077 /* items to shift up (accounting for the moved source pointer) */
1078 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
18ef8bea
BT
1079
1080 /* This optimization is by Ben Tilly and it does
1081 * things differently from what Sarathy (gsar)
1082 * is describing. The downside of this optimization is
1083 * that leaves "holes" (uninitialized and hopefully unused areas)
1084 * to the Perl stack, but on the other hand this
1085 * shouldn't be a problem. If Sarathy's idea gets
1086 * implemented, this optimization should become
1087 * irrelevant. --jhi */
1088 if (shift < count)
1089 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1090
924508f0
GS
1091 EXTEND(SP,shift);
1092 src = SP;
1093 dst = (SP += shift);
3280af22
NIS
1094 PL_markstack_ptr[-1] += shift;
1095 *PL_markstack_ptr += shift;
544f3153 1096 while (count--)
a0d0e21e
LW
1097 *dst-- = *src--;
1098 }
544f3153 1099 /* copy the new items down to the destination list */
ac27b0f5 1100 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
22023b26 1101 if (gimme == G_ARRAY) {
b2a2a901
DM
1102 /* add returned items to the collection (making mortal copies
1103 * if necessary), then clear the current temps stack frame
1104 * *except* for those items. We do this splicing the items
1105 * into the start of the tmps frame (so some items may be on
59d53fd6 1106 * the tmps stack twice), then moving PL_tmps_floor above
b2a2a901
DM
1107 * them, then freeing the frame. That way, the only tmps that
1108 * accumulate over iterations are the return values for map.
1109 * We have to do to this way so that everything gets correctly
1110 * freed if we die during the map.
1111 */
1112 I32 tmpsbase;
1113 I32 i = items;
1114 /* make space for the slice */
1115 EXTEND_MORTAL(items);
1116 tmpsbase = PL_tmps_floor + 1;
1117 Move(PL_tmps_stack + tmpsbase,
1118 PL_tmps_stack + tmpsbase + items,
1119 PL_tmps_ix - PL_tmps_floor,
1120 SV*);
1121 PL_tmps_ix += items;
1122
1123 while (i-- > 0) {
1124 SV *sv = POPs;
1125 if (!SvTEMP(sv))
1126 sv = sv_mortalcopy(sv);
1127 *dst-- = sv;
1128 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1129 }
1130 /* clear the stack frame except for the items */
1131 PL_tmps_floor += items;
1132 FREETMPS;
1133 /* FREETMPS may have cleared the TEMP flag on some of the items */
1134 i = items;
1135 while (i-- > 0)
1136 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
22023b26 1137 }
bfed75c6 1138 else {
22023b26
TP
1139 /* scalar context: we don't care about which values map returns
1140 * (we use undef here). And so we certainly don't want to do mortal
1141 * copies of meaningless values. */
1142 while (items-- > 0) {
b988aa42 1143 (void)POPs;
22023b26
TP
1144 *dst-- = &PL_sv_undef;
1145 }
b2a2a901 1146 FREETMPS;
22023b26 1147 }
a0d0e21e 1148 }
b2a2a901
DM
1149 else {
1150 FREETMPS;
1151 }
d343c3ef 1152 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
1153
1154 /* All done yet? */
3280af22 1155 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
a0d0e21e
LW
1156
1157 (void)POPMARK; /* pop top */
d343c3ef 1158 LEAVE_with_name("grep"); /* exit outer scope */
a0d0e21e 1159 (void)POPMARK; /* pop src */
3280af22 1160 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1161 (void)POPMARK; /* pop dst */
3280af22 1162 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1163 if (gimme == G_SCALAR) {
7cc47870
RGS
1164 if (PL_op->op_private & OPpGREP_LEX) {
1165 SV* sv = sv_newmortal();
1166 sv_setiv(sv, items);
1167 PUSHs(sv);
1168 }
1169 else {
1170 dTARGET;
1171 XPUSHi(items);
1172 }
a0d0e21e 1173 }
54310121
PP
1174 else if (gimme == G_ARRAY)
1175 SP += items;
a0d0e21e
LW
1176 RETURN;
1177 }
1178 else {
1179 SV *src;
1180
d343c3ef 1181 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1182 SAVEVPTR(PL_curpm);
a0d0e21e 1183
544f3153 1184 /* set $_ to the new source item */
3280af22 1185 src = PL_stack_base[PL_markstack_ptr[-1]];
a0d0e21e 1186 SvTEMP_off(src);
59f00321
RGS
1187 if (PL_op->op_private & OPpGREP_LEX)
1188 PAD_SVl(PL_op->op_targ) = src;
1189 else
414bf5ae 1190 DEFSV_set(src);
a0d0e21e
LW
1191
1192 RETURNOP(cLOGOP->op_other);
1193 }
1194}
1195
a0d0e21e
LW
1196/* Range stuff. */
1197
1198PP(pp_range)
1199{
97aff369 1200 dVAR;
a0d0e21e 1201 if (GIMME == G_ARRAY)
1a67a97c 1202 return NORMAL;
538573f7 1203 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1a67a97c 1204 return cLOGOP->op_other;
538573f7 1205 else
1a67a97c 1206 return NORMAL;
a0d0e21e
LW
1207}
1208
1209PP(pp_flip)
1210{
97aff369 1211 dVAR;
39644a26 1212 dSP;
a0d0e21e
LW
1213
1214 if (GIMME == G_ARRAY) {
1a67a97c 1215 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1216 }
1217 else {
1218 dTOPss;
44f8325f 1219 SV * const targ = PAD_SV(PL_op->op_targ);
bfed75c6 1220 int flip = 0;
790090df 1221
bfed75c6 1222 if (PL_op->op_private & OPpFLIP_LINENUM) {
4e3399f9
YST
1223 if (GvIO(PL_last_in_gv)) {
1224 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1225 }
1226 else {
fafc274c 1227 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
44f8325f
AL
1228 if (gv && GvSV(gv))
1229 flip = SvIV(sv) == SvIV(GvSV(gv));
4e3399f9 1230 }
bfed75c6
AL
1231 } else {
1232 flip = SvTRUE(sv);
1233 }
1234 if (flip) {
a0d0e21e 1235 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 1236 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1237 sv_setiv(targ, 1);
3e3baf6d 1238 SETs(targ);
a0d0e21e
LW
1239 RETURN;
1240 }
1241 else {
1242 sv_setiv(targ, 0);
924508f0 1243 SP--;
1a67a97c 1244 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1245 }
1246 }
76f68e9b 1247 sv_setpvs(TARG, "");
a0d0e21e
LW
1248 SETs(targ);
1249 RETURN;
1250 }
1251}
1252
8e9bbdb9
RGS
1253/* This code tries to decide if "$left .. $right" should use the
1254 magical string increment, or if the range is numeric (we make
1255 an exception for .."0" [#18165]). AMS 20021031. */
1256
1257#define RANGE_IS_NUMERIC(left,right) ( \
b0e74086
RGS
1258 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1259 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
e0ab1c0e 1260 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
b15aece3 1261 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
e0ab1c0e 1262 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1263
a0d0e21e
LW
1264PP(pp_flop)
1265{
97aff369 1266 dVAR; dSP;
a0d0e21e
LW
1267
1268 if (GIMME == G_ARRAY) {
1269 dPOPPOPssrl;
86cb7173 1270
5b295bef
RD
1271 SvGETMAGIC(left);
1272 SvGETMAGIC(right);
a0d0e21e 1273
8e9bbdb9 1274 if (RANGE_IS_NUMERIC(left,right)) {
901017d6
AL
1275 register IV i, j;
1276 IV max;
4fe3f0fa
MHM
1277 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1278 (SvOK(right) && SvNV(right) > IV_MAX))
d470f89e 1279 DIE(aTHX_ "Range iterator outside integer range");
a0d0e21e
LW
1280 i = SvIV(left);
1281 max = SvIV(right);
bbce6d69 1282 if (max >= i) {
c1ab3db2
AK
1283 j = max - i + 1;
1284 EXTEND_MORTAL(j);
1285 EXTEND(SP, j);
bbce6d69 1286 }
c1ab3db2
AK
1287 else
1288 j = 0;
1289 while (j--) {
901017d6 1290 SV * const sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
1291 PUSHs(sv);
1292 }
1293 }
1294 else {
44f8325f 1295 SV * const final = sv_mortalcopy(right);
13c5b33c 1296 STRLEN len;
823a54a3 1297 const char * const tmps = SvPV_const(final, len);
a0d0e21e 1298
901017d6 1299 SV *sv = sv_mortalcopy(left);
13c5b33c 1300 SvPV_force_nolen(sv);
89ea2908 1301 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 1302 XPUSHs(sv);
b15aece3 1303 if (strEQ(SvPVX_const(sv),tmps))
89ea2908 1304 break;
a0d0e21e
LW
1305 sv = sv_2mortal(newSVsv(sv));
1306 sv_inc(sv);
1307 }
a0d0e21e
LW
1308 }
1309 }
1310 else {
1311 dTOPss;
901017d6 1312 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
4e3399f9 1313 int flop = 0;
a0d0e21e 1314 sv_inc(targ);
4e3399f9
YST
1315
1316 if (PL_op->op_private & OPpFLIP_LINENUM) {
1317 if (GvIO(PL_last_in_gv)) {
1318 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1319 }
1320 else {
fafc274c 1321 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
4e3399f9
YST
1322 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1323 }
1324 }
1325 else {
1326 flop = SvTRUE(sv);
1327 }
1328
1329 if (flop) {
a0d0e21e 1330 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
396482e1 1331 sv_catpvs(targ, "E0");
a0d0e21e
LW
1332 }
1333 SETs(targ);
1334 }
1335
1336 RETURN;
1337}
1338
1339/* Control. */
1340
27da23d5 1341static const char * const context_name[] = {
515afda2 1342 "pseudo-block",
f31522f3 1343 NULL, /* CXt_WHEN never actually needs "block" */
76753e7f 1344 NULL, /* CXt_BLOCK never actually needs "block" */
f31522f3 1345 NULL, /* CXt_GIVEN never actually needs "block" */
76753e7f
NC
1346 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1347 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1348 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1349 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
515afda2 1350 "subroutine",
76753e7f 1351 "format",
515afda2 1352 "eval",
515afda2 1353 "substitution",
515afda2
NC
1354};
1355
76e3520e 1356STATIC I32
06b5626a 1357S_dopoptolabel(pTHX_ const char *label)
a0d0e21e 1358{
97aff369 1359 dVAR;
a0d0e21e 1360 register I32 i;
a0d0e21e 1361
7918f24d
NC
1362 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1363
a0d0e21e 1364 for (i = cxstack_ix; i >= 0; i--) {
901017d6 1365 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1366 switch (CxTYPE(cx)) {
a0d0e21e 1367 case CXt_SUBST:
a0d0e21e 1368 case CXt_SUB:
7766f137 1369 case CXt_FORMAT:
a0d0e21e 1370 case CXt_EVAL:
0a753a76 1371 case CXt_NULL:
a2a5de95
NC
1372 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1373 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1374 if (CxTYPE(cx) == CXt_NULL)
1375 return -1;
1376 break;
c6fdafd0 1377 case CXt_LOOP_LAZYIV:
d01136d6 1378 case CXt_LOOP_LAZYSV:
3b719c58
NC
1379 case CXt_LOOP_FOR:
1380 case CXt_LOOP_PLAIN:
7e8f1eac
AD
1381 {
1382 const char *cx_label = CxLABEL(cx);
1383 if (!cx_label || strNE(label, cx_label) ) {
1c98cc53 1384 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
7e8f1eac 1385 (long)i, cx_label));
a0d0e21e
LW
1386 continue;
1387 }
1c98cc53 1388 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
a0d0e21e 1389 return i;
7e8f1eac 1390 }
a0d0e21e
LW
1391 }
1392 }
1393 return i;
1394}
1395
0d863452
RH
1396
1397
e50aee73 1398I32
864dbfa3 1399Perl_dowantarray(pTHX)
e50aee73 1400{
97aff369 1401 dVAR;
f54cb97a 1402 const I32 gimme = block_gimme();
54310121
PP
1403 return (gimme == G_VOID) ? G_SCALAR : gimme;
1404}
1405
1406I32
864dbfa3 1407Perl_block_gimme(pTHX)
54310121 1408{
97aff369 1409 dVAR;
06b5626a 1410 const I32 cxix = dopoptosub(cxstack_ix);
e50aee73 1411 if (cxix < 0)
46fc3d4c 1412 return G_VOID;
e50aee73 1413
54310121 1414 switch (cxstack[cxix].blk_gimme) {
d2719217
GS
1415 case G_VOID:
1416 return G_VOID;
54310121 1417 case G_SCALAR:
e50aee73 1418 return G_SCALAR;
54310121
PP
1419 case G_ARRAY:
1420 return G_ARRAY;
1421 default:
cea2e8a9 1422 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
d2719217
GS
1423 /* NOTREACHED */
1424 return 0;
54310121 1425 }
e50aee73
AD
1426}
1427
78f9721b
SM
1428I32
1429Perl_is_lvalue_sub(pTHX)
1430{
97aff369 1431 dVAR;
06b5626a 1432 const I32 cxix = dopoptosub(cxstack_ix);
78f9721b
SM
1433 assert(cxix >= 0); /* We should only be called from inside subs */
1434
bafb2adc
NC
1435 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1436 return CxLVAL(cxstack + cxix);
78f9721b
SM
1437 else
1438 return 0;
1439}
1440
76e3520e 1441STATIC I32
901017d6 1442S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
2c375eb9 1443{
97aff369 1444 dVAR;
a0d0e21e 1445 I32 i;
7918f24d
NC
1446
1447 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1448
a0d0e21e 1449 for (i = startingblock; i >= 0; i--) {
901017d6 1450 register const PERL_CONTEXT * const cx = &cxstk[i];
6b35e009 1451 switch (CxTYPE(cx)) {
a0d0e21e
LW
1452 default:
1453 continue;
1454 case CXt_EVAL:
1455 case CXt_SUB:
7766f137 1456 case CXt_FORMAT:
1c98cc53 1457 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
a0d0e21e
LW
1458 return i;
1459 }
1460 }
1461 return i;
1462}
1463
76e3520e 1464STATIC I32
cea2e8a9 1465S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e 1466{
97aff369 1467 dVAR;
a0d0e21e 1468 I32 i;
a0d0e21e 1469 for (i = startingblock; i >= 0; i--) {
06b5626a 1470 register const PERL_CONTEXT *cx = &cxstack[i];
6b35e009 1471 switch (CxTYPE(cx)) {
a0d0e21e
LW
1472 default:
1473 continue;
1474 case CXt_EVAL:
1c98cc53 1475 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
a0d0e21e
LW
1476 return i;
1477 }
1478 }
1479 return i;
1480}
1481
76e3520e 1482STATIC I32
cea2e8a9 1483S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e 1484{
97aff369 1485 dVAR;
a0d0e21e 1486 I32 i;
a0d0e21e 1487 for (i = startingblock; i >= 0; i--) {
901017d6 1488 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1489 switch (CxTYPE(cx)) {
a0d0e21e 1490 case CXt_SUBST:
a0d0e21e 1491 case CXt_SUB:
7766f137 1492 case CXt_FORMAT:
a0d0e21e 1493 case CXt_EVAL:
0a753a76 1494 case CXt_NULL:
a2a5de95
NC
1495 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1496 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1497 if ((CxTYPE(cx)) == CXt_NULL)
1498 return -1;
1499 break;
c6fdafd0 1500 case CXt_LOOP_LAZYIV:
d01136d6 1501 case CXt_LOOP_LAZYSV:
3b719c58
NC
1502 case CXt_LOOP_FOR:
1503 case CXt_LOOP_PLAIN:
1c98cc53 1504 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
a0d0e21e
LW
1505 return i;
1506 }
1507 }
1508 return i;
1509}
1510
0d863452
RH
1511STATIC I32
1512S_dopoptogiven(pTHX_ I32 startingblock)
1513{
97aff369 1514 dVAR;
0d863452
RH
1515 I32 i;
1516 for (i = startingblock; i >= 0; i--) {
1517 register const PERL_CONTEXT *cx = &cxstack[i];
1518 switch (CxTYPE(cx)) {
1519 default:
1520 continue;
1521 case CXt_GIVEN:
1c98cc53 1522 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
0d863452 1523 return i;
3b719c58
NC
1524 case CXt_LOOP_PLAIN:
1525 assert(!CxFOREACHDEF(cx));
1526 break;
c6fdafd0 1527 case CXt_LOOP_LAZYIV:
d01136d6 1528 case CXt_LOOP_LAZYSV:
3b719c58 1529 case CXt_LOOP_FOR:
0d863452 1530 if (CxFOREACHDEF(cx)) {
1c98cc53 1531 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
0d863452
RH
1532 return i;
1533 }
1534 }
1535 }
1536 return i;
1537}
1538
1539STATIC I32
1540S_dopoptowhen(pTHX_ I32 startingblock)
1541{
97aff369 1542 dVAR;
0d863452
RH
1543 I32 i;
1544 for (i = startingblock; i >= 0; i--) {
1545 register const PERL_CONTEXT *cx = &cxstack[i];
1546 switch (CxTYPE(cx)) {
1547 default:
1548 continue;
1549 case CXt_WHEN:
1c98cc53 1550 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
0d863452
RH
1551 return i;
1552 }
1553 }
1554 return i;
1555}
1556
a0d0e21e 1557void
864dbfa3 1558Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1559{
97aff369 1560 dVAR;
a0d0e21e
LW
1561 I32 optype;
1562
1563 while (cxstack_ix > cxix) {
b0d9ce38 1564 SV *sv;
06b5626a 1565 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1c98cc53 1566 DEBUG_CX("UNWIND"); \
a0d0e21e 1567 /* Note: we don't need to restore the base context info till the end. */
6b35e009 1568 switch (CxTYPE(cx)) {
c90c0ff4
PP
1569 case CXt_SUBST:
1570 POPSUBST(cx);
1571 continue; /* not break */
a0d0e21e 1572 case CXt_SUB:
b0d9ce38
GS
1573 POPSUB(cx,sv);
1574 LEAVESUB(sv);
a0d0e21e
LW
1575 break;
1576 case CXt_EVAL:
1577 POPEVAL(cx);
1578 break;
c6fdafd0 1579 case CXt_LOOP_LAZYIV:
d01136d6 1580 case CXt_LOOP_LAZYSV:
3b719c58
NC
1581 case CXt_LOOP_FOR:
1582 case CXt_LOOP_PLAIN:
a0d0e21e
LW
1583 POPLOOP(cx);
1584 break;
0a753a76 1585 case CXt_NULL:
a0d0e21e 1586 break;
7766f137
GS
1587 case CXt_FORMAT:
1588 POPFORMAT(cx);
1589 break;
a0d0e21e 1590 }
c90c0ff4 1591 cxstack_ix--;
a0d0e21e 1592 }
1b6737cc 1593 PERL_UNUSED_VAR(optype);
a0d0e21e
LW
1594}
1595
5a844595
GS
1596void
1597Perl_qerror(pTHX_ SV *err)
1598{
97aff369 1599 dVAR;
7918f24d
NC
1600
1601 PERL_ARGS_ASSERT_QERROR;
1602
6b2fb389
DM
1603 if (PL_in_eval) {
1604 if (PL_in_eval & EVAL_KEEPERR) {
1605 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1606 SvPV_nolen_const(err));
1607 }
1608 else
1609 sv_catsv(ERRSV, err);
1610 }
5a844595
GS
1611 else if (PL_errors)
1612 sv_catsv(PL_errors, err);
1613 else
be2597df 1614 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
13765c85
DM
1615 if (PL_parser)
1616 ++PL_parser->error_count;
5a844595
GS
1617}
1618
bb4c52e0 1619void
c5df3096 1620Perl_die_unwind(pTHX_ SV *msv)
a0d0e21e 1621{
27da23d5 1622 dVAR;
c5df3096 1623 SV *exceptsv = sv_mortalcopy(msv);
96d9b9cd 1624 U8 in_eval = PL_in_eval;
c5df3096 1625 PERL_ARGS_ASSERT_DIE_UNWIND;
87582a92 1626
96d9b9cd 1627 if (in_eval) {
a0d0e21e 1628 I32 cxix;
a0d0e21e 1629 I32 gimme;
a0d0e21e 1630
22a30693
Z
1631 /*
1632 * Historically, perl used to set ERRSV ($@) early in the die
1633 * process and rely on it not getting clobbered during unwinding.
1634 * That sucked, because it was liable to get clobbered, so the
1635 * setting of ERRSV used to emit the exception from eval{} has
1636 * been moved to much later, after unwinding (see just before
1637 * JMPENV_JUMP below). However, some modules were relying on the
1638 * early setting, by examining $@ during unwinding to use it as
1639 * a flag indicating whether the current unwinding was caused by
1640 * an exception. It was never a reliable flag for that purpose,
1641 * being totally open to false positives even without actual
1642 * clobberage, but was useful enough for production code to
1643 * semantically rely on it.
1644 *
1645 * We'd like to have a proper introspective interface that
1646 * explicitly describes the reason for whatever unwinding
1647 * operations are currently in progress, so that those modules
1648 * work reliably and $@ isn't further overloaded. But we don't
1649 * have one yet. In its absence, as a stopgap measure, ERRSV is
1650 * now *additionally* set here, before unwinding, to serve as the
1651 * (unreliable) flag that it used to.
1652 *
1653 * This behaviour is temporary, and should be removed when a
1654 * proper way to detect exceptional unwinding has been developed.
1655 * As of 2010-12, the authors of modules relying on the hack
1656 * are aware of the issue, because the modules failed on
1657 * perls 5.13.{1..7} which had late setting of $@ without this
1658 * early-setting hack.
1659 */
1660 if (!(in_eval & EVAL_KEEPERR)) {
1661 SvTEMP_off(exceptsv);
1662 sv_setsv(ERRSV, exceptsv);
1663 }
1664
5a844595
GS
1665 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1666 && PL_curstackinfo->si_prev)
1667 {
bac4b2ad 1668 dounwind(-1);
d3acc0f7 1669 POPSTACK;
bac4b2ad 1670 }
e336de0d 1671
a0d0e21e
LW
1672 if (cxix >= 0) {
1673 I32 optype;
b6494f15 1674 SV *namesv;
35a4481c 1675 register PERL_CONTEXT *cx;
901017d6 1676 SV **newsp;
8f89e5a9
Z
1677 COP *oldcop;
1678 JMPENV *restartjmpenv;
1679 OP *restartop;
a0d0e21e
LW
1680
1681 if (cxix < cxstack_ix)
1682 dounwind(cxix);
1683
3280af22 1684 POPBLOCK(cx,PL_curpm);
6b35e009 1685 if (CxTYPE(cx) != CXt_EVAL) {
7d0994e0 1686 STRLEN msglen;
96d9b9cd 1687 const char* message = SvPVx_const(exceptsv, msglen);
10edeb5d 1688 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
bf49b057 1689 PerlIO_write(Perl_error_log, message, msglen);
a0d0e21e
LW
1690 my_exit(1);
1691 }
1692 POPEVAL(cx);
b6494f15 1693 namesv = cx->blk_eval.old_namesv;
8f89e5a9
Z
1694 oldcop = cx->blk_oldcop;
1695 restartjmpenv = cx->blk_eval.cur_top_env;
1696 restartop = cx->blk_eval.retop;
a0d0e21e
LW
1697
1698 if (gimme == G_SCALAR)
3280af22
NIS
1699 *++newsp = &PL_sv_undef;
1700 PL_stack_sp = newsp;
a0d0e21e
LW
1701
1702 LEAVE;
748a9306 1703
7fb6a879
GS
1704 /* LEAVE could clobber PL_curcop (see save_re_context())
1705 * XXX it might be better to find a way to avoid messing with
1706 * PL_curcop in save_re_context() instead, but this is a more
1707 * minimal fix --GSAR */
8f89e5a9 1708 PL_curcop = oldcop;
7fb6a879 1709
7a2e2cd6 1710 if (optype == OP_REQUIRE) {
96d9b9cd 1711 const char* const msg = SvPVx_nolen_const(exceptsv);
b6494f15
VP
1712 (void)hv_store(GvHVn(PL_incgv),
1713 SvPVX_const(namesv), SvCUR(namesv),
27bcc0a7 1714 &PL_sv_undef, 0);
27e90453
DM
1715 /* note that unlike pp_entereval, pp_require isn't
1716 * supposed to trap errors. So now that we've popped the
1717 * EVAL that pp_require pushed, and processed the error
1718 * message, rethrow the error */
9fed9930
NC
1719 Perl_croak(aTHX_ "%sCompilation failed in require",
1720 *msg ? msg : "Unknown error\n");
7a2e2cd6 1721 }
c5df3096 1722 if (in_eval & EVAL_KEEPERR) {
7ce09284
Z
1723 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1724 SvPV_nolen_const(exceptsv));
96d9b9cd
Z
1725 }
1726 else {
1727 sv_setsv(ERRSV, exceptsv);
1728 }
8f89e5a9
Z
1729 PL_restartjmpenv = restartjmpenv;
1730 PL_restartop = restartop;
bb4c52e0
GG
1731 JMPENV_JUMP(3);
1732 /* NOTREACHED */
a0d0e21e
LW
1733 }
1734 }
87582a92 1735
96d9b9cd 1736 write_to_stderr(exceptsv);
f86702cc
PP
1737 my_failure_exit();
1738 /* NOTREACHED */
a0d0e21e
LW
1739}
1740
1741PP(pp_xor)
1742{
97aff369 1743 dVAR; dSP; dPOPTOPssrl;
a0d0e21e
LW
1744 if (SvTRUE(left) != SvTRUE(right))
1745 RETSETYES;
1746 else
1747 RETSETNO;
1748}
1749
8dff4fc5
BM
1750/*
1751=for apidoc caller_cx
1752
1753The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1754returned C<PERL_CONTEXT> structure can be interrogated to find all the
1755information returned to Perl by C<caller>. Note that XSUBs don't get a
1756stack frame, so C<caller_cx(0, NULL)> will return information for the
1757immediately-surrounding Perl code.
1758
1759This function skips over the automatic calls to C<&DB::sub> made on the
1760behalf of the debugger. If the stack frame requested was a sub called by
1761C<DB::sub>, the return value will be the frame for the call to
1762C<DB::sub>, since that has the correct line number/etc. for the call
1763site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1764frame for the sub call itself.
1765
1766=cut
1767*/
1768
1769const PERL_CONTEXT *
1770Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
a0d0e21e 1771{
a0d0e21e 1772 register I32 cxix = dopoptosub(cxstack_ix);
901017d6
AL
1773 register const PERL_CONTEXT *cx;
1774 register const PERL_CONTEXT *ccstack = cxstack;
1775 const PERL_SI *top_si = PL_curstackinfo;
27d41816 1776
a0d0e21e 1777 for (;;) {
2c375eb9
GS
1778 /* we may be in a higher stacklevel, so dig down deeper */
1779 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1780 top_si = top_si->si_prev;
1781 ccstack = top_si->si_cxstack;
1782 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1783 }
8dff4fc5
BM
1784 if (cxix < 0)
1785 return NULL;
f2a7f298 1786 /* caller() should not report the automatic calls to &DB::sub */
1787 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1788 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1789 count++;
1790 if (!count--)
1791 break;
2c375eb9 1792 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1793 }
2c375eb9
GS
1794
1795 cx = &ccstack[cxix];
8dff4fc5
BM
1796 if (dbcxp) *dbcxp = cx;
1797
7766f137 1798 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1799 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1800 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1801 field below is defined for any cx. */
f2a7f298 1802 /* caller() should not report the automatic calls to &DB::sub */
1803 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1804 cx = &ccstack[dbcxix];
06a5b730
PP
1805 }
1806
8dff4fc5
BM
1807 return cx;
1808}
1809
1810PP(pp_caller)
1811{
1812 dVAR;
1813 dSP;
1814 register const PERL_CONTEXT *cx;
1815 const PERL_CONTEXT *dbcx;
1816 I32 gimme;
1817 const char *stashname;
1818 I32 count = 0;
1819
1820 if (MAXARG)
1821 count = POPi;
1822
1823 cx = caller_cx(count, &dbcx);
1824 if (!cx) {
1825 if (GIMME != G_ARRAY) {
1826 EXTEND(SP, 1);
1827 RETPUSHUNDEF;
1828 }
1829 RETURN;
1830 }
1831
ed094faf 1832 stashname = CopSTASHPV(cx->blk_oldcop);
a0d0e21e 1833 if (GIMME != G_ARRAY) {
27d41816 1834 EXTEND(SP, 1);
ed094faf 1835 if (!stashname)
3280af22 1836 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1837 else {
1838 dTARGET;
ed094faf 1839 sv_setpv(TARG, stashname);
49d8d3a1
MB
1840 PUSHs(TARG);
1841 }
a0d0e21e
LW
1842 RETURN;
1843 }
a0d0e21e 1844
b3ca2e83 1845 EXTEND(SP, 11);
27d41816 1846
ed094faf 1847 if (!stashname)
3280af22 1848 PUSHs(&PL_sv_undef);
49d8d3a1 1849 else
6e449a3a
MHM
1850 mPUSHs(newSVpv(stashname, 0));
1851 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1852 mPUSHi((I32)CopLINE(cx->blk_oldcop));
a0d0e21e
LW
1853 if (!MAXARG)
1854 RETURN;
7766f137 1855 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
8dff4fc5 1856 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
7766f137 1857 /* So is ccstack[dbcxix]. */
07b8c804 1858 if (isGV(cvgv)) {
561b68a9 1859 SV * const sv = newSV(0);
c445ea15 1860 gv_efullname3(sv, cvgv, NULL);
6e449a3a 1861 mPUSHs(sv);
bf38a478 1862 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1863 }
1864 else {
84bafc02 1865 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1866 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1867 }
a0d0e21e
LW
1868 }
1869 else {
84bafc02 1870 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
6e449a3a 1871 mPUSHi(0);
a0d0e21e 1872 }
54310121
PP
1873 gimme = (I32)cx->blk_gimme;
1874 if (gimme == G_VOID)
3280af22 1875 PUSHs(&PL_sv_undef);
54310121 1876 else
98625aca 1877 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1878 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1879 /* eval STRING */
85a64632 1880 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
4633a7c4 1881 PUSHs(cx->blk_eval.cur_text);
3280af22 1882 PUSHs(&PL_sv_no);
0f79a09d 1883 }
811a4de9 1884 /* require */
0f79a09d 1885 else if (cx->blk_eval.old_namesv) {
6e449a3a 1886 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1887 PUSHs(&PL_sv_yes);
06a5b730 1888 }
811a4de9
GS
1889 /* eval BLOCK (try blocks have old_namesv == 0) */
1890 else {
1891 PUSHs(&PL_sv_undef);
1892 PUSHs(&PL_sv_undef);
1893 }
4633a7c4 1894 }
a682de96
GS
1895 else {
1896 PUSHs(&PL_sv_undef);
1897 PUSHs(&PL_sv_undef);
1898 }
bafb2adc 1899 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1900 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1901 {
66a1b24b
AL
1902 AV * const ary = cx->blk_sub.argarray;
1903 const int off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1904
5b235299
NC
1905 if (!PL_dbargs)
1906 Perl_init_dbargs(aTHX);
a0d0e21e 1907
3280af22
NIS
1908 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1909 av_extend(PL_dbargs, AvFILLp(ary) + off);
1910 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1911 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1912 }
f3aa04c2
GS
1913 /* XXX only hints propagated via op_private are currently
1914 * visible (others are not easily accessible, since they
1915 * use the global PL_hints) */
6e449a3a 1916 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1917 {
1918 SV * mask ;
72dc9ed5 1919 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 1920
ac27b0f5 1921 if (old_warnings == pWARN_NONE ||
114bafba 1922 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
e476b1b5 1923 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
ac27b0f5 1924 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
1925 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1926 /* Get the bit mask for $warnings::Bits{all}, because
1927 * it could have been extended by warnings::register */
1928 SV **bits_all;
6673a63c 1929 HV * const bits = get_hv("warnings::Bits", 0);
017a3ce5 1930 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
1931 mask = newSVsv(*bits_all);
1932 }
1933 else {
1934 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1935 }
1936 }
e476b1b5 1937 else
72dc9ed5 1938 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 1939 mPUSHs(mask);
e476b1b5 1940 }
b3ca2e83 1941
c28fe1ec 1942 PUSHs(cx->blk_oldcop->cop_hints_hash ?
20439bc7 1943 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
b3ca2e83 1944 : &PL_sv_undef);
a0d0e21e
LW
1945 RETURN;
1946}
1947
a0d0e21e
LW
1948PP(pp_reset)
1949{
97aff369 1950 dVAR;
39644a26 1951 dSP;
10edeb5d 1952 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
11faa288 1953 sv_reset(tmps, CopSTASH(PL_curcop));
3280af22 1954 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1955 RETURN;
1956}
1957
dd2155a4
DM
1958/* like pp_nextstate, but used instead when the debugger is active */
1959
a0d0e21e
LW
1960PP(pp_dbstate)
1961{
27da23d5 1962 dVAR;
533c011a 1963 PL_curcop = (COP*)PL_op;
a0d0e21e 1964 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 1965 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1966 FREETMPS;
1967
f410a211
NC
1968 PERL_ASYNC_CHECK();
1969
5df8de69
DM
1970 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1971 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 1972 {
39644a26 1973 dSP;
c09156bb 1974 register PERL_CONTEXT *cx;
f54cb97a 1975 const I32 gimme = G_ARRAY;
eb160463 1976 U8 hasargs;
0bd48802
AL
1977 GV * const gv = PL_DBgv;
1978 register CV * const cv = GvCV(gv);
a0d0e21e 1979
a0d0e21e 1980 if (!cv)
cea2e8a9 1981 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 1982
aea4f609
DM
1983 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1984 /* don't do recursive DB::DB call */
a0d0e21e 1985 return NORMAL;
748a9306 1986
a57c6685 1987 ENTER;
4633a7c4
LW
1988 SAVETMPS;
1989
3280af22 1990 SAVEI32(PL_debug);
55497cff 1991 SAVESTACK_POS();
3280af22 1992 PL_debug = 0;
748a9306 1993 hasargs = 0;
924508f0 1994 SPAGAIN;
748a9306 1995
aed2304a 1996 if (CvISXSUB(cv)) {
c127bd3a
SF
1997 CvDEPTH(cv)++;
1998 PUSHMARK(SP);
1999 (void)(*CvXSUB(cv))(aTHX_ cv);
2000 CvDEPTH(cv)--;
2001 FREETMPS;
a57c6685 2002 LEAVE;
c127bd3a
SF
2003 return NORMAL;
2004 }
2005 else {
2006 PUSHBLOCK(cx, CXt_SUB, SP);
2007 PUSHSUB_DB(cx);
2008 cx->blk_sub.retop = PL_op->op_next;
2009 CvDEPTH(cv)++;
2010 SAVECOMPPAD();
2011 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2012 RETURNOP(CvSTART(cv));
2013 }
a0d0e21e
LW
2014 }
2015 else
2016 return NORMAL;
2017}
2018
a0d0e21e
LW
2019PP(pp_enteriter)
2020{
27da23d5 2021 dVAR; dSP; dMARK;
c09156bb 2022 register PERL_CONTEXT *cx;
f54cb97a 2023 const I32 gimme = GIMME_V;
df530c37 2024 void *itervar; /* location of the iteration variable */
840fe433 2025 U8 cxtype = CXt_LOOP_FOR;
a0d0e21e 2026
d343c3ef 2027 ENTER_with_name("loop1");
4633a7c4
LW
2028 SAVETMPS;
2029
aafca525
DM
2030 if (PL_op->op_targ) { /* "my" variable */
2031 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
14f338dc
DM
2032 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2033 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2034 SVs_PADSTALE, SVs_PADSTALE);
2035 }
09edbca0 2036 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
89e00a7c 2037#ifdef USE_ITHREADS
df530c37 2038 itervar = PL_comppad;
89e00a7c 2039#else
aafca525 2040 itervar = &PAD_SVl(PL_op->op_targ);
7766f137 2041#endif
54b9620d 2042 }
aafca525 2043 else { /* symbol table variable */
159b6efe 2044 GV * const gv = MUTABLE_GV(POPs);
f83b46a0
DM
2045 SV** svp = &GvSV(gv);
2046 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
561b68a9 2047 *svp = newSV(0);
df530c37 2048 itervar = (void *)gv;
54b9620d 2049 }
4633a7c4 2050
0d863452
RH
2051 if (PL_op->op_private & OPpITER_DEF)
2052 cxtype |= CXp_FOR_DEF;
2053
d343c3ef 2054 ENTER_with_name("loop2");
a0d0e21e 2055
7766f137 2056 PUSHBLOCK(cx, cxtype, SP);
df530c37 2057 PUSHLOOP_FOR(cx, itervar, MARK);
533c011a 2058 if (PL_op->op_flags & OPf_STACKED) {
d01136d6
BS
2059 SV *maybe_ary = POPs;
2060 if (SvTYPE(maybe_ary) != SVt_PVAV) {
89ea2908 2061 dPOPss;
d01136d6 2062 SV * const right = maybe_ary;
984a4bea
RD
2063 SvGETMAGIC(sv);
2064 SvGETMAGIC(right);
4fe3f0fa 2065 if (RANGE_IS_NUMERIC(sv,right)) {
d01136d6 2066 cx->cx_type &= ~CXTYPEMASK;
c6fdafd0
NC
2067 cx->cx_type |= CXt_LOOP_LAZYIV;
2068 /* Make sure that no-one re-orders cop.h and breaks our
2069 assumptions */
2070 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
a2309040
JH
2071#ifdef NV_PRESERVES_UV
2072 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2073 (SvNV(sv) > (NV)IV_MAX)))
2074 ||
2075 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2076 (SvNV(right) < (NV)IV_MIN))))
2077#else
2078 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2079 ||
2080 ((SvNV(sv) > 0) &&
2081 ((SvUV(sv) > (UV)IV_MAX) ||
2082 (SvNV(sv) > (NV)UV_MAX)))))
2083 ||
2084 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2085 ||
2086 ((SvNV(right) > 0) &&
2087 ((SvUV(right) > (UV)IV_MAX) ||
2088 (SvNV(right) > (NV)UV_MAX))))))
2089#endif
076d9a11 2090 DIE(aTHX_ "Range iterator outside integer range");
d01136d6
BS
2091 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2092 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
d4665a05
DM
2093#ifdef DEBUGGING
2094 /* for correct -Dstv display */
2095 cx->blk_oldsp = sp - PL_stack_base;
2096#endif
89ea2908 2097 }
3f63a782 2098 else {
d01136d6
BS
2099 cx->cx_type &= ~CXTYPEMASK;
2100 cx->cx_type |= CXt_LOOP_LAZYSV;
2101 /* Make sure that no-one re-orders cop.h and breaks our
2102 assumptions */
2103 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2104 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2105 cx->blk_loop.state_u.lazysv.end = right;
2106 SvREFCNT_inc(right);
2107 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
2108 /* This will do the upgrade to SVt_PV, and warn if the value
2109 is uninitialised. */
10516c54 2110 (void) SvPV_nolen_const(right);
267cc4a8
NC
2111 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2112 to replace !SvOK() with a pointer to "". */
2113 if (!SvOK(right)) {
2114 SvREFCNT_dec(right);
d01136d6 2115 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 2116 }
3f63a782 2117 }
89ea2908 2118 }
d01136d6 2119 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
502c6561 2120 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
d01136d6
BS
2121 SvREFCNT_inc(maybe_ary);
2122 cx->blk_loop.state_u.ary.ix =
2123 (PL_op->op_private & OPpITER_REVERSED) ?
2124 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2125 -1;
ef3e5ea9 2126 }
89ea2908 2127 }
d01136d6
BS
2128 else { /* iterating over items on the stack */
2129 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
ef3e5ea9 2130 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6 2131 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
ef3e5ea9
NC
2132 }
2133 else {
d01136d6 2134 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
ef3e5ea9 2135 }
4633a7c4 2136 }
a0d0e21e
LW
2137
2138 RETURN;
2139}
2140
2141PP(pp_enterloop)
2142{
27da23d5 2143 dVAR; dSP;
c09156bb 2144 register PERL_CONTEXT *cx;
f54cb97a 2145 const I32 gimme = GIMME_V;
a0d0e21e 2146
d343c3ef 2147 ENTER_with_name("loop1");
a0d0e21e 2148 SAVETMPS;
d343c3ef 2149 ENTER_with_name("loop2");
a0d0e21e 2150
3b719c58
NC
2151 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2152 PUSHLOOP_PLAIN(cx, SP);
a0d0e21e
LW
2153
2154 RETURN;
2155}
2156
2157PP(pp_leaveloop)
2158{
27da23d5 2159 dVAR; dSP;
c09156bb 2160 register PERL_CONTEXT *cx;
a0d0e21e
LW
2161 I32 gimme;
2162 SV **newsp;
2163 PMOP *newpm;
2164 SV **mark;
2165
2166 POPBLOCK(cx,newpm);
3b719c58 2167 assert(CxTYPE_is_LOOP(cx));
4fdae800 2168 mark = newsp;
a8bba7fa 2169 newsp = PL_stack_base + cx->blk_loop.resetsp;
f86702cc 2170
a1f49e72 2171 TAINT_NOT;
54310121 2172 if (gimme == G_VOID)
6f207bd3 2173 NOOP;
54310121
PP
2174 else if (gimme == G_SCALAR) {
2175 if (mark < SP)
2176 *++newsp = sv_mortalcopy(*SP);
2177 else
3280af22 2178 *++newsp = &PL_sv_undef;
a0d0e21e
LW
2179 }
2180 else {
a1f49e72 2181 while (mark < SP) {
a0d0e21e 2182 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
2183 TAINT_NOT; /* Each item is independent */
2184 }
a0d0e21e 2185 }
f86702cc
PP
2186 SP = newsp;
2187 PUTBACK;
2188
a8bba7fa 2189 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
3280af22 2190 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2191
d343c3ef
GG
2192 LEAVE_with_name("loop2");
2193 LEAVE_with_name("loop1");
a0d0e21e 2194
f86702cc 2195 return NORMAL;
a0d0e21e
LW
2196}
2197
2198PP(pp_return)
2199{
27da23d5 2200 dVAR; dSP; dMARK;
c09156bb 2201 register PERL_CONTEXT *cx;
f86702cc 2202 bool popsub2 = FALSE;
b45de488 2203 bool clear_errsv = FALSE;
fa1e92c4 2204 bool lval = FALSE;
a0d0e21e
LW
2205 I32 gimme;
2206 SV **newsp;
2207 PMOP *newpm;
2208 I32 optype = 0;
b6494f15 2209 SV *namesv;
b0d9ce38 2210 SV *sv;
b263a1ad 2211 OP *retop = NULL;
a0d0e21e 2212
0bd48802
AL
2213 const I32 cxix = dopoptosub(cxstack_ix);
2214
9850bf21
RH
2215 if (cxix < 0) {
2216 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2217 * sort block, which is a CXt_NULL
2218 * not a CXt_SUB */
2219 dounwind(0);
d7507f74
RH
2220 PL_stack_base[1] = *PL_stack_sp;
2221 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
2222 return 0;
2223 }
9850bf21
RH
2224 else
2225 DIE(aTHX_ "Can't return outside a subroutine");
a0d0e21e 2226 }
a0d0e21e
LW
2227 if (cxix < cxstack_ix)
2228 dounwind(cxix);
2229
d7507f74
RH
2230 if (CxMULTICALL(&cxstack[cxix])) {
2231 gimme = cxstack[cxix].blk_gimme;
2232 if (gimme == G_VOID)
2233 PL_stack_sp = PL_stack_base;
2234 else if (gimme == G_SCALAR) {
2235 PL_stack_base[1] = *PL_stack_sp;
2236 PL_stack_sp = PL_stack_base + 1;
2237 }
9850bf21 2238 return 0;
d7507f74 2239 }
9850bf21 2240
a0d0e21e 2241 POPBLOCK(cx,newpm);
6b35e009 2242 switch (CxTYPE(cx)) {
a0d0e21e 2243 case CXt_SUB:
f86702cc 2244 popsub2 = TRUE;
fa1e92c4 2245 lval = !!CvLVALUE(cx->blk_sub.cv);
f39bc417 2246 retop = cx->blk_sub.retop;
5dd42e15 2247 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
a0d0e21e
LW
2248 break;
2249 case CXt_EVAL:
b45de488
GS
2250 if (!(PL_in_eval & EVAL_KEEPERR))
2251 clear_errsv = TRUE;
a0d0e21e 2252 POPEVAL(cx);
b6494f15 2253 namesv = cx->blk_eval.old_namesv;
f39bc417 2254 retop = cx->blk_eval.retop;
1d76a5c3
GS
2255 if (CxTRYBLOCK(cx))
2256 break;
748a9306
LW
2257 if (optype == OP_REQUIRE &&
2258 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2259 {
54310121 2260 /* Unassume the success we assumed earlier. */
b6494f15
VP
2261 (void)hv_delete(GvHVn(PL_incgv),
2262 SvPVX_const(namesv), SvCUR(namesv),
2263 G_DISCARD);
2264 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
748a9306 2265 }
a0d0e21e 2266 break;
7766f137
GS
2267 case CXt_FORMAT:
2268 POPFORMAT(cx);
f39bc417 2269 retop = cx->blk_sub.retop;
7766f137 2270 break;
a0d0e21e 2271 default:
cea2e8a9 2272 DIE(aTHX_ "panic: return");
a0d0e21e
LW
2273 }
2274
a1f49e72 2275 TAINT_NOT;
a0d0e21e 2276 if (gimme == G_SCALAR) {
a29cdaf0
IZ
2277 if (MARK < SP) {
2278 if (popsub2) {
a8bba7fa 2279 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2280 if (SvTEMP(TOPs)) {
2281 *++newsp = SvREFCNT_inc(*SP);
2282 FREETMPS;
2283 sv_2mortal(*newsp);
959e3673
GS
2284 }
2285 else {
2286 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
a29cdaf0 2287 FREETMPS;
959e3673
GS
2288 *++newsp = sv_mortalcopy(sv);
2289 SvREFCNT_dec(sv);
a29cdaf0 2290 }
959e3673
GS
2291 }
2292 else
fa1e92c4
FC
2293 *++newsp =
2294 (lval || SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
959e3673
GS
2295 }
2296 else
a29cdaf0 2297 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2298 }
2299 else
3280af22 2300 *++newsp = &PL_sv_undef;
a0d0e21e 2301 }
54310121 2302 else if (gimme == G_ARRAY) {
a1f49e72 2303 while (++MARK <= SP) {
fa1e92c4 2304 *++newsp = popsub2 && (lval || SvTEMP(*MARK))
f86702cc 2305 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2306 TAINT_NOT; /* Each item is independent */
2307 }
a0d0e21e 2308 }
3280af22 2309 PL_stack_sp = newsp;
a0d0e21e 2310
5dd42e15 2311 LEAVE;
f86702cc
PP
2312 /* Stack values are safe: */
2313 if (popsub2) {
5dd42e15 2314 cxstack_ix--;
b0d9ce38 2315 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2316 }
b0d9ce38 2317 else
c445ea15 2318 sv = NULL;
3280af22 2319 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2320
b0d9ce38 2321 LEAVESUB(sv);
8433848b 2322 if (clear_errsv) {
ab69dbc2 2323 CLEAR_ERRSV();
8433848b 2324 }
f39bc417 2325 return retop;
a0d0e21e
LW
2326}
2327
2328PP(pp_last)
2329{
27da23d5 2330 dVAR; dSP;
a0d0e21e 2331 I32 cxix;
c09156bb 2332 register PERL_CONTEXT *cx;
f86702cc 2333 I32 pop2 = 0;
a0d0e21e 2334 I32 gimme;
8772537c 2335 I32 optype;
b263a1ad 2336 OP *nextop = NULL;
a0d0e21e
LW
2337 SV **newsp;
2338 PMOP *newpm;
a8bba7fa 2339 SV **mark;
c445ea15 2340 SV *sv = NULL;
9d4ba2ae 2341
a0d0e21e 2342
533c011a 2343 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2344 cxix = dopoptoloop(cxstack_ix);
2345 if (cxix < 0)
a651a37d 2346 DIE(aTHX_ "Can't \"last\" outside a loop block");
a0d0e21e
LW
2347 }
2348 else {
2349 cxix = dopoptolabel(cPVOP->op_pv);
2350 if (cxix < 0)
cea2e8a9 2351 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
a0d0e21e
LW
2352 }
2353 if (cxix < cxstack_ix)
2354 dounwind(cxix);
2355
2356 POPBLOCK(cx,newpm);
5dd42e15 2357 cxstack_ix++; /* temporarily protect top context */
a8bba7fa 2358 mark = newsp;
6b35e009 2359 switch (CxTYPE(cx)) {
c6fdafd0 2360 case CXt_LOOP_LAZYIV:
d01136d6 2361 case CXt_LOOP_LAZYSV:
3b719c58
NC
2362 case CXt_LOOP_FOR:
2363 case CXt_LOOP_PLAIN:
2364 pop2 = CxTYPE(cx);
a8bba7fa 2365 newsp = PL_stack_base + cx->blk_loop.resetsp;
022eaa24 2366 nextop = cx->blk_loop.my_op->op_lastop->op_next;
a0d0e21e 2367 break;
f86702cc 2368 case CXt_SUB:
f86702cc 2369 pop2 = CXt_SUB;
f39bc417 2370 nextop = cx->blk_sub.retop;
a0d0e21e 2371 break;
f86702cc
PP
2372 case CXt_EVAL:
2373 POPEVAL(cx);
f39bc417 2374 nextop = cx->blk_eval.retop;
a0d0e21e 2375 break;
7766f137
GS
2376 case CXt_FORMAT:
2377 POPFORMAT(cx);
f39bc417 2378 nextop = cx->blk_sub.retop;
7766f137 2379 break;
a0d0e21e 2380 default:
cea2e8a9 2381 DIE(aTHX_ "panic: last");
a0d0e21e
LW
2382 }
2383
a1f49e72 2384 TAINT_NOT;
a0d0e21e 2385 if (gimme == G_SCALAR) {
f86702cc
PP
2386 if (MARK < SP)
2387 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2388 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 2389 else
3280af22 2390 *++newsp = &PL_sv_undef;
a0d0e21e 2391 }
54310121 2392 else if (gimme == G_ARRAY) {
a1f49e72 2393 while (++MARK <= SP) {
f86702cc
PP
2394 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2395 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2396 TAINT_NOT; /* Each item is independent */
2397 }
f86702cc
PP
2398 }
2399 SP = newsp;
2400 PUTBACK;
2401
5dd42e15
DM
2402 LEAVE;
2403 cxstack_ix--;
f86702cc
PP
2404 /* Stack values are safe: */
2405 switch (pop2) {
c6fdafd0 2406 case CXt_LOOP_LAZYIV:
3b719c58 2407 case CXt_LOOP_PLAIN:
d01136d6 2408 case CXt_LOOP_LAZYSV:
3b719c58 2409 case CXt_LOOP_FOR:
a8bba7fa 2410 POPLOOP(cx); /* release loop vars ... */
4fdae800 2411 LEAVE;
f86702cc
PP
2412 break;
2413 case CXt_SUB:
b0d9ce38 2414 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2415 break;
a0d0e21e 2416 }
3280af22 2417 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2418
b0d9ce38 2419 LEAVESUB(sv);
9d4ba2ae
AL
2420 PERL_UNUSED_VAR(optype);
2421 PERL_UNUSED_VAR(gimme);
f86702cc 2422 return nextop;
a0d0e21e
LW
2423}
2424
2425PP(pp_next)
2426{
27da23d5 2427 dVAR;
a0d0e21e 2428 I32 cxix;
c09156bb 2429 register PERL_CONTEXT *cx;
85538317 2430 I32 inner;
a0d0e21e 2431
533c011a 2432 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2433 cxix = dopoptoloop(cxstack_ix);
2434 if (cxix < 0)
a651a37d 2435 DIE(aTHX_ "Can't \"next\" outside a loop block");
a0d0e21e
LW
2436 }
2437 else {
2438 cxix = dopoptolabel(cPVOP->op_pv);
2439 if (cxix < 0)
cea2e8a9 2440 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
a0d0e21e
LW
2441 }
2442 if (cxix < cxstack_ix)
2443 dounwind(cxix);
2444
85538317
GS
2445 /* clear off anything above the scope we're re-entering, but
2446 * save the rest until after a possible continue block */
2447 inner = PL_scopestack_ix;
1ba6ee2b 2448 TOPBLOCK(cx);
85538317
GS
2449 if (PL_scopestack_ix < inner)
2450 leave_scope(PL_scopestack[PL_scopestack_ix]);
3a1b2b9e 2451 PL_curcop = cx->blk_oldcop;
d57ce4df 2452 return (cx)->blk_loop.my_op->op_nextop;
a0d0e21e
LW
2453}
2454
2455PP(pp_redo)
2456{
27da23d5 2457 dVAR;
a0d0e21e 2458 I32 cxix;
c09156bb 2459 register PERL_CONTEXT *cx;
a0d0e21e 2460 I32 oldsave;
a034e688 2461 OP* redo_op;
a0d0e21e 2462
533c011a 2463 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2464 cxix = dopoptoloop(cxstack_ix);
2465 if (cxix < 0)
a651a37d 2466 DIE(aTHX_ "Can't \"redo\" outside a loop block");
a0d0e21e
LW
2467 }
2468 else {
2469 cxix = dopoptolabel(cPVOP->op_pv);
2470 if (cxix < 0)
cea2e8a9 2471 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
a0d0e21e
LW
2472 }
2473 if (cxix < cxstack_ix)
2474 dounwind(cxix);
2475
022eaa24 2476 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
a034e688
DM
2477 if (redo_op->op_type == OP_ENTER) {
2478 /* pop one less context to avoid $x being freed in while (my $x..) */
2479 cxstack_ix++;
2480 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2481 redo_op = redo_op->op_next;
2482 }
2483
a0d0e21e 2484 TOPBLOCK(cx);
3280af22 2485 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e 2486 LEAVE_SCOPE(oldsave);
936c78b5 2487 FREETMPS;
3a1b2b9e 2488 PL_curcop = cx->blk_oldcop;
a034e688 2489 return redo_op;
a0d0e21e
LW
2490}
2491
0824fdcb 2492STATIC OP *
bfed75c6 2493S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
a0d0e21e 2494{
97aff369 2495 dVAR;
a0d0e21e 2496 OP **ops = opstack;
bfed75c6 2497 static const char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 2498
7918f24d
NC
2499 PERL_ARGS_ASSERT_DOFINDLABEL;
2500
fc36a67e 2501 if (ops >= oplimit)
cea2e8a9 2502 Perl_croak(aTHX_ too_deep);
11343788
MB
2503 if (o->op_type == OP_LEAVE ||
2504 o->op_type == OP_SCOPE ||
2505 o->op_type == OP_LEAVELOOP ||
33d34e4c 2506 o->op_type == OP_LEAVESUB ||
11343788 2507 o->op_type == OP_LEAVETRY)
fc36a67e 2508 {
5dc0d613 2509 *ops++ = cUNOPo->op_first;
fc36a67e 2510 if (ops >= oplimit)
cea2e8a9 2511 Perl_croak(aTHX_ too_deep);
fc36a67e 2512 }
c4aa4e48 2513 *ops = 0;
11343788 2514 if (o->op_flags & OPf_KIDS) {
aec46f14 2515 OP *kid;
a0d0e21e 2516 /* First try all the kids at this level, since that's likeliest. */
11343788 2517 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
7e8f1eac
AD
2518 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2519 const char *kid_label = CopLABEL(kCOP);
2520 if (kid_label && strEQ(kid_label, label))
2521 return kid;
2522 }
a0d0e21e 2523 }
11343788 2524 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 2525 if (kid == PL_lastgotoprobe)
a0d0e21e 2526 continue;
ed8d0fe2
SM
2527 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2528 if (ops == opstack)
2529 *ops++ = kid;
2530 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2531 ops[-1]->op_type == OP_DBSTATE)
2532 ops[-1] = kid;
2533 else
2534 *ops++ = kid;
2535 }
155aba94 2536 if ((o = dofindlabel(kid, label, ops, oplimit)))
11343788 2537 return o;
a0d0e21e
LW
2538 }
2539 }
c4aa4e48 2540 *ops = 0;
a0d0e21e
LW
2541 return 0;
2542}
2543
a0d0e21e
LW
2544PP(pp_goto)
2545{
27da23d5 2546 dVAR; dSP;
cbbf8932 2547 OP *retop = NULL;
a0d0e21e 2548 I32 ix;
c09156bb 2549 register PERL_CONTEXT *cx;
fc36a67e
PP
2550#define GOTO_DEPTH 64
2551 OP *enterops[GOTO_DEPTH];
cbbf8932 2552 const char *label = NULL;
bfed75c6
AL
2553 const bool do_dump = (PL_op->op_type == OP_DUMP);
2554 static const char must_have_label[] = "goto must have label";
a0d0e21e 2555
533c011a 2556 if (PL_op->op_flags & OPf_STACKED) {
9d4ba2ae 2557 SV * const sv = POPs;
a0d0e21e
LW
2558
2559 /* This egregious kludge implements goto &subroutine */
2560 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2561 I32 cxix;
c09156bb 2562 register PERL_CONTEXT *cx;
ea726b52 2563 CV *cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2564 SV** mark;
2565 I32 items = 0;
2566 I32 oldsave;
b1464ded 2567 bool reified = 0;
a0d0e21e 2568
e8f7dd13 2569 retry:
4aa0a1f7 2570 if (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2571 const GV * const gv = CvGV(cv);
e8f7dd13 2572 if (gv) {
7fc63493 2573 GV *autogv;
e8f7dd13
GS
2574 SV *tmpstr;
2575 /* autoloaded stub? */
2576 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2577 goto retry;
2578 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2579 GvNAMELEN(gv), FALSE);
2580 if (autogv && (cv = GvCV(autogv)))
2581 goto retry;
2582 tmpstr = sv_newmortal();
c445ea15 2583 gv_efullname3(tmpstr, gv, NULL);
be2597df 2584 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
4aa0a1f7 2585 }
cea2e8a9 2586 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2587 }
2588
a0d0e21e 2589 /* First do some returnish stuff. */
b37c2d43 2590 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
71fc2216 2591 FREETMPS;
a0d0e21e
LW
2592 cxix = dopoptosub(cxstack_ix);
2593 if (cxix < 0)
cea2e8a9 2594 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
a0d0e21e
LW
2595 if (cxix < cxstack_ix)
2596 dounwind(cxix);
2597 TOPBLOCK(cx);
2d43a17f 2598 SPAGAIN;
564abe23 2599 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2600 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89
DM
2601 if (CxREALEVAL(cx))
2602 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2603 else
2604 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2605 }
9850bf21
RH
2606 else if (CxMULTICALL(cx))
2607 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
bafb2adc 2608 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
d8b46c1b 2609 /* put @_ back onto stack */
a0d0e21e 2610 AV* av = cx->blk_sub.argarray;
bfed75c6 2611
93965878 2612 items = AvFILLp(av) + 1;
a45cdc79
DM
2613 EXTEND(SP, items+1); /* @_ could have been extended. */
2614 Copy(AvARRAY(av), SP + 1, items, SV*);
3280af22
NIS
2615 SvREFCNT_dec(GvAV(PL_defgv));
2616 GvAV(PL_defgv) = cx->blk_sub.savearray;
b1464ded 2617 CLEAR_ARGARRAY(av);
d8b46c1b 2618 /* abandon @_ if it got reified */
62b1ebc2 2619 if (AvREAL(av)) {
b1464ded
DM
2620 reified = 1;
2621 SvREFCNT_dec(av);
d8b46c1b
GS
2622 av = newAV();
2623 av_extend(av, items-1);
11ca45c0 2624 AvREIFY_only(av);
ad64d0ec 2625 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
62b1ebc2 2626 }
a0d0e21e 2627 }
aed2304a 2628 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
890ce7af 2629 AV* const av = GvAV(PL_defgv);
1fa4e549 2630 items = AvFILLp(av) + 1;
a45cdc79
DM
2631 EXTEND(SP, items+1); /* @_ could have been extended. */
2632 Copy(AvARRAY(av), SP + 1, items, SV*);
1fa4e549 2633 }
a45cdc79
DM
2634 mark = SP;
2635 SP += items;
6b35e009 2636 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 2637 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 2638 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 2639 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
2640 LEAVE_SCOPE(oldsave);
2641
2642 /* Now do some callish stuff. */
2643 SAVETMPS;
5023d17a 2644 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
aed2304a 2645 if (CvISXSUB(cv)) {
b37c2d43 2646 OP* const retop = cx->blk_sub.retop;
9410e98d
RB
2647 SV **newsp __attribute__unused__;
2648 I32 gimme __attribute__unused__;
b1464ded
DM
2649 if (reified) {
2650 I32 index;
2651 for (index=0; index<items; index++)
2652 sv_2mortal(SP[-index]);
2653 }
1fa4e549 2654
b37c2d43
AL
2655 /* XS subs don't have a CxSUB, so pop it */
2656 POPBLOCK(cx, PL_curpm);
2657 /* Push a mark for the start of arglist */
2658 PUSHMARK(mark);
2659 PUTBACK;
2660 (void)(*CvXSUB(cv))(aTHX_ cv);
a57c6685 2661 LEAVE;
5eff7df7 2662 return retop;
a0d0e21e
LW
2663 }
2664 else {
b37c2d43 2665 AV* const padlist = CvPADLIST(cv);
6b35e009 2666 if (CxTYPE(cx) == CXt_EVAL) {
85a64632 2667 PL_in_eval = CxOLD_IN_EVAL(cx);
3280af22 2668 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22 2669 cx->cx_type = CXt_SUB;
b150fb22 2670 }
a0d0e21e 2671 cx->blk_sub.cv = cv;
1a5b3db4 2672 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2673
a0d0e21e
LW
2674 CvDEPTH(cv)++;
2675 if (CvDEPTH(cv) < 2)
74c765eb 2676 SvREFCNT_inc_simple_void_NN(cv);
dd2155a4 2677 else {
2b9dff67 2678 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2679 sub_crush_depth(cv);
26019298 2680 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2681 }
fd617465
DM
2682 SAVECOMPPAD();
2683 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 2684 if (CxHASARGS(cx))
6d4ff0d2 2685 {
502c6561 2686 AV *const av = MUTABLE_AV(PAD_SVl(0));
a0d0e21e 2687
3280af22 2688 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 2689 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 2690 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2691 cx->blk_sub.argarray = av;
a0d0e21e
LW
2692
2693 if (items >= AvMAX(av) + 1) {
b37c2d43 2694 SV **ary = AvALLOC(av);
a0d0e21e
LW
2695 if (AvARRAY(av) != ary) {
2696 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 2697 AvARRAY(av) = ary;
a0d0e21e
LW
2698 }
2699 if (items >= AvMAX(av) + 1) {
2700 AvMAX(av) = items - 1;
2701 Renew(ary,items+1,SV*);
2702 AvALLOC(av) = ary;
9c6bc640 2703 AvARRAY(av) = ary;
a0d0e21e
LW
2704 }
2705 }
a45cdc79 2706 ++mark;
a0d0e21e 2707 Copy(mark,AvARRAY(av),items,SV*);
93965878 2708 AvFILLp(av) = items - 1;
d8b46c1b 2709 assert(!AvREAL(av));
b1464ded
DM
2710 if (reified) {
2711 /* transfer 'ownership' of refcnts to new @_ */
2712 AvREAL_on(av);
2713 AvREIFY_off(av);
2714 }
a0d0e21e
LW
2715 while (items--) {
2716 if (*mark)
2717 SvTEMP_off(*mark);
2718 mark++;
2719 }
2720 }
491527d0 2721 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 2722 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43 2723 if (PERLDB_GOTO) {
b96d8cd9 2724 CV * const gotocv = get_cvs("DB::goto", 0);
b37c2d43
AL
2725 if (gotocv) {
2726 PUSHMARK( PL_stack_sp );
ad64d0ec 2727 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
2728 PL_stack_sp--;
2729 }
491527d0 2730 }
1ce6579f 2731 }
a0d0e21e
LW
2732 RETURNOP(CvSTART(cv));
2733 }
2734 }
1614b0e3 2735 else {
0510663f 2736 label = SvPV_nolen_const(sv);
1614b0e3 2737 if (!(do_dump || *label))
cea2e8a9 2738 DIE(aTHX_ must_have_label);
1614b0e3 2739 }
a0d0e21e 2740 }
533c011a 2741 else if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 2742 if (! do_dump)
cea2e8a9 2743 DIE(aTHX_ must_have_label);
a0d0e21e
LW
2744 }
2745 else
2746 label = cPVOP->op_pv;
2747
f410a211
NC
2748 PERL_ASYNC_CHECK();
2749
a0d0e21e 2750 if (label && *label) {
cbbf8932 2751 OP *gotoprobe = NULL;
3b2447bc 2752 bool leaving_eval = FALSE;
33d34e4c 2753 bool in_block = FALSE;
cbbf8932 2754 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
2755
2756 /* find label */
2757
d4c19fe8 2758 PL_lastgotoprobe = NULL;
a0d0e21e
LW
2759 *enterops = 0;
2760 for (ix = cxstack_ix; ix >= 0; ix--) {
2761 cx = &cxstack[ix];
6b35e009 2762 switch (CxTYPE(cx)) {
a0d0e21e 2763 case CXt_EVAL:
3b2447bc 2764 leaving_eval = TRUE;
971ecbe6 2765 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
2766 gotoprobe = (last_eval_cx ?
2767 last_eval_cx->blk_eval.old_eval_root :
2768 PL_eval_root);
2769 last_eval_cx = cx;
9c5794fe
RH
2770 break;
2771 }
2772 /* else fall through */
c6fdafd0 2773 case CXt_LOOP_LAZYIV:
d01136d6 2774 case CXt_LOOP_LAZYSV:
3b719c58
NC
2775 case CXt_LOOP_FOR:
2776 case CXt_LOOP_PLAIN:
bb5aedc1
VP
2777 case CXt_GIVEN:
2778 case CXt_WHEN:
a0d0e21e
LW
2779 gotoprobe = cx->blk_oldcop->op_sibling;
2780 break;
2781 case CXt_SUBST:
2782 continue;
2783 case CXt_BLOCK:
33d34e4c 2784 if (ix) {
a0d0e21e 2785 gotoprobe = cx->blk_oldcop->op_sibling;
33d34e4c
AE
2786 in_block = TRUE;
2787 } else
3280af22 2788 gotoprobe = PL_main_root;
a0d0e21e 2789 break;
b3933176 2790 case CXt_SUB:
9850bf21 2791 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
b3933176
CS
2792 gotoprobe = CvROOT(cx->blk_sub.cv);
2793 break;
2794 }
2795 /* FALL THROUGH */
7766f137 2796 case CXt_FORMAT:
0a753a76 2797 case CXt_NULL:
a651a37d 2798 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
2799 default:
2800 if (ix)
cea2e8a9 2801 DIE(aTHX_ "panic: goto");
3280af22 2802 gotoprobe = PL_main_root;
a0d0e21e
LW
2803 break;
2804 }
2b597662
GS
2805 if (gotoprobe) {
2806 retop = dofindlabel(gotoprobe, label,
2807 enterops, enterops + GOTO_DEPTH);
2808 if (retop)
2809 break;
eae48c89
Z
2810 if (gotoprobe->op_sibling &&
2811 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
2812 gotoprobe->op_sibling->op_sibling) {
2813 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
2814 label, enterops, enterops + GOTO_DEPTH);
2815 if (retop)
2816 break;
2817 }
2b597662 2818 }
3280af22 2819 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
2820 }
2821 if (!retop)
cea2e8a9 2822 DIE(aTHX_ "Can't find label %s", label);
a0d0e21e 2823
3b2447bc
RH
2824 /* if we're leaving an eval, check before we pop any frames
2825 that we're not going to punt, otherwise the error
2826 won't be caught */
2827
2828 if (leaving_eval && *enterops && enterops[1]) {
2829 I32 i;
2830 for (i = 1; enterops[i]; i++)
2831 if (enterops[i]->op_type == OP_ENTERITER)
2832 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2833 }
2834
b500e03b
GG
2835 if (*enterops && enterops[1]) {
2836 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2837 if (enterops[i])
2838 deprecate("\"goto\" to jump into a construct");
2839 }
2840
a0d0e21e
LW
2841 /* pop unwanted frames */
2842
2843 if (ix < cxstack_ix) {
2844 I32 oldsave;
2845
2846 if (ix < 0)
2847 ix = 0;
2848 dounwind(ix);
2849 TOPBLOCK(cx);
3280af22 2850 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
2851 LEAVE_SCOPE(oldsave);
2852 }
2853
2854 /* push wanted frames */
2855
748a9306 2856 if (*enterops && enterops[1]) {
0bd48802 2857 OP * const oldop = PL_op;
33d34e4c
AE
2858 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2859 for (; enterops[ix]; ix++) {
533c011a 2860 PL_op = enterops[ix];
84902520
TB
2861 /* Eventually we may want to stack the needed arguments
2862 * for each op. For now, we punt on the hard ones. */
533c011a 2863 if (PL_op->op_type == OP_ENTERITER)
894356b3 2864 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
16c91539 2865 PL_op->op_ppaddr(aTHX);
a0d0e21e 2866 }
533c011a 2867 PL_op = oldop;
a0d0e21e
LW
2868 }
2869 }
2870
2871 if (do_dump) {
a5f75d66 2872#ifdef VMS
6b88bc9c 2873 if (!retop) retop = PL_main_start;
a5f75d66 2874#endif
3280af22
NIS
2875 PL_restartop = retop;
2876 PL_do_undump = TRUE;
a0d0e21e
LW
2877
2878 my_unexec();
2879
3280af22
NIS
2880 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2881 PL_do_undump = FALSE;
a0d0e21e
LW
2882 }
2883
2884 RETURNOP(retop);
2885}
2886
2887PP(pp_exit)
2888{
97aff369 2889 dVAR;
39644a26 2890 dSP;
a0d0e21e
LW
2891 I32 anum;
2892
2893 if (MAXARG < 1)
2894 anum = 0;
ff0cee69 2895 else {
a0d0e21e 2896 anum = SvIVx(POPs);
d98f61e7
GS
2897#ifdef VMS
2898 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
ff0cee69 2899 anum = 0;
96e176bf 2900 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
ff0cee69
PP
2901#endif
2902 }
cc3604b1 2903 PL_exit_flags |= PERL_EXIT_EXPECTED;
81d86705
NC
2904#ifdef PERL_MAD
2905 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2906 if (anum || !(PL_minus_c && PL_madskills))
2907 my_exit(anum);
2908#else
a0d0e21e 2909 my_exit(anum);
81d86705 2910#endif
3280af22 2911 PUSHs(&PL_sv_undef);
a0d0e21e
LW
2912 RETURN;
2913}
2914
a0d0e21e
LW
2915/* Eval. */
2916
0824fdcb 2917STATIC void
cea2e8a9 2918S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 2919{
504618e9 2920 const char *s = SvPVX_const(sv);
890ce7af 2921 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 2922 I32 line = 1;
a0d0e21e 2923
7918f24d
NC
2924 PERL_ARGS_ASSERT_SAVE_LINES;
2925
a0d0e21e 2926 while (s && s < send) {
f54cb97a 2927 const char *t;
b9f83d2f 2928 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 2929
1d963ff3 2930 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
2931 if (t)
2932 t++;
2933 else
2934 t = send;
2935
2936 sv_setpvn(tmpstr, s, t - s);
2937 av_store(array, line++, tmpstr);
2938 s = t;
2939 }
2940}
2941
22f16304
RU
2942/*
2943=for apidoc docatch
2944
2945Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2946
29470 is used as continue inside eval,
2948
29493 is used for a die caught by an inner eval - continue inner loop
2950
2951See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
2952establish a local jmpenv to handle exception traps.
2953
2954=cut
2955*/
0824fdcb 2956STATIC OP *
cea2e8a9 2957S_docatch(pTHX_ OP *o)
1e422769 2958{
97aff369 2959 dVAR;
6224f72b 2960 int ret;
06b5626a 2961 OP * const oldop = PL_op;
db36c5a1 2962 dJMPENV;
1e422769 2963
1e422769 2964#ifdef DEBUGGING
54310121 2965 assert(CATCH_GET == TRUE);
1e422769 2966#endif
312caa8e 2967 PL_op = o;
8bffa5f8 2968
14dd3ad8 2969 JMPENV_PUSH(ret);
6224f72b 2970 switch (ret) {
312caa8e 2971 case 0:
abd70938
DM
2972 assert(cxstack_ix >= 0);
2973 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2974 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
14dd3ad8 2975 redo_body:
85aaa934 2976 CALLRUNOPS(aTHX);
312caa8e
CS
2977 break;
2978 case 3:
8bffa5f8 2979 /* die caught by an inner eval - continue inner loop */
febb3a6d
Z
2980 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
2981 PL_restartjmpenv = NULL;
312caa8e
CS
2982 PL_op = PL_restartop;
2983 PL_restartop = 0;
2984 goto redo_body;
2985 }
2986 /* FALL THROUGH */
2987 default:
14dd3ad8 2988 JMPENV_POP;
533c011a 2989 PL_op = oldop;
6224f72b 2990 JMPENV_JUMP(ret);
1e422769 2991 /* NOTREACHED */
1e422769 2992 }
14dd3ad8 2993 JMPENV_POP;
533c011a 2994 PL_op = oldop;
5f66b61c 2995 return NULL;
1e422769
PP
2996}
2997
ee23ad3b
NC
2998/* James Bond: Do you expect me to talk?
2999 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3000
3001 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3002 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3003
3004 Currently it is not used outside the core code. Best if it stays that way.
d59a8b3e
NC
3005
3006 Hence it's now deprecated, and will be removed.
ee23ad3b 3007*/
c277df42 3008OP *
bfed75c6 3009Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
c277df42
IZ
3010/* sv Text to convert to OP tree. */
3011/* startop op_free() this to undo. */
3012/* code Short string id of the caller. */
3013{
d59a8b3e
NC
3014 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3015 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3016}
3017
3018/* Don't use this. It will go away without warning once the regexp engine is
3019 refactored not to use it. */
3020OP *
3021Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3022 PAD **padp)
3023{
27da23d5 3024 dVAR; dSP; /* Make POPBLOCK work. */
c277df42
IZ
3025 PERL_CONTEXT *cx;
3026 SV **newsp;
b094c71d 3027 I32 gimme = G_VOID;
c277df42
IZ
3028 I32 optype;
3029 OP dummy;
83ee9e09
GS
3030 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3031 char *tmpbuf = tbuf;
c277df42 3032 char *safestr;
a3985cdc 3033 int runtime;
601f1833 3034 CV* runcv = NULL; /* initialise to avoid compiler warnings */
f7997f86 3035 STRLEN len;
634d6919 3036 bool need_catch;
c277df42 3037
d59a8b3e 3038 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
7918f24d 3039
d343c3ef 3040 ENTER_with_name("eval");
27fcb6ee 3041 lex_start(sv, NULL, LEX_START_SAME_FILTER);
c277df42
IZ
3042 SAVETMPS;
3043 /* switch to eval mode */
3044
923e4eb5 3045 if (IN_PERL_COMPILETIME) {
f4dd75d9 3046 SAVECOPSTASH_FREE(&PL_compiling);
11faa288 3047 CopSTASH_set(&PL_compiling, PL_curstash);
cbce877f 3048 }
83ee9e09 3049 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
9d4ba2ae 3050 SV * const sv = sv_newmortal();
83ee9e09
GS
3051 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3052 code, (unsigned long)++PL_evalseq,
3053 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3054 tmpbuf = SvPVX(sv);
fc009855 3055 len = SvCUR(sv);
83ee9e09
GS
3056 }
3057 else
d9fad198
JH
3058 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3059 (unsigned long)++PL_evalseq);
f4dd75d9 3060 SAVECOPFILE_FREE(&PL_compiling);
57843af0 3061 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 3062 SAVECOPLINE(&PL_compiling);
57843af0 3063 CopLINE_set(&PL_compiling, 1);
c277df42
IZ
3064 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3065 deleting the eval's FILEGV from the stash before gv_check() runs
3066 (i.e. before run-time proper). To work around the coredump that
3067 ensues, we always turn GvMULTI_on for any globals that were
3068 introduced within evals. See force_ident(). GSAR 96-10-12 */
f7997f86
NC
3069 safestr = savepvn(tmpbuf, len);
3070 SAVEDELETE(PL_defstash, safestr, len);
b3ac6de7 3071 SAVEHINTS();
d1ca3daa 3072#ifdef OP_IN_REGISTER
6b88bc9c 3073 PL_opsave = op;
d1ca3daa 3074#else
7766f137 3075 SAVEVPTR(PL_op);
d1ca3daa 3076#endif
c277df42 3077
a3985cdc 3078 /* we get here either during compilation, or via pp_regcomp at runtime */
923e4eb5 3079 runtime = IN_PERL_RUNTIME;
a3985cdc 3080 if (runtime)
558b4424 3081 {
d819b83a 3082 runcv = find_runcv(NULL);
a3985cdc 3083
558b4424
FC
3084 /* At run time, we have to fetch the hints from PL_curcop. */
3085 PL_hints = PL_curcop->cop_hints;
3086 if (PL_hints & HINT_LOCALIZE_HH) {
3087 /* SAVEHINTS created a new HV in PL_hintgv, which we
3088 need to GC */
3089 SvREFCNT_dec(GvHV(PL_hintgv));
3090 GvHV(PL_hintgv) =
3091 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3092 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3093 }
3094 SAVECOMPILEWARNINGS();
3095 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3096 cophh_free(CopHINTHASH_get(&PL_compiling));
3097 /* XXX Does this need to avoid copying a label? */
3098 PL_compiling.cop_hints_hash
3099 = cophh_copy(PL_curcop->cop_hints_hash);
3100 }
3101
533c011a 3102 PL_op = &dummy;
13b51b79 3103 PL_op->op_type = OP_ENTEREVAL;
533c011a 3104 PL_op->op_flags = 0; /* Avoid uninit warning. */
923e4eb5 3105 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
6b75f042 3106 PUSHEVAL(cx, 0);
634d6919
GG
3107 need_catch = CATCH_GET;
3108 CATCH_SET(TRUE);
a3985cdc
DM
3109
3110 if (runtime)
410be5db 3111 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
a3985cdc 3112 else
410be5db 3113 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
634d6919 3114 CATCH_SET(need_catch);
13b51b79 3115 POPBLOCK(cx,PL_curpm);
e84b9f1f 3116 POPEVAL(cx);
c277df42
IZ
3117
3118 (*startop)->op_type = OP_NULL;
22c35a8c 3119 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
f3548bdc 3120 /* XXX DAPM do this properly one year */
502c6561 3121 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
d343c3ef 3122 LEAVE_with_name("eval");
923e4eb5 3123 if (IN_PERL_COMPILETIME)
623e6609 3124 CopHINTS_set(&PL_compiling, PL_hints);
d1ca3daa 3125#ifdef OP_IN_REGISTER
6b88bc9c 3126 op = PL_opsave;
d1ca3daa 3127#endif
9d4ba2ae
AL
3128 PERL_UNUSED_VAR(newsp);
3129 PERL_UNUSED_VAR(optype);
3130
410be5db 3131 return PL_eval_start;
c277df42
IZ
3132}
3133
a3985cdc
DM
3134
3135/*
3136=for apidoc find_runcv
3137
3138Locate the CV corresponding to the currently executing sub or eval.
d819b83a
DM
3139If db_seqp is non_null, skip CVs that are in the DB package and populate
3140*db_seqp with the cop sequence number at the point that the DB:: code was
3141entered. (allows debuggers to eval in the scope of the breakpoint rather
cf525c36 3142than in the scope of the debugger itself).
a3985cdc
DM
3143
3144=cut
3145*/
3146
3147CV*
d819b83a 3148Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3149{
97aff369 3150 dVAR;
a3985cdc 3151 PERL_SI *si;
a3985cdc 3152
d819b83a
DM
3153 if (db_seqp)
3154 *db_seqp = PL_curcop->cop_seq;
a3985cdc 3155 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3156 I32 ix;
a3985cdc 3157 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 3158 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
d819b83a 3159 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1b6737cc 3160 CV * const cv = cx->blk_sub.cv;
d819b83a
DM
3161 /* skip DB:: code */
3162 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3163 *db_seqp = cx->blk_oldcop->cop_seq;
3164 continue;
3165 }
3166 return cv;
3167 }
a3985cdc
DM
3168 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3169 return PL_compcv;
3170 }
3171 }
3172 return PL_main_cv;
3173}
3174
3175
27e90453
DM
3176/* Run yyparse() in a setjmp wrapper. Returns:
3177 * 0: yyparse() successful
3178 * 1: yyparse() failed
3179 * 3: yyparse() died
3180 */
3181STATIC int
28ac2b49 3182S_try_yyparse(pTHX_ int gramtype)
27e90453
DM
3183{
3184 int ret;
3185 dJMPENV;
3186
3187 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3188 JMPENV_PUSH(ret);
3189 switch (ret) {
3190 case 0:
28ac2b49 3191 ret = yyparse(gramtype) ? 1 : 0;
27e90453
DM
3192 break;
3193 case 3:
3194 break;
3195 default:
3196 JMPENV_POP;
3197 JMPENV_JUMP(ret);
3198 /* NOTREACHED */
3199 }
3200 JMPENV_POP;
3201 return ret;
3202}
3203
3204
a3985cdc
DM
3205/* Compile a require/do, an eval '', or a /(?{...})/.
3206 * In the last case, startop is non-null, and contains the address of
3207 * a pointer that should be set to the just-compiled code.
3208 * outside is the lexically enclosing CV (if any) that invoked us.
410be5db
DM
3209 * Returns a bool indicating whether the compile was successful; if so,
3210 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3211 * pushes undef (also croaks if startop != NULL).
a3985cdc
DM
3212 */
3213
410be5db 3214STATIC bool
a3985cdc 3215S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
a0d0e21e 3216{
27da23d5 3217 dVAR; dSP;
46c461b5 3218 OP * const saveop = PL_op;
27e90453
DM
3219 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3220 int yystatus;
a0d0e21e 3221
27e90453 3222 PL_in_eval = (in_require
6dc8a9e4
IZ
3223 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3224 : EVAL_INEVAL);
a0d0e21e 3225
1ce6579f
PP
3226 PUSHMARK(SP);
3227
3280af22 3228 SAVESPTR(PL_compcv);
ea726b52 3229 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
1aff0e91 3230 CvEVAL_on(PL_compcv);
2090ab20
JH
3231 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3232 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3233
a3985cdc 3234 CvOUTSIDE_SEQ(PL_compcv) = seq;
ea726b52 3235 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3236
dd2155a4 3237 /* set up a scratch pad */
a0d0e21e 3238
dd2155a4 3239 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
cecbe010 3240 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3241
07055b4c 3242
81d86705
NC
3243 if (!PL_madskills)
3244 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
748a9306 3245
a0d0e21e
LW
3246 /* make sure we compile in the right package */
3247
ed094faf 3248 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3280af22 3249 SAVESPTR(PL_curstash);
ed094faf 3250 PL_curstash = CopSTASH(PL_curcop);
a0d0e21e 3251 }
3c10abe3 3252 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3280af22
NIS
3253 SAVESPTR(PL_beginav);
3254 PL_beginav = newAV();
3255 SAVEFREESV(PL_beginav);
3c10abe3
AG
3256 SAVESPTR(PL_unitcheckav);
3257 PL_unitcheckav = newAV();
3258 SAVEFREESV(PL_unitcheckav);
a0d0e21e 3259
81d86705 3260#ifdef PERL_MAD
9da243ce 3261 SAVEBOOL(PL_madskills);
81d86705
NC
3262 PL_madskills = 0;
3263#endif
3264
a0d0e21e
LW
3265 /* try to compile it */
3266
5f66b61c 3267 PL_eval_root = NULL;
3280af22 3268 PL_curcop = &PL_compiling;
fc15ae8f 3269 CopARYBASE_set(PL_curcop, 0);
5f66b61c 3270 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
faef0170 3271 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2
RGS
3272 else
3273 CLEAR_ERRSV();
27e90453 3274
a88d97bf 3275 CALL_BLOCK_HOOKS(bhk_eval, saveop);
52db365a 3276
27e90453
DM
3277 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3278 * so honour CATCH_GET and trap it here if necessary */
3279
28ac2b49 3280 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
27e90453
DM
3281
3282 if (yystatus || PL_parser->error_count || !PL_eval_root) {
0c58d367 3283 SV **newsp; /* Used by POPBLOCK. */
b6494f15 3284 PERL_CONTEXT *cx = NULL;
27e90453 3285 I32 optype; /* Used by POPEVAL. */
b6494f15 3286 SV *namesv = NULL;
9d4ba2ae 3287 const char *msg;
bfed75c6 3288
27e90453
DM
3289 PERL_UNUSED_VAR(newsp);
3290 PERL_UNUSED_VAR(optype);
3291
c86ffc32
DM
3292 /* note that if yystatus == 3, then the EVAL CX block has already
3293 * been popped, and various vars restored */
533c011a 3294 PL_op = saveop;
27e90453 3295 if (yystatus != 3) {
c86ffc32
DM
3296 if (PL_eval_root) {
3297 op_free(PL_eval_root);
3298 PL_eval_root = NULL;
3299 }
27e90453
DM
3300 SP = PL_stack_base + POPMARK; /* pop original mark */
3301 if (!startop) {
3302 POPBLOCK(cx,PL_curpm);
3303 POPEVAL(cx);
b6494f15 3304 namesv = cx->blk_eval.old_namesv;
27e90453 3305 }
c277df42 3306 }
27e90453
DM
3307 if (yystatus != 3)
3308 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
9d4ba2ae
AL
3309
3310 msg = SvPVx_nolen_const(ERRSV);
27e90453 3311 if (in_require) {
b6494f15
VP
3312 if (!cx) {
3313 /* If cx is still NULL, it means that we didn't go in the
3314 * POPEVAL branch. */
3315 cx = &cxstack[cxstack_ix];
3316 assert(CxTYPE(cx) == CXt_EVAL);
3317 namesv = cx->blk_eval.old_namesv;
3318 }
3319 (void)hv_store(GvHVn(PL_incgv),
3320 SvPVX_const(namesv), SvCUR(namesv),
3321 &PL_sv_undef, 0);
58d3fd3b
SH
3322 Perl_croak(aTHX_ "%sCompilation failed in require",
3323 *msg ? msg : "Unknown error\n");
5a844595
GS
3324 }
3325 else if (startop) {
27e90453
DM
3326 if (yystatus != 3) {
3327 POPBLOCK(cx,PL_curpm);
3328 POPEVAL(cx);
3329 }
5a844595
GS
3330 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3331 (*msg ? msg : "Unknown error\n"));
7a2e2cd6 3332 }
9d7f88dd 3333 else {
9d7f88dd 3334 if (!*msg) {
6502358f 3335 sv_setpvs(ERRSV, "Compilation error");
9d7f88dd
SR
3336 }
3337 }
410be5db
DM
3338 PUSHs(&PL_sv_undef);
3339 PUTBACK;
3340 return FALSE;
a0d0e21e 3341 }
57843af0 3342 CopLINE_set(&PL_compiling, 0);
c277df42 3343 if (startop) {
3280af22 3344 *startop = PL_eval_root;
c277df42 3345 } else
3280af22 3346 SAVEFREEOP(PL_eval_root);
0c58d367
RGS
3347
3348 /* Set the context for this new optree.
021f53de
GG
3349 * Propagate the context from the eval(). */
3350 if ((gimme & G_WANT) == G_VOID)
3280af22 3351 scalarvoid(PL_eval_root);
7df0357e 3352 else if ((gimme & G_WANT) == G_ARRAY)
3280af22 3353 list(PL_eval_root);
a0d0e21e 3354 else
3280af22 3355 scalar(PL_eval_root);
a0d0e21e
LW
3356
3357 DEBUG_x(dump_eval());
3358
55497cff 3359 /* Register with debugger: */
6482a30d 3360 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
b96d8cd9 3361 CV * const cv = get_cvs("DB::postponed", 0);
55497cff
PP
3362 if (cv) {
3363 dSP;
924508f0 3364 PUSHMARK(SP);
ad64d0ec 3365 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
55497cff 3366 PUTBACK;
ad64d0ec 3367 call_sv(MUTABLE_SV(cv), G_DISCARD);
55497cff
PP
3368 }
3369 }
3370
8ed49485
FC
3371 if (PL_unitcheckav) {
3372 OP *es = PL_eval_start;
3c10abe3 3373 call_list(PL_scopestack_ix, PL_unitcheckav);
8ed49485
FC
3374 PL_eval_start = es;
3375 }
3c10abe3 3376
a0d0e21e
LW
3377 /* compiled okay, so do it */
3378
3280af22
NIS
3379 CvDEPTH(PL_compcv) = 1;
3380 SP = PL_stack_base + POPMARK; /* pop original mark */
533c011a 3381 PL_op = saveop; /* The caller may need it. */
bc177e6b 3382 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
5dc0d613 3383
410be5db
DM
3384 PUTBACK;
3385 return TRUE;
a0d0e21e
LW
3386}
3387
a6c40364 3388STATIC PerlIO *
282b29ee 3389S_check_type_and_open(pTHX_ SV *name)
ce8abf5f
SP
3390{
3391 Stat_t st;
282b29ee
NC
3392 const char *p = SvPV_nolen_const(name);
3393 const int st_rc = PerlLIO_stat(p, &st);
df528165 3394
7918f24d
NC
3395 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3396
6b845e56 3397 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
4608196e 3398 return NULL;
ce8abf5f
SP
3399 }
3400
ccb84406
NC
3401#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3402 return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3403#else
282b29ee 3404 return PerlIO_open(p, PERL_SCRIPT_MODE);
ccb84406 3405#endif
ce8abf5f
SP
3406}
3407
75c20bac 3408#ifndef PERL_DISABLE_PMC
ce8abf5f 3409STATIC PerlIO *
282b29ee 3410S_doopen_pm(pTHX_ SV *name)
b295d113 3411{
282b29ee
NC
3412 STRLEN namelen;
3413 const char *p = SvPV_const(name, namelen);
b295d113 3414
7918f24d
NC
3415 PERL_ARGS_ASSERT_DOOPEN_PM;
3416
282b29ee 3417 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
eb70bb4a 3418