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