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