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